-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprog.baz
More file actions
220 lines (172 loc) · 4.64 KB
/
prog.baz
File metadata and controls
220 lines (172 loc) · 4.64 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
field hello = "hello world from baz\n"
field prompt1 = "enter name:\n"
field prompt2 = "that is not a name.\n"
field prompt3 = "hello "
field dot = "."
field nl = "\n"
# all functions are inlined
# arguments can be placed in specified register using `reg_...` syntax
func exit(v : reg_rdi) {
mov(rax, 60) # exit system call
mov(rdi, v) # return code
syscall()
}
# single statement blocks can ommit { ... }
func assert(expr : bool) if not expr exit(1)
func print(len : reg_rdx, ptr : reg_rsi) {
mov(rax, 1) # write system call
mov(rdi, 0) # file descriptor for standard out
mov(rsi, ptr) # buffer address
mov(rdx, len) # buffer size
syscall()
}
func read(len : reg_rdx, ptr : reg_rsi) : i64 nbytes {
mov(rax, 0) # read system call
mov(rdi, 0) # file descriptor for standard input
mov(rsi, ptr) # buffer address
mov(rdx, len) # buffer size
syscall()
mov(nbytes, rax) # return value
}
# user types are defined using keyword `type`
# default type is `i64` and does not need to be specified
type point {x, y}
type object {pos : point, color : i32}
type world { locations : i64[8] }
# function arguments are equivalent to mutable references
func foo(pt : point) {
pt.x = 0b10 # binary value 2
pt.y = 0xb # hex value 11
}
# default argument type is `i64`
func bar(arg) {
if arg == 0 return
arg = 0xff
}
# return target is specified as a variable, in this case `res`
func inv(i : i32) : i32 res {
res = ~i
}
func baz(arg) : i64 res {
res = arg * 2
}
# array arguments are declared with type and []
func faz(arg : i32[]) {
arg[1] = 0xfe
}
type str {
len : i8,
data : i8[127]
}
func str_in(s : str) {
mov(rax, 0) # read system call
mov(rdi, 0) # file descriptor for standard input
mov(rsi, address_of(s.data)) # buffer address
mov(rdx, array_size_of(s.data)) # buffer size
syscall()
mov(s.len, rax - 1) # return value
}
func str_out(s : str) {
mov(rax, 1) # write system call
mov(rdi, 0) # file descriptor for standard out
mov(rsi, address_of(s.data)) # buffer address
mov(rdx, s.len) # buffer size
syscall()
}
func main() {
var arr : i32[4]
# arrays are initialized to 0
var ix = 1
arr[ix] = 2
arr[ix + 1] = arr[ix]
assert(arr[1] == 2)
assert(arr[2] == 2)
array_copy(arr[2], arr, 2)
# copy from, to, number of elements
assert(arr[0] == 2)
var arr1 : i32[8]
array_copy(arr, arr1, 4)
assert(arrays_equal(arr, arr1, 4))
# note: `arrays_equal` is built-in function
arr1[2] = -1
assert(not arrays_equal(arr, arr1, 4))
ix = 3
arr[ix] = ~inv(arr[ix - 1])
assert(arr[ix] == 2)
faz(arr)
assert(arr[1] == 0xfe)
var p : point = {0, 0}
foo(p)
assert(p.x == 2)
assert(p.y == 0xb)
var q : point = p
assert(equal(p, q))
# note: `equal` is built-in function to compare user types for equality
# or same size arrays
q.x = 3
assert(not equal(p, q))
var i = 0
bar(i)
assert(i == 0)
i = 1
bar(i)
assert(i == 0xff)
var j = 1
var k = baz(j)
assert(k == 2)
k = baz(1)
assert(k == 2)
var p0 : point = {baz(2), 0}
assert(p0.x == 4)
var x = 1
var y = 2
var o1 : object = {{x * 10, y}, 0xff0000}
assert(o1.pos.x == 10)
assert(o1.pos.y == 2)
assert(o1.color == 0xff0000)
var p1 : point = {-x, -y}
o1.pos = p1
assert(o1.pos.x == -1)
assert(o1.pos.y == -2)
var o2 : object = o1
assert(o2.pos.x == -1)
assert(o2.pos.y == -2)
assert(o2.color == 0xff0000)
var o3 : object[1]
# index 0 in an array can be accessed without array index
o3.pos.y = 73
assert(o3[0].pos.y == 73)
var worlds : world[8]
worlds[1].locations[1] = 0xffee
assert(worlds[1].locations[1] == 0xffee)
array_copy(
worlds[1].locations,
worlds[0].locations,
array_size_of(worlds.locations)
)
# note: `array_copy` is built-in and can use indexed positions
# `array_size_of` is built-in
assert(worlds[0].locations[1] == 0xffee)
assert(arrays_equal(
worlds[0].locations,
worlds[1].locations,
array_size_of(worlds.locations)
))
var nm : str
print(hello.len, hello)
loop {
print(prompt1.len, prompt1)
str_in(nm)
if nm.len == 0 {
break
} else if nm.len <= 4 {
print(prompt2.len, prompt2)
continue
} else {
print(prompt3.len, prompt3)
str_out(nm)
print(dot.len, dot)
print(nl.len, nl)
}
}
}