Tired covid stuff

main
Micke Nordin 2 years ago
parent ca3ce26369
commit f7b9cd734a
Signed by: micke
GPG Key ID: 0DA0A7A5708FE257

@ -0,0 +1,10 @@
func: hello(string: x) = {
stdout: out = x
}
string: name
name = hej hopp
hello(name)

@ -0,0 +1,101 @@
[
{
"variant": "type",
"value": "func",
"line_number": 1
},
{
"variant": "operator",
"value": ":",
"line_number": 1
},
{
"variant": "function_declaration",
"value": "hello",
"params": [
{
"type": "string",
"name": "x"
}
],
"line_number": 1
},
{
"variant": "operator",
"value": "=",
"line_number": 1
},
{
"variant": "start_block",
"value": "{",
"line_number": 1
},
{
"variant": "type",
"value": "stdout",
"line_number": 2
},
{
"variant": "operator",
"value": ":",
"line_number": 2
},
{
"variant": "signifier",
"value": "out",
"line_number": 2
},
{
"variant": "operator",
"value": "=",
"line_number": 2
},
{
"variant": "expression",
"value": "x",
"line_number": 2
},
{
"variant": "end_block",
"value": "}",
"line_number": 3
},
{
"variant": "type",
"value": "string",
"line_number": 5
},
{
"variant": "operator",
"value": ":",
"line_number": 5
},
{
"variant": "signifier",
"value": "name",
"line_number": 5
},
{
"variant": "signifier",
"value": "name",
"line_number": 6
},
{
"variant": "operator",
"value": "=",
"line_number": 6
},
{
"variant": "expression",
"value": "hej hopp",
"line_number": 6
},
{
"variant": "function_call",
"value": "hello",
"params": [
"name"
],
"line_number": 9
}
]

@ -1,11 +1,16 @@
#!/usr/bin/env python3
import json
import sys
def find(needle: str, haystack: str) -> bool:
return haystack.find(needle) != -1
source: list = list()
with open('./prog.mn', 'r') as prog:
program = './prog.mn'
if len(sys.argv) > 1:
program = sys.argv[1]
with open(program, 'r') as prog:
source = prog.readlines()
num_lines = len(source)

@ -1,8 +1,12 @@
#!/usr/bin/env python3
import json
import operator
import sys
tokens = dict()
with open('./tokens.json', 'r') as json_tokens:
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())
@ -43,6 +47,17 @@ def collect_body(index: int) -> list:
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)):
@ -69,15 +84,21 @@ def collapse_tokens(uncollapsed_tokens: list) -> list:
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_declaration':
# old_body = token['body'].copy()
# new_body = collapse_tokens(old_body)
# collapsed_tokens[index]['body'] = new_body
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))

Loading…
Cancel
Save