Skip to content

Commit

Permalink
Allow future function label assignment.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkroeni authored and liorgold2 committed Sep 28, 2021
1 parent cf8266f commit c25f826
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/starkware/cairo/lang/compiler/ast/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def to_expr_str(self):
return self.identifier.to_expr_str()

@property
def locaion(self):
def location(self):
return self.identifier.location

def get_children(self) -> Sequence[Optional[AstNode]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ExprDeref,
Expression,
ExprHint,
ExprFutureLabel,
ExprIdentifier,
ExprNeg,
ExprOperator,
Expand Down Expand Up @@ -161,6 +162,12 @@ def rewrite_ExprDeref(self, expr: ExprDeref, sim: SimplicityLevel):
)
return expr if sim is SimplicityLevel.OPERATION else self.wrap(expr)

def rewrite_ExprFutureLabel(self, expr: ExprFutureLabel, sim: SimplicityLevel):
# Treat this as a constant.
if sim in [SimplicityLevel.DEREF_CONST, SimplicityLevel.OPERATION]:
return expr
return self.wrap(expr)

def rewrite_ExprHint(self, expr: ExprHint, sim: SimplicityLevel):
return self.wrap(expr)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1899,7 +1899,7 @@ def get_variable(self, var: ExprIdentifier):
assert identifier_definition is not None

if isinstance(identifier_definition, FutureIdentifierDefinition):
if identifier_definition.identifier_type == LabelDefinition:
if identifier_definition.identifier_type in [LabelDefinition, FunctionDefinition]:
# Allow future label assignment.
return ExprFutureLabel(identifier=var)
raise PreprocessorError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from starkware.cairo.lang.compiler.error_handling import LocationError
from starkware.cairo.lang.compiler.identifier_definition import (
ConstDefinition,
FunctionDefinition,
LabelDefinition,
ReferenceDefinition,
)
Expand Down Expand Up @@ -167,6 +168,37 @@ def test_assign_future_label():
)


def test_assign_future_function_label():
code = """\
g(f)
g((f + 1) * 2 + 3)
func f() -> ():
ret
end
func g(x: felt) -> ():
ret
end
"""
program = preprocess_str(code=code, prime=PRIME)
f_definition = program.identifiers.get_by_full_name(ScopedName.from_string("test_scope.f"))
assert isinstance(f_definition, FunctionDefinition)
assert (
program.format()
== f"""\
[ap] = {f_definition.pc}; ap++
call rel 13
[ap] = {f_definition.pc}; ap++
[ap] = [ap + (-1)] + 1; ap++
[ap] = [ap + (-1)] * 2; ap++
[ap] = [ap + (-1)] + 3; ap++
call rel 3
ret
ret
"""
)


def test_temporary_variable():
code = """\
struct T:
Expand Down

0 comments on commit c25f826

Please sign in to comment.