Compare commits
3 Commits
d78a77b075
...
1e7657ea2a
| Author | SHA1 | Date | |
|---|---|---|---|
| 1e7657ea2a | |||
| 3fdf7bb99d | |||
| 3fcd82cc04 |
@@ -5,9 +5,9 @@ A very cool language
|
|||||||
## Features
|
## Features
|
||||||
* Clean indentation-based syntax
|
* Clean indentation-based syntax
|
||||||
* Compiles to x86_64 Assembly
|
* Compiles to x86_64 Assembly
|
||||||
* Pretty big [standard library](https://github.com/antpiasecki/zern/tree/main/src/std)
|
* Growing [standard library](https://github.com/antpiasecki/zern/tree/main/src/std)
|
||||||
* Produces tiny static executables (~30KB with musl)
|
* Produces tiny static executables (~30KB with musl)
|
||||||
* ~~No libc required~~ (SOON; still used for memory allocation and DNS resolution)
|
* No libc required!
|
||||||
* Sometimes works
|
* Sometimes works
|
||||||
* Has the pipe operator
|
* Has the pipe operator
|
||||||
|
|
||||||
|
|||||||
@@ -11,15 +11,15 @@ extern IsKeyDown
|
|||||||
struct CHIP8
|
struct CHIP8
|
||||||
memory: ptr
|
memory: ptr
|
||||||
pc: i64
|
pc: i64
|
||||||
stack: array
|
stack: Array
|
||||||
sp: i64
|
sp: i64
|
||||||
reg: ptr
|
reg: ptr
|
||||||
I: i64
|
I: i64
|
||||||
delay_timer: i64
|
delay_timer: i64
|
||||||
sound_timer: i64
|
sound_timer: i64
|
||||||
keyboard: array
|
keyboard: Array
|
||||||
display: ptr
|
display: ptr
|
||||||
keyboard_map: array
|
keyboard_map: Array
|
||||||
|
|
||||||
func chip8_create[] : CHIP8
|
func chip8_create[] : CHIP8
|
||||||
let c: CHIP8 = new CHIP8
|
let c: CHIP8 = new CHIP8
|
||||||
@@ -33,7 +33,7 @@ func chip8_create[] : CHIP8
|
|||||||
c->stack = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
c->stack = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||||
c->keyboard = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
c->keyboard = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||||
|
|
||||||
let fonts: array = [0xf0, 0x90, 0x90, 0x90, 0xf0, 0x20, 0x60, 0x20, 0x20, 0x70, 0xf0, 0x10, 0xf0, 0x80, 0xf0, 0xf0, 0x10, 0xf0, 0x10, 0xf0, 0x90, 0x90, 0xf0, 0x10, 0x10, 0xf0, 0x80, 0xf0, 0x10, 0xf0, 0xf0, 0x80, 0xf0, 0x90, 0xf0, 0xf0, 0x10, 0x20, 0x40, 0x40, 0xf0, 0x90, 0xf0, 0x90, 0xf0, 0xf0, 0x90, 0xf0, 0x10, 0xf0, 0xf0, 0x90, 0xf0, 0x90, 0x90, 0xe0, 0x90, 0xe0, 0x90, 0xe0, 0xf0, 0x80, 0x80, 0x80, 0xf0, 0xe0, 0x90, 0x90, 0x90, 0xe0, 0xf0, 0x80, 0xf0, 0x80, 0xf0, 0xf0, 0x80, 0xf0, 0x80, 0x80]
|
let fonts: Array = [0xf0, 0x90, 0x90, 0x90, 0xf0, 0x20, 0x60, 0x20, 0x20, 0x70, 0xf0, 0x10, 0xf0, 0x80, 0xf0, 0xf0, 0x10, 0xf0, 0x10, 0xf0, 0x90, 0x90, 0xf0, 0x10, 0x10, 0xf0, 0x80, 0xf0, 0x10, 0xf0, 0xf0, 0x80, 0xf0, 0x90, 0xf0, 0xf0, 0x10, 0x20, 0x40, 0x40, 0xf0, 0x90, 0xf0, 0x90, 0xf0, 0xf0, 0x90, 0xf0, 0x10, 0xf0, 0xf0, 0x90, 0xf0, 0x90, 0x90, 0xe0, 0x90, 0xe0, 0x90, 0xe0, 0xf0, 0x80, 0x80, 0x80, 0xf0, 0xe0, 0x90, 0x90, 0x90, 0xe0, 0xf0, 0x80, 0xf0, 0x80, 0xf0, 0xf0, 0x80, 0xf0, 0x80, 0x80]
|
||||||
for i in 0..80
|
for i in 0..80
|
||||||
c->memory[i] = array.nth(fonts, i)
|
c->memory[i] = array.nth(fonts, i)
|
||||||
mem.free(fonts)
|
mem.free(fonts)
|
||||||
@@ -314,7 +314,7 @@ func chip8_step[c: CHIP8] : void
|
|||||||
else if op == 0xb
|
else if op == 0xb
|
||||||
c->pc = nnn + c->reg[0]
|
c->pc = nnn + c->reg[0]
|
||||||
else if op == 0xc
|
else if op == 0xc
|
||||||
c->reg[x] = (math.floor(os.urandom_i64()) % 256) & kk
|
c->reg[x] = (math.abs(os.urandom_i64()) % 256) & kk
|
||||||
else if op == 0xd
|
else if op == 0xd
|
||||||
c->reg[0xf] = 0
|
c->reg[0xf] = 0
|
||||||
for row in 0..n
|
for row in 0..n
|
||||||
@@ -389,14 +389,13 @@ func main[argc: i64, argv: ptr] : i64
|
|||||||
|
|
||||||
let c: CHIP8 = chip8_create()
|
let c: CHIP8 = chip8_create()
|
||||||
|
|
||||||
let bytes_size: ptr = 0
|
let buffer: io.Buffer = io.read_binary_file(path)
|
||||||
let bytes: ptr = io.read_binary_file(path, ^bytes_size)
|
|
||||||
|
|
||||||
for i in 0..bytes_size
|
for i in 0..buffer->size
|
||||||
c->memory[0x200 + i] = bytes[i]
|
c->memory[0x200 + i] = buffer->data[i]
|
||||||
|
|
||||||
if disassemble
|
if disassemble
|
||||||
chip8_disassemble(c, bytes_size / 2)
|
chip8_disassemble(c, buffer->size / 2)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
InitWindow(640, 320, "CHIP-8")
|
InitWindow(640, 320, "CHIP-8")
|
||||||
|
|||||||
@@ -1,70 +0,0 @@
|
|||||||
func main[argc: i64, argv: ptr] : i64
|
|
||||||
if argc < 2
|
|
||||||
dbg.panic("url missing")
|
|
||||||
|
|
||||||
let url: str = mem.read64(argv + 8)
|
|
||||||
|
|
||||||
if str.len(url) <= 7
|
|
||||||
dbg.panic("missing url scheme")
|
|
||||||
|
|
||||||
if !str.equal(str.substr(url, 0, 7), "http://")
|
|
||||||
dbg.panic("invalid url scheme")
|
|
||||||
|
|
||||||
let url_len: i64 = str.len(url)
|
|
||||||
let host_start = 7
|
|
||||||
let i: i64 = host_start
|
|
||||||
while i < url_len
|
|
||||||
if url[i] == '/'
|
|
||||||
break
|
|
||||||
i = i + 1
|
|
||||||
|
|
||||||
let host: str = str.substr(url, host_start, i - host_start)
|
|
||||||
let path: str = "/"
|
|
||||||
if i < url_len
|
|
||||||
path = str.substr(url, i, url_len - i)
|
|
||||||
|
|
||||||
let s: i64 = net.connect(host, 80)
|
|
||||||
if s < 0
|
|
||||||
dbg.panic("failed to connect")
|
|
||||||
|
|
||||||
// very leaky
|
|
||||||
let req: str = "GET "
|
|
||||||
req = str.concat(req, path)
|
|
||||||
req = str.concat(req, " HTTP/1.0\r\nHost: ")
|
|
||||||
req = str.concat(req, host)
|
|
||||||
req = str.concat(req, "\r\nConnection: close\r\n\r\n")
|
|
||||||
net.send(s, req, str.len(req))
|
|
||||||
mem.free(req)
|
|
||||||
|
|
||||||
let header_buf: str = mem.alloc(8192)
|
|
||||||
let header_size = 0
|
|
||||||
let found: bool = false
|
|
||||||
let end_index: i64 = -1
|
|
||||||
|
|
||||||
while !found && header_size < 8192
|
|
||||||
let n: i64 = net.read(s, header_buf + header_size, 8192 - header_size)
|
|
||||||
if n <= 0
|
|
||||||
break
|
|
||||||
let current_size: i64 = header_size + n
|
|
||||||
i = 0
|
|
||||||
while i <= current_size - 4
|
|
||||||
if header_buf[i] == 13 && header_buf[i + 1] == 10 && header_buf[i + 2] == 13 && header_buf[i + 3] == 10
|
|
||||||
found = true
|
|
||||||
end_index = i + 4
|
|
||||||
break
|
|
||||||
i = i + 1
|
|
||||||
header_size = current_size
|
|
||||||
|
|
||||||
if end_index < header_size
|
|
||||||
io.print_sized(header_buf + end_index, header_size - end_index)
|
|
||||||
mem.free(header_buf)
|
|
||||||
|
|
||||||
let buffer: ptr = mem.alloc(4096)
|
|
||||||
while true
|
|
||||||
let n: i64 = net.read(s, buffer, 4096)
|
|
||||||
if n <= 0
|
|
||||||
break
|
|
||||||
io.print_sized(buffer, n)
|
|
||||||
mem.free(buffer)
|
|
||||||
|
|
||||||
net.close(s)
|
|
||||||
@@ -1,29 +1,29 @@
|
|||||||
func part1[l1: array, l2: array] : void
|
func part1[l1: Array, l2: Array] : void
|
||||||
let out = 0
|
let out = 0
|
||||||
|
|
||||||
for i in 0..array.size(l1)
|
for i in 0..l1->size
|
||||||
out = out + math.abs(array.nth(l1, i) - array.nth(l2, i))
|
out = out + math.abs(array.nth(l1, i) - array.nth(l2, i))
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
func part2[l1: array, l2: array] : void
|
func part2[l1: Array, l2: Array] : void
|
||||||
let out = 0
|
let out = 0
|
||||||
|
|
||||||
for i in 0..array.size(l1)
|
for i in 0..l1->size
|
||||||
out = out + array.nth(l1, i) * alg.count(l2, array.nth(l1, i))
|
out = out + array.nth(l1, i) * alg.count(l2, array.nth(l1, i))
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||||
|
|
||||||
let l1: array = []
|
let l1: Array = []
|
||||||
let l2: array = []
|
let l2: Array = []
|
||||||
|
|
||||||
for i in 0..array.size(lines)
|
for i in 0..lines->size
|
||||||
let line: str = array.nth(lines, i)
|
let line: str = array.nth(lines, i)
|
||||||
|
|
||||||
let parts: array = str.split(line, " ")
|
let parts: Array = str.split(line, " ")
|
||||||
|
|
||||||
array.push(l1, str.parse_i64(array.nth(parts, 0)))
|
array.push(l1, str.parse_i64(array.nth(parts, 0)))
|
||||||
array.push(l2, str.parse_i64(array.nth(parts, 1)))
|
array.push(l2, str.parse_i64(array.nth(parts, 1)))
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
func check[report: array] : bool
|
func check[report: Array] : bool
|
||||||
let increasing: bool = array.nth(report, 0) < array.nth(report, 1)
|
let increasing: bool = array.nth(report, 0) < array.nth(report, 1)
|
||||||
|
|
||||||
for i in 0..array.size(report)-1
|
for i in 0..report->size-1
|
||||||
if array.nth(report, i) > array.nth(report, i + 1) && increasing
|
if array.nth(report, i) > array.nth(report, i + 1) && increasing
|
||||||
return false
|
return false
|
||||||
if array.nth(report, i) < array.nth(report, i + 1) && !increasing
|
if array.nth(report, i) < array.nth(report, i + 1) && !increasing
|
||||||
@@ -13,24 +13,25 @@ func check[report: array] : bool
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
||||||
func part1[data: array] : void
|
func part1[data: Array] : void
|
||||||
let out = 0
|
let out = 0
|
||||||
|
|
||||||
for i in 0..array.size(data)
|
for i in 0..data->size
|
||||||
if check(array.nth(data, i))
|
if check(array.nth(data, i))
|
||||||
out = out + 1
|
out = out + 1
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
func part2[data: array] : void
|
func part2[data: Array] : void
|
||||||
let out = 0
|
let out = 0
|
||||||
|
|
||||||
for i in 0..array.size(data)
|
for i in 0..data->size
|
||||||
if check(array.nth(data, i))
|
if check(array.nth(data, i))
|
||||||
out = out + 1
|
out = out + 1
|
||||||
else
|
else
|
||||||
for j in 0..array.size(array.nth(data, i))
|
let arr: Array = array.nth(data, i)
|
||||||
let sliced: array = array.concat(array.slice(array.nth(data, i), 0, j), array.slice(array.nth(data, i), j + 1, array.size(array.nth(data, i)) - (j + 1)))
|
for j in 0..arr->size
|
||||||
|
let sliced: Array = array.concat(array.slice(arr, 0, j), array.slice(arr, j + 1, arr->size - (j + 1)))
|
||||||
|
|
||||||
if check(sliced)
|
if check(sliced)
|
||||||
out = out + 1
|
out = out + 1
|
||||||
@@ -39,14 +40,14 @@ func part2[data: array] : void
|
|||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||||
|
|
||||||
let data: array = []
|
let data: Array = []
|
||||||
for i in 0..array.size(lines)
|
for i in 0..lines->size
|
||||||
let line: str = array.nth(lines, i)
|
let line: str = array.nth(lines, i)
|
||||||
|
|
||||||
let report: array = str.split(line, " ")
|
let report: Array = str.split(line, " ")
|
||||||
for i in 0..array.size(report)
|
for i in 0..report->size
|
||||||
array.set(report, i, str.parse_i64(array.nth(report, i)))
|
array.set(report, i, str.parse_i64(array.nth(report, i)))
|
||||||
array.push(data, report)
|
array.push(data, report)
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
func check[lines: array, x: i64, y: i64, dx: i64, dy: i64] : bool
|
func check[lines: Array, x: i64, y: i64, dx: i64, dy: i64] : bool
|
||||||
if x + dx * 3 < 0 || x + dx * 3 >= array.size(lines) || y + dy * 3 < 0 || y + dy * 3 >= str.len(array.nth(lines, 0))
|
if x + dx * 3 < 0 || x + dx * 3 >= lines->size || y + dy * 3 < 0 || y + dy * 3 >= str.len(array.nth(lines, 0))
|
||||||
return false
|
return false
|
||||||
|
|
||||||
if array.nth(lines, x)[y] != 'X'
|
if array.nth(lines, x)[y] != 'X'
|
||||||
@@ -13,10 +13,10 @@ func check[lines: array, x: i64, y: i64, dx: i64, dy: i64] : bool
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
||||||
func part1[lines: array] : void
|
func part1[lines: Array] : void
|
||||||
let out = 0
|
let out = 0
|
||||||
|
|
||||||
for x in 0..array.size(lines)
|
for x in 0..lines->size
|
||||||
for y in 0..str.len(array.nth(lines, x))
|
for y in 0..str.len(array.nth(lines, x))
|
||||||
if check(lines, x, y, 0, 1)
|
if check(lines, x, y, 0, 1)
|
||||||
out = out + 1
|
out = out + 1
|
||||||
@@ -37,10 +37,10 @@ func part1[lines: array] : void
|
|||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
func part2[lines: array] : void
|
func part2[lines: Array] : void
|
||||||
let out = 0
|
let out = 0
|
||||||
|
|
||||||
for x in 1..array.size(lines)-1
|
for x in 1..lines->size-1
|
||||||
for y in 1..str.len(array.nth(lines, x))-1
|
for y in 1..str.len(array.nth(lines, x))-1
|
||||||
if array.nth(lines, x)[y] == 'A'
|
if array.nth(lines, x)[y] == 'A'
|
||||||
let s: str = mem.alloc(5)
|
let s: str = mem.alloc(5)
|
||||||
@@ -56,7 +56,7 @@ func part2[lines: array] : void
|
|||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||||
|
|
||||||
part1(lines)
|
part1(lines)
|
||||||
part2(lines)
|
part2(lines)
|
||||||
|
|||||||
@@ -1,22 +1,22 @@
|
|||||||
func rule_exists[rules_left: array, rules_right: array, a: i64, b: i64] : bool
|
func rule_exists[rules_left: Array, rules_right: Array, a: i64, b: i64] : bool
|
||||||
for k in 0..array.size(rules_left)
|
for k in 0..rules_left->size
|
||||||
if array.nth(rules_left, k) == a && array.nth(rules_right, k) == b
|
if array.nth(rules_left, k) == a && array.nth(rules_right, k) == b
|
||||||
return true
|
return true
|
||||||
return false
|
return false
|
||||||
|
|
||||||
func check[update: array, rules_left: array, rules_right: array] : bool
|
func check[update: Array, rules_left: Array, rules_right: Array] : bool
|
||||||
for i in 0..array.size(update)
|
for i in 0..update->size
|
||||||
for j in 0..i
|
for j in 0..i
|
||||||
if rule_exists(rules_left, rules_right, array.nth(update, i), array.nth(update, j))
|
if rule_exists(rules_left, rules_right, array.nth(update, i), array.nth(update, j))
|
||||||
return false
|
return false
|
||||||
return true
|
return true
|
||||||
|
|
||||||
func sort_by_rules[update: array, rules_left: array, rules_right: array] : void
|
func sort_by_rules[update: Array, rules_left: Array, rules_right: Array] : void
|
||||||
let swapped: bool = true
|
let swapped: bool = true
|
||||||
|
|
||||||
while swapped
|
while swapped
|
||||||
swapped = false
|
swapped = false
|
||||||
for i in 0..array.size(update)-1
|
for i in 0..update->size-1
|
||||||
let a: i64 = array.nth(update, i)
|
let a: i64 = array.nth(update, i)
|
||||||
let b: i64 = array.nth(update, i+1)
|
let b: i64 = array.nth(update, i+1)
|
||||||
if rule_exists(rules_left, rules_right, b, a)
|
if rule_exists(rules_left, rules_right, b, a)
|
||||||
@@ -25,47 +25,47 @@ func sort_by_rules[update: array, rules_left: array, rules_right: array] : void
|
|||||||
array.set(update, i+1, tmp)
|
array.set(update, i+1, tmp)
|
||||||
swapped = true
|
swapped = true
|
||||||
|
|
||||||
func part1[updates: array, rules_left: array, rules_right: array] : void
|
func part1[updates: Array, rules_left: Array, rules_right: Array] : void
|
||||||
let out = 0
|
let out = 0
|
||||||
|
|
||||||
for i in 0..array.size(updates)
|
for i in 0..updates->size
|
||||||
let update: array = array.nth(updates, i)
|
let update: Array = array.nth(updates, i)
|
||||||
if check(update, rules_left, rules_right)
|
if check(update, rules_left, rules_right)
|
||||||
out = out + array.nth(update, array.size(update) / 2)
|
out = out + array.nth(update, update->size / 2)
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
func part2[updates: array, rules_left: array, rules_right: array] : void
|
func part2[updates: Array, rules_left: Array, rules_right: Array] : void
|
||||||
let out = 0
|
let out = 0
|
||||||
|
|
||||||
for i in 0..array.size(updates)
|
for i in 0..updates->size
|
||||||
let update: array = array.nth(updates, i)
|
let update: Array = array.nth(updates, i)
|
||||||
if !check(update, rules_left, rules_right)
|
if !check(update, rules_left, rules_right)
|
||||||
sort_by_rules(update, rules_left, rules_right)
|
sort_by_rules(update, rules_left, rules_right)
|
||||||
out = out + array.nth(update, array.size(update) / 2)
|
out = out + array.nth(update, update->size / 2)
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
let data: array = io.read_file("input.txt") |> str.split("\n\n")
|
let data: Array = io.read_file("input.txt") |> str.split("\n\n")
|
||||||
|
|
||||||
let rules_left: array = []
|
let rules_left: Array = []
|
||||||
let rules_right: array = []
|
let rules_right: Array = []
|
||||||
let rules_lines: array = str.split(array.nth(data, 0), "\n")
|
let rules_lines: Array = str.split(array.nth(data, 0), "\n")
|
||||||
for i in 0..array.size(rules_lines)
|
for i in 0..rules_lines->size
|
||||||
let line: str = array.nth(rules_lines, i)
|
let line: str = array.nth(rules_lines, i)
|
||||||
let parts: array = str.split(line, "|")
|
let parts: Array = str.split(line, "|")
|
||||||
|
|
||||||
array.push(rules_left, str.parse_i64(array.nth(parts, 0)))
|
array.push(rules_left, str.parse_i64(array.nth(parts, 0)))
|
||||||
array.push(rules_right, str.parse_i64(array.nth(parts, 1)))
|
array.push(rules_right, str.parse_i64(array.nth(parts, 1)))
|
||||||
|
|
||||||
let updates: array = []
|
let updates: Array = []
|
||||||
let updates_lines: array = str.split(array.nth(data, 1), "\n")
|
let updates_lines: Array = str.split(array.nth(data, 1), "\n")
|
||||||
for i in 0..array.size(updates_lines)
|
for i in 0..updates_lines->size
|
||||||
let line: str = array.nth(updates_lines, i)
|
let line: str = array.nth(updates_lines, i)
|
||||||
let xs: array = str.split(line, ",")
|
let xs: Array = str.split(line, ",")
|
||||||
|
|
||||||
for i in 0..array.size(xs)
|
for i in 0..xs->size
|
||||||
array.set(xs, i, str.parse_i64(array.nth(xs, i)))
|
array.set(xs, i, str.parse_i64(array.nth(xs, i)))
|
||||||
array.push(updates, xs)
|
array.push(updates, xs)
|
||||||
|
|
||||||
|
|||||||
@@ -9,23 +9,24 @@ func concat[a: i64, b: i64] : i64
|
|||||||
mem.free(ab_str)
|
mem.free(ab_str)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
func solve[ops: array, e: array] : i64
|
func solve[ops: Array, e: Array] : i64
|
||||||
let n: i64 = array.size(array.nth(e, 1)) - 1
|
let e_1: Array = array.nth(e, 1)
|
||||||
let indices: array = []
|
let n: i64 = e_1->size - 1
|
||||||
|
let indices: Array = []
|
||||||
for i in 0..n
|
for i in 0..n
|
||||||
array.push(indices, 0)
|
array.push(indices, 0)
|
||||||
|
|
||||||
while true
|
while true
|
||||||
let res: i64 = array.nth(array.nth(e, 1), 0)
|
let res: i64 = array.nth(e_1, 0)
|
||||||
for i in 0..n
|
for i in 0..n
|
||||||
let op: str = array.nth(ops, array.nth(indices, i))
|
let op: str = array.nth(ops, array.nth(indices, i))
|
||||||
|
|
||||||
if str.equal(op, "add")
|
if str.equal(op, "add")
|
||||||
res = res + array.nth(array.nth(e, 1), i + 1)
|
res = res + array.nth(e_1, i + 1)
|
||||||
else if str.equal(op, "mul")
|
else if str.equal(op, "mul")
|
||||||
res = res * array.nth(array.nth(e, 1), i + 1)
|
res = res * array.nth(e_1, i + 1)
|
||||||
else if str.equal(op, "concat")
|
else if str.equal(op, "concat")
|
||||||
res = concat(res, array.nth(array.nth(e, 1), i + 1))
|
res = concat(res, array.nth(e_1, i + 1))
|
||||||
if res == array.nth(e, 0)
|
if res == array.nth(e, 0)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@ func solve[ops: array, e: array] : i64
|
|||||||
|
|
||||||
while i >= 0
|
while i >= 0
|
||||||
array.set(indices, i, array.nth(indices, i) + 1)
|
array.set(indices, i, array.nth(indices, i) + 1)
|
||||||
if array.nth(indices, i) < array.size(ops)
|
if array.nth(indices, i) < ops->size
|
||||||
done = false
|
done = false
|
||||||
break
|
break
|
||||||
array.set(indices, i, 0)
|
array.set(indices, i, 0)
|
||||||
@@ -43,32 +44,32 @@ func solve[ops: array, e: array] : i64
|
|||||||
if done
|
if done
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
func part1[equations: array] : void
|
func part1[equations: Array] : void
|
||||||
let out = 0
|
let out = 0
|
||||||
|
|
||||||
for i in 0..array.size(equations)
|
for i in 0..equations->size
|
||||||
out = out + solve(["add", "mul"], array.nth(equations, i))
|
out = out + solve(["add", "mul"], array.nth(equations, i))
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
func part2[equations: array] : void
|
func part2[equations: Array] : void
|
||||||
let out = 0
|
let out = 0
|
||||||
|
|
||||||
for i in 0..array.size(equations)
|
for i in 0..equations->size
|
||||||
out = out + solve(["add", "mul", "concat"], array.nth(equations, i))
|
out = out + solve(["add", "mul", "concat"], array.nth(equations, i))
|
||||||
|
|
||||||
io.println_i64(out)
|
io.println_i64(out)
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||||
let equations: array = []
|
let equations: Array = []
|
||||||
|
|
||||||
for i in 0..array.size(lines)
|
for i in 0..lines->size
|
||||||
let line: str = array.nth(lines, i)
|
let line: str = array.nth(lines, i)
|
||||||
let parts: array = str.split(line, ": ")
|
let parts: Array = str.split(line, ": ")
|
||||||
|
|
||||||
let xs: array = str.split(array.nth(parts, 1), " ")
|
let xs: Array = str.split(array.nth(parts, 1), " ")
|
||||||
for j in 0..array.size(xs)
|
for j in 0..xs->size
|
||||||
array.set(xs, j, str.parse_i64(array.nth(xs, j)))
|
array.set(xs, j, str.parse_i64(array.nth(xs, j)))
|
||||||
|
|
||||||
array.push(equations, [str.parse_i64(array.nth(parts, 0)), xs])
|
array.push(equations, [str.parse_i64(array.nth(parts, 0)), xs])
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
func part1[lines: array] : void
|
func part1[lines: Array] : void
|
||||||
let password = 0
|
let password = 0
|
||||||
let dial = 50
|
let dial = 50
|
||||||
|
|
||||||
for i in 0..array.size(lines)
|
for i in 0..lines->size
|
||||||
let line: str = array.nth(lines, i)
|
let line: str = array.nth(lines, i)
|
||||||
let dir: u8 = line[0]
|
let dir: u8 = line[0]
|
||||||
let n: i64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
let n: i64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||||
@@ -21,11 +21,11 @@ func part1[lines: array] : void
|
|||||||
|
|
||||||
io.println_i64(password)
|
io.println_i64(password)
|
||||||
|
|
||||||
func part2[lines: array] : void
|
func part2[lines: Array] : void
|
||||||
let password = 0
|
let password = 0
|
||||||
let dial = 50
|
let dial = 50
|
||||||
|
|
||||||
for i in 0..array.size(lines)
|
for i in 0..lines->size
|
||||||
let line: str = array.nth(lines, i)
|
let line: str = array.nth(lines, i)
|
||||||
let dir: u8 = line[0]
|
let dir: u8 = line[0]
|
||||||
let n: i64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
let n: i64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||||
@@ -47,7 +47,7 @@ func part2[lines: array] : void
|
|||||||
io.println_i64(password)
|
io.println_i64(password)
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||||
|
|
||||||
part1(lines)
|
part1(lines)
|
||||||
part2(lines)
|
part2(lines)
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ func part1_is_invalid_id[s: str] : bool
|
|||||||
|
|
||||||
return str.equal(first, second)
|
return str.equal(first, second)
|
||||||
|
|
||||||
func part1[ranges: array] : void
|
func part1[ranges: Array] : void
|
||||||
let sum = 0
|
let sum = 0
|
||||||
|
|
||||||
for i in 0..array.size(ranges)
|
for i in 0..ranges->size
|
||||||
let parts: array = array.nth(ranges, i) |> str.split("-")
|
let parts: Array = array.nth(ranges, i) |> str.split("-")
|
||||||
let start: i64 = array.nth(parts, 0) |> str.parse_i64()
|
let start: i64 = array.nth(parts, 0) |> str.parse_i64()
|
||||||
let end: i64 = array.nth(parts, 1) |> str.parse_i64()
|
let end: i64 = array.nth(parts, 1) |> str.parse_i64()
|
||||||
|
|
||||||
@@ -39,11 +39,11 @@ func part2_is_invalid_id[s: str] : bool
|
|||||||
return true
|
return true
|
||||||
return false
|
return false
|
||||||
|
|
||||||
func part2[ranges: array] : void
|
func part2[ranges: Array] : void
|
||||||
let sum = 0
|
let sum = 0
|
||||||
|
|
||||||
for i in 0..array.size(ranges)
|
for i in 0..ranges->size
|
||||||
let parts: array = array.nth(ranges, i) |> str.split("-")
|
let parts: Array = array.nth(ranges, i) |> str.split("-")
|
||||||
let start: i64 = array.nth(parts, 0) |> str.parse_i64()
|
let start: i64 = array.nth(parts, 0) |> str.parse_i64()
|
||||||
let end: i64 = array.nth(parts, 1) |> str.parse_i64()
|
let end: i64 = array.nth(parts, 1) |> str.parse_i64()
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ func part2[ranges: array] : void
|
|||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
let ranges: array = io.read_file("input.txt") |> str.split(",")
|
let ranges: Array = io.read_file("input.txt") |> str.split(",")
|
||||||
|
|
||||||
part1(ranges)
|
part1(ranges)
|
||||||
part2(ranges)
|
part2(ranges)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
func part1[lines: array] : void
|
func part1[lines: Array] : void
|
||||||
let sum = 0
|
let sum = 0
|
||||||
|
|
||||||
for i in 0..array.size(lines)
|
for i in 0..lines->size
|
||||||
let line: str = array.nth(lines, i)
|
let line: str = array.nth(lines, i)
|
||||||
|
|
||||||
let largest = 0
|
let largest = 0
|
||||||
@@ -35,17 +35,17 @@ func part2_rec[bank: str, start: i64, remaining: i64] : i64
|
|||||||
else
|
else
|
||||||
return largest
|
return largest
|
||||||
|
|
||||||
func part2[lines: array] : void
|
func part2[lines: Array] : void
|
||||||
let sum = 0
|
let sum = 0
|
||||||
|
|
||||||
for i in 0..array.size(lines)
|
for i in 0..lines->size
|
||||||
let line: str = array.nth(lines, i)
|
let line: str = array.nth(lines, i)
|
||||||
|
|
||||||
sum = sum + part2_rec(line, 0, 12)
|
sum = sum + part2_rec(line, 0, 12)
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||||
|
|
||||||
part1(lines)
|
part1(lines)
|
||||||
part2(lines)
|
part2(lines)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
func main[] : i64
|
func main[] : i64
|
||||||
let N = 20
|
let N = 20
|
||||||
|
|
||||||
let grid: array = []
|
let grid: Array = []
|
||||||
array.push(grid, [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8])
|
array.push(grid, [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8])
|
||||||
array.push(grid, [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0])
|
array.push(grid, [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0])
|
||||||
array.push(grid, [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65])
|
array.push(grid, [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65])
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
func main[] : i64
|
func main[] : i64
|
||||||
let n: array = []
|
let n: Array = []
|
||||||
array.push(n, 1)
|
array.push(n, 1)
|
||||||
|
|
||||||
for j in 0..1000
|
for j in 0..1000
|
||||||
let carry = 0
|
let carry = 0
|
||||||
for i in 0..array.size(n)
|
for i in 0..n->size
|
||||||
let tmp: i64 = array.nth(n, i) * 2 + carry
|
let tmp: i64 = array.nth(n, i) * 2 + carry
|
||||||
array.set(n, i, tmp % 10)
|
array.set(n, i, tmp % 10)
|
||||||
carry = tmp / 10
|
carry = tmp / 10
|
||||||
@@ -13,7 +13,7 @@ func main[] : i64
|
|||||||
carry = carry / 10
|
carry = carry / 10
|
||||||
|
|
||||||
let sum = 0
|
let sum = 0
|
||||||
for i in 0..array.size(n)
|
for i in 0..n->size
|
||||||
sum = sum + array.nth(n, i)
|
sum = sum + array.nth(n, i)
|
||||||
|
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
func main[] : i64
|
func main[] : i64
|
||||||
let s1: array = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
|
let s1: Array = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
|
||||||
let s2: array = [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
|
let s2: Array = [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
|
||||||
let s3: array = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]
|
let s3: Array = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]
|
||||||
|
|
||||||
let sum = 0
|
let sum = 0
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
func findmax[triangle: array, row: i64, col: i64] : i64
|
func findmax[triangle: Array, row: i64, col: i64] : i64
|
||||||
if row == 14
|
if row == 14
|
||||||
return array.nth(array.nth(triangle, row), col)
|
return array.nth(array.nth(triangle, row), col)
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ func findmax[triangle: array, row: i64, col: i64] : i64
|
|||||||
return array.nth(array.nth(triangle, row), col) + math.max(left, right)
|
return array.nth(array.nth(triangle, row), col) + math.max(left, right)
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
let triangle: array = []
|
let triangle: Array = []
|
||||||
array.push(triangle, [75])
|
array.push(triangle, [75])
|
||||||
array.push(triangle, [95, 64])
|
array.push(triangle, [95, 64])
|
||||||
array.push(triangle, [17, 47, 82])
|
array.push(triangle, [17, 47, 82])
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
func multiply[n: array, x: i64] : void
|
func multiply[n: Array, x: i64] : void
|
||||||
let carry = 0
|
let carry = 0
|
||||||
for i in 0..array.size(n)
|
for i in 0..n->size
|
||||||
let prod: i64 = array.nth(n, i) * x + carry
|
let prod: i64 = array.nth(n, i) * x + carry
|
||||||
array.set(n, i, prod % 10)
|
array.set(n, i, prod % 10)
|
||||||
carry = prod / 10
|
carry = prod / 10
|
||||||
@@ -9,13 +9,13 @@ func multiply[n: array, x: i64] : void
|
|||||||
carry = carry / 10
|
carry = carry / 10
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
let n: array = [1]
|
let n: Array = [1]
|
||||||
|
|
||||||
for i in 2..101
|
for i in 2..101
|
||||||
multiply(n, i)
|
multiply(n, i)
|
||||||
|
|
||||||
let sum = 0
|
let sum = 0
|
||||||
for i in 0..array.size(n)
|
for i in 0..n->size
|
||||||
sum = sum + array.nth(n, i)
|
sum = sum + array.nth(n, i)
|
||||||
|
|
||||||
io.println_i64(sum)
|
io.println_i64(sum)
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
func main[] : i64
|
func main[] : i64
|
||||||
let arr: array = []
|
let arr: Array = []
|
||||||
for i in 0..10
|
for i in 0..10
|
||||||
array.push(arr, math.abs(os.urandom_i64()) % 1000)
|
array.push(arr, math.abs(os.urandom_i64()) % 1000)
|
||||||
|
|
||||||
for i in 0..array.size(arr)
|
for i in 0..arr->size
|
||||||
io.println_i64(array.nth(arr, i))
|
io.println_i64(array.nth(arr, i))
|
||||||
io.println("------------")
|
io.println("------------")
|
||||||
|
|
||||||
alg.quicksort(arr)
|
alg.quicksort(arr)
|
||||||
|
|
||||||
for i in 0..array.size(arr)
|
for i in 0..arr->size
|
||||||
io.println_i64(array.nth(arr, i))
|
io.println_i64(array.nth(arr, i))
|
||||||
|
|
||||||
array.free(arr)
|
array.free(arr)
|
||||||
|
|||||||
@@ -1,22 +1,21 @@
|
|||||||
func rule110_step[state: array] : array
|
func rule110_step[state: Array] : Array
|
||||||
let new_state: array = []
|
let new_state: Array = []
|
||||||
let state_len: i64 = array.size(state)
|
|
||||||
|
|
||||||
for i in 0..state_len
|
for i in 0..state->size
|
||||||
let left: bool = false
|
let left: bool = false
|
||||||
if i - 1 >= 0
|
if i - 1 >= 0
|
||||||
left = array.nth(state, i - 1)
|
left = array.nth(state, i - 1)
|
||||||
let center: bool = array.nth(state, i)
|
let center: bool = array.nth(state, i)
|
||||||
let right: bool = false
|
let right: bool = false
|
||||||
if i + 1 < state_len
|
if i + 1 < state->size
|
||||||
right = array.nth(state, i + 1)
|
right = array.nth(state, i + 1)
|
||||||
|
|
||||||
array.push(new_state, !((!left && !center && !right) || (left && !center && !right) || (left && center && right)))
|
array.push(new_state, !((!left && !center && !right) || (left && !center && !right) || (left && center && right)))
|
||||||
|
|
||||||
return new_state
|
return new_state
|
||||||
|
|
||||||
func print_state[state: array]: void
|
func print_state[state: Array]: void
|
||||||
for i in 0..array.size(state)
|
for i in 0..state->size
|
||||||
if array.nth(state, i)
|
if array.nth(state, i)
|
||||||
io.print_char('#')
|
io.print_char('#')
|
||||||
else
|
else
|
||||||
@@ -26,7 +25,7 @@ func print_state[state: array]: void
|
|||||||
func main[] : i64
|
func main[] : i64
|
||||||
let SIZE = 60
|
let SIZE = 60
|
||||||
|
|
||||||
let state: array = []
|
let state: Array = []
|
||||||
for i in 0..SIZE
|
for i in 0..SIZE
|
||||||
array.push(state, false)
|
array.push(state, false)
|
||||||
array.push(state, true)
|
array.push(state, true)
|
||||||
|
|||||||
@@ -17,11 +17,11 @@ func main[] : i64
|
|||||||
|
|
||||||
rc = sqlite3_open("todo.db", ^db)
|
rc = sqlite3_open("todo.db", ^db)
|
||||||
if rc
|
if rc
|
||||||
dbg.panic("failed to open db")
|
panic("failed to open db")
|
||||||
|
|
||||||
rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS todo(id INTEGER PRIMARY KEY AUTOINCREMENT, task TEXT NOT NULL);", 0, 0, 0)
|
rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS todo(id INTEGER PRIMARY KEY AUTOINCREMENT, task TEXT NOT NULL);", 0, 0, 0)
|
||||||
if rc
|
if rc
|
||||||
dbg.panic(sqlite3_errmsg(db))
|
panic(sqlite3_errmsg(db))
|
||||||
|
|
||||||
while true
|
while true
|
||||||
io.println("1. List tasks")
|
io.println("1. List tasks")
|
||||||
@@ -54,7 +54,7 @@ func main[] : i64
|
|||||||
sqlite3_prepare_v2(db, "INSERT INTO todo(task) VALUES (?);", -1, ^stmt, 0)
|
sqlite3_prepare_v2(db, "INSERT INTO todo(task) VALUES (?);", -1, ^stmt, 0)
|
||||||
sqlite3_bind_text(stmt, 1, task, -1, 0)
|
sqlite3_bind_text(stmt, 1, task, -1, 0)
|
||||||
if sqlite3_step(stmt) != 101
|
if sqlite3_step(stmt) != 101
|
||||||
dbg.panic(sqlite3_errmsg(db))
|
panic(sqlite3_errmsg(db))
|
||||||
|
|
||||||
io.println("\nTask added\n")
|
io.println("\nTask added\n")
|
||||||
else if choice == 3
|
else if choice == 3
|
||||||
@@ -64,7 +64,7 @@ func main[] : i64
|
|||||||
sqlite3_prepare_v2(db, "DELETE FROM todo WHERE id = ?;", -1, ^stmt, 0)
|
sqlite3_prepare_v2(db, "DELETE FROM todo WHERE id = ?;", -1, ^stmt, 0)
|
||||||
sqlite3_bind_int(stmt, 1, id, -1, 0)
|
sqlite3_bind_int(stmt, 1, id, -1, 0)
|
||||||
if sqlite3_step(stmt) != 101
|
if sqlite3_step(stmt) != 101
|
||||||
dbg.panic(sqlite3_errmsg(db))
|
panic(sqlite3_errmsg(db))
|
||||||
|
|
||||||
io.println("\nTask deleted\n")
|
io.println("\nTask deleted\n")
|
||||||
|
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ macro_rules! emit {
|
|||||||
static REGISTERS: [&str; 6] = ["rdi", "rsi", "rdx", "rcx", "r8", "r9"];
|
static REGISTERS: [&str; 6] = ["rdi", "rsi", "rdx", "rcx", "r8", "r9"];
|
||||||
|
|
||||||
// TODO: currently they are all just 64 bit values
|
// TODO: currently they are all just 64 bit values
|
||||||
static BUILTIN_TYPES: [&str; 7] = ["void", "u8", "i64", "str", "bool", "ptr", "array"];
|
static BUILTIN_TYPES: [&str; 6] = ["void", "u8", "i64", "str", "bool", "ptr"];
|
||||||
|
|
||||||
pub struct CodegenX86_64<'a> {
|
pub struct CodegenX86_64<'a> {
|
||||||
output: String,
|
output: String,
|
||||||
@@ -106,6 +106,20 @@ impl<'a> CodegenX86_64<'a> {
|
|||||||
"section .note.GNU-stack
|
"section .note.GNU-stack
|
||||||
db 0
|
db 0
|
||||||
|
|
||||||
|
section .bss
|
||||||
|
_heap_head: resq 1
|
||||||
|
_heap_tail: resq 1
|
||||||
|
|
||||||
|
section .text._builtin_heap_head
|
||||||
|
_builtin_heap_head:
|
||||||
|
lea rax, [rel _heap_head]
|
||||||
|
ret
|
||||||
|
|
||||||
|
section .text._builtin_heap_tail
|
||||||
|
_builtin_heap_tail:
|
||||||
|
lea rax, [rel _heap_tail]
|
||||||
|
ret
|
||||||
|
|
||||||
section .text._builtin_read64
|
section .text._builtin_read64
|
||||||
_builtin_read64:
|
_builtin_read64:
|
||||||
mov rax, qword [rdi]
|
mov rax, qword [rdi]
|
||||||
@@ -226,7 +240,7 @@ _builtin_environ:
|
|||||||
Stmt::Function {
|
Stmt::Function {
|
||||||
name,
|
name,
|
||||||
params,
|
params,
|
||||||
return_type: _,
|
return_type,
|
||||||
body,
|
body,
|
||||||
exported,
|
exported,
|
||||||
} => {
|
} => {
|
||||||
@@ -239,10 +253,17 @@ _builtin_environ:
|
|||||||
emit!(&mut self.output, " mov rbp, rsp");
|
emit!(&mut self.output, " mov rbp, rsp");
|
||||||
emit!(&mut self.output, " sub rsp, 256"); // TODO
|
emit!(&mut self.output, " sub rsp, 256"); // TODO
|
||||||
|
|
||||||
|
if !self.is_valid_type_name(&return_type.lexeme) {
|
||||||
|
return error!(
|
||||||
|
&return_type.loc,
|
||||||
|
"unrecognized type: ".to_owned() + &return_type.lexeme
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
for (i, param) in params.iter().enumerate() {
|
for (i, param) in params.iter().enumerate() {
|
||||||
if !self.is_valid_type_name(¶m.var_type.lexeme) {
|
if !self.is_valid_type_name(¶m.var_type.lexeme) {
|
||||||
return error!(
|
return error!(
|
||||||
&name.loc,
|
¶m.var_name.loc,
|
||||||
"unrecognized type: ".to_owned() + ¶m.var_type.lexeme
|
"unrecognized type: ".to_owned() + ¶m.var_type.lexeme
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func crypto.blake2b._G[v: ptr, a: i64, b: i64, c: i64, d: i64, x: i64, y: i64] :
|
|||||||
mem.write64(v + c * 8, mem.read64(v + c * 8) + mem.read64(v + d * 8))
|
mem.write64(v + c * 8, mem.read64(v + c * 8) + mem.read64(v + d * 8))
|
||||||
mem.write64(v + b * 8, crypto.rotr64(mem.read64(v + b * 8) ^ mem.read64(v + c * 8), 63))
|
mem.write64(v + b * 8, crypto.rotr64(mem.read64(v + b * 8) ^ mem.read64(v + c * 8), 63))
|
||||||
|
|
||||||
func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, iv: array, sigma: array] : void
|
func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, iv: Array, sigma: Array] : void
|
||||||
let v: ptr = mem.alloc(16 * 8)
|
let v: ptr = mem.alloc(16 * 8)
|
||||||
let m: ptr = mem.alloc(16 * 8)
|
let m: ptr = mem.alloc(16 * 8)
|
||||||
|
|
||||||
@@ -60,17 +60,17 @@ func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, i
|
|||||||
|
|
||||||
func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputlen: i64] : ptr
|
func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputlen: i64] : ptr
|
||||||
if outlen == 0 || outlen > 64 || keylen > 64
|
if outlen == 0 || outlen > 64 || keylen > 64
|
||||||
dbg.panic("invalid length passed to crypto.blake2b.hash")
|
panic("invalid length passed to crypto.blake2b.hash")
|
||||||
let out: ptr = mem.alloc(outlen)
|
let out: ptr = mem.alloc(outlen)
|
||||||
|
|
||||||
// IV[i] = floor(2**w * frac(sqrt(prime(i+1)))), where prime(i)
|
// IV[i] = floor(2**w * frac(sqrt(prime(i+1)))), where prime(i)
|
||||||
// is the i:th prime number ( 2, 3, 5, 7, 11, 13, 17, 19 )
|
// is the i:th prime number ( 2, 3, 5, 7, 11, 13, 17, 19 )
|
||||||
// and sqrt(x) is the square root of x.
|
// and sqrt(x) is the square root of x.
|
||||||
// https://www.ietf.org/rfc/rfc7693#section-2.6
|
// https://www.ietf.org/rfc/rfc7693#section-2.6
|
||||||
let iv: array = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179]
|
let iv: Array = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179]
|
||||||
|
|
||||||
// https://crypto.stackexchange.com/questions/108987/rationale-for-blake2-message-schedule
|
// https://crypto.stackexchange.com/questions/108987/rationale-for-blake2-message-schedule
|
||||||
let sigma: array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3,11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4,7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8,9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13,2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9,12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11,13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10,6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5,10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3]
|
let sigma: Array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3,11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4,7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8,9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13,2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9,12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11,13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10,6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5,10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3]
|
||||||
|
|
||||||
let h: ptr = mem.alloc(8 * 8)
|
let h: ptr = mem.alloc(8 * 8)
|
||||||
let t0 = 0
|
let t0 = 0
|
||||||
|
|||||||
306
src/std/std.zr
306
src/std/std.zr
@@ -1,19 +1,128 @@
|
|||||||
extern malloc
|
func panic[msg: str] : void
|
||||||
extern realloc
|
|
||||||
extern free
|
|
||||||
extern gethostbyname
|
|
||||||
|
|
||||||
func dbg.panic[msg: str] : void
|
|
||||||
io.print("PANIC: ")
|
io.print("PANIC: ")
|
||||||
io.println(msg)
|
io.println(msg)
|
||||||
(0 / 0) // crashes program which is kinda better since you get a gdb backtrace
|
(0 / 0) // crashes program which is kinda better since you get a gdb backtrace
|
||||||
os.exit(1)
|
os.exit(1)
|
||||||
|
|
||||||
func mem.alloc[x: i64] : ptr
|
const MEM_BLOCK_SIZE = 24
|
||||||
return malloc(x)
|
|
||||||
|
struct mem.Block
|
||||||
|
size: i64
|
||||||
|
free: bool
|
||||||
|
next: mem.Block
|
||||||
|
|
||||||
|
func mem._align[x: i64] : i64
|
||||||
|
return (x + 7) & -8
|
||||||
|
|
||||||
|
func mem._split_block[blk: mem.Block, needed: i64] : void
|
||||||
|
if blk->size >= needed + MEM_BLOCK_SIZE + 8
|
||||||
|
let new_blk: mem.Block = blk + MEM_BLOCK_SIZE + needed
|
||||||
|
new_blk->size = blk->size - needed - MEM_BLOCK_SIZE
|
||||||
|
new_blk->free = true
|
||||||
|
new_blk->next = blk->next
|
||||||
|
|
||||||
|
blk->size = needed
|
||||||
|
blk->next = new_blk
|
||||||
|
|
||||||
|
if mem.read64(_builtin_heap_tail()) == blk
|
||||||
|
mem.write64(_builtin_heap_tail(), new_blk)
|
||||||
|
|
||||||
|
func mem._request_space[size: i64] : mem.Block
|
||||||
|
let blk: mem.Block = _builtin_syscall(SYS_brk, 0)
|
||||||
|
let new_brk: ptr = blk + MEM_BLOCK_SIZE + size
|
||||||
|
let result: ptr = _builtin_syscall(SYS_brk, new_brk)
|
||||||
|
if result != new_brk
|
||||||
|
return 0
|
||||||
|
|
||||||
|
blk->size = size
|
||||||
|
blk->free = false
|
||||||
|
blk->next = 0
|
||||||
|
|
||||||
|
let tail: mem.Block = mem.read64(_builtin_heap_tail())
|
||||||
|
if tail
|
||||||
|
tail->next = blk
|
||||||
|
|
||||||
|
mem.write64(_builtin_heap_tail(), blk)
|
||||||
|
return blk
|
||||||
|
|
||||||
|
func mem._coalesce[] : void
|
||||||
|
let cur: mem.Block = mem.read64(_builtin_heap_head())
|
||||||
|
while cur && cur->next
|
||||||
|
let next: mem.Block = cur->next
|
||||||
|
if cur->free && next->free
|
||||||
|
cur->size = cur->size + MEM_BLOCK_SIZE + next->size
|
||||||
|
cur->next = next->next
|
||||||
|
if !cur->next
|
||||||
|
mem.write64(_builtin_heap_tail(), cur)
|
||||||
|
else
|
||||||
|
cur = cur->next
|
||||||
|
|
||||||
|
func mem.alloc[size: i64] : ptr
|
||||||
|
if size == 0
|
||||||
|
return 0
|
||||||
|
|
||||||
|
size = mem._align(size)
|
||||||
|
|
||||||
|
let cur: mem.Block = mem.read64(_builtin_heap_head())
|
||||||
|
while cur
|
||||||
|
if cur->free && cur->size >= size
|
||||||
|
mem._split_block(cur, size)
|
||||||
|
cur->free = false
|
||||||
|
return cur + MEM_BLOCK_SIZE
|
||||||
|
cur = cur->next
|
||||||
|
|
||||||
|
let blk: mem.Block = mem._request_space(size)
|
||||||
|
if !blk
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if !mem.read64(_builtin_heap_head())
|
||||||
|
mem.write64(_builtin_heap_head(), blk)
|
||||||
|
|
||||||
|
return blk + MEM_BLOCK_SIZE
|
||||||
|
|
||||||
func mem.free[x: ptr] : void
|
func mem.free[x: ptr] : void
|
||||||
free(x)
|
if !x
|
||||||
|
return 0
|
||||||
|
|
||||||
|
let blk: mem.Block = x - MEM_BLOCK_SIZE
|
||||||
|
blk->free = true
|
||||||
|
mem._coalesce()
|
||||||
|
|
||||||
|
func mem.realloc[x: ptr, new_size: i64] : ptr
|
||||||
|
if !x
|
||||||
|
return mem.alloc(new_size)
|
||||||
|
|
||||||
|
if new_size == 0
|
||||||
|
mem.free(x)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
new_size = mem._align(new_size)
|
||||||
|
|
||||||
|
let blk: mem.Block = x - MEM_BLOCK_SIZE
|
||||||
|
if blk->size >= new_size
|
||||||
|
mem._split_block(blk, new_size)
|
||||||
|
return x
|
||||||
|
|
||||||
|
let next: mem.Block = blk->next
|
||||||
|
if next && next->free
|
||||||
|
let combined: i64 = blk->size + MEM_BLOCK_SIZE + next->size
|
||||||
|
if combined >= new_size
|
||||||
|
blk->size = combined
|
||||||
|
blk->next = next->next
|
||||||
|
if !blk->next
|
||||||
|
mem.write64(_builtin_heap_tail(), blk)
|
||||||
|
mem._split_block(blk, new_size)
|
||||||
|
return x
|
||||||
|
|
||||||
|
let new_ptr: ptr = mem.alloc(new_size)
|
||||||
|
if !new_ptr
|
||||||
|
return 0
|
||||||
|
|
||||||
|
for i in 0..blk->size
|
||||||
|
new_ptr[i] = x[i]
|
||||||
|
|
||||||
|
mem.free(x)
|
||||||
|
return new_ptr
|
||||||
|
|
||||||
func mem.zero_and_free[x: ptr, size: i64] : void
|
func mem.zero_and_free[x: ptr, size: i64] : void
|
||||||
mem.zero(x, size)
|
mem.zero(x, size)
|
||||||
@@ -90,7 +199,7 @@ func io.read_line[]: str
|
|||||||
func io.read_file[path: str] : str
|
func io.read_file[path: str] : str
|
||||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||||
if fd <= 0
|
if fd <= 0
|
||||||
dbg.panic("failed to open file")
|
panic("failed to open file")
|
||||||
|
|
||||||
let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
||||||
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
||||||
@@ -101,24 +210,28 @@ func io.read_file[path: str] : str
|
|||||||
buffer[n] = 0
|
buffer[n] = 0
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
func io.read_binary_file[path: str, len_ptr: ptr] : ptr
|
struct io.Buffer
|
||||||
|
data: ptr
|
||||||
|
size: i64
|
||||||
|
|
||||||
|
func io.read_binary_file[path: str] : io.Buffer
|
||||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||||
if fd < 0
|
if fd < 0
|
||||||
dbg.panic("failed to open file")
|
panic("failed to open file")
|
||||||
|
|
||||||
let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
let buffer: io.Buffer = new io.Buffer
|
||||||
|
buffer->size = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
||||||
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
||||||
|
|
||||||
let buffer: ptr = mem.alloc(size)
|
buffer->data = mem.alloc(buffer->size)
|
||||||
_builtin_syscall(SYS_read, fd, buffer, size)
|
_builtin_syscall(SYS_read, fd, buffer->data, buffer->size)
|
||||||
_builtin_syscall(SYS_close, fd)
|
_builtin_syscall(SYS_close, fd)
|
||||||
mem.write64(len_ptr, size)
|
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
func io.write_file[path: str, content: str] : void
|
func io.write_file[path: str, content: str] : void
|
||||||
let fd: ptr = _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644)
|
let fd: ptr = _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644)
|
||||||
if fd < 0
|
if fd < 0
|
||||||
dbg.panic("failed to open file")
|
panic("failed to open file")
|
||||||
|
|
||||||
_builtin_syscall(SYS_write, fd, content, str.len(content))
|
_builtin_syscall(SYS_write, fd, content, str.len(content))
|
||||||
_builtin_syscall(SYS_close, fd)
|
_builtin_syscall(SYS_close, fd)
|
||||||
@@ -195,7 +308,7 @@ func str.find[haystack: str, needle: str] : i64
|
|||||||
|
|
||||||
func str.substr[s: str, start: i64, length: i64] : str
|
func str.substr[s: str, start: i64, length: i64] : str
|
||||||
if start < 0 || length < 0 || start + length > str.len(s)
|
if start < 0 || length < 0 || start + length > str.len(s)
|
||||||
dbg.panic("str.substr out of bounds")
|
panic("str.substr out of bounds")
|
||||||
|
|
||||||
let out: str = mem.alloc(length + 1)
|
let out: str = mem.alloc(length + 1)
|
||||||
for i in 0..length
|
for i in 0..length
|
||||||
@@ -219,10 +332,10 @@ func str.trim[s: str] : str
|
|||||||
|
|
||||||
return str.substr(s, start, end - start + 1)
|
return str.substr(s, start, end - start + 1)
|
||||||
|
|
||||||
func str.split[haystack: str, needle: str]: array
|
func str.split[haystack: str, needle: str]: Array
|
||||||
let haystack_len: i64 = str.len(haystack)
|
let haystack_len: i64 = str.len(haystack)
|
||||||
let needle_len: i64 = str.len(needle)
|
let needle_len: i64 = str.len(needle)
|
||||||
let result: array = []
|
let result: Array = []
|
||||||
|
|
||||||
if !needle_len
|
if !needle_len
|
||||||
if !haystack_len
|
if !haystack_len
|
||||||
@@ -410,7 +523,7 @@ func math.lcm[a: i64, b: i64] : i64
|
|||||||
|
|
||||||
func math.isqrt[n: i64] : i64
|
func math.isqrt[n: i64] : i64
|
||||||
if n < 0
|
if n < 0
|
||||||
dbg.panic("negative number passed to math.isqrt")
|
panic("negative number passed to math.isqrt")
|
||||||
if n == 0 || n == 1
|
if n == 0 || n == 1
|
||||||
return n
|
return n
|
||||||
|
|
||||||
@@ -438,85 +551,74 @@ func math.is_prime[n: i64]: bool
|
|||||||
i = i + 6
|
i = i + 6
|
||||||
return true
|
return true
|
||||||
|
|
||||||
func array.new[] : array
|
struct Array
|
||||||
// [ 8 bytes - ptr to data ] [ 8 bytes - size ] [ 8 bytes - capacity ]
|
data: ptr
|
||||||
let arr: ptr = mem.alloc(24)
|
size: i64
|
||||||
mem.zero(arr, 24)
|
capacity: i64
|
||||||
return arr
|
|
||||||
|
|
||||||
func array.nth[xs: array, n: i64] : i64
|
func array.new[] : Array
|
||||||
if n < 0 || n >= array.size(xs)
|
return new Array
|
||||||
dbg.panic("array.nth out of bounds")
|
|
||||||
let data: ptr = mem.read64(xs)
|
|
||||||
return mem.read64(data + n * 8)
|
|
||||||
|
|
||||||
func array.set[xs: array, n: i64, x: i64] : void
|
func array.nth[xs: Array, n: i64] : i64
|
||||||
if n < 0 || n >= array.size(xs)
|
if n < 0 || n >= xs->size
|
||||||
dbg.panic("array.set out of bounds")
|
panic("array.nth out of bounds")
|
||||||
let data: ptr = mem.read64(xs)
|
return mem.read64(xs->data + n * 8)
|
||||||
mem.write64(data + n * 8, x)
|
|
||||||
|
|
||||||
func array.push[xs: array, x: i64] : void
|
func array.set[xs: Array, n: i64, x: i64] : void
|
||||||
let data: ptr = mem.read64(xs)
|
if n < 0 || n >= xs->size
|
||||||
let capacity: i64 = mem.read64(xs + 8)
|
panic("array.set out of bounds")
|
||||||
let size: i64 = mem.read64(xs + 16)
|
mem.write64(xs->data + n * 8, x)
|
||||||
|
|
||||||
if size == capacity
|
func array.push[xs: Array, x: i64] : void
|
||||||
|
if xs->size == xs->capacity
|
||||||
let new_capacity = 4
|
let new_capacity = 4
|
||||||
if capacity != 0
|
if xs->capacity != 0
|
||||||
new_capacity = capacity * 2
|
new_capacity = xs->capacity * 2
|
||||||
let new_data: ptr = realloc(data, new_capacity * 8)
|
xs->data = mem.realloc(xs->data, new_capacity * 8)
|
||||||
mem.write64(xs, new_data)
|
xs->capacity = new_capacity
|
||||||
mem.write64(xs + 8, new_capacity)
|
|
||||||
data = new_data
|
|
||||||
|
|
||||||
mem.write64(data + size * 8, x)
|
mem.write64(xs->data + xs->size * 8, x)
|
||||||
mem.write64(xs + 16, size + 1)
|
xs->size = xs->size + 1
|
||||||
|
|
||||||
func array.size[xs: array] : i64
|
func array.free[xs: Array] : void
|
||||||
return mem.read64(xs + 16)
|
if xs->data != 0
|
||||||
|
mem.free(xs->data)
|
||||||
func array.free[xs: array] : void
|
|
||||||
let data: ptr = mem.read64(xs)
|
|
||||||
if data != 0
|
|
||||||
mem.free(data)
|
|
||||||
mem.free(xs)
|
mem.free(xs)
|
||||||
|
|
||||||
func array.pop[xs: array] : i64
|
func array.pop[xs: Array] : i64
|
||||||
let size: i64 = array.size(xs)
|
if xs->size == 0
|
||||||
if size == 0
|
panic("array.pop on empty array")
|
||||||
dbg.panic("array.pop on empty array")
|
let x: i64 = array.nth(xs, xs->size - 1)
|
||||||
let x: i64 = array.nth(xs, size - 1)
|
xs->size = xs->size - 1
|
||||||
mem.write64(xs + 16, size - 1)
|
|
||||||
return x
|
return x
|
||||||
|
|
||||||
func array.slice[xs: array, start: i64, length: i64] : array
|
func array.slice[xs: Array, start: i64, length: i64] : Array
|
||||||
if start < 0 || length < 0 || start + length > array.size(xs)
|
if start < 0 || length < 0 || start + length > xs->size
|
||||||
dbg.panic("array.slice out of bounds")
|
panic("array.slice out of bounds")
|
||||||
|
|
||||||
let new_array: array = []
|
let new_array: Array = []
|
||||||
for i in 0..length
|
for i in 0..length
|
||||||
array.push(new_array, array.nth(xs, start + i))
|
array.push(new_array, array.nth(xs, start + i))
|
||||||
return new_array
|
return new_array
|
||||||
|
|
||||||
func array.concat[a: array, b: array] : array
|
func array.concat[a: Array, b: Array] : Array
|
||||||
let new_array: array = []
|
let new_array: Array = []
|
||||||
for i in 0..array.size(a)
|
for i in 0..a->size
|
||||||
array.push(new_array, array.nth(a, i))
|
array.push(new_array, array.nth(a, i))
|
||||||
for i in 0..array.size(b)
|
for i in 0..b->size
|
||||||
array.push(new_array, array.nth(b, i))
|
array.push(new_array, array.nth(b, i))
|
||||||
return new_array
|
return new_array
|
||||||
|
|
||||||
func alg.quicksort[arr: array] : void
|
func alg.quicksort[arr: Array] : void
|
||||||
alg._do_quicksort(arr, 0, array.size(arr) - 1)
|
alg._do_quicksort(arr, 0, arr->size - 1)
|
||||||
|
|
||||||
func alg._do_quicksort[arr: array, low: i64, high: i64] : void
|
func alg._do_quicksort[arr: Array, low: i64, high: i64] : void
|
||||||
if low < high
|
if low < high
|
||||||
let i: i64 = alg._partition(arr, low, high)
|
let i: i64 = alg._partition(arr, low, high)
|
||||||
alg._do_quicksort(arr, low, i - 1)
|
alg._do_quicksort(arr, low, i - 1)
|
||||||
alg._do_quicksort(arr, i + 1, high)
|
alg._do_quicksort(arr, i + 1, high)
|
||||||
|
|
||||||
func alg._partition[arr: array, low: i64, high: i64] : i64
|
func alg._partition[arr: Array, low: i64, high: i64] : i64
|
||||||
let pivot: i64 = array.nth(arr, high)
|
let pivot: i64 = array.nth(arr, high)
|
||||||
let i: i64 = low - 1
|
let i: i64 = low - 1
|
||||||
for j in (low)..high
|
for j in (low)..high
|
||||||
@@ -530,29 +632,28 @@ func alg._partition[arr: array, low: i64, high: i64] : i64
|
|||||||
array.set(arr, high, temp)
|
array.set(arr, high, temp)
|
||||||
return i + 1
|
return i + 1
|
||||||
|
|
||||||
func alg.count[arr: array, item: i64] : i64
|
func alg.count[arr: Array, item: i64] : i64
|
||||||
let count = 0
|
let count = 0
|
||||||
let size: i64 = array.size(arr)
|
for i in 0..arr->size
|
||||||
for i in 0..size
|
|
||||||
if array.nth(arr, i) == item
|
if array.nth(arr, i) == item
|
||||||
count = count + 1
|
count = count + 1
|
||||||
return count
|
return count
|
||||||
|
|
||||||
func alg.map[arr: array, fn: ptr] : array
|
func alg.map[arr: Array, fn: ptr] : Array
|
||||||
let out: array = []
|
let out: Array = []
|
||||||
for i in 0..array.size(arr)
|
for i in 0..arr->size
|
||||||
array.push(out, fn(array.nth(arr, i)))
|
array.push(out, fn(array.nth(arr, i)))
|
||||||
return out
|
return out
|
||||||
|
|
||||||
func alg.filter[arr: array, fn: ptr] : array
|
func alg.filter[arr: Array, fn: ptr] : Array
|
||||||
let out: array = []
|
let out: Array = []
|
||||||
for i in 0..array.size(arr)
|
for i in 0..arr->size
|
||||||
if fn(array.nth(arr, i))
|
if fn(array.nth(arr, i))
|
||||||
array.push(out, array.nth(arr, i))
|
array.push(out, array.nth(arr, i))
|
||||||
return out
|
return out
|
||||||
|
|
||||||
func alg.reduce[arr: array, fn: ptr, acc: i64] : i64
|
func alg.reduce[arr: Array, fn: ptr, acc: i64] : i64
|
||||||
for i in 0..array.size(arr)
|
for i in 0..arr->size
|
||||||
acc = fn(acc, array.nth(arr, i))
|
acc = fn(acc, array.nth(arr, i))
|
||||||
return acc
|
return acc
|
||||||
|
|
||||||
@@ -572,21 +673,23 @@ func os.urandom_i64[]: i64
|
|||||||
mem.free(buffer)
|
mem.free(buffer)
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
struct os.Timeval
|
||||||
|
tv_sec: i64
|
||||||
|
tv_usec: i64
|
||||||
|
|
||||||
func os.time[] : i64
|
func os.time[] : i64
|
||||||
let tv: ptr = mem.alloc(16)
|
let tv: os.Timeval = new os.Timeval
|
||||||
_builtin_syscall(SYS_gettimeofday, tv, 0)
|
_builtin_syscall(SYS_gettimeofday, tv, 0)
|
||||||
let seconds: i64 = mem.read64(tv)
|
|
||||||
let microseconds: i64 = mem.read64(tv + 8)
|
|
||||||
mem.free(tv)
|
mem.free(tv)
|
||||||
return seconds * 1000 + microseconds / 1000
|
return tv->tv_sec * 1000 + tv->tv_usec / 1000
|
||||||
|
|
||||||
// voodoo magic
|
// voodoo magic
|
||||||
func os.shell[command: str] : i64
|
func os.shell[command: str] : i64
|
||||||
let pid: i64 = _builtin_syscall(SYS_fork)
|
let pid: i64 = _builtin_syscall(SYS_fork)
|
||||||
if pid == 0
|
if pid == 0
|
||||||
// leaky but not sure where can i free it
|
// leaky but not sure where can i free it
|
||||||
let argv: array = ["sh", "-c", command, 0]
|
let argv: Array = ["sh", "-c", command, 0]
|
||||||
_builtin_syscall(SYS_execve, "/bin/sh", mem.read64(argv), _builtin_environ())
|
_builtin_syscall(SYS_execve, "/bin/sh", argv->data, _builtin_environ())
|
||||||
_builtin_syscall(SYS_exit, 1)
|
_builtin_syscall(SYS_exit, 1)
|
||||||
else
|
else
|
||||||
let status = 0
|
let status = 0
|
||||||
@@ -600,12 +703,12 @@ func os.shell[command: str] : i64
|
|||||||
else
|
else
|
||||||
return -(st & 0x7f)
|
return -(st & 0x7f)
|
||||||
|
|
||||||
func os.listdir[path: str] : array
|
func os.listdir[path: str] : Array
|
||||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||||
if fd < 0
|
if fd < 0
|
||||||
return []
|
return []
|
||||||
|
|
||||||
let files: array = []
|
let files: Array = []
|
||||||
let buf: ptr = mem.alloc(1024)
|
let buf: ptr = mem.alloc(1024)
|
||||||
while true
|
while true
|
||||||
let n: i64 = _builtin_syscall(SYS_getdents64, fd, buf, 1024)
|
let n: i64 = _builtin_syscall(SYS_getdents64, fd, buf, 1024)
|
||||||
@@ -667,13 +770,7 @@ func net.listen[packed_host: i64, port: i64] : i64
|
|||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
func net.connect[host: str, port: i64] : i64
|
func net.connect[packed_host: i64, port: i64] : i64
|
||||||
let he: ptr = gethostbyname(host)
|
|
||||||
if he == 0
|
|
||||||
return -1
|
|
||||||
|
|
||||||
let ip_ptr: ptr = mem.read64(mem.read64(he + 24))
|
|
||||||
|
|
||||||
let s: i64 = _builtin_syscall(SYS_socket, 2, 1, 0)
|
let s: i64 = _builtin_syscall(SYS_socket, 2, 1, 0)
|
||||||
if s < 0
|
if s < 0
|
||||||
return -1
|
return -1
|
||||||
@@ -681,12 +778,13 @@ func net.connect[host: str, port: i64] : i64
|
|||||||
let sa: ptr = mem.alloc(16)
|
let sa: ptr = mem.alloc(16)
|
||||||
mem.zero(sa, 16)
|
mem.zero(sa, 16)
|
||||||
sa[0] = 2
|
sa[0] = 2
|
||||||
|
sa[1] = 0
|
||||||
sa[2] = (port >> 8) & 255
|
sa[2] = (port >> 8) & 255
|
||||||
sa[3] = port & 255
|
sa[3] = port & 255
|
||||||
sa[4] = ip_ptr[0]
|
sa[4] = (packed_host >> 24) & 255
|
||||||
sa[5] = ip_ptr[1]
|
sa[5] = (packed_host >> 16) & 255
|
||||||
sa[6] = ip_ptr[2]
|
sa[6] = (packed_host >> 8) & 255
|
||||||
sa[7] = ip_ptr[3]
|
sa[7] = packed_host & 255
|
||||||
|
|
||||||
if _builtin_syscall(SYS_connect, s, sa, 16) < 0
|
if _builtin_syscall(SYS_connect, s, sa, 16) < 0
|
||||||
mem.free(sa)
|
mem.free(sa)
|
||||||
|
|||||||
32
test.zr
32
test.zr
@@ -1,17 +1,17 @@
|
|||||||
func run_test[x: str] : void
|
func run_test[x: str] : void
|
||||||
let build_blacklist: array = ["examples/puzzles", "examples/raylib.zr", "examples/x11.zr", "examples/sqlite_todo.zr"]
|
let build_blacklist: Array = ["examples/puzzles", "examples/raylib.zr", "examples/chip8.zr", "examples/sqlite_todo.zr"]
|
||||||
let run_blacklist: array = ["/aoc", "guess_number.zr", "tcp_server.zr"]
|
let run_blacklist: Array = ["/aoc", "guess_number.zr", "tcp_server.zr"]
|
||||||
|
|
||||||
for i in 0..array.size(build_blacklist)
|
for i in 0..build_blacklist->size
|
||||||
if str.equal(x, array.nth(build_blacklist, i))
|
if str.equal(x, array.nth(build_blacklist, i))
|
||||||
io.print("\033[93mSkipping ")
|
io.print("Skipping ")
|
||||||
io.print(x)
|
io.print(x)
|
||||||
io.println("...\033[0m")
|
io.println("...")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
io.print("\033[94mBuilding ")
|
io.print("Building ")
|
||||||
io.print(x)
|
io.print(x)
|
||||||
io.print("...\033[0m ")
|
io.print("... ")
|
||||||
|
|
||||||
let build_start_time: i64 = os.time()
|
let build_start_time: i64 = os.time()
|
||||||
if os.shell(str.concat("./target/release/zern ", x)) != 0
|
if os.shell(str.concat("./target/release/zern ", x)) != 0
|
||||||
@@ -21,16 +21,16 @@ func run_test[x: str] : void
|
|||||||
io.print_i64(build_end_time - build_start_time)
|
io.print_i64(build_end_time - build_start_time)
|
||||||
io.println("ms")
|
io.println("ms")
|
||||||
|
|
||||||
for i in 0..array.size(run_blacklist)
|
for i in 0..run_blacklist->size
|
||||||
if str.find(x, array.nth(run_blacklist, i)) != -1
|
if str.find(x, array.nth(run_blacklist, i)) != -1
|
||||||
io.print("\033[93mSkipping ")
|
io.print("Skipping ")
|
||||||
io.print(x)
|
io.print(x)
|
||||||
io.println("...\033[0m")
|
io.println("...")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
io.print("\033[95mRunning ")
|
io.print("Running ")
|
||||||
io.print(x)
|
io.print(x)
|
||||||
io.println("...\033[0m")
|
io.println("...")
|
||||||
|
|
||||||
let run_cmd: str = "./out"
|
let run_cmd: str = "./out"
|
||||||
if str.equal(x, "examples/curl.zr")
|
if str.equal(x, "examples/curl.zr")
|
||||||
@@ -43,15 +43,15 @@ func run_test[x: str] : void
|
|||||||
os.exit(1)
|
os.exit(1)
|
||||||
let run_end_time: i64 = os.time()
|
let run_end_time: i64 = os.time()
|
||||||
|
|
||||||
io.print("\033[92mRunning ")
|
io.print("Running ")
|
||||||
io.print(x)
|
io.print(x)
|
||||||
io.print(" took\033[0m ")
|
io.print(" took ")
|
||||||
io.print_i64(run_end_time - run_start_time)
|
io.print_i64(run_end_time - run_start_time)
|
||||||
io.println("ms")
|
io.println("ms")
|
||||||
|
|
||||||
func run_directory[dir: str] : void
|
func run_directory[dir: str] : void
|
||||||
let files: array = os.listdir(dir)
|
let files: Array = os.listdir(dir)
|
||||||
for i in 0..array.size(files)
|
for i in 0..files->size
|
||||||
run_test(str.concat(dir, array.nth(files, i)))
|
run_test(str.concat(dir, array.nth(files, i)))
|
||||||
|
|
||||||
array.free(files)
|
array.free(files)
|
||||||
|
|||||||
Reference in New Issue
Block a user