Further progress

This commit is contained in:
2025-09-01 21:06:05 +10:00
parent a604b8d5c4
commit 5ebf653342
4 changed files with 38 additions and 28 deletions

View File

@@ -1,17 +1,12 @@
import sys import sys
import preprocessor import preprocessor
import token
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Usage: hgc (file)") print("Usage: hgc (file)")
exit(1) exit(1)
file = open(sys.argv[1]).readlines() with open(sys.argv[1], "r") as file:
lines = [preprocessor.process_line(line) for line in file]
lines: list[token.tokenlist] = []
for line in file:
lines.append(preprocessor.process_line(line))
for line in lines: for line in lines:
print(line) print(line)

View File

@@ -2,6 +2,8 @@ import token
delimiters = ["=", ">", "<", "+", "-", "*", "/", " "] delimiters = ["=", ">", "<", "+", "-", "*", "/", " "]
quick_tokens = [">", "<", "+", "-", "*", "/"]
def doNothing(): def doNothing():
return return
@@ -16,19 +18,25 @@ def process_line(process: str) -> list[token.Token]:
if prevEquals and c != '=': if prevEquals and c != '=':
tokens.append(token.Token("=")) tokens.append(token.Token("="))
prevEquals = False prevEquals = False
match c: if c in quick_tokens:
case '\n': tokens.append(token.Token(c))
doNothing() if buf != "":
case ' ': tokens.append(token.Token(buf))
doNothing() buf = ""
case '=': else:
if prevEquals: match c:
prevEquals = False case '\n':
tokens.append(token.Token(buf)) doNothing()
else: case ' ':
prevEquals = True doNothing()
case _: case '=':
buf += c if prevEquals:
prevEquals = False
tokens.append(token.Token("=="))
else:
prevEquals = True
case _:
buf += c
if buf != "": if buf != "":
tokens.append(token.Token(buf)) tokens.append(token.Token(buf))

View File

@@ -1,3 +1,7 @@
let dingus = 10 let dingus = 10
if "test" == "test"
end
print(dingus) print(dingus)

View File

@@ -13,17 +13,18 @@ class TokenType(Enum):
ELSE = 9 ELSE = 9
WHILE = 10 WHILE = 10
LET = 11 LET = 11
END = 12
ADD = 12 ADD = 13
SUBTRACT = 13 SUBTRACT = 14
MULTIPLY = 14 MULTIPLY = 15
DIVIDE = 15 DIVIDE = 16
SET = 16 SET = 17
EQUAL = 17 EQUAL = 18
GREATER = 18 GREATER = 19
LESSER = 19 LESSER = 20
UNKNOWN = 0 UNKNOWN = 0
@@ -50,6 +51,8 @@ def get_type(process: str) -> TokenType:
return TokenType.WHILE return TokenType.WHILE
case "true" | "false": case "true" | "false":
return TokenType.BOOLEAN return TokenType.BOOLEAN
case "end":
return TokenType.END
case "+": case "+":
return TokenType.ADD return TokenType.ADD
case "-": case "-":