From 5ebf653342d96e1ae832e48acf535d10d2298431 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Mon, 1 Sep 2025 21:06:05 +1000 Subject: [PATCH] Further progress --- main.py | 9 ++------- preprocessor.py | 34 +++++++++++++++++++++------------- test.high | 4 ++++ token.py | 19 +++++++++++-------- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/main.py b/main.py index af7762d..5f26564 100644 --- a/main.py +++ b/main.py @@ -1,17 +1,12 @@ import sys import preprocessor -import token if len(sys.argv) < 2: print("Usage: hgc (file)") exit(1) -file = open(sys.argv[1]).readlines() - -lines: list[token.tokenlist] = [] - -for line in file: - lines.append(preprocessor.process_line(line)) +with open(sys.argv[1], "r") as file: + lines = [preprocessor.process_line(line) for line in file] for line in lines: print(line) \ No newline at end of file diff --git a/preprocessor.py b/preprocessor.py index 1c9ddc4..82da1b0 100644 --- a/preprocessor.py +++ b/preprocessor.py @@ -2,6 +2,8 @@ import token delimiters = ["=", ">", "<", "+", "-", "*", "/", " "] +quick_tokens = [">", "<", "+", "-", "*", "/"] + def doNothing(): return @@ -16,19 +18,25 @@ def process_line(process: str) -> list[token.Token]: if prevEquals and c != '=': tokens.append(token.Token("=")) prevEquals = False - match c: - case '\n': - doNothing() - case ' ': - doNothing() - case '=': - if prevEquals: - prevEquals = False - tokens.append(token.Token(buf)) - else: - prevEquals = True - case _: - buf += c + if c in quick_tokens: + tokens.append(token.Token(c)) + if buf != "": + tokens.append(token.Token(buf)) + buf = "" + else: + match c: + case '\n': + doNothing() + case ' ': + doNothing() + case '=': + if prevEquals: + prevEquals = False + tokens.append(token.Token("==")) + else: + prevEquals = True + case _: + buf += c if buf != "": tokens.append(token.Token(buf)) diff --git a/test.high b/test.high index 0785a87..cf01afc 100644 --- a/test.high +++ b/test.high @@ -1,3 +1,7 @@ let dingus = 10 +if "test" == "test" + +end + print(dingus) \ No newline at end of file diff --git a/token.py b/token.py index 02e512e..279efe8 100644 --- a/token.py +++ b/token.py @@ -13,17 +13,18 @@ class TokenType(Enum): ELSE = 9 WHILE = 10 LET = 11 + END = 12 - ADD = 12 - SUBTRACT = 13 - MULTIPLY = 14 - DIVIDE = 15 + ADD = 13 + SUBTRACT = 14 + MULTIPLY = 15 + DIVIDE = 16 - SET = 16 + SET = 17 - EQUAL = 17 - GREATER = 18 - LESSER = 19 + EQUAL = 18 + GREATER = 19 + LESSER = 20 UNKNOWN = 0 @@ -50,6 +51,8 @@ def get_type(process: str) -> TokenType: return TokenType.WHILE case "true" | "false": return TokenType.BOOLEAN + case "end": + return TokenType.END case "+": return TokenType.ADD case "-":