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.
105 lines
3.8 KiB
105 lines
3.8 KiB
#!/usr/bin/env python3
|
|
import json
|
|
import operator
|
|
import sys
|
|
tokens = dict()
|
|
program = './tokens.json'
|
|
if len(sys.argv) > 1:
|
|
program = sys.argv[1]
|
|
with open(program, '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()
|
|
token = peek(index + 1)
|
|
if token['variant'] != "operator":
|
|
return uncollapsed_tokens
|
|
maybe_expression = peek(index + 2)
|
|
if maybe_expression['variant'] == 'expression':
|
|
uncollapsed_tokens.append(token)
|
|
return uncollapsed_tokens
|
|
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
|
|
|
|
def collect_parameters(params, line_number, usefull_tokens) -> list:
|
|
result = list()
|
|
for param in params:
|
|
candidate_token = dict()
|
|
for token in usefull_tokens:
|
|
if line_number > token['line_number'] and param == token['signifier']:
|
|
candidate_token = token
|
|
if candidate_token:
|
|
result.append(candidate_token)
|
|
return result
|
|
|
|
def collapse_tokens(uncollapsed_tokens: list) -> list:
|
|
usefull_tokens = list()
|
|
for index in range(0, len(uncollapsed_tokens)):
|
|
token = uncollapsed_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)
|
|
|
|
if token['variant'] == 'function_call':
|
|
usefull_token = {"variant": "function_call", "signifier": token['value'], "line_number": token['line_number'], "params": token['params']}
|
|
|
|
usefull_tokens.append(usefull_token)
|
|
|
|
usefull_tokens.sort(key=operator.itemgetter('line_number'))
|
|
return usefull_tokens
|
|
|
|
collapsed_tokens = collapse_tokens(tokens)
|
|
|
|
for index in range(0, len(collapsed_tokens)):
|
|
token = collapsed_tokens[index]
|
|
if token['variant'] == 'function_call':
|
|
function = collect_parameters([token['signifier']], token['line_number'],collapsed_tokens.copy())
|
|
collapsed_tokens[index]['function'] = function
|
|
params = collect_parameters(token['params'], token['line_number'],collapsed_tokens.copy())
|
|
collapsed_tokens[index]['params'] = params
|
|
print(json.dumps(collapsed_tokens))
|