Repository navigation
Expand file tree
/
Copy pathcore.rv
More file actions
228 lines (181 loc) · 4.47 KB
/
Copy pathcore.rv
File metadata and controls
228 lines (181 loc) · 4.47 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Tags for core types. Objects with these tags are only produced by builtins.
export { Tag, Bits, Float32, Float64, Ref, Func, Closure }
Tag = tag"/Tag"
Bits = tag"/Bits"
Float32 = tag"/Float32"
Float64 = tag"/Float64"
Ref = tag"/Ref"
Func = tag"/Func"
Closure = tag"/Closure"
# These functions have built-in methods attached.
# TODO: could add a `fn { foo }` syntax to declare a function with no methods.
export {
pack, part, nparts, packcat, widen, shortcutEquals
bitsize, bitcast, bitcast_s
bitshl, bitshr_u, bitshr_s, bitand, bitor, bitxor, bitadd, bitsub, bitmul, bitdiv_u, bitdiv_s, bitrem_u, bitrem_s
biteq, biteqz, bitne, bitgt_u, bitge_u, bitlt_u, bitle_u, bitgt_s, bitge_s, bitlt_s, bitle_s
nil?, notnil, tagcast, tagstring, function, invoke, allocs, frees
forward, reverse
alloc, load, store
}
pack = tag"/pack"
part = tag"/part"
nparts = tag"/nparts"
packcat = tag"/packcat"
# set = tag"/set"
widen = tag"/widen"
shortcutEquals = tag"/shortcutEquals"
bitsize = tag"/bitsize"
bitcast = tag"/bitcast"
bitcast_s = tag"/bitcast_s"
# TODO rename to `core.shl` etc
bitshl = tag"/bitshl"
bitshr_u = tag"/bitshr_u"
bitshr_s = tag"/bitshr_s"
bitand = tag"/bitand"
bitor = tag"/bitor"
bitxor = tag"/bitxor"
bitadd = tag"/bitadd"
bitsub = tag"/bitsub"
bitmul = tag"/bitmul"
bitdiv_u = tag"/bitdiv_u"
bitdiv_s = tag"/bitdiv_s"
bitrem_u = tag"/bitrem_u"
bitrem_s = tag"/bitrem_s"
biteq = tag"/biteq"
biteqz = tag"/biteqz"
bitne = tag"/bitne"
bitgt_u = tag"/bitgt_u"
bitge_u = tag"/bitge_u"
bitlt_u = tag"/bitlt_u"
bitle_u = tag"/bitle_u"
bitgt_s = tag"/bitgt_s"
bitge_s = tag"/bitge_s"
bitlt_s = tag"/bitlt_s"
bitle_s = tag"/bitle_s"
nil? = tag"/nil?"
notnil = tag"/notnil"
tagcast = tag"/tagcast"
tagstring = tag"/tagstring"
function = tag"/function"
invoke = tag"/invoke"
allocs = tag"/allocs"
frees = tag"/frees"
alloc = tag"/alloc"
load = tag"/load"
store = tag"/store"
# length = tag"/length"
forward = tag"/forward"
reverse = tag"/reverse"
# Forward declarations, to avoid circularity.
export {
==, !=, !, +, -, *, /, ^, >, <, >=, <=, &, |, ..
show, print, println, replshow, string, concat
length, get, set, field, method, append, empty?, rest
iterate, next, condition
abort, js, jsref, Int32, Int64, String
}
== = tag"common/=="
!= = tag"common/!="
! = tag"common/!"
+ = tag"common/+"
- = tag"common/-"
* = tag"common/*"
/ = tag"common//"
^ = tag"common/^"
> = tag"common/>"
< = tag"common/<"
>= = tag"common/>="
<= = tag"common/<="
& = tag"common/&"
| = tag"common/|"
|> = tag"common/|>"
.. = tag"common/.."
show = tag"common/show"
print = tag"common/print"
println = tag"common/println"
replshow = tag"common/replshow"
string = tag"common/string"
concat = tag"common/concat"
length = tag"common/length"
get = tag"common/get"
set = tag"common/set"
field = tag"common/field"
method = tag"common/method"
append = tag"common/append"
empty? = tag"common/empty?"
rest = tag"common/rest"
iterate = tag"common/iterate"
next = tag"common/next"
condition = tag"common/condition"
abort = tag"common/abort"
js = tag"common/js"
jsref = tag"common/jsref"
Int32 = tag"common/Int32"
Int64 = tag"common/Int64"
String = tag"common.strings/String"
# Core definitions and fallbacks.
export {
Optional, Some, Nil, nil, Result, Ok, Err, unwrap, tag, identity, showPack
true, false
}
true = pack(tag"common.integer/Bool", bits"1")
false = pack(tag"common.integer/Bool", bits"0")
fn tag(x) { part(x, 0) }
@extend
fn method(x, f, args...) { field(x, f)(args...) }
@doc """
identity(x) == x
The identity function.
"""
fn identity(x) { x }
@extend
fn a == b { shortcutEquals(a, b) }
@extend
fn a != b { !(a == b) }
@extend
fn (x |> f) { f(x) }
fn showPack(x) {
print("pack(")
show(tag(x))
i = 1
while i <= nparts(x) {
print(", ")
show(part(x, i))
i = i + 1
}
print(")")
}
@extend
fn show(x) { showPack(x) }
@extend
fn print(x) { show(x) }
@extend
fn println() { print("\n") }
@extend
fn println(x) {
print(x)
println()
}
@extend
fn replshow(x) {
show(x)
println()
}
matchTrait = tag"common.patterns/matchTrait"
castTrait = tag"common.patterns/castTrait"
@extend
fn matchTrait(_, _) {}
# matchTrait is a shallow check, looking only at the tag. castTrait also checks
# the contents, which lets us narrow Any types.
@extend
fn castTrait(T, x) { matchTrait(T, x) }
bundle Optional { Some(x), Nil() }
nil = Nil()
@extend
fn show(_: Nil) { print("nil") }
@extend
fn replshow(_: Nil) {}
bundle Result { Ok(x), Err(e) }
fn unwrap(Ok(x)) { x }
fn unwrap(Err(x)) { abort("Tried to unwrap Err", x) }