From f7b9cd734a22d2c8e689a7d43d6d3881733ad6f3 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Tue, 14 Jun 2022 23:50:45 +0200 Subject: [PATCH] Tired covid stuff --- hello.mn | 10 +++++ hello.tokens | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ lexer.py | 7 +++- pass1.py | 35 ++++++++++++++---- 4 files changed, 145 insertions(+), 8 deletions(-) create mode 100644 hello.mn create mode 100644 hello.tokens diff --git a/hello.mn b/hello.mn new file mode 100644 index 0000000..60f58aa --- /dev/null +++ b/hello.mn @@ -0,0 +1,10 @@ +func: hello(string: x) = { + stdout: out = x +} + +string: name +name = hej hopp + + +hello(name) + diff --git a/hello.tokens b/hello.tokens new file mode 100644 index 0000000..43b37d7 --- /dev/null +++ b/hello.tokens @@ -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 + } +] diff --git a/lexer.py b/lexer.py index a3619fe..91bed77 100755 --- a/lexer.py +++ b/lexer.py @@ -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) diff --git a/pass1.py b/pass1.py index bd7bb59..e71e147 100755 --- a/pass1.py +++ b/pass1.py @@ -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))