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.

45 lines
1.3 KiB

2 years ago
#!/usr/bin/env python3
import json
tokens = dict()
with open('./tokens.json', 'r') as json_tokens:
tokens = json.loads(json_tokens.read())
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 peek(index: int) -> dict:
global tokens
result = dict()
if index < len(tokens):
result = tokens[index]
return result
current_scope = "main"
stack = [{"scope": current_scope}]
ast = {current_scope: {"env": stack[-1] }}
for index in range(0, len(tokens)):
token = tokens[index]
if token['variant'] == "start_block":
current_scope = "user_" + str(token["line_number"])
stack.append({"scope": current_scope} )
ast[current_scope] = {}
if token['variant'] == "end_block":
old_stack = stack.pop()
ast[current_scope]["env"] = old_stack
current_scope = stack[-1]["scope"]
if token['variant'] == "type":
maybe_signifier = peek(index + 2)
if maybe_signifier["variant"] == "signifier":
name = maybe_signifier['value']
stack[-1][name] = {"type": token['value']}
print(json.dumps(ast))