Skip to content

Commit

Permalink
Make field access limited by struct scope too. All tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpressey committed Feb 15, 2022
1 parent 1f6326e commit 3458fb6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
25 changes: 17 additions & 8 deletions src/castile/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ def collect_struct(self, ast):
type_exprs = []
i = 0
field_defns = ast.children[0].children
scope_idents = ast.children[1].children if len(ast.children) > 1 else None
scope_idents = None
if len(ast.children) > 1:
scope_idents = [a.value for a in ast.children[1].children]
for child in field_defns:
assert child.tag == 'FieldDefn', child.tag
field_name = child.value
Expand Down Expand Up @@ -243,13 +245,14 @@ def type_of(self, ast):
raise CastileTypeError("undefined struct %s" % t.name)
struct_defn = self.structs[t.name]
if struct_defn.scope_idents is not None:
scope_idents = [ast.value for ast in struct_defn.scope_idents]
if self.current_defn not in scope_idents:
raise CastileTypeError("inaccessible struct %s for make: %s not in %s" %
(t.name, self.current_defn, scope_idents)
if self.current_defn not in struct_defn.scope_idents:
raise CastileTypeError("inaccessible struct %s for make: %s not in %s" %
(t.name, self.current_defn, struct_defn.scope_idents)
)
if len(struct_defn.content_types) != len(ast.children) - 1:
raise CastileTypeError("argument mismatch")
raise CastileTypeError("argument mismatch; expected {}, got {} in {}".format(
len(struct_defn.content_types), len(ast.children) - 1, ast
))
i = 0
for defn in ast.children[1:]:
name = defn.value
Expand All @@ -263,15 +266,21 @@ def type_of(self, ast):
ast.type = self.type_of(ast.children[0])
elif ast.tag == 'Index':
t = self.type_of(ast.children[0])
struct_defn = self.structs[t.name]
if struct_defn.scope_idents is not None:
if self.current_defn not in struct_defn.scope_idents:
raise CastileTypeError("inaccessible struct %s for access: %s not in %s" %
(t.name, self.current_defn, struct_defn.scope_idents)
)
field_name = ast.value
struct_fields = self.structs[t.name].field_names
struct_fields = struct_defn.field_names
if field_name not in struct_fields:
raise CastileTypeError("undefined field")
index = struct_fields[field_name]
# we make this value available to compiler backends
ast.aux = index
# we look up the type from the StructDefinition
ast.type = self.structs[t.name].content_types[index]
ast.type = struct_defn.content_types[index]
elif ast.tag == 'TypeCase':
t1 = self.type_of(ast.children[0])
t2 = self.type_of(ast.children[1])
Expand Down
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if [ ! x`command -v gcc` = x ]; then
APPLIANCES="$APPLIANCES tests/appliances/castile-c-c.md"
fi

APPLIANCES="tests/appliances/castile-c-c.md"
#APPLIANCES="tests/appliances/castile-c-c.md"
#APPLIANCES="tests/appliances/castile-c-javascript.md"

falderal $APPLIANCES tests/Castile.md
Expand Down
8 changes: 4 additions & 4 deletions tests/Castile.md
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ is accomplished.
| struct list {
| value: string;
| next: list|void;
| } for (cons, singleton, len)
| } for (cons, singleton, length)
|
| fun cons(v: string, l: list) {
| make list(value:v, next:l as list|void)
Expand All @@ -1287,7 +1287,7 @@ is accomplished.
| struct list {
| value: string;
| next: list|void;
| } for (cons, singleton, len)
| } for (cons, singleton, length)
|
| fun cons(v: string, l: list) {
| make list(value:v, next:l as list|void)
Expand All @@ -1312,7 +1312,7 @@ is accomplished.
| struct list {
| value: string;
| next: list|void;
| } for (cons, singleton, len)
| } for (cons, singleton, length)
|
| fun cons(v: string, l: list) {
| make list(value:v, next:l as list|void)
Expand All @@ -1326,4 +1326,4 @@ is accomplished.
| l = cons("first", cons("second", singleton("third")));
| print(l.value);
| }
? value
? struct

0 comments on commit 3458fb6

Please sign in to comment.