-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.abc
208 lines (174 loc) · 4.49 KB
/
core.abc
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
macro type_integer 0
macro type_array 1
macro type_reference 2
macro type_type 3
macro type_null 4
macro type_bool 5
macro type_integer_u8 0
macro type_integer_u16 1
macro type_integer_u32 2
macro type_integer_u64 3
macro type_integer_i8 4
macro type_integer_i16 5
macro type_integer_i32 6
macro type_integer_i64 7
macro value_literal 0
macro value_variable 1
# making `if` and `loop` functions requires adding the ability for functions to
# manipualte the AST under them, so for now they are intrinsics until this
# functionality can be generically ironed out.
# assignment `:=` is also an intrinsic.
# `len` returns the number of items in an array, `0` for `null` and `1` for every other type.
# Compares 2 values and sets `x0` to `1` if they equal or sets `out` to `0` if they are not equal.
def =
# Require the given type is an array.
asm mov x0, type_array
asm mov x1, typeof in
asm cmp x0, x1
asm bne failure
# Require the given array has 2 elements.
asm mov x0, len in
asm mov x1, 2
asm cmp x0, x1
asm bne failure
# When lhs is u8
asm mov x0, typeof in[0]
asm mov x1, type_integer_u8
asm cmp x0, x1
asm bne lhs_not_u8
# rhs must be u8
asm mov x0, typeof in[1]
asm mov x1, type_integer_u8
asm cmp x0, x1
asm bne failure
# When lhs is literal
asm mov x0, valueof in[0]
asm mov x1, value_literal
asm cmp x0, x1
asm bne lhs_not_literal_u8
# When rhs is literal
asm mov x0, valueof in[1]
asm mov x1, value_literal
asm cmp x0, x1
asm bne rhs_not_literal_u8
# Output must be a variable
asm mov x0, valueof out
asm mov x1, value_variable
asm cmp x0, x1
asm bne failure
# Output must be a bool
asm mov x0, typeof out
asm mov x1, type_bool
asm cmp x0, x1
asm bne failure
# When lhs is a liteal u8 and rhs is a literal u8
asm mov x0, in[0]
asm mov x1, in[1]
asm cmp, x0, x1
# Set out
asm ldr x0, =out
asm movb, w1, 0
asm bne, not_equal
asm movb, w1, 1
asm not_equal:
asm strb w1, [x0]
asm b, success
asm rhs_not_literal_u8:
# todo
asm lhs_not_literal_u8:
# todo
asm lhs_not_u8:
# todo
# If requirements are not met hit failure case.
asm failure:
fail
asm success:
# If `in` is false, fails.
def require
# `in` must be a `bool`
asm mov x0, typeof in
asm mov x1, type_bool
asm cmp x0, x1
asm bne, failure
# `in` must be a variable
asm mov x0, valueof in
asm mov x1, value_variable
asm cmp x0, x1
asm bne, failure
# `in` must be equal to `1`
asm ldr x0, =in
asm ldrb w0, [x0]
asm movb w1, 1
asm cmp w0, w1
asm be, success
asm failure:
fail
asm success:
# Assigns rhs to lhs.
def :=
require typeof in = type_array
require len in = 1
require typeof in[0] = typeof in[1]
require valueof in[0] = value_variable
if valueof in[1] = value_literal
if typeof in[1] = type_integer_u8
asm ldr x0, =in[0]
asm movb w1, rhs
asm strb w1, [x0]
if typeof in[1] = type_integer_u16
# ...
# ...
# ...
def +=
require typeof in = type_array
require len in = 2
lhs := in[0]
rhs := in[1]
lhsv := valueof lhs
rhsv := valueof rhs
require lhsv = value_variable
lhst := typeof lhs
rhst := typeof rhs
require lhst = rhst
if rhsv = value_literal
if lhst[0] = type_integer
if lhst[1] = type_integer_u8
asm ldr x0, =lhs
asm ldrb w1, [x0]
asm add w1, rhs
asm strb w1, [x0]
if lhst[1] = type_integer_u16
# ...
# ...
# ...
if rhsv = variable
if lhst = u8
# ...
# ...
# ...
def sizeof
t := typeof in
require t = type_array
n := len in
require n = 2
lhs = in[0]
rhs = in[1]
lhst = typeof lhs
if lhst[0] = type_integer
if lhst[1] = type_integer_u8:
lhs := 1
if lhst[1] = type_integer_u16:
lhs := 2
if lhst[1] = type_integer_u32:
lhs := 4
if lhst[1] = type_integer_u64:
lhs := 8
if lhst[1] = type_integer_i8:
lhs := 1
if lhst[1] = type_integer_i16:
lhs := 2
if lhst[1] = type_integer_i32:
lhs := 4
if lhst[1] = type_integer_i64:
lhs := 8
# TODO Handle arrays and references