From 3f5db406ebe87e44d540d093e092284a9df0f952 Mon Sep 17 00:00:00 2001 From: Pavel Demin Date: Sat, 5 Oct 2019 11:26:43 +0200 Subject: [PATCH] improve compatibility with older python 3 versions --- ast.py | 6 +++--- fx92.py | 8 ++++---- interpreter.py | 4 ++-- lexer.py | 15 +++++++-------- parser.py | 2 +- printer.py | 6 +++--- 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/ast.py b/ast.py index 2edfb14..a95b29d 100644 --- a/ast.py +++ b/ast.py @@ -1,6 +1,6 @@ # fx-92 Scientifique Collège+ language interpreter: AST definition -import enum +import aenum as enum #--- # Internal AST node representation @@ -58,9 +58,9 @@ class Node: return str(self.value) try: name = N(self.type).name - return f"" + return "".format(name) except ValueError: - return f"" + return "".format(hex(self.type)) @property def value(self): diff --git a/fx92.py b/fx92.py index 9e24a21..b468ef3 100755 --- a/fx92.py +++ b/fx92.py @@ -13,8 +13,8 @@ from interpreter import Context # Main program #--- -usage_string = f""" -usage: {sys.argv[0]} [-s|-u] [options...] +usage_string = """ +usage: {} [-s|-u] [options...] If "-" is specified as input, stdin is read. @@ -26,7 +26,7 @@ Output options: --quiet Do not show the SDL window --save= Save a copy of the screen output in a bitmap file --scale= 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 diff --git a/interpreter.py b/interpreter.py index 560e862..252cdea 100644 --- a/interpreter.py +++ b/interpreter.py @@ -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.""" diff --git a/lexer.py b/lexer.py index 27b6885..ec52e33 100644 --- a/lexer.py +++ b/lexer.py @@ -81,7 +81,7 @@ class Token: try: base = T(self.type).name except ValueError: - base = f"" + base = "".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.""" diff --git a/parser.py b/parser.py index 35f4333..b4d7c44 100644 --- a/parser.py +++ b/parser.py @@ -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) diff --git a/printer.py b/printer.py index 94e1d73..0929d0c 100644 --- a/printer.py +++ b/printer.py @@ -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: