1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
from tokens import *
import sys
from collections import namedtuple
FuncImpl = namedtuple("FuncImpl", ("func", "impl"))
STD = {}
DEBUG = True
USER = {}
def _get_debug():
return DEBUG
def std_exit(status=None):
out = 0 if status is None else status.value
sys.exit(out)
return NebBool(True) # this should never be reached
def std_print(arg):
print(arg.value)
#return [] # TODO this should return empty list
return NebBool(True)
def std_debug_on():
global DEBUG
DEBUG = True
return NebBool(True)
def std_debug_off():
global DEBUG
DEBUG = False
return NebBool(True)
# math
def std_add(arg1, arg2):
res = arg1.value + arg2.value
if isinstance(arg1, NebFloat) or isinstance(arg2, NebFloat):
return NebFloat(res)
else:
return NebInt(res)
def std_subtract(arg1, arg2):
res = arg1.value - arg2.value
if isinstance(arg1, NebFloat) or isinstance(arg2, NebFloat):
return NebFloat(res)
else:
return NebInt(res)
def std_multiply(arg1, arg2):
res = arg1.value * arg2.value
if isinstance(arg1, NebFloat) or isinstance(arg2, NebFloat):
return NebFloat(res)
else:
return NebInt(res)
# strings
def std_concat(arg1, arg2):
return NebString(f"{arg1.value}{arg2.value}")
# flow control
def std_if(cond, t_branch, f_branch):
if cond.value:
if isinstance(t_branch, NebExpression):
ret = evaluate_expression(t_branch)
else:
ret = t_branch
else:
if isinstance(t_branch, NebExpression):
ret = evaluate_expression(f_branch)
else:
ret = f_branch
return ret
# type validation
def std_is_string(arg):
if isinstance(arg, NebString):
return NebBool(True)
else:
return NebBool(False)
def std_is_int(arg):
if isinstance(arg, NebInt):
return NebBool(True)
else:
return NebBool(False)
def std_is_float(arg):
if isinstance(arg, NebFloat):
return NebBool(True)
else:
return NebBool(False)
def std_is_number(arg):
if isinstance(arg, NebNumber):
return NebBool(True)
else:
return NebBool(False)
def std_is_bool(arg):
if isinstance(arg, NebBool):
return NebBool(True)
else:
return NebBool(False)
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
validated = 0
in_sig = expr.args
for value in this_func:
sig = value.func.args
validated = 0
if len(sig) != len(in_sig):
continue
for idx in range(len(sig)):
if isinstance(in_sig[idx], sig[idx]):
validated += 1
if validated == len(sig):
ret = value.impl(*(expr.args))
return ret
# evaluate inner expressions, if possible/necessary
for idx, arg in enumerate(expr.args):
if isinstance(arg, NebExpression):
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_std():
print_string = FuncImpl(NebFunction("print", [NebString], NebString), std_print)
STD["print"] = [print_string]
exit_ = FuncImpl(NebFunction("exit", [], NebBool), std_exit)
exit_int = FuncImpl(NebFunction("exit", [NebInt], NebBool), std_exit)
STD["exit"] = [exit_, exit_int]
debug_on = FuncImpl(NebFunction("debug-on", [], NebBool), std_debug_on)
STD["debug-on"] = [debug_on]
debug_off = FuncImpl(NebFunction("debug-off", [], NebBool), std_debug_off)
STD["debug-off"] = [debug_off]
# arithmetic
add = FuncImpl(NebFunction("+", [NebNumber, NebNumber], NebNumber), std_add)
STD["+"] = [add]
subtract = FuncImpl(NebFunction("-", [NebNumber, NebNumber], NebNumber), std_subtract)
STD["-"] = [subtract]
multiply = FuncImpl(NebFunction("*", [NebNumber, NebNumber], NebNumber), std_multiply)
STD["*"] = [multiply]
# strings
concat_string_string = FuncImpl(NebFunction("concat", [NebString, NebString], NebString), std_concat)
STD["concat"] = [concat_string_string]
# flow control
if_bool_expr_expr = FuncImpl(NebFunction("if", [NebBool, NebAny, NebAny], NebAny), std_if)
STD["if"] = [if_bool_expr_expr]
# type checking
is_string = FuncImpl(NebFunction("string?", [NebAny], NebBool), std_is_string)
STD["string?"] = [is_string]
is_int = FuncImpl(NebFunction("string?", [NebAny], NebBool), std_is_int)
STD["int?"] = [is_int]
is_float = FuncImpl(NebFunction("string?", [NebAny], NebBool), std_is_float)
STD["float?"] = [is_float]
is_number = FuncImpl(NebFunction("string?", [NebAny], NebBool), std_is_number)
STD["number?"] = [is_number]
is_bool = FuncImpl(NebFunction("string?", [NebAny], NebBool), std_is_bool)
STD["bool?"] = [is_bool]
build_std()
|