Merge branch 'master' of Pavel/fx92-interpreter into master
This commit is contained in:
commit
1e7ca230d4
18
ast.py
18
ast.py
|
@ -2,6 +2,20 @@
|
|||
|
||||
import enum
|
||||
|
||||
def auto_builder():
|
||||
number = 0
|
||||
def auto():
|
||||
nonlocal number
|
||||
number += 1
|
||||
return number
|
||||
return auto
|
||||
|
||||
try:
|
||||
enum.auto()
|
||||
except AttributeError:
|
||||
enum.auto = auto_builder()
|
||||
del auto_builder
|
||||
|
||||
#---
|
||||
# Internal AST node representation
|
||||
#---
|
||||
|
@ -58,9 +72,9 @@ class Node:
|
|||
return str(self.value)
|
||||
try:
|
||||
name = N(self.type).name
|
||||
return f"<Node:{name}>"
|
||||
return "<Node:{}>".format(name)
|
||||
except ValueError:
|
||||
return f"<Node:{hex(self.type)}>"
|
||||
return "<Node:{}>".format(hex(self.type))
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
|
|
8
fx92.py
8
fx92.py
|
@ -13,8 +13,8 @@ from interpreter import Context
|
|||
# Main program
|
||||
#---
|
||||
|
||||
usage_string = f"""
|
||||
usage: {sys.argv[0]} [-s|-u] <file> [options...]
|
||||
usage_string = """
|
||||
usage: {} [-s|-u] <file> [options...]
|
||||
|
||||
If "-" is specified as input, stdin is read.
|
||||
|
||||
|
@ -26,7 +26,7 @@ Output options:
|
|||
--quiet Do not show the SDL window
|
||||
--save=<file> Save a copy of the screen output in a bitmap file
|
||||
--scale=<n> Window scale up (default 4, max 16)
|
||||
""".strip()
|
||||
""".format(sys.argv[0]).strip()
|
||||
|
||||
def usage(exitcode=None):
|
||||
print(usage_string, file=sys.stderr)
|
||||
|
@ -55,7 +55,7 @@ def main():
|
|||
|
||||
except getopt.GetoptError as e:
|
||||
print("error:", e, file=sys.stderr)
|
||||
print(f"Try '{sys.argv[0]} --help' for details.", file=sys.stderr)
|
||||
print("Try '{} --help' for details.".format(sys.argv[0]), file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
# Other parameters
|
||||
|
|
|
@ -212,7 +212,7 @@ class Context:
|
|||
}[n.args[0]]
|
||||
|
||||
if f is None:
|
||||
raise Exception(f"Function {n.args[0]} not there yet x_x")
|
||||
raise Exception("Function {} not there yet x_x".format(n.args[0]))
|
||||
return f(arg)
|
||||
|
||||
elif n.type == N.REL:
|
||||
|
@ -230,7 +230,7 @@ class Context:
|
|||
|
||||
else:
|
||||
t = N(n.type).name
|
||||
raise Exception(f"Unhandled node of type {t}")
|
||||
raise Exception("Unhandled node of type {}".format(t))
|
||||
|
||||
def runs(self, n):
|
||||
"""Evaluate all the arguments of a node."""
|
||||
|
|
15
lexer.py
15
lexer.py
|
@ -81,7 +81,7 @@ class Token:
|
|||
try:
|
||||
base = T(self.type).name
|
||||
except ValueError:
|
||||
base = f"<Token:{hex(self.type)}>"
|
||||
base = "<Token:{}>".format(hex(self.type))
|
||||
|
||||
if self.args:
|
||||
args = "(" + ",".join(repr(arg) for arg in self.args) + ")"
|
||||
|
@ -140,7 +140,7 @@ class BitcodeLexer(LexerBase):
|
|||
if h[p] in [0xF9, 0xFB]:
|
||||
# Stop if there is no trailing byte
|
||||
if p >= len(h) - 1:
|
||||
print(f"[lexer] Invalid trailing byte {hex(h[p])}")
|
||||
print("[lexer] Invalid trailing byte {}".format(hex(h[p])))
|
||||
p = len(h)
|
||||
return Token(T.END)
|
||||
|
||||
|
@ -158,7 +158,7 @@ class BitcodeLexer(LexerBase):
|
|||
if h[p] == 0xFB and h[p+1] in rels:
|
||||
return Token(T.REL, rels[h[p+1]])
|
||||
|
||||
print(f"[lexer] Unknown opcode {hex(code)}")
|
||||
print("[lexer] Unknown opcode {}".format(hex(code)))
|
||||
self.errors += 1
|
||||
|
||||
# Try to read another token
|
||||
|
@ -190,11 +190,11 @@ class BitcodeLexer(LexerBase):
|
|||
match = re.match(re_const, h[p:])
|
||||
|
||||
if match is not None:
|
||||
text = match[1].replace(b'\x2E', b'.').replace(b'\x2D', b'e')
|
||||
text = match.group(1).replace(b'\x2E', b'.').replace(b'\x2D', b'e')
|
||||
self.pos += len(text) - 1
|
||||
|
||||
f = float(text.decode('utf-8'))
|
||||
if match[2] == "%":
|
||||
if match.group(2) == "%":
|
||||
f /= 100
|
||||
return Token(T.CONST, f)
|
||||
|
||||
|
@ -241,7 +241,7 @@ class BitcodeLexer(LexerBase):
|
|||
if code in fun:
|
||||
return Token(T.FUN, fun[code])
|
||||
|
||||
print(f"[lexer] Unknown opcode {hex(code)}")
|
||||
print("[lexer] Unknown opcode {}".format(hex(code)))
|
||||
self.errors += 1
|
||||
|
||||
# Try to read another token after skipping one byte
|
||||
|
@ -283,7 +283,6 @@ class UrlLexer(BitcodeLexer):
|
|||
|
||||
if not re.fullmatch(r'(?:[0-9a-fA-F]{2})+', url):
|
||||
print("[urlparser] URL is not strict hexa, noise will be skipped")
|
||||
|
||||
super().__init__(bytes.fromhex(url))
|
||||
|
||||
#---
|
||||
|
@ -397,7 +396,7 @@ class TextLexer(LexerBase):
|
|||
err = s[0]
|
||||
self.code = s[1] if len(s) > 1 else ""
|
||||
|
||||
raise Exception(f"Lexical error near '{err}'")
|
||||
raise Exception("Lexical error near '{}'".format(err))
|
||||
|
||||
def at_end(self):
|
||||
"""Check whether the whole input has been read."""
|
||||
|
|
|
@ -74,7 +74,7 @@ class Parser:
|
|||
expected = [T(t).name for t in types]
|
||||
got = T(self.la.type).name
|
||||
pos = self.lexer.position
|
||||
err = f"Expected one of {expected}, got {got} (at token {pos})"
|
||||
err = "Expected one of {}, got {} (at token {})".format(expected, got, pos)
|
||||
print("[urlparser] " + err)
|
||||
raise Exception("Syntax error: " + err)
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ def print_ast(n, lang="en", indent=0):
|
|||
print(" " * indent, end="")
|
||||
|
||||
if not isinstance(n, Node):
|
||||
print(f"{type(n)}({n})")
|
||||
print("{}({})".format(type(n), n))
|
||||
return
|
||||
|
||||
if n.type in [N.CONST, N.VAR, N.REL]:
|
||||
|
@ -54,7 +54,7 @@ def print_ast(n, lang="en", indent=0):
|
|||
if hasattr(lang, id):
|
||||
print(getattr(lang, id).format(*n.args))
|
||||
else:
|
||||
print(f"{n.type.name}")
|
||||
print("{}".format(n.type.name))
|
||||
|
||||
if n.type in [N.FORWARD, N.ROTATE, N.ORIENT, N.GOTO] and \
|
||||
n.constchildren():
|
||||
|
@ -63,7 +63,7 @@ def print_ast(n, lang="en", indent=0):
|
|||
if n.type == N.ASSIGN:
|
||||
print_ast(n.args[0], lang=lang, indent=indent+2)
|
||||
print(" " * (indent+2), end="")
|
||||
print(f"->{n.args[1]}")
|
||||
print("->{}".format(n.args[1]))
|
||||
return
|
||||
|
||||
for arg in n.args:
|
||||
|
|
Loading…
Reference in New Issue