Add pass1 parser

main
Micke Nordin 3 years ago
parent 8cd72a3aec
commit ed7378090b
Signed by: micke
GPG Key ID: 0DA0A7A5708FE257

@ -0,0 +1,73 @@
#!/usr/bin/env python3
import json
import operator
tokens = dict()
with open('./tokens.json', 'r') as json_tokens:
tokens = json.loads(json_tokens.read())
usefull_tokens = list()
def peek(index: int) -> dict:
global tokens
result = dict()
if index < len(tokens):
result = tokens[index]
return result
def find_token_indicies(key: str) -> list:
global tokens
result = list()
index = 0
for token in tokens:
if token["value"] == key:
result.append(index)
index = index + 1
return result
def collect_body(index: int) -> list():
result = list()
token = peek(index + 1)
if token['variant'] != "operator":
return result
maybe_expression = peek(index + 2)
if maybe_expression['variant'] == 'expression':
result.append(token)
return result
elif maybe_expression['variant'] == 'start_block':
index = index + 2
while True:
index = index + 1
collectable = peek(index)
if collectable['variant'] == 'end_block':
break
result.append(collectable)
return result
for index in range(0, len(tokens)):
token = tokens[index]
# This is a declaration
if token['variant'] == 'type':
maybe_signifier = peek(index + 2)
expression = ''
line_number = token['line_number']
if maybe_signifier["variant"] == "signifier":
name = maybe_signifier['value']
for i in find_token_indicies(name):
maybe_expression = peek(i + 2)
if maybe_expression['variant'] == 'expression':
line_number = maybe_expression['line_number']
expression = maybe_expression['value']
break
usefull_token = { "variant": "variable_declaration", "signifier": name, "type": token['value'], "line_number": line_number, "expression": expression }
usefull_tokens.append(usefull_token)
if token['variant'] == 'function_declaration':
body = collect_body(index)
usefull_token = { "variant": "function_declaration", "signifier": token['value'], "line_number": token['line_number'], "params": token['params'] ,"body": body}
usefull_tokens.append(usefull_token)
sorted_tokens = usefull_tokens.sort(key=operator.itemgetter('line_number'))
print(json.dumps(usefull_tokens))
Loading…
Cancel
Save