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
198 lines (150 loc) · 3.72 KB
/
Copy pathkernel.js
File metadata and controls
198 lines (150 loc) · 3.72 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
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
import Erlang from './erlang';
import SpecialForms from './kernel/special_forms';
import JS from './kernel/js';
let Kernel = {
__MODULE__: Erlang.atom('Kernel'),
SpecialForms: SpecialForms,
JS: JS,
defmodule: function(alias, list2, root){
let moduleAtom = alias[alias.length - 1];
let parent = Kernel.JS.create_namespace(alias, root);
return Object.assign(parent, list2(moduleAtom));
},
tl: function(list){
return Erlang.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 Erlang.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 Erlang.bitstring;
},
__in__: function(left, right){
for(let x of collection){
if(x === value){
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[i];
}
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, fun, args){
if(arguments.length === 3){
return module[fun].apply(null, args);
}else{
return module.apply(null, fun);
}
},
to_string: function(arg){
return arg.toString();
},
match__qmark__: function(pattern, expr, guard = () => true){
if(!guard()){
return false;
}
if(pattern === undefined){
return true;
}
if(Kernel.is_atom(expr)){
return Kernel.is_atom(pattern) && pattern === expr;
}else if(Kernel.is_nil(expr) || Kernel.is_number(expr) || Kernel.is_binary(expr) || Kernel.is_boolean(expr)){
return pattern === expr;
}else if(Kernel.is_tuple(expr)){
return Kernel.is_tuple(pattern) && Kernel.match__qmark__(pattern.value(), expr.value());
}else if(Kernel.is_list(expr)){
if(Kernel.length(pattern) !== Kernel.length(expr)){
return false;
}
for (let i = 0; i <= pattern.length; i++) {
if(Kernel.match__qmark__(pattern[i], expr[i]) === false){
return false;
}
}
return true;
}else if(Kernel.is_map(expr)){
if(!Kernel.is_map(pattern)){
return false;
}
for(let key in pattern){
if(!(key in expr)){
return false;
}
if(Kernel.match__qmark__(pattern[key], expr[key]) === false){
return false;
}
}
return true;
}
}
};
export default Kernel;