-
Notifications
You must be signed in to change notification settings - Fork 10
Open
Description
algorand/go-algorand#4737 is introducing support for macros in TEAL.
One useful thing to do with macros is use them to define the constants Tealish supports.
I believe the following patch will enable that:
diff --git a/tealish/expression_nodes.py b/tealish/expression_nodes.py
index 4b365ac..409daca 100644
--- a/tealish/expression_nodes.py
+++ b/tealish/expression_nodes.py
@@ -78,9 +78,9 @@ class Constant(BaseNode):
def write_teal(self, writer):
if self.type == "int":
- writer.write(self, f"pushint {self.value} // {self.name}")
+ writer.write(self, f"pushint {self.name} // {self.value}")
elif self.type == "bytes":
- writer.write(self, f"pushbytes {self.value} // {self.name}")
+ writer.write(self, f"pushbytes {self.name} // {self.value}")
def _tealish(self, formatter=None):
return f"{self.name}"
diff --git a/tealish/nodes.py b/tealish/nodes.py
index 62f9ce1..d5a53d9 100644
--- a/tealish/nodes.py
+++ b/tealish/nodes.py
@@ -334,6 +334,7 @@ class Const(LineStatement):
scope["consts"][self.name] = [self.type, self.expression.value]
def write_teal(self, writer):
+ writer.write (self, f"#define {self.name} {self.expression.value}")
pass
def _tealish(self, formatter=None):
this will result in tealish that goes from
#pragma version 8
struct Item:
id: int
foo: int
name: bytes[10]
end
const int KNOWN_ID = 64738
const bytes BOX_NAME = "tealishbox"
const bytes KNOWN_ITEM = "fearghal"
box<Item> item1 = CreateBox(BOX_NAME)
item1.id = KNOWN_ID
item1.foo = KNOWN_ID + 1
item1.name = KNOWN_ITEM
log(itob(item1.id))
log(itob(item1.foo))
log(item1.name)
to TEAL
#pragma version 8
#define KNOWN_ID 64738
#define BOX_NAME "tealishbox"
#define KNOWN_ITEM "fearghal"
// box<Item> item1 = CreateBox(BOX_NAME) [slot 0]
pushbytes BOX_NAME // "tealishbox"
dup
pushint 26
box_create
assert // assert created
store 0 // item1
// item1.id = KNOWN_ID [box]
load 0 // box key item1
pushint 0 // offset
pushint KNOWN_ID // 64738
itob
box_replace // item1.id
// item1.foo = KNOWN_ID + 1 [box]
load 0 // box key item1
pushint 8 // offset
pushint KNOWN_ID // 64738
pushint 1
+
itob
box_replace // item1.foo
// item1.name = KNOWN_ITEM [box]
load 0 // box key item1
pushint 16 // offset
pushbytes KNOWN_ITEM // "fearghal"
box_replace // item1.name
// log(itob(item1.id))
load 0 // box key item1
pushint 0 // offset
pushint 8 // size
box_extract // item1.id
btoi
itob
log
// log(itob(item1.foo))
load 0 // box key item1
pushint 8 // offset
pushint 8 // size
box_extract // item1.foo
btoi
itob
log
// log(item1.name)
load 0 // box key item1
pushint 16 // offset
pushint 10 // size
box_extract // item1.name
log
Metadata
Metadata
Assignees
Labels
No labels