Files
BrainAssembly/src/preprocess.py

31 lines
873 B
Python
Raw Normal View History

2025-10-18 19:55:02 +11:00
def preprocess(code: str) -> list[list[str]]:
token : str = ''
tokenise : list[list[str]] = [[]]
2025-10-16 13:21:27 +11:00
isString : bool = False
2025-10-18 19:55:02 +11:00
isComment : bool = False
2025-10-16 13:21:27 +11:00
for i in code:
2025-10-18 19:55:02 +11:00
if i == '\n':
2025-10-16 13:21:27 +11:00
isComment = False
2025-10-18 19:55:02 +11:00
if tokenise[-1] != []:
if token != '':
tokenise[-1].append(token)
tokenise.append([])
token = ''
2025-10-16 13:21:27 +11:00
elif isComment:
pass
2025-10-18 19:55:02 +11:00
elif i == '#':
isComment = True
elif (i != ' ') | isString:
2025-10-16 13:21:27 +11:00
token += i
2025-10-18 19:55:02 +11:00
if (i == '\"'):
isString = not(isString)
else:
tokenise[-1].append(token)
2025-10-16 13:21:27 +11:00
token = ''
2025-10-18 19:55:02 +11:00
if token != '':
tokenise[-1].append(token)
token = ''
if tokenise[-1] == []:
tokenise = tokenise[:-1]
2025-10-16 13:21:27 +11:00
return tokenise