Skip to content

Commit

Permalink
Merge pull request #50 from tinymanorg/pseudo-ops
Browse files Browse the repository at this point in the history
Support the 'method' pseudo op
  • Loading branch information
fergalwalsh authored Mar 24, 2023
2 parents 8ac2ad0 + 5c57a00 commit 8d56928
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions tealish/expression_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ def process_op_call(self, op: Op) -> None:
immediates[i] = x.name
elif isinstance(x, Integer):
immediates[i] = x.value
elif isinstance(x, Bytes):
immediates[i] = f'"{x.value}"'
self.immediate_args = " ".join(map(str, immediates))
returns = op.returns_types[::-1]
self.type = returns[0] if len(returns) == 1 else returns
Expand Down
17 changes: 16 additions & 1 deletion tealish/langspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@

abc = "ABCDEFGHIJK"

# Hopefully these will eventually be added to langspec.json. Including here until then.
# Note: Other pseudo ops like addr and base32 are not included here because their syntax isn't parseable by Tealish currently.
# e.g addr(RIKLQ5HEVXAOAWYSW2LGQFYGWVO4J6LIAQQ72ZRULHZ4KS5NRPCCKYPCUU) is not parseable because the address isn't quoted.
pseudo_ops = [
{
"Name": "method",
"Opcode": "method",
"Size": 2,
"Args": [],
"Returns": ["B"],
},
]


_opcode_type_map = {
".": AVMType.any,
Expand Down Expand Up @@ -187,7 +200,9 @@ class LangSpec:
def __init__(self, spec: Dict[str, Any]) -> None:
self.is_packaged = False
self.spec = spec
self.ops: Dict[str, Op] = {op["Name"]: Op(op) for op in spec["Ops"]}
self.ops: Dict[str, Op] = {
op["Name"]: Op(op) for op in (spec["Ops"] + pseudo_ops)
}

self.fields: Dict[str, Any] = {
"Global": self.ops["global"].arg_enum_dict,
Expand Down
20 changes: 20 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,26 @@ def test_pass_int_field_assignment(self):
)


class TestPseudoOp(unittest.TestCase):
def test_pass_method_void(self):
teal = compile_expression_min('method("name(uint64,uint64)")')
self.assertListEqual(teal, ['method "name(uint64,uint64)"'])

def test_pass_method_return(self):
teal = compile_expression_min('method("name(uint64,uint64)uint64")')
self.assertListEqual(teal, ['method "name(uint64,uint64)uint64"'])

@expectedFailure
def test_pass_method_const(self):
teal = compile_min(
[
'const bytes SIG = "name(uint64,uint64)"',
"log(method(SIG))",
]
)
self.assertListEqual(teal, ['method "name(uint64,uint64)"', "log"])


class TestEverythingProgram(unittest.TestCase):
maxDiff = None

Expand Down

0 comments on commit 8d56928

Please sign in to comment.