🐐 String interpolation
This commit is contained in:
@@ -275,6 +275,7 @@ class DataRangeArray(Node):
|
||||
class String(Node):
|
||||
def __init__(self, value) -> None:
|
||||
self.value = value
|
||||
self.quote = '"'
|
||||
|
||||
def __eq__(self, other):
|
||||
return type(self) == type(other) and self.value == other.value
|
||||
@@ -283,12 +284,60 @@ class String(Node):
|
||||
return f"String({self.value})"
|
||||
|
||||
def print(self):
|
||||
return f'"{self.value}"'
|
||||
if self.quote == "'":
|
||||
return f"'{self.value}'"
|
||||
escaped = self.value.replace('{', '{{').replace('}', '}}')
|
||||
return f'"{escaped}"'
|
||||
|
||||
def _eval(self, vtable):
|
||||
return vtable, ValStr(self.value)
|
||||
|
||||
|
||||
def _flip_quotes(node, quote):
|
||||
"""Recursively set quote style on all String nodes in an expression tree."""
|
||||
if isinstance(node, String):
|
||||
node.quote = quote
|
||||
for attr in vars(node).values():
|
||||
if isinstance(attr, Node):
|
||||
_flip_quotes(attr, quote)
|
||||
elif isinstance(attr, list):
|
||||
for item in attr:
|
||||
if isinstance(item, Node):
|
||||
_flip_quotes(item, quote)
|
||||
|
||||
|
||||
class InterpolatedString(Node):
|
||||
def __init__(self, parts) -> None:
|
||||
self.parts = parts
|
||||
|
||||
def __eq__(self, other):
|
||||
return type(self) == type(other) and self.parts == other.parts
|
||||
|
||||
def __repr__(self):
|
||||
return f"InterpolatedString([{rep_join(self.parts)}])"
|
||||
|
||||
def print(self):
|
||||
result = '"'
|
||||
for part in self.parts:
|
||||
if isinstance(part, String):
|
||||
result += part.value.replace('{', '{{').replace('}', '}}')
|
||||
else:
|
||||
_flip_quotes(part, "'")
|
||||
result += '{' + part.print() + '}'
|
||||
_flip_quotes(part, '"')
|
||||
result += '"'
|
||||
return result
|
||||
|
||||
def _eval(self, vtable):
|
||||
magnvm = "MAGNVM" in vtable["#modules"]
|
||||
svbnvlla = "SVBNVLLA" in vtable["#modules"]
|
||||
pieces = []
|
||||
for part in self.parts:
|
||||
vtable, val = part.eval(vtable)
|
||||
pieces.append(make_string(val, magnvm, svbnvlla))
|
||||
return vtable, ValStr(''.join(pieces))
|
||||
|
||||
|
||||
class Numeral(Node):
|
||||
def __init__(self, value: str) -> None:
|
||||
self.value = value
|
||||
|
||||
Reference in New Issue
Block a user