forked from elixirscript/elixirscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.js
More file actions
154 lines (114 loc) · 2.61 KB
/
Copy pathkernel.js
File metadata and controls
154 lines (114 loc) · 2.61 KB
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
import SpecialForms from './kernel/special_forms';
import Patterns from './patterns/patterns';
import Tuple from './tuple';
let Kernel = {
SpecialForms: SpecialForms,
tl: function(list){
return SpecialForms.list(...list.slice(1));
},
hd: function(list){
return list[0];
},
is_nil: function(x){
return x == null;
},
is_atom: function(x){
return typeof x === 'symbol';
},
is_binary: function (x){
return typeof x === 'string' || x instanceof String;
},
is_boolean: function (x){
return typeof x === 'boolean' || x instanceof Boolean;
},
is_function: function(x, arity = -1){
return typeof x === 'function' || x instanceof Function;
},
// from: http://stackoverflow.com/a/3885844
is_float: function(x){
return x === +x && x !== (x|0);
},
is_integer: function(x){
return x === +x && x === (x|0);
},
is_list: function(x){
return x instanceof Array;
},
is_map: function(x){
return typeof x === 'object' || x instanceof Object;
},
is_number: function(x){
return Kernel.is_integer(x) || Kernel.is_float(x);
},
is_tuple: function(x){
return x instanceof Tuple;
},
length: function(x){
return x.length;
},
is_pid: function(x){
return false;
},
is_port: function(x){
},
is_reference: function(x){
},
is_bitstring: function(x){
return Kernel.is_binary(x) || x instanceof SpecialForms.bitstring;
},
__in__: function(left, right){
for(let x of right){
if(Kernel.match__qmark__(left, x)){
return true;
}
}
return false;
},
abs: function(number){
return Math.abs(number);
},
round: function(number){
return Math.round(number);
},
elem: function(tuple, index){
if(Kernel.is_list(tuple)){
return tuple[index];
}
return tuple.get(index);
},
rem: function(left, right){
return left % right;
},
div: function(left, right){
return left / right;
},
and: function(left, right){
return left && right;
},
or: function(left, right){
return left || right;
},
not: function(arg){
return !arg;
},
apply: function(module, func, args){
if(arguments.length === 3){
return module[func].apply(null, args);
}else{
return module.apply(null, func);
}
},
to_string: function(arg){
if(Kernel.is_tuple(arg)){
return Tuple.to_string(arg);
}
return arg.toString();
},
throw: function(e){
throw e;
},
match__qmark__: function(pattern, expr, guard = () => true){
return Patterns.match_no_throw(pattern, expr, guard) != null;
}
};
export default Kernel;