Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addr literal #61

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tealish/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

if TYPE_CHECKING:
from . import TealWriter
from .nodes import Block, Node, Func
from .nodes import Block, Node, Func, Literal

lang_spec = get_active_langspec()

Expand Down Expand Up @@ -154,10 +154,10 @@ def lookup_func(self, name: str) -> "Func":
def lookup_var(self, name: str) -> Any:
return self.get_scope().lookup_var(name)

def lookup_const(self, name: str) -> Tuple["AVMType", ConstValue]:
def lookup_const(self, name: str) -> Tuple["AVMType", Union["Literal", ConstValue]]:
return self.get_scope().lookup_const(name)

def lookup_avm_constant(self, name: str) -> Tuple["AVMType", Any]:
def lookup_avm_constant(self, name: str) -> Tuple["AVMType", ConstValue]:
return lang_spec.lookup_avm_constant(name)

# TODO: these attributes are only available on Node and other children types
Expand Down
17 changes: 12 additions & 5 deletions tealish/expression_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from .errors import CompileError
from .tealish_builtins import AVMType, get_struct
from .langspec import Op, type_lookup
from .scope import ConstValue


if TYPE_CHECKING:
from . import TealWriter
from .nodes import Node, Func, GenericExpression
from .nodes import Node, Func, GenericExpression, Literal


class Integer(BaseNode):
Expand Down Expand Up @@ -66,9 +67,11 @@ def __init__(self, name: str, parent: Optional[BaseNode] = None) -> None:
self.name = name
self.type: AVMType = AVMType.none
self.parent = parent
self.value: Union[ConstValue, "Literal"]

def process(self) -> None:
type, value = None, None
type: AVMType = AVMType.none
value: Union[ConstValue, "Literal"] = 0
try:
# user defined const
type, value = self.lookup_const(self.name)
Expand All @@ -87,10 +90,14 @@ def process(self) -> None:
self.value = value

def write_teal(self, writer: "TealWriter") -> None:
if self.type == AVMType.int:
writer.write(self, f"pushint {self.value} // {self.name}") # type: ignore
elif self.type == AVMType.bytes:
if isinstance(self.value, str) or isinstance(self.value, bytes):
assert self.type == AVMType.bytes
writer.write(self, f"pushbytes {self.value} // {self.name}") # type: ignore
elif isinstance(self.value, int):
assert self.type == AVMType.int
writer.write(self, f"pushint {self.value} // {self.name}")
else:
self.value.write_teal(writer)

def _tealish(self) -> str:
return f"{self.name}"
Expand Down
43 changes: 39 additions & 4 deletions tealish/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
from .scope import Scope, VarType

LITERAL_INT = r"[0-9]+"
LITERAL_BYTES = r'"(.+)"'
LITERAL_BYTE_STRING = r'"(.+)"'
LITERAL_BYTE_HEX = r"0x([a-fA-F0-9]+)"
LITERAL_BYTE_ADDR = r"([A-Z2-7]+)"
VARIABLE_NAME = r"[a-z_][a-zA-Z0-9_]*"

if TYPE_CHECKING:
Expand Down Expand Up @@ -140,7 +142,12 @@ class Literal(Expression):

@classmethod
def parse(cls, line: str, parent: Node, compiler: "TealishCompiler") -> Node:
matchable: List[Type[Expression]] = [LiteralInt, LiteralBytes]
matchable: List[Type[Expression]] = [
LiteralAddr,
LiteralHex,
LiteralInt,
LiteralBytes,
]
for expr in matchable:
if expr.match(line):
return expr(line, parent, compiler)
Expand All @@ -162,7 +169,35 @@ def _tealish(self) -> str:


class LiteralBytes(Literal):
pattern = rf"(?P<value>{LITERAL_BYTES})$"
pattern = rf"(?P<value>{LITERAL_BYTE_STRING})$"
value: str

def write_teal(self, writer: "TealWriter") -> None:
writer.write(self, f"pushbytes {self.value}")

def type(self) -> AVMType:
return AVMType.bytes

def _tealish(self) -> str:
return f"{self.value}"


class LiteralAddr(Literal):
pattern = rf"addr\((?P<value>{LITERAL_BYTE_ADDR})\)$"
value: str

def write_teal(self, writer: "TealWriter") -> None:
writer.write(self, f"addr {self.value}")

def type(self) -> AVMType:
return AVMType.bytes

def _tealish(self) -> str:
return f"addr({self.value})"


class LiteralHex(Literal):
pattern = rf"(?P<value>{LITERAL_BYTE_HEX})$"
value: str

def write_teal(self, writer: "TealWriter") -> None:
Expand Down Expand Up @@ -379,7 +414,7 @@ class Const(LineStatement):

def process(self) -> None:
scope = self.get_current_scope()
scope.declare_const(self.name, (self.type, self.expression.value))
scope.declare_const(self.name, (self.type, self.expression))

def write_teal(self, writer: "TealWriter") -> None:
pass
Expand Down
8 changes: 4 additions & 4 deletions tealish/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

if TYPE_CHECKING:
from .tealish_builtins import AVMType
from .nodes import Func, Block
from .nodes import Func, Block, Literal


VarType = Union["AVMType", Tuple[str, str]]
Expand All @@ -25,7 +25,7 @@ def __init__(
slot_range if slot_range is not None else (0, 200)
)

self.consts: Dict[str, Tuple["AVMType", ConstValue]] = {}
self.consts: Dict[str, Tuple["AVMType", Union[ConstValue, "Literal"]]] = {}
self.blocks: Dict[str, "Block"] = {}
self.functions: Dict[str, "Func"] = {}

Expand Down Expand Up @@ -63,11 +63,11 @@ def delete_var(self, name: str) -> None:
del self.slots[name]

def declare_const(
self, name: str, const_data: Tuple["AVMType", ConstValue]
self, name: str, const_data: Tuple["AVMType", Union["Literal", ConstValue]]
) -> None:
self.consts[name] = const_data

def lookup_const(self, name: str) -> Tuple["AVMType", ConstValue]:
def lookup_const(self, name: str) -> Tuple["AVMType", Union["Literal", ConstValue]]:
if name not in self.consts:
raise KeyError(f'Const "{name}" not declared in current scope')
return self.consts[name]
Expand Down
4 changes: 3 additions & 1 deletion tealish/tealish_expressions.tx
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ GroupIndex: NegativeGroupIndex | PositiveGroupIndex | Expression;
NegativeGroupIndex: '-' index=INT;
PositiveGroupIndex: '+' index=INT;
Integer: value=/[0-9_]+/;
Bytes: value=STRING;
HexBytes: value=/0x([a-fA-F0-9]+)/;
AddrBytes: value='addr('/([A-Z2-7]+)/ ')';
Bytes: value=STRING | HexBytes | AddrBytes;
Loading