Further progress
This commit is contained in:
9
main.py
9
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)
|
@@ -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))
|
||||
|
||||
|
19
token.py
19
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 "-":
|
||||
|
Reference in New Issue
Block a user