From cf94c84e344e0cb26ce32fe797866accf343fb49 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Wed, 15 Jun 2022 15:37:11 +0200 Subject: [PATCH] Can now do arithmatic --- int.mn | 22 +++++++++++++ interpreter.py | 87 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 int.mn diff --git a/int.mn b/int.mn new file mode 100644 index 0000000..6a0b862 --- /dev/null +++ b/int.mn @@ -0,0 +1,22 @@ +func: print(number: x) = { + stdout: out = x +} + +number: test = 5 +number: another +another = 5.5 + +number: third = test + another +number: fourth = test - another +number: fifth = test * another +number: sixth = test / another +number: seventh = test % another +number: eighth = 7 % test +number: nineth = 7 + 5 +print(third) +print(fourth) +print(fifth) +print(sixth) +print(seventh) +print(eighth) +print(nineth) diff --git a/interpreter.py b/interpreter.py index f222b32..1e19edc 100755 --- a/interpreter.py +++ b/interpreter.py @@ -13,32 +13,77 @@ def throw(token: dict, message): print("\033[4m" + token['value'] + "\033[0m") sys.exit(1) -def parse_expression(params, expression): +def resolve_variabel(tokens, signifier): + for token in tokens: + if token['variant'] == "variable_declaration" and token['signifier'] == signifier: + return token + +def parse_expression(variable_list, expression, all_tokens): split_on_space = expression.split(" ") if len(split_on_space) == 1: - for param in params: - if param['signifier'] == expression and param['type'] == 'string': - return param['expression'] + for param in variable_list: + if param['signifier'] == expression: + match param['type']: + case 'string': + return param['expression'] + case 'number': + resolved = list() + for part in param['expression'].split(' '): + if part.isalpha(): + variable = resolve_variabel(all_tokens, part) + resolved.append(variable) + elif part == '+': + resolved.append({"variant": "operator", "type": "addition"}) + elif part == '-': + resolved.append({"variant": "operator", "type": "subtraction"}) + elif part == '*': + resolved.append({"variant": "operator", "type": "multiplication"}) + elif part == '/': + resolved.append({"variant": "operator", "type": "division"}) + elif part == '%': + resolved.append({"variant": "operator", "type": "modulo"}) + elif float(part): + resolved.append({"expression": part}) + if len(resolved) == 3: + lhs = resolved[0]['expression'] + operator = resolved[1] + rhs = resolved[2]['expression'] + match operator['type']: + case "addition": + return str(float(lhs) + float(rhs)) + case "subtraction": + return str(float(lhs) - float(rhs)) + case "multiplication": + return str(float(lhs) * float(rhs)) + case "division": + return str(float(lhs) / float(rhs)) + case "modulo": + return str(float(lhs) % float(rhs)) + else: return "Unknown" for token in tokens: - if token['variant'] == "syntax_error": - throw(token, "Syntax error") - if token['variant'] == "function_call": - args = token['params'] - function = token['function'] - params = function['params'] - if len(args) != len(params): - throw(token, "Wrong number of arguments") - for index in range(0, len(args)): - if args[index]['type'] != params[index]['type']: - throw(token, "Type missmatch on arguments") - signifier = params[index]['name'] - params[index] = args[index] - params[index]['signifier'] = signifier - for part in function['body']: - if part['type'] == "stdout": - print(parse_expression(params, part['expression'])) + match token['variant']: + case "syntax_error": + throw(token, "Syntax error") + case "function_call": + args = token['params'] + function = token['function'] + params = function['params'] + if len(args) != len(params): + print(params) + throw(token, "Wrong number of arguments") + for index in range(0, len(args)): + if args[index]['type'] != params[index]['type']: + throw(token, "Type missmatch on arguments") + signifier = params[index]['name'] + params[index] = args[index] + params[index]['signifier'] = signifier + for part in function['body']: + if part['type'] == "stdout": + print(parse_expression(params, part['expression'], tokens)) + case _: + pass # print(token)