Abstract syntax tree

This commit is contained in:
2025-09-02 20:41:17 +10:00
parent 5ebf653342
commit 8d3ffb7b9c
5 changed files with 222 additions and 50 deletions

View File

@@ -1,44 +1,66 @@
import token
delimiters = ["=", ">", "<", "+", "-", "*", "/", " "]
delimiters = ["=", ">", "<", "+", "-", "*", "/", "(", ")"]
quick_tokens = [">", "<", "+", "-", "*", "/"]
quick_tokens = [">", "<", "+", "-", "*", "/", "(", ")"]
def doNothing():
return
def process_line(process: str) -> list[token.Token]:
buf = ""
tokens: list[token.Token] = []
prevEquals = False
for c in process:
if c in delimiters and buf != "":
tokens.append(token.Token(buf))
buf = ""
if prevEquals and c != '=':
tokens.append(token.Token("="))
prevEquals = False
if c in quick_tokens:
tokens.append(token.Token(c))
if buf != "":
buf = ""
i = 0
while i < len(process):
char = process[i]
if char == '"':
# End of buffer before string starts
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 != "":
i += 1
start = i
while i < len(process) and process[i] != '"':
i += 1
string_content = process[start:i]
# Create string token, assuming constructor wants quotes
tokens.append(token.Token(f'"{string_content}"'))
if i < len(process) and process[i] == '"':
i += 1 # Skip closing quote
continue
if char.isspace():
if buf:
tokens.append(token.Token(buf))
buf = ""
i += 1
continue
# Handle multi-char operators like '=='
if char == '=' and i + 1 < len(process) and process[i+1] == '=':
if buf:
tokens.append(token.Token(buf))
buf = ""
tokens.append(token.Token('=='))
i += 2
continue
if char in delimiters:
if buf:
tokens.append(token.Token(buf))
buf = ""
tokens.append(token.Token(char))
i += 1
continue
buf += char
i += 1
if buf:
tokens.append(token.Token(buf))
return tokens