Start of parser

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

@ -74,9 +74,13 @@ for lineno in range(1, num_lines + 1):
elif name == "if": elif name == "if":
conditional = {"variant": "conditional", "value": name, "condition": ','.join(params), "line_number": lineno} conditional = {"variant": "conditional", "value": name, "condition": ','.join(params), "line_number": lineno}
tokens.append(conditional) tokens.append(conditional)
expression = {"variant": "start_block", "value": "{", "line_number": lineno}
tokens.append(expression)
else: else:
loop = {"variant": "loop", "value": name, "condition": ','.join(params), "line_number": lineno} loop = {"variant": "loop", "value": name, "condition": ','.join(params), "line_number": lineno}
tokens.append(loop) tokens.append(loop)
expression = {"variant": "start_block", "value": "{", "line_number": lineno}
tokens.append(expression)
else: else:
if line.strip() == '{': if line.strip() == '{':
expression = {"variant": "start_block", "value": '{', "line_number": lineno} expression = {"variant": "start_block", "value": '{', "line_number": lineno}

@ -0,0 +1,44 @@
#!/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))

@ -0,0 +1,263 @@
[
{
"variant": "type",
"value": "float",
"line_number": 1
},
{
"variant": "operator",
"value": ":",
"line_number": 1
},
{
"variant": "signifier",
"value": "x",
"line_number": 1
},
{
"variant": "operator",
"value": "=",
"line_number": 1
},
{
"variant": "expression",
"value": "10.1",
"line_number": 1
},
{
"variant": "type",
"value": "func",
"line_number": 3
},
{
"variant": "operator",
"value": ":",
"line_number": 3
},
{
"variant": "function_declaration",
"value": "hello",
"params": [
{
"type": "float",
"name": "x"
}
],
"line_number": 3
},
{
"variant": "operator",
"value": "=",
"line_number": 3
},
{
"variant": "start_block",
"value": "{",
"line_number": 3
},
{
"variant": "type",
"value": "stdout",
"line_number": 4
},
{
"variant": "operator",
"value": ":",
"line_number": 4
},
{
"variant": "signifier",
"value": "out",
"line_number": 4
},
{
"variant": "operator",
"value": "=",
"line_number": 4
},
{
"variant": "expression",
"value": "x * 10",
"line_number": 4
},
{
"variant": "end_block",
"value": "}",
"line_number": 5
},
{
"variant": "start_block",
"value": "{",
"line_number": 7
},
{
"variant": "type",
"value": "str",
"line_number": 8
},
{
"variant": "operator",
"value": ":",
"line_number": 8
},
{
"variant": "signifier",
"value": "name",
"line_number": 8
},
{
"variant": "type",
"value": "int",
"line_number": 9
},
{
"variant": "operator",
"value": ":",
"line_number": 9
},
{
"variant": "signifier",
"value": "var",
"line_number": 9
},
{
"variant": "operator",
"value": "=",
"line_number": 9
},
{
"variant": "expression",
"value": "1",
"line_number": 9
},
{
"variant": "signifier",
"value": "name",
"line_number": 10
},
{
"variant": "operator",
"value": "=",
"line_number": 10
},
{
"variant": "expression",
"value": "hej hopp",
"line_number": 10
},
{
"variant": "end_block",
"value": "}",
"line_number": 11
},
{
"variant": "function_call",
"value": "hello",
"params": [
"x"
],
"line_number": 14
},
{
"variant": "type",
"value": "func",
"line_number": 16
},
{
"variant": "operator",
"value": ":",
"line_number": 16
},
{
"variant": "function_declaration",
"value": "good_bye",
"params": [
{
"type": "void",
"name": "none"
}
],
"line_number": 16
},
{
"variant": "operator",
"value": "=",
"line_number": 16
},
{
"variant": "expression",
"value": "int: getout = 0",
"line_number": 16
},
{
"variant": "function_call",
"value": "good_bye",
"params": [
""
],
"line_number": 18
},
{
"variant": "conditional",
"value": "if",
"condition": "x ? 1",
"line_number": 20
},
{
"variant": "start_block",
"value": "{",
"line_number": 20
},
{
"variant": "function_call",
"value": "hello",
"params": [
"x"
],
"line_number": 21
},
{
"variant": "end_block",
"value": "}",
"line_number": 22
},
{
"variant": "loop",
"value": "while",
"condition": "x < 1",
"line_number": 24
},
{
"variant": "start_block",
"value": "{",
"line_number": 24
},
{
"variant": "signifier",
"value": "x",
"line_number": 25
},
{
"variant": "operator",
"value": "=",
"line_number": 25
},
{
"variant": "expression",
"value": "x -1",
"line_number": 25
},
{
"variant": "function_call",
"value": "hello",
"params": [
"x"
],
"line_number": 26
},
{
"variant": "end_block",
"value": "}",
"line_number": 27
}
]
Loading…
Cancel
Save