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))