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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
from tokens import *
import sys
from collections import namedtuple
import subprocess
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 NebNil()
def std_print(arg):
print(arg.value)
return NebNil()
def std_print_all(arg):
items = arg.items
for idx, item in enumerate(items):
if isinstance(item, NebExpression):
items[idx] = evaluate_expression(item)
elif not isinstance(item, NebString):
raise exception("print-all needs all strings!")
for x in items:
std_print(x)
return NebNil()
def std_debug_on():
global DEBUG
DEBUG = True
return NebNil()
def std_debug_off():
global DEBUG
DEBUG = False
return NebNil()
# math
def std_add(arg, rest):
float_type = True if isinstance(arg, NebFloat) else False
if isinstance(rest, NebNumber):
acc = arg.value + rest.value
float_type = float_type or isinstance(rest, NebFloat)
else:
acc = arg.value
for item in rest:
acc += item.value
float_type = float_type or isinstance(item, NebFloat)
if float_type:
return NebFloat(acc)
else:
return NebInt(acc)
def std_subtract(arg, rest):
float_type = True if isinstance(arg, NebFloat) else False
if isinstance(rest, NebNumber):
acc = arg.value - rest.value
float_type = float_type or isinstance(rest, NebFloat)
else:
acc = arg.value
for item in rest:
acc -= item.value
float_type = float_type or isinstance(item, NebFloat)
if float_type:
return NebFloat(acc)
else:
return NebInt(acc)
def std_multiply(arg, rest):
float_type = True if isinstance(arg, NebFloat) else False
if isinstance(rest, NebNumber):
acc = arg.value * rest.value
float_type = float_type or isinstance(rest, NebFloat)
else:
acc = arg.value
for item in rest:
acc *= item.value
float_type = float_type or isinstance(item, NebFloat)
if float_type:
return NebFloat(acc)
else:
return NebInt(acc)
# strings
def std_concat(arg, rest):
out = f"{arg.value}"
if isinstance(rest, NebString):
return NebString(f"{out}{rest.value}")
else:
return NebString(f"{out}" + "".join(f"{item.value}" for item in rest))
# flow control
def std_if(cond, t_branch, f_branch=None):
if cond.value:
if isinstance(t_branch, NebExpression):
return evaluate_expression(t_branch)
else:
return t_branch
elif f_branch is not None:
if isinstance(f_branch, NebExpression):
return evaluate_expression(f_branch)
else:
return f_branch
#return NebBool(True)
return NebNil()
# 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)
# type conversion
def std_literal_to_string(arg):
if isinstance(arg, NebString):
return arg
else:
return NebString(f"{arg.value}".lower())
# shell
def std_shell(arg):
assert isinstance(arg, NebString)
lst = arg.value.split(" ")
ret = subprocess.run(lst, capture_output=True, check=True)
stdout_as_list = ret.stdout.decode("utf-8").split("\n")[:-1]
return NebList(list(NebString(x) for x in stdout_as_list))
def std_shell_pipe(args):
prev = None
stdout = subprocess.PIPE
for idx, arg in enumerate(args.items):
if idx == len(args.items) - 1:
stdout = None
lst = arg.value.split(" ")
if prev is None:
if stdout is None:
ret = subprocess.run(lst, capture_output=True, check=True)
else:
ret = subprocess.run(lst, stdout=stdout, check=True)
else:
if stdout is None:
ret = subprocess.run(lst, input=prev.stdout, capture_output=True, check=True)
else:
ret = subprocess.run(lst, stdout=stdout, input=prev.stdout, check=True)
prev = ret
stdout_as_list = prev.stdout.decode("utf-8").split("\n")[:-1]
return NebList(list(NebString(x) for x in stdout_as_list))
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:
validated = 0
sig = value.func.args
# if many is defined, it can take one or more of them
many = value.func.many
# if we need exact arguments, the signatures must match length
if many is None and len(sig) != len(in_sig):
continue
# if we have "many", must have at least the exact length
elif len(in_sig) < len(sig):
continue
# loop through explicits
for idx in range(len(sig)):
if isinstance(in_sig[idx], sig[idx]):
validated += 1
# return if we've found it
if validated == len(in_sig):
ret = value.impl(*(expr.args))
return ret
# loop through the remainder of in_sig
many_idx = 0
first_args = in_sig[0:len(sig)]
rest_args = in_sig[len(sig):]
for arg in rest_args:
if isinstance(arg, many[many_idx]):
validated += 1
if len(many) - 1 == many_idx:
many_idx = 0
else:
many_idx += 1
if validated == len(in_sig):
# move end of first list to beginning of second list
new_args = first_args[:-1]
new_rest = [first_args[-1]] + rest_args
new_args.append(new_rest)
ret = value.impl(*(new_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], NebNil), std_print)
STD["print"] = [print_string]
print_all = FuncImpl(NebFunction("print-all", [NebList], NebNil), std_print_all)
STD["print-all"] = [print_all]
exit_ = FuncImpl(NebFunction("exit", [], NebNil), std_exit)
exit_int = FuncImpl(NebFunction("exit", [NebInt], NebNil), std_exit)
STD["exit"] = [exit_, exit_int]
debug_on = FuncImpl(NebFunction("debug-on", [], NebNil), std_debug_on)
STD["debug-on"] = [debug_on]
debug_off = FuncImpl(NebFunction("debug-off", [], NebNil), std_debug_off)
STD["debug-off"] = [debug_off]
# arithmetic
add = FuncImpl(NebFunction("+", [NebNumber, NebNumber], NebNumber, [NebNumber]), std_add)
STD["+"] = [add]
subtract = FuncImpl(NebFunction("-", [NebNumber, NebNumber], NebNumber, [NebNumber]), std_subtract)
STD["-"] = [subtract]
multiply = FuncImpl(NebFunction("*", [NebNumber, NebNumber], NebNumber, [NebNumber]), std_multiply)
STD["*"] = [multiply]
# strings
concat_string_string = FuncImpl(NebFunction("concat", [NebString, NebString], NebString, [NebString]), std_concat)
STD["concat"] = [concat_string_string]
# flow control
#if_bool_any_any = FuncImpl(NebFunction("if", [NebBool, NebAny, NebAny], NebAny), std_if)
#if_bool_any = FuncImpl(NebFunction("if", [NebBool, NebAny], NebAny), std_if)
if_bool_expr_expr = FuncImpl(NebFunction("if", [NebBool, NebExpression, NebExpression], NebAny), std_if)
if_bool_expr_any = FuncImpl(NebFunction("if", [NebBool, NebExpression, NebAny], NebAny), std_if)
if_bool_any_expr = FuncImpl(NebFunction("if", [NebBool, NebAny, NebExpression], NebAny), std_if)
if_bool_any_any = FuncImpl(NebFunction("if", [NebBool, NebAny, NebAny], NebAny), std_if)
if_bool_expr = FuncImpl(NebFunction("if", [NebBool, NebExpression], NebAny), std_if)
if_bool_any = FuncImpl(NebFunction("if", [NebBool, NebAny], NebAny), std_if)
STD["if"] = [
if_bool_expr_expr,
if_bool_expr_any,
if_bool_any_expr,
if_bool_any_any,
if_bool_expr,
if_bool_any]
# 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]
# type conversion
int_to_string = FuncImpl(NebFunction("int->string", [NebInt], NebString), std_literal_to_string)
STD["int->string"] = [int_to_string]
float_to_string = FuncImpl(NebFunction("float->string", [NebFloat], NebString), std_literal_to_string)
STD["float->string"] = [float_to_string]
number_to_string = FuncImpl(NebFunction("number->string", [NebNumber], NebString), std_literal_to_string)
STD["number->string"] = [number_to_string]
bool_to_string = FuncImpl(NebFunction("bool->string", [NebBool], NebString), std_literal_to_string)
STD["bool->string"] = [bool_to_string]
to_string = FuncImpl(NebFunction("->string", [NebLiteral], NebString), std_literal_to_string)
STD["->string"] = [to_string]
# shell
shell_string = FuncImpl(NebFunction("$", [NebString], NebList), std_shell)
STD["$"] = [shell_string]
shell_pipe = FuncImpl(NebFunction("$|", [NebList], NebList), std_shell_pipe)
STD["$|"] = [shell_pipe]
build_std()
|