git @ Cat's Eye Technologies Castile / 3458fb6
Make field access limited by struct scope too. All tests pass. Chris Pressey 3 years ago
3 changed file(s) with 22 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
6666 type_exprs = []
6767 i = 0
6868 field_defns = ast.children[0].children
69 scope_idents = ast.children[1].children if len(ast.children) > 1 else None
69 scope_idents = None
70 if len(ast.children) > 1:
71 scope_idents = [a.value for a in ast.children[1].children]
7072 for child in field_defns:
7173 assert child.tag == 'FieldDefn', child.tag
7274 field_name = child.value
242244 raise CastileTypeError("undefined struct %s" % t.name)
243245 struct_defn = self.structs[t.name]
244246 if struct_defn.scope_idents is not None:
245 scope_idents = [ast.value for ast in struct_defn.scope_idents]
246 if self.current_defn not in scope_idents:
247 raise CastileTypeError("inaccessible struct %s for make: %s not in %s" %
248 (t.name, self.current_defn, scope_idents)
247 if self.current_defn not in struct_defn.scope_idents:
248 raise CastileTypeError("inaccessible struct %s for make: %s not in %s" %
249 (t.name, self.current_defn, struct_defn.scope_idents)
249250 )
250251 if len(struct_defn.content_types) != len(ast.children) - 1:
251 raise CastileTypeError("argument mismatch")
252 raise CastileTypeError("argument mismatch; expected {}, got {} in {}".format(
253 len(struct_defn.content_types), len(ast.children) - 1, ast
254 ))
252255 i = 0
253256 for defn in ast.children[1:]:
254257 name = defn.value
262265 ast.type = self.type_of(ast.children[0])
263266 elif ast.tag == 'Index':
264267 t = self.type_of(ast.children[0])
268 struct_defn = self.structs[t.name]
269 if struct_defn.scope_idents is not None:
270 if self.current_defn not in struct_defn.scope_idents:
271 raise CastileTypeError("inaccessible struct %s for access: %s not in %s" %
272 (t.name, self.current_defn, struct_defn.scope_idents)
273 )
265274 field_name = ast.value
266 struct_fields = self.structs[t.name].field_names
275 struct_fields = struct_defn.field_names
267276 if field_name not in struct_fields:
268277 raise CastileTypeError("undefined field")
269278 index = struct_fields[field_name]
270279 # we make this value available to compiler backends
271280 ast.aux = index
272281 # we look up the type from the StructDefinition
273 ast.type = self.structs[t.name].content_types[index]
282 ast.type = struct_defn.content_types[index]
274283 elif ast.tag == 'TypeCase':
275284 t1 = self.type_of(ast.children[0])
276285 t2 = self.type_of(ast.children[1])
2323 APPLIANCES="$APPLIANCES tests/appliances/castile-c-c.md"
2424 fi
2525
26 APPLIANCES="tests/appliances/castile-c-c.md"
26 #APPLIANCES="tests/appliances/castile-c-c.md"
2727 #APPLIANCES="tests/appliances/castile-c-javascript.md"
2828
2929 falderal $APPLIANCES tests/Castile.md
12611261 | struct list {
12621262 | value: string;
12631263 | next: list|void;
1264 | } for (cons, singleton, len)
1264 | } for (cons, singleton, length)
12651265 |
12661266 | fun cons(v: string, l: list) {
12671267 | make list(value:v, next:l as list|void)
12861286 | struct list {
12871287 | value: string;
12881288 | next: list|void;
1289 | } for (cons, singleton, len)
1289 | } for (cons, singleton, length)
12901290 |
12911291 | fun cons(v: string, l: list) {
12921292 | make list(value:v, next:l as list|void)
13111311 | struct list {
13121312 | value: string;
13131313 | next: list|void;
1314 | } for (cons, singleton, len)
1314 | } for (cons, singleton, length)
13151315 |
13161316 | fun cons(v: string, l: list) {
13171317 | make list(value:v, next:l as list|void)
13251325 | l = cons("first", cons("second", singleton("third")));
13261326 | print(l.value);
13271327 | }
1328 ? value
1328 ? struct