Files
highground/preprocessor.py
2025-09-02 20:41:17 +10:00

66 lines
1.7 KiB
Python

import token
delimiters = ["=", ">", "<", "+", "-", "*", "/", "(", ")"]
quick_tokens = [">", "<", "+", "-", "*", "/", "(", ")"]
def doNothing():
return
def process_line(process: str) -> list[token.Token]:
tokens: list[token.Token] = []
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 = ""
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