From ca3ce26369fa205723d46cb43ecf90d323e41409 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Wed, 8 Jun 2022 19:45:27 +0200 Subject: [PATCH] Refactor a small bit --- pass1.py | 74 ++++++++------- prog.mn | 5 +- tokens.json | 264 +--------------------------------------------------- 3 files changed, 46 insertions(+), 297 deletions(-) diff --git a/pass1.py b/pass1.py index b18f912..bd7bb59 100755 --- a/pass1.py +++ b/pass1.py @@ -5,7 +5,6 @@ tokens = dict() with open('./tokens.json', 'r') as json_tokens: tokens = json.loads(json_tokens.read()) -usefull_tokens = list() def peek(index: int) -> dict: global tokens @@ -24,15 +23,16 @@ def find_token_indicies(key: str) -> list: index = index + 1 return result -def collect_body(index: int) -> list(): - result = list() +def collect_body(index: int) -> list: + global tokens + uncollapsed_tokens = list() token = peek(index + 1) if token['variant'] != "operator": - return result + return uncollapsed_tokens maybe_expression = peek(index + 2) if maybe_expression['variant'] == 'expression': - result.append(token) - return result + uncollapsed_tokens.append(token) + return uncollapsed_tokens elif maybe_expression['variant'] == 'start_block': index = index + 2 while True: @@ -40,34 +40,44 @@ def collect_body(index: int) -> list(): collectable = peek(index) if collectable['variant'] == 'end_block': break - result.append(collectable) + uncollapsed_tokens.append(collectable) + return uncollapsed_tokens - 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) -for index in range(0, len(tokens)): - token = tokens[index] + usefull_tokens.sort(key=operator.itemgetter('line_number')) + return usefull_tokens - # 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) +collapsed_tokens = collapse_tokens(tokens) -sorted_tokens = usefull_tokens.sort(key=operator.itemgetter('line_number')) -print(json.dumps(usefull_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 +print(json.dumps(collapsed_tokens)) diff --git a/prog.mn b/prog.mn index 3211eb7..18d91de 100644 --- a/prog.mn +++ b/prog.mn @@ -13,8 +13,9 @@ func: hello(float: x) = { hello(x) -func: good_bye(void: none) = int: getout = 0 - +func: good_bye(void: none) = { + int: getout = 0 +} good_bye() if (x ? 1) { diff --git a/tokens.json b/tokens.json index de55cff..a457e7a 100644 --- a/tokens.json +++ b/tokens.json @@ -1,263 +1 @@ -[ - { - "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 - } -] +[{"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": "start_block", "value": "{", "line_number": 16}, {"variant": "type", "value": "int", "line_number": 17}, {"variant": "operator", "value": ":", "line_number": 17}, {"variant": "signifier", "value": "getout", "line_number": 17}, {"variant": "operator", "value": "=", "line_number": 17}, {"variant": "expression", "value": "0", "line_number": 17}, {"variant": "end_block", "value": "}", "line_number": 18}, {"variant": "function_call", "value": "good_bye", "params": [""], "line_number": 19}, {"variant": "conditional", "value": "if", "condition": "x ? 1", "line_number": 21}, {"variant": "start_block", "value": "{", "line_number": 21}, {"variant": "function_call", "value": "hello", "params": ["x"], "line_number": 22}, {"variant": "end_block", "value": "}", "line_number": 23}, {"variant": "loop", "value": "while", "condition": "x < 1", "line_number": 25}, {"variant": "start_block", "value": "{", "line_number": 25}, {"variant": "signifier", "value": "x", "line_number": 26}, {"variant": "operator", "value": "=", "line_number": 26}, {"variant": "expression", "value": "x -1", "line_number": 26}, {"variant": "function_call", "value": "hello", "params": ["x"], "line_number": 27}, {"variant": "end_block", "value": "}", "line_number": 28}]