Files
BrainAssembly/src/preprocess.py
2025-11-01 10:30:03 +11:00

31 lines
871 B
Python

def preprocess(code: str) -> list[list[str]]:
token : str = ''
tokenise : list[list[str]] = [[]]
isString : bool = False
isComment : bool = False
for i in code:
if i == '\n':
isComment = False
if token != '':
tokenise[-1].append(token)
if tokenise[-1]:
tokenise.append([])
token = ''
elif isComment:
pass
elif i == '#':
isComment = True
elif (i != ' ') | isString:
token += i
if (i == '\"'):
isString = not(isString)
elif token != '':
tokenise[-1].append(token)
token = ''
if token != '':
tokenise[-1].append(token)
token = ''
if tokenise[-1] == []:
tokenise = tokenise[:-1]
return tokenise