aboutsummaryrefslogtreecommitdiff
path: root/std.py
diff options
context:
space:
mode:
authormryouse2022-05-11 03:22:13 +0000
committermryouse2022-05-11 03:22:13 +0000
commit9a9f38aa564e0e15726d5b5dadb4098dde7cb0bb (patch)
tree2b3c7ad9fb4b0190dc1de4161b232e30d307e308 /std.py
parent93d9ed5115cd32d14d7b0f4ccc672eaf6060ab05 (diff)
basic flow control!
Diffstat (limited to 'std.py')
-rw-r--r--std.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/std.py b/std.py
index 1779b49..8897446 100644
--- a/std.py
+++ b/std.py
@@ -1,4 +1,5 @@
from tokens import *
+#from runner import evaluate_expression
import sys
from collections import namedtuple
@@ -6,6 +7,7 @@ FuncImpl = namedtuple("FuncImpl", ("func", "impl"))
STD = {}
DEBUG = True
+USER = {}
def _get_debug():
return DEBUG
@@ -49,6 +51,39 @@ def std_multiply(arg1, arg2):
typ = NebType.FLOAT
return NebLiteral(typ, arg1.value * arg2.value)
+# strings
+def std_concat(arg1, arg2):
+ return NebLiteral(NebType.STRING, f"{arg1.value}{arg2.value}")
+
+# flow control
+def std_if(cond, t_branch, f_branch):
+ if cond.value:
+ ret = evaluate_expression(t_branch)
+ else:
+ ret = evaluate_expression(f_branch)
+ #return NebLiteral(NebType.BOOL, True)
+ #return NebLiteral(, True)
+ return ret
+
+def evaluate_expression(expr):
+ if not expr.symbol.name in STD:
+ raise Exception(f"no such symbol: {expr.symbol.name}")
+ this_func = STD[expr.symbol.name]
+
+ # try to match the signature
+ in_sig = expr.in_sig()
+ if in_sig in this_func:
+ ret = this_func[expr.in_sig()].impl(*(expr.args))
+ return ret
+
+ # evaluate inner expressions, if possible/necessary
+ for idx, arg in enumerate(expr.args):
+ if arg.type_ == NebType.EXPR:
+ expr.args[idx] = evaluate_expression(arg)
+ return evaluate_expression(expr)
+
+ raise Exception(f"'{expr.symbol.name}' called with unknown signature: '{expr.in_sig()}'")
+
def build_sig_dict(*args):
return {arg.func.in_sig(): arg for arg in args}
@@ -84,4 +119,12 @@ def build_std():
multiply_float_float = FuncImpl(NebFunction("*", [NebType.FLOAT, NebType.FLOAT], NebType.FLOAT), std_multiply)
STD["*"] = build_sig_dict(multiply_int_int, multiply_int_float, multiply_float_int, multiply_float_float)
+ # strings
+ concat_string_string = FuncImpl(NebFunction("concat", [NebType.STRING, NebType.STRING], NebType.STRING), std_concat)
+ STD["concat"] = build_sig_dict(concat_string_string)
+
+ # flow control
+ if_bool_expr_expr = FuncImpl(NebFunction("if", [NebType.BOOL, NebType.EXPR, NebType.EXPR], NebType.ANY), std_if)
+ STD["if"] = build_sig_dict(if_bool_expr_expr)
+
build_std()