Can now do arithmatic

main
Micke Nordin 2 years ago
parent 62718009e9
commit cf94c84e34
Signed by: micke
GPG Key ID: 0DA0A7A5708FE257

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

@ -13,24 +13,67 @@ def throw(token: dict, message):
print("\033[4m" + token['value'] + "\033[0m") print("\033[4m" + token['value'] + "\033[0m")
sys.exit(1) 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(" ") split_on_space = expression.split(" ")
if len(split_on_space) == 1: if len(split_on_space) == 1:
for param in params: for param in variable_list:
if param['signifier'] == expression and param['type'] == 'string': if param['signifier'] == expression:
match param['type']:
case 'string':
return param['expression'] 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: else:
return "Unknown" return "Unknown"
for token in tokens: for token in tokens:
if token['variant'] == "syntax_error": match token['variant']:
case "syntax_error":
throw(token, "Syntax error") throw(token, "Syntax error")
if token['variant'] == "function_call": case "function_call":
args = token['params'] args = token['params']
function = token['function'] function = token['function']
params = function['params'] params = function['params']
if len(args) != len(params): if len(args) != len(params):
print(params)
throw(token, "Wrong number of arguments") throw(token, "Wrong number of arguments")
for index in range(0, len(args)): for index in range(0, len(args)):
if args[index]['type'] != params[index]['type']: if args[index]['type'] != params[index]['type']:
@ -40,5 +83,7 @@ for token in tokens:
params[index]['signifier'] = signifier params[index]['signifier'] = signifier
for part in function['body']: for part in function['body']:
if part['type'] == "stdout": if part['type'] == "stdout":
print(parse_expression(params, part['expression'])) print(parse_expression(params, part['expression'], tokens))
case _:
pass # print(token)

Loading…
Cancel
Save