You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.0 KiB

2 years ago
#!/usr/bin/env python3
import json
import operator
tokens = dict()
with open('./tokens.json', 'r') as json_tokens:
tokens = json.loads(json_tokens.read())
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:
global tokens
uncollapsed_tokens = list()
2 years ago
token = peek(index + 1)
if token['variant'] != "operator":
return uncollapsed_tokens
2 years ago
maybe_expression = peek(index + 2)
if maybe_expression['variant'] == 'expression':
uncollapsed_tokens.append(token)
return uncollapsed_tokens
2 years ago
elif maybe_expression['variant'] == 'start_block':
index = index + 2
while True:
index = index + 1
collectable = peek(index)
if collectable['variant'] == 'end_block':
break
uncollapsed_tokens.append(collectable)
return uncollapsed_tokens
2 years ago
def collapse_tokens(uncollapsed_tokens: list) -> list:
usefull_tokens = list()
for index in range(0, len(uncollapsed_tokens)):
token = uncollapsed_tokens[index]
2 years ago
# 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)
2 years ago
usefull_tokens.sort(key=operator.itemgetter('line_number'))
return usefull_tokens
2 years ago
collapsed_tokens = collapse_tokens(tokens)
2 years ago
#for index in range(0, len(collapsed_tokens)):
# token = collapsed_tokens[index]
# if token['variant'] == 'function_declaration':
# old_body = token['body'].copy()
# new_body = collapse_tokens(old_body)
# collapsed_tokens[index]['body'] = new_body
print(json.dumps(collapsed_tokens))