This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
025num.cc
55 lines (46 loc) · 1.66 KB
/
025num.cc
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
//// compiled primitives for numbers
COMPILE_FN(+, compiledfn_add, "($x $y)",
cell* x = lookup("$x"); cell* y = lookup("$y");
if (x->type == FLOAT || y->type == FLOAT)
return mkref(new_num(to_float(x) + to_float(y)));
else
return mkref(new_num(to_int(x) + to_int(y)));
)
COMPILE_FN(-, compiledfn_subtract, "($x $y)",
cell* x = lookup("$x"); cell* y = lookup("$y");
if (x->type == FLOAT || y->type == FLOAT)
return mkref(new_num(to_float(x) - to_float(y)));
else
return mkref(new_num(to_int(x) - to_int(y)));
)
COMPILE_FN(*, compiledfn_multiply, "($x $y)",
cell* x = lookup("$x"); cell* y = lookup("$y");
if (x->type == FLOAT || y->type == FLOAT)
return mkref(new_num(to_float(x) * to_float(y)));
else
return mkref(new_num(to_int(x) * to_int(y)));
)
COMPILE_FN(/, compiledfn_divide, "($x $y)",
cell* x = lookup("$x"); cell* y = lookup("$y");
return mkref(new_num(to_float(x) / to_float(y)));
)
COMPILE_FN(%, compiledfn_modulo, "($x $y)",
return mkref(new_num(to_int(lookup("$x")) % to_int(lookup("$y")))); // what does modulo of floats mean?
)
#include <math.h>
COMPILE_FN(^, compiledfn_exp, "($base $exp)",
return mkref(new_num(pow(to_float(lookup("$base")), to_float(lookup("$exp")))));
)
COMPILE_FN(<, compiledfn_lesser, "($x $y)",
cell* x = lookup("$x");
cell* y = lookup("$y");
// support (a < b < c)
if (x == sym_false || y == sym_false) return mkref(sym_false);
return to_float(x) < to_float(y) ? mkref(y) : mkref(sym_false);
)
COMPILE_FN(int, compiledfn_integer, "($x)",
return mkref(new_num(to_int(lookup("$x"))));
)
COMPILE_FN(num, compiledfn_num, "($x)",
return mkref(new_num(atof(to_string(lookup("$x")).c_str())));
)