Skip to content

Commit

Permalink
Fix statics
Browse files Browse the repository at this point in the history
  • Loading branch information
angelcaru committed Jun 1, 2024
1 parent 9ccc036 commit ceab54a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 4 additions & 2 deletions core/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def assign(
return res
return res.success(value)

res.register(context.symbol_table.set_var(var_name, value, qualifier))
qualifier_str = None if qualifier is None else qualifier.value
assert qualifier_str is None or isinstance(qualifier_str, str)
res.register(context.symbol_table.set_var(var_name, value, qualifier_str))
if res.should_return():
return res
return res.success(value)
Expand Down Expand Up @@ -524,7 +526,7 @@ def visit_FuncDefNode(self, node: FuncDefNode, context: Context) -> RTResult[Val
assert context.symbol_table is not None
assert isinstance(func_name, str), "this could be a bug in the parser"
if node.static:
context.symbol_table.set_static(func_name, func_value)
context.symbol_table.set_static(func_name, func_value, "var")
else:
context.symbol_table.set(func_name, func_value)

Expand Down
6 changes: 2 additions & 4 deletions core/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1690,13 +1690,11 @@ def set(self, name: str, value: Value) -> RTResult[None]:
self.symbols[name] = value
return RTResult[None]().success(None)

def set_var(self, name: str, value: Value, qualifier_tok: Optional[Token] = None) -> RTResult[None]:
def set_var(self, name: str, value: Value, qualifier: Optional[str] = None) -> RTResult[None]:
if name in self.consts:
return RTResult[None]().failure(
RTError(value.pos_start, value.pos_end, f"Cannot reassign to constant {name}", value.context)
)
qualifier = None if qualifier_tok is None else qualifier_tok.value
assert qualifier is None or isinstance(qualifier, str)
match qualifier:
case None:
if name in self.symbols:
Expand All @@ -1723,7 +1721,7 @@ def set_var(self, name: str, value: Value, qualifier_tok: Optional[Token] = None
self.consts.add(name)
return RTResult[None]().success(None)

def set_static(self, name: str, value: Value, qualifier: Optional[Token] = None) -> RTResult[None]:
def set_static(self, name: str, value: Value, qualifier: Optional[str] = None) -> RTResult[None]:
res = RTResult[None]()
res.register(self.set_var(name, value, qualifier))
if res.should_return():
Expand Down
4 changes: 2 additions & 2 deletions tests/classes.rn
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class Foo {
return Foo(this.x + other.x)
}

fun i_am_static() {
static fun i_am_static() {
print("i am static")
}
const c = "i am static const"
static const c = "i am static const"
}

var a = Foo(34)
Expand Down

0 comments on commit ceab54a

Please sign in to comment.