-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathkernel.ex
More file actions
209 lines (164 loc) · 3.92 KB
/
kernel.ex
File metadata and controls
209 lines (164 loc) · 3.92 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
197
198
199
200
201
202
203
204
205
206
207
208
209
defmodule ElixirScript.Kernel do
@moduledoc false
import Kernel, only: [defmodule: 2, def: 1, def: 2, defp: 2,
defmacro: 1, defmacro: 2, defmacrop: 2, ||: 2, !: 1,
++: 2, in: 2, &&: 2, ===: 2, @: 1, sigil_r: 2]
require JS
defmacro if(condition, clauses) do
build_if(condition, clauses)
end
defp build_if(condition, do: do_clause) do
build_if(condition, do: do_clause, else: nil)
end
defp build_if(condition, do: do_clause, else: else_clause) do
quote do
case unquote(condition) do
x when x in [false, nil] ->
unquote(else_clause)
_ ->
unquote(do_clause)
end
end
end
defmacro unless(condition, clauses) do
build_unless(condition, clauses)
end
defp build_unless(condition, do: do_clause) do
build_unless(condition, do: do_clause, else: nil)
end
defp build_unless(condition, do: do_clause, else: else_clause) do
quote do
if(unquote(condition), do: unquote(else_clause), else: unquote(do_clause))
end
end
def abs(number) do
JS.Math.abs(number)
end
def apply(fun, args) do
fun.apply(fun, args)
end
def apply(module, fun, args) do
module[Atom.to_string(fun)].apply(module[Atom.to_string(fun)], args)
end
def binary_part(binary, start, len) do
binary.substring(start, len)
end
def hd(list) do
list[0]
end
def tl(list) do
list.slice(1)
end
def is_atom(term) do
JS.typeof(term) === "symbol"
end
def is_binary(term) do
JS.typeof(term) === "string"
end
def is_bitstring(term) do
is_binary(term) || JS.instanceof(term, Bootstrap.Core.BitString)
end
def is_boolean(term) do
JS.typeof(term) === "boolean" || JS.instanceof(term, Boolean)
end
def is_float(term) do
is_number(term) && !JS.Number.isInteger(term)
end
def is_function(term) do
is_function(term, 0)
end
def is_function(term, _) do
JS.typeof(term) === "function" || JS.instanceof(term, Function)
end
def is_integer(term) do
JS.Number.isInteger(term)
end
def is_list(term) do
JS.Array.isArray(term)
end
def is_number(term) do
JS.typeof(term) === "number" || JS.instanceof(term, Number)
end
def is_pid(term) do
JS.instanceof(term, Bootstrap.Core.PID)
end
def is_tuple(term) do
JS.instanceof(term, Bootstrap.Core.Tuple)
end
def is_map(term) do
JS.typeof(term) === "object" || JS.instanceof(term, Object)
end
def is_port(_) do
false
end
def is_reference(_) do
false
end
def length(term) do
term.length
end
def map_size(term) do
JS.Object.keys(term).length
end
def max(first, second) do
JS.Math.max(first, second)
end
def min(first, second) do
JS.Math.min(first, second)
end
def round(number) do
JS.Math.round(number)
end
def trunc(number) do
JS.Math.floor(number)
end
def tuple_size(tuple) do
tuple.count()
end
def elem(tuple, index) do
tuple.get(index)
end
def is_nil(term) do
term === nil
end
defmacro sigil_r({:<<>>, _meta, [string]}, options) do
str_options = List.to_string(options)
quote do
Regex.compile!(unquote(string), unquote(str_options))
end
end
defmacro match?(pattern, expr) do
quote do
case unquote(expr) do
unquote(pattern) ->
true
_ ->
false
end
end
end
defmacro to_string(arg) when Kernel.is_binary(arg) do
arg
end
defmacro to_string(arg) do
quote do
String.Chars.to_string(unquote(arg))
end
end
defmacro left |> {fun, context, params} do
{fun, context, [left] ++ params }
end
defmacro left in right do
quote do
ElixirScript.Bootstrap.Functions.contains(unquote(left), unquote(right))
end
end
defmacro first .. last do
quote do
%ElixirScript.Range{ first: unquote(first), last: unquote(last) }
end
end
def throw(term) do
JS.throw(term)
end
end