aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runner.py21
-rw-r--r--std.py43
2 files changed, 44 insertions, 20 deletions
diff --git a/runner.py b/runner.py
index 7de2666..69112c5 100644
--- a/runner.py
+++ b/runner.py
@@ -1,5 +1,5 @@
from tokens import *
-from std import STD
+from std import STD, evaluate_expression
def peek(inp):
@@ -7,25 +7,6 @@ def peek(inp):
return None
return inp[0]
-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 evaluate(items, pop):
nxt = peek(items)
if nxt is None:
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()