Compare commits
2 Commits
174dbfa740
...
bbd4caa05c
| Author | SHA1 | Date | |
|---|---|---|---|
| bbd4caa05c | |||
| 4fda79f0bc |
@@ -13,11 +13,11 @@ A very cool language
|
||||
## Syntax
|
||||
```rust
|
||||
func main[] : i64
|
||||
let answer = math.abs(os.urandom_i64()) % 100
|
||||
answer := math.abs(os.urandom_i64()) % 100
|
||||
|
||||
while true
|
||||
io.println("Guess a number: ")
|
||||
let guess = io.read_line() |> str.trim() |> str.parse_i64()
|
||||
guess := io.read_line() |> str.trim() |> str.parse_i64()
|
||||
|
||||
if guess == answer
|
||||
io.println("You win!")
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
func main[] : i64
|
||||
// https://brainfuck.org/sierpinski.b
|
||||
let src = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
||||
let src_len = str.len(src)
|
||||
let i = 0
|
||||
src := "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
||||
src_len := str.len(src)
|
||||
i := 0
|
||||
|
||||
let memory = mem.alloc(30000)
|
||||
memory := mem.alloc(30000)
|
||||
mem.zero(memory, 30000)
|
||||
let p = 0
|
||||
p := 0
|
||||
|
||||
while i < src_len
|
||||
let op = src[i]
|
||||
op := src[i]
|
||||
|
||||
if op == '>'
|
||||
p = p + 1
|
||||
@@ -26,7 +26,7 @@ func main[] : i64
|
||||
else if op == '['
|
||||
if !memory[p]
|
||||
i = i + 1
|
||||
let opened = 0
|
||||
opened := 0
|
||||
while i < src_len && !(src[i] == ']' && !opened)
|
||||
if src[i] == '['
|
||||
opened = opened + 1
|
||||
@@ -36,7 +36,7 @@ func main[] : i64
|
||||
else if op == ']'
|
||||
if memory[p]
|
||||
i = i - 1
|
||||
let closed = 0
|
||||
closed := 0
|
||||
while i >= 0 && !(src[i] == '[' && !closed)
|
||||
if src[i] == ']'
|
||||
closed = closed + 1
|
||||
|
||||
@@ -22,7 +22,7 @@ struct CHIP8
|
||||
keyboard_map: Array
|
||||
|
||||
func chip8_create[] : CHIP8
|
||||
let c = new* CHIP8
|
||||
c := new* CHIP8
|
||||
c->memory = mem.alloc(4096)
|
||||
mem.zero(c->memory, 4096)
|
||||
c->display = mem.alloc(64*32)
|
||||
@@ -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->keyboard = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
|
||||
let fonts = [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]
|
||||
fonts := [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
|
||||
c->memory[i] = array.nth(fonts, i)
|
||||
mem.free(fonts)
|
||||
@@ -58,18 +58,18 @@ func chip8_disassemble[c: CHIP8, ins_count: i64] : void
|
||||
for i in 0..ins_count
|
||||
io.printf("0x%x: ", c->pc)
|
||||
|
||||
let high = c->memory[c->pc] as i64
|
||||
let low = c->memory[c->pc + 1] as i64
|
||||
high := c->memory[c->pc] as i64
|
||||
low := c->memory[c->pc + 1] as i64
|
||||
c->pc = c->pc + 2
|
||||
|
||||
let ins = (high << 8) | low
|
||||
let nnn = ins & 0x0fff
|
||||
let kk = ins & 0x00ff
|
||||
let n = ins & 0x000f
|
||||
let x = (ins >> 8) & 0x0f
|
||||
let y = (ins >> 4) & 0x0f
|
||||
ins := (high << 8) | low
|
||||
nnn := ins & 0x0fff
|
||||
kk := ins & 0x00ff
|
||||
n := ins & 0x000f
|
||||
x := (ins >> 8) & 0x0f
|
||||
y := (ins >> 4) & 0x0f
|
||||
|
||||
let op = (ins >> 12) & 0x0f
|
||||
op := (ins >> 12) & 0x0f
|
||||
if op == 0x0
|
||||
if nnn == 0x0e0
|
||||
io.println("CLS")
|
||||
@@ -154,18 +154,18 @@ func chip8_disassemble[c: CHIP8, ins_count: i64] : void
|
||||
io.printf("??? (%x)\n", ins)
|
||||
|
||||
func chip8_step[c: CHIP8] : void
|
||||
let high = c->memory[c->pc] as i64
|
||||
let low = c->memory[c->pc + 1] as i64
|
||||
high := c->memory[c->pc] as i64
|
||||
low := c->memory[c->pc + 1] as i64
|
||||
c->pc = c->pc + 2
|
||||
|
||||
let ins = (high << 8) | low
|
||||
let nnn = ins & 0x0fff
|
||||
let kk = ins & 0x00ff
|
||||
let n = ins & 0x000f
|
||||
let x = (ins >> 8) & 0x0f
|
||||
let y = (ins >> 4) & 0x0f
|
||||
ins := (high << 8) | low
|
||||
nnn := ins & 0x0fff
|
||||
kk := ins & 0x00ff
|
||||
n := ins & 0x000f
|
||||
x := (ins >> 8) & 0x0f
|
||||
y := (ins >> 4) & 0x0f
|
||||
|
||||
let op = (ins >> 12) & 0x0f
|
||||
op := (ins >> 12) & 0x0f
|
||||
if op == 0x0
|
||||
if nnn == 0x0e0
|
||||
mem.zero(c->display, 64 * 32)
|
||||
@@ -201,7 +201,7 @@ func chip8_step[c: CHIP8] : void
|
||||
else if n == 0x3
|
||||
c->reg[x] = c->reg[x] ^ c->reg[y]
|
||||
else if n == 0x4
|
||||
let res: u8 = c->reg[x] + c->reg[y]
|
||||
res : u8 = c->reg[x] + c->reg[y]
|
||||
c->reg[0xf] = (res > 0xff) as u8
|
||||
c->reg[x] = res
|
||||
else if n == 0x5
|
||||
@@ -230,9 +230,9 @@ func chip8_step[c: CHIP8] : void
|
||||
for row in 0..n
|
||||
for col in 0..8
|
||||
if (c->memory[c->I + row] & (0x80 >> col)) != 0
|
||||
let pixel_x: u8 = (c->reg[x] + col) % 64
|
||||
let pixel_y: u8 = (c->reg[y] + row) % 32
|
||||
let offset = pixel_x as i64 + (pixel_y * 64)
|
||||
pixel_x : u8 = (c->reg[x] + col) % 64
|
||||
pixel_y : u8 = (c->reg[y] + row) % 32
|
||||
offset := pixel_x as i64 + (pixel_y * 64)
|
||||
|
||||
if c->display[offset] == 1
|
||||
c->reg[0xf] = 1
|
||||
@@ -248,7 +248,7 @@ func chip8_step[c: CHIP8] : void
|
||||
if kk == 0x07
|
||||
c->reg[x] = c->delay_timer
|
||||
else if kk == 0x0A
|
||||
let key_pressed = false
|
||||
key_pressed := false
|
||||
while !key_pressed && !WindowShouldClose()
|
||||
for i in 0..16
|
||||
if IsKeyDown(array.nth(c->keyboard_map, i))
|
||||
@@ -274,11 +274,11 @@ func chip8_step[c: CHIP8] : void
|
||||
c->reg[i] = c->memory[c->I + i]
|
||||
|
||||
func main[argc: i64, argv: ptr] : i64
|
||||
let path = 0 as str
|
||||
let disassemble = false
|
||||
path := 0 as str
|
||||
disassemble := false
|
||||
|
||||
for i in 1..argc
|
||||
let arg = mem.read64(argv + i * 8) as str
|
||||
arg := mem.read64(argv + i * 8) as str
|
||||
if str.equal(arg, "-d")
|
||||
disassemble = true
|
||||
else
|
||||
@@ -288,9 +288,9 @@ func main[argc: i64, argv: ptr] : i64
|
||||
io.println("Usage: chip8 -d <path>")
|
||||
return 1
|
||||
|
||||
let c = chip8_create()
|
||||
c := chip8_create()
|
||||
|
||||
let buffer: io.Buffer = must(io.read_binary_file?(path))
|
||||
buffer : io.Buffer = must(io.read_binary_file?(path))
|
||||
|
||||
for i in 0..buffer->size
|
||||
c->memory[0x200 + i] = buffer->data[i]
|
||||
|
||||
@@ -3,7 +3,7 @@ func main[argc: i64, argv: ptr] : i64
|
||||
io.println("ERROR: url is missing")
|
||||
return 1
|
||||
|
||||
let url = mem.read64(argv + 8) as str
|
||||
url := mem.read64(argv + 8) as str
|
||||
|
||||
if str.len(url) <= 7
|
||||
io.println("ERROR: invalid url (Did you forget \"http://\"?)")
|
||||
@@ -13,25 +13,25 @@ func main[argc: i64, argv: ptr] : i64
|
||||
io.println("ERROR: only http scheme is supported")
|
||||
return 1
|
||||
|
||||
let url_len = str.len(url)
|
||||
let host_start = 7
|
||||
let i = host_start
|
||||
url_len := str.len(url)
|
||||
host_start := 7
|
||||
i := host_start
|
||||
while i < url_len
|
||||
if url[i] == '/'
|
||||
break
|
||||
i = i + 1
|
||||
|
||||
let host = str.substr(url, host_start, i - host_start)
|
||||
let path = "/"
|
||||
host := str.substr(url, host_start, i - host_start)
|
||||
path := "/"
|
||||
if i < url_len
|
||||
path = str.substr(url, i, url_len - i)
|
||||
|
||||
let addr: i64 = must(net.resolve?(host))
|
||||
addr : i64 = must(net.resolve?(host))
|
||||
|
||||
let s: i64 = must(net.connect?(addr, 80))
|
||||
s : i64 = must(net.connect?(addr, 80))
|
||||
|
||||
// TODO: add string builder to std
|
||||
let req = "GET "
|
||||
req := "GET "
|
||||
req = str.concat(req, path)
|
||||
req = str.concat(req, " HTTP/1.0\r\nHost: ")
|
||||
req = str.concat(req, host)
|
||||
@@ -39,16 +39,16 @@ func main[argc: i64, argv: ptr] : i64
|
||||
net.send(s, req as ptr, str.len(req))
|
||||
mem.free(req)
|
||||
|
||||
let header_buf = mem.alloc(8192) as str
|
||||
let header_size = 0
|
||||
let found = false
|
||||
let end_index = -1
|
||||
header_buf := mem.alloc(8192) as str
|
||||
header_size := 0
|
||||
found := false
|
||||
end_index := -1
|
||||
|
||||
while !found && header_size < 8192
|
||||
let n = net.read(s, header_buf as ptr + header_size, 8192 - header_size)
|
||||
n := net.read(s, header_buf as ptr + header_size, 8192 - header_size)
|
||||
if n <= 0
|
||||
break
|
||||
let current_size = header_size + n
|
||||
current_size := header_size + n
|
||||
i = 0
|
||||
while i <= current_size - 4
|
||||
if header_buf[i] == '\r' && header_buf[i + 1] == '\n' && header_buf[i + 2] == '\r' && header_buf[i + 3] == '\n'
|
||||
@@ -62,9 +62,9 @@ func main[argc: i64, argv: ptr] : i64
|
||||
io.print_sized(header_buf as ptr + end_index, header_size as i64 - end_index)
|
||||
mem.free(header_buf)
|
||||
|
||||
let buffer = mem.alloc(4096)
|
||||
buffer := mem.alloc(4096)
|
||||
while true
|
||||
let n = net.read(s, buffer, 4096)
|
||||
n := net.read(s, buffer, 4096)
|
||||
if n <= 0
|
||||
break
|
||||
io.print_sized(buffer, n)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
func main[] : i64
|
||||
let a = 0
|
||||
let b = 1
|
||||
a := 0
|
||||
b := 1
|
||||
|
||||
while a < 100000
|
||||
io.println_i64(a)
|
||||
let temp = b
|
||||
temp := b
|
||||
b = a + b
|
||||
a = temp
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
func main[] : i64
|
||||
let answer = math.abs(os.urandom_i64()) % 100
|
||||
answer := math.abs(os.urandom_i64()) % 100
|
||||
|
||||
while true
|
||||
io.println("Guess a number: ")
|
||||
let guess = io.read_line() |> str.trim() |> str.parse_i64()
|
||||
guess := io.read_line() |> str.trim() |> str.parse_i64()
|
||||
|
||||
if guess == answer
|
||||
io.println("You win!")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func part1[l1: Array, l2: Array] : void
|
||||
let out = 0
|
||||
out := 0
|
||||
|
||||
for i in 0..l1->size
|
||||
out = out + math.abs(array.nth(l1, i) - array.nth(l2, i))
|
||||
@@ -7,7 +7,7 @@ func part1[l1: Array, l2: Array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[l1: Array, l2: Array] : void
|
||||
let out = 0
|
||||
out := 0
|
||||
|
||||
for i in 0..l1->size
|
||||
out = out + array.nth(l1, i) * alg.count(l2, array.nth(l1, i))
|
||||
@@ -15,15 +15,15 @@ func part2[l1: Array, l2: Array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : i64
|
||||
let lines: Array = must(io.read_text_file?("input.txt")) |> str.split("\n")
|
||||
lines : Array = must(io.read_text_file?("input.txt")) |> str.split("\n")
|
||||
|
||||
let l1 = []
|
||||
let l2 = []
|
||||
l1 := []
|
||||
l2 := []
|
||||
|
||||
for i in 0..lines->size
|
||||
let line: str = array.nth(lines, i)
|
||||
line : str = array.nth(lines, i)
|
||||
|
||||
let parts = str.split(line, " ")
|
||||
parts := str.split(line, " ")
|
||||
|
||||
array.push(l1, str.parse_i64(array.nth(parts, 0)))
|
||||
array.push(l2, str.parse_i64(array.nth(parts, 1)))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func check[report: Array] : bool
|
||||
let increasing = array.nth(report, 0) < array.nth(report, 1)
|
||||
increasing := array.nth(report, 0) < array.nth(report, 1)
|
||||
|
||||
for i in 0..report->size-1
|
||||
if array.nth(report, i) > array.nth(report, i + 1) && increasing
|
||||
@@ -7,14 +7,14 @@ func check[report: Array] : bool
|
||||
if array.nth(report, i) < array.nth(report, i + 1) && !increasing
|
||||
return false
|
||||
|
||||
let diff = math.abs(array.nth(report, i) - array.nth(report, i + 1))
|
||||
diff := math.abs(array.nth(report, i) - array.nth(report, i + 1))
|
||||
if diff < 1 || diff > 3
|
||||
return false
|
||||
|
||||
return true
|
||||
|
||||
func part1[data: Array] : void
|
||||
let out = 0
|
||||
out := 0
|
||||
|
||||
for i in 0..data->size
|
||||
if check(array.nth(data, i))
|
||||
@@ -23,15 +23,15 @@ func part1[data: Array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[data: Array] : void
|
||||
let out = 0
|
||||
out := 0
|
||||
|
||||
for i in 0..data->size
|
||||
if check(array.nth(data, i))
|
||||
out = out + 1
|
||||
else
|
||||
let arr: Array = array.nth(data, i)
|
||||
arr : Array = array.nth(data, i)
|
||||
for j in 0..arr->size
|
||||
let sliced = array.concat(array.slice(arr, 0, j), array.slice(arr, j + 1, arr->size - (j + 1)))
|
||||
sliced := array.concat(array.slice(arr, 0, j), array.slice(arr, j + 1, arr->size - (j + 1)))
|
||||
|
||||
if check(sliced)
|
||||
out = out + 1
|
||||
@@ -40,13 +40,13 @@ func part2[data: Array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : i64
|
||||
let lines = must(io.read_text_file?("input.txt")) |> str.split("\n")
|
||||
lines := must(io.read_text_file?("input.txt")) |> str.split("\n")
|
||||
|
||||
let data = []
|
||||
data := []
|
||||
for i in 0..lines->size
|
||||
let line: str = array.nth(lines, i)
|
||||
line : str = array.nth(lines, i)
|
||||
|
||||
let report = str.split(line, " ")
|
||||
report := str.split(line, " ")
|
||||
for i in 0..report->size
|
||||
array.set(report, i, str.parse_i64(array.nth(report, i)))
|
||||
array.push(data, report)
|
||||
|
||||
@@ -14,7 +14,7 @@ func check[lines: Array, x: i64, y: i64, dx: i64, dy: i64] : bool
|
||||
return true
|
||||
|
||||
func part1[lines: Array] : void
|
||||
let out = 0
|
||||
out := 0
|
||||
|
||||
for x in 0..lines->size
|
||||
for y in 0..str.len(array.nth(lines, x))
|
||||
@@ -38,12 +38,12 @@ func part1[lines: Array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[lines: Array] : void
|
||||
let out = 0
|
||||
out := 0
|
||||
|
||||
for x in 1..lines->size-1
|
||||
for y in 1..str.len(array.nth(lines, x))-1
|
||||
if array.nth(lines, x)[y] == 'A'
|
||||
let s = mem.alloc(5) as str
|
||||
s := mem.alloc(5) as str
|
||||
s[0] = array.nth(lines, x - 1)[y - 1]
|
||||
s[1] = array.nth(lines, x + 1)[y - 1]
|
||||
s[2] = array.nth(lines, x + 1)[y + 1]
|
||||
@@ -56,7 +56,7 @@ func part2[lines: Array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : i64
|
||||
let lines: Array = must(io.read_text_file?("input.txt")) |> str.split("\n")
|
||||
lines : Array = must(io.read_text_file?("input.txt")) |> str.split("\n")
|
||||
|
||||
part1(lines)
|
||||
part2(lines)
|
||||
|
||||
@@ -12,34 +12,34 @@ func check[update: Array, rules_left: Array, rules_right: Array] : bool
|
||||
return true
|
||||
|
||||
func sort_by_rules[update: Array, rules_left: Array, rules_right: Array] : void
|
||||
let swapped = true
|
||||
swapped := true
|
||||
|
||||
while swapped
|
||||
swapped = false
|
||||
for i in 0..update->size-1
|
||||
let a: i64 = array.nth(update, i)
|
||||
let b: i64 = array.nth(update, i+1)
|
||||
a : i64 = array.nth(update, i)
|
||||
b : i64 = array.nth(update, i+1)
|
||||
if rule_exists(rules_left, rules_right, b, a)
|
||||
let tmp = a
|
||||
tmp := a
|
||||
array.set(update, i, b)
|
||||
array.set(update, i+1, tmp)
|
||||
swapped = true
|
||||
|
||||
func part1[updates: Array, rules_left: Array, rules_right: Array] : void
|
||||
let out = 0
|
||||
out := 0
|
||||
|
||||
for i in 0..updates->size
|
||||
let update: Array = array.nth(updates, i)
|
||||
update : Array = array.nth(updates, i)
|
||||
if check(update, rules_left, rules_right)
|
||||
out = out + array.nth(update, update->size / 2)
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[updates: Array, rules_left: Array, rules_right: Array] : void
|
||||
let out = 0
|
||||
out := 0
|
||||
|
||||
for i in 0..updates->size
|
||||
let update: Array = array.nth(updates, i)
|
||||
update : Array = array.nth(updates, i)
|
||||
if !check(update, rules_left, rules_right)
|
||||
sort_by_rules(update, rules_left, rules_right)
|
||||
out = out + array.nth(update, update->size / 2)
|
||||
@@ -47,23 +47,23 @@ func part2[updates: Array, rules_left: Array, rules_right: Array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : i64
|
||||
let data = must(io.read_text_file?("input.txt")) |> str.split("\n\n")
|
||||
data := must(io.read_text_file?("input.txt")) |> str.split("\n\n")
|
||||
|
||||
let rules_left = []
|
||||
let rules_right = []
|
||||
let rules_lines = str.split(array.nth(data, 0), "\n")
|
||||
rules_left := []
|
||||
rules_right := []
|
||||
rules_lines := str.split(array.nth(data, 0), "\n")
|
||||
for i in 0..rules_lines->size
|
||||
let line: str = array.nth(rules_lines, i)
|
||||
let parts = str.split(line, "|")
|
||||
line : str = array.nth(rules_lines, i)
|
||||
parts := str.split(line, "|")
|
||||
|
||||
array.push(rules_left, str.parse_i64(array.nth(parts, 0)))
|
||||
array.push(rules_right, str.parse_i64(array.nth(parts, 1)))
|
||||
|
||||
let updates = []
|
||||
let updates_lines = str.split(array.nth(data, 1), "\n")
|
||||
updates := []
|
||||
updates_lines := str.split(array.nth(data, 1), "\n")
|
||||
for i in 0..updates_lines->size
|
||||
let line: str = array.nth(updates_lines, i)
|
||||
let xs = str.split(line, ",")
|
||||
line : str = array.nth(updates_lines, i)
|
||||
xs := str.split(line, ",")
|
||||
|
||||
for i in 0..xs->size
|
||||
array.set(xs, i, str.parse_i64(array.nth(xs, i)))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
func concat[a: i64, b: i64] : i64
|
||||
let a_str = str.from_i64(a)
|
||||
let b_str = str.from_i64(b)
|
||||
let ab_str = str.concat(a_str, b_str)
|
||||
let out = str.parse_i64(ab_str)
|
||||
a_str := str.from_i64(a)
|
||||
b_str := str.from_i64(b)
|
||||
ab_str := str.concat(a_str, b_str)
|
||||
out := str.parse_i64(ab_str)
|
||||
// without freeing the program works but leaks 2GB of memory :p
|
||||
mem.free(a_str)
|
||||
mem.free(b_str)
|
||||
@@ -10,16 +10,16 @@ func concat[a: i64, b: i64] : i64
|
||||
return out
|
||||
|
||||
func solve[ops: Array, e: Array] : i64
|
||||
let e_1: Array = array.nth(e, 1)
|
||||
let n = e_1->size - 1
|
||||
let indices = []
|
||||
e_1 : Array = array.nth(e, 1)
|
||||
n := e_1->size - 1
|
||||
indices := []
|
||||
for i in 0..n
|
||||
array.push(indices, 0)
|
||||
|
||||
while true
|
||||
let res: i64 = array.nth(e_1, 0)
|
||||
res : i64 = array.nth(e_1, 0)
|
||||
for i in 0..n
|
||||
let op: str = array.nth(ops, array.nth(indices, i))
|
||||
op : str = array.nth(ops, array.nth(indices, i))
|
||||
|
||||
if str.equal(op, "add")
|
||||
res = res + array.nth(e_1, i + 1)
|
||||
@@ -30,8 +30,8 @@ func solve[ops: Array, e: Array] : i64
|
||||
if res == array.nth(e, 0)
|
||||
return res
|
||||
|
||||
let done = true
|
||||
let i = n - 1
|
||||
done := true
|
||||
i := n - 1
|
||||
|
||||
while i >= 0
|
||||
array.set(indices, i, array.nth(indices, i) + 1)
|
||||
@@ -45,7 +45,7 @@ func solve[ops: Array, e: Array] : i64
|
||||
return 0
|
||||
|
||||
func part1[equations: Array] : void
|
||||
let out = 0
|
||||
out := 0
|
||||
|
||||
for i in 0..equations->size
|
||||
out = out + solve(["add", "mul"], array.nth(equations, i))
|
||||
@@ -53,7 +53,7 @@ func part1[equations: Array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[equations: Array] : void
|
||||
let out = 0
|
||||
out := 0
|
||||
|
||||
for i in 0..equations->size
|
||||
out = out + solve(["add", "mul", "concat"], array.nth(equations, i))
|
||||
@@ -61,14 +61,14 @@ func part2[equations: Array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : i64
|
||||
let lines = must(io.read_text_file?("input.txt")) |> str.split("\n")
|
||||
let equations = []
|
||||
lines := must(io.read_text_file?("input.txt")) |> str.split("\n")
|
||||
equations := []
|
||||
|
||||
for i in 0..lines->size
|
||||
let line: str = array.nth(lines, i)
|
||||
let parts = str.split(line, ": ")
|
||||
line : str = array.nth(lines, i)
|
||||
parts := str.split(line, ": ")
|
||||
|
||||
let xs = str.split(array.nth(parts, 1), " ")
|
||||
xs := str.split(array.nth(parts, 1), " ")
|
||||
for j in 0..xs->size
|
||||
array.set(xs, j, str.parse_i64(array.nth(xs, j)))
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
func part1[lines: Array] : void
|
||||
let password = 0
|
||||
let dial = 50
|
||||
password := 0
|
||||
dial := 50
|
||||
|
||||
for i in 0..lines->size
|
||||
let line: str = array.nth(lines, i)
|
||||
let dir = line[0]
|
||||
let n = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||
line : str = array.nth(lines, i)
|
||||
dir := line[0]
|
||||
n := str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||
|
||||
if dir == 'L'
|
||||
dial = dial - n
|
||||
@@ -22,13 +22,13 @@ func part1[lines: Array] : void
|
||||
io.println_i64(password)
|
||||
|
||||
func part2[lines: Array] : void
|
||||
let password = 0
|
||||
let dial = 50
|
||||
password := 0
|
||||
dial := 50
|
||||
|
||||
for i in 0..lines->size
|
||||
let line: str = array.nth(lines, i)
|
||||
let dir = line[0]
|
||||
let n = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||
line : str = array.nth(lines, i)
|
||||
dir := line[0]
|
||||
n := str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||
|
||||
if dir == 'L'
|
||||
for i in 0..n
|
||||
@@ -47,7 +47,7 @@ func part2[lines: Array] : void
|
||||
io.println_i64(password)
|
||||
|
||||
func main[] : i64
|
||||
let lines = must(io.read_text_file?("input.txt")) |> str.split("\n")
|
||||
lines := must(io.read_text_file?("input.txt")) |> str.split("\n")
|
||||
|
||||
part1(lines)
|
||||
part2(lines)
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
func part1_is_invalid_id[s: str] : bool
|
||||
let len = str.len(s)
|
||||
len := str.len(s)
|
||||
if len % 2 != 0
|
||||
return false
|
||||
|
||||
let first = str.substr(s, 0, len / 2)
|
||||
let second = str.substr(s, len / 2, len / 2)
|
||||
first := str.substr(s, 0, len / 2)
|
||||
second := str.substr(s, len / 2, len / 2)
|
||||
|
||||
return str.equal(first, second)
|
||||
|
||||
func part1[ranges: Array] : void
|
||||
let sum = 0
|
||||
sum := 0
|
||||
|
||||
for i in 0..ranges->size
|
||||
let parts = array.nth(ranges, i) |> str.split("-")
|
||||
let start = array.nth(parts, 0) |> str.parse_i64()
|
||||
let end = array.nth(parts, 1) |> str.parse_i64()
|
||||
parts := array.nth(ranges, i) |> str.split("-")
|
||||
start := array.nth(parts, 0) |> str.parse_i64()
|
||||
end := array.nth(parts, 1) |> str.parse_i64()
|
||||
|
||||
for n in (start)..end+1
|
||||
if part1_is_invalid_id(str.from_i64(n))
|
||||
@@ -24,15 +24,15 @@ func part1[ranges: Array] : void
|
||||
|
||||
// had to cheat this one
|
||||
func part2_is_invalid_id[s: str] : bool
|
||||
let len = str.len(s)
|
||||
len := str.len(s)
|
||||
if len < 2
|
||||
return false
|
||||
for div in 1..(len / 2 + 1)
|
||||
if len % div == 0
|
||||
let u = str.substr(s, 0, div)
|
||||
let is_repeat = true
|
||||
u := str.substr(s, 0, div)
|
||||
is_repeat := true
|
||||
for k in 1..(len / div)
|
||||
let segment = str.substr(s, k * div, div)
|
||||
segment := str.substr(s, k * div, div)
|
||||
if !str.equal(segment, u)
|
||||
is_repeat = false
|
||||
if is_repeat
|
||||
@@ -40,12 +40,12 @@ func part2_is_invalid_id[s: str] : bool
|
||||
return false
|
||||
|
||||
func part2[ranges: Array] : void
|
||||
let sum = 0
|
||||
sum := 0
|
||||
|
||||
for i in 0..ranges->size
|
||||
let parts = array.nth(ranges, i) |> str.split("-")
|
||||
let start = array.nth(parts, 0) |> str.parse_i64()
|
||||
let end = array.nth(parts, 1) |> str.parse_i64()
|
||||
parts := array.nth(ranges, i) |> str.split("-")
|
||||
start := array.nth(parts, 0) |> str.parse_i64()
|
||||
end := array.nth(parts, 1) |> str.parse_i64()
|
||||
|
||||
for n in (start)..end+1
|
||||
if part2_is_invalid_id(str.from_i64(n))
|
||||
@@ -54,7 +54,7 @@ func part2[ranges: Array] : void
|
||||
io.println_i64(sum)
|
||||
|
||||
func main[] : i64
|
||||
let ranges = must(io.read_text_file?("input.txt")) |> str.split(",")
|
||||
ranges := must(io.read_text_file?("input.txt")) |> str.split(",")
|
||||
|
||||
part1(ranges)
|
||||
part2(ranges)
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
func part1[lines: Array] : void
|
||||
let sum = 0
|
||||
sum := 0
|
||||
|
||||
for i in 0..lines->size
|
||||
let line: str = array.nth(lines, i)
|
||||
line : str = array.nth(lines, i)
|
||||
|
||||
let largest = 0
|
||||
largest := 0
|
||||
for j in 0..str.len(line)
|
||||
for k in (j+1)..str.len(line)
|
||||
let s = mem.alloc(3) as str
|
||||
s := mem.alloc(3) as str
|
||||
s[0] = line[j]
|
||||
s[1] = line[k]
|
||||
s[2] = 0
|
||||
let n = str.parse_i64(s)
|
||||
n := str.parse_i64(s)
|
||||
if n > largest
|
||||
largest = n
|
||||
|
||||
@@ -20,12 +20,12 @@ func part1[lines: Array] : void
|
||||
|
||||
// had to cheat this one
|
||||
func part2_rec[bank: str, start: i64, remaining: i64] : i64
|
||||
let largest = 0
|
||||
let largest_idx = start
|
||||
let len = str.len(bank)
|
||||
largest := 0
|
||||
largest_idx := start
|
||||
len := str.len(bank)
|
||||
|
||||
for i in (start)..(len-remaining+1)
|
||||
let v = (bank[i] - '0') as i64
|
||||
v := (bank[i] - '0') as i64
|
||||
if v > largest
|
||||
largest = v
|
||||
largest_idx = i
|
||||
@@ -36,16 +36,16 @@ func part2_rec[bank: str, start: i64, remaining: i64] : i64
|
||||
return largest
|
||||
|
||||
func part2[lines: Array] : void
|
||||
let sum = 0
|
||||
sum := 0
|
||||
|
||||
for i in 0..lines->size
|
||||
let line: str = array.nth(lines, i)
|
||||
line : str = array.nth(lines, i)
|
||||
|
||||
sum = sum + part2_rec(line, 0, 12)
|
||||
io.println_i64(sum)
|
||||
|
||||
func main[] : i64
|
||||
let lines = must(io.read_text_file?("input.txt")) |> str.split("\n")
|
||||
lines := must(io.read_text_file?("input.txt")) |> str.split("\n")
|
||||
|
||||
part1(lines)
|
||||
part2(lines)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func main[] : i64
|
||||
let sum = 0
|
||||
sum := 0
|
||||
|
||||
for i in 0..266000
|
||||
if i % 2 != 0
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func main[] : i64
|
||||
let sum = 0
|
||||
sum := 0
|
||||
|
||||
for i in 0..1000
|
||||
if i % 5 == 0 || i % 3 == 0
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
func main[] : i64
|
||||
let sum = 0
|
||||
let a = 0
|
||||
let b = 1
|
||||
sum := 0
|
||||
a := 0
|
||||
b := 1
|
||||
|
||||
while a < 4000000
|
||||
if a % 2 == 0
|
||||
sum = sum + a
|
||||
let temp = b
|
||||
temp := b
|
||||
b = a + b
|
||||
a = temp
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
func main[] : i64
|
||||
let n = 600851475143
|
||||
let f = 2
|
||||
n := 600851475143
|
||||
f := 2
|
||||
|
||||
while f * f <= n
|
||||
if n % f == 0
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
func main[] : i64
|
||||
let out = 0
|
||||
out := 0
|
||||
|
||||
for a in 500..1000
|
||||
for b in 500..1000
|
||||
if a * b > out
|
||||
let s = str.from_i64(a * b)
|
||||
let s_rev = str.reverse(s)
|
||||
s := str.from_i64(a * b)
|
||||
s_rev := str.reverse(s)
|
||||
if str.equal(s, s_rev)
|
||||
out = a * b
|
||||
mem.free(s)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func main[] : i64
|
||||
let out = 1
|
||||
out := 1
|
||||
|
||||
for i in 1..21
|
||||
out = math.lcm(out, i)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
func main[] : i64
|
||||
let sum_of_squares = 0
|
||||
sum_of_squares := 0
|
||||
for i in 1..101
|
||||
sum_of_squares = sum_of_squares + i * i
|
||||
|
||||
let square_of_sum = 0
|
||||
square_of_sum := 0
|
||||
for i in 1..101
|
||||
square_of_sum = square_of_sum + i
|
||||
square_of_sum = square_of_sum * square_of_sum
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : i64
|
||||
let found = 0
|
||||
found := 0
|
||||
|
||||
let i = 1
|
||||
i := 1
|
||||
while true
|
||||
if math.is_prime(i)
|
||||
found = found + 1
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
func main[] : i64
|
||||
let n = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
|
||||
n := "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
|
||||
|
||||
let out = 0
|
||||
let max = str.len(n) - 13
|
||||
out := 0
|
||||
max := str.len(n) - 13
|
||||
for i in 0..max
|
||||
let s = 1
|
||||
let j = 0
|
||||
s := 1
|
||||
j := 0
|
||||
while j < 13
|
||||
s = s * (n[i + j] - '0')
|
||||
j = j + 1
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : i64
|
||||
for a in 1..1000
|
||||
for b in 1..1000
|
||||
let c = 1000 - b - a
|
||||
c := 1000 - b - a
|
||||
if a * a + b * b == c * c
|
||||
io.println_i64(a * b * c)
|
||||
return 0
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func main[] : i64
|
||||
let sum = 0
|
||||
sum := 0
|
||||
|
||||
for i in 0..2000000
|
||||
if math.is_prime(i)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : i64
|
||||
let N = 20
|
||||
N := 20
|
||||
|
||||
let grid = []
|
||||
grid := []
|
||||
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, [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65])
|
||||
@@ -23,14 +23,14 @@ func main[] : i64
|
||||
array.push(grid, [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54])
|
||||
array.push(grid, [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48])
|
||||
|
||||
let out = 0
|
||||
out := 0
|
||||
|
||||
for i in 0..N-3
|
||||
for j in 0..N-3
|
||||
let h: i64 = array.nth(array.nth(grid, i), j) * array.nth(array.nth(grid, i), j+1) * array.nth(array.nth(grid, i), j+2) * array.nth(array.nth(grid, i), j+3)
|
||||
let v: i64 = array.nth(array.nth(grid, j), i) * array.nth(array.nth(grid, j+1), i) * array.nth(array.nth(grid, j+2), i) * array.nth(array.nth(grid, j+3), i)
|
||||
let d1: i64 = array.nth(array.nth(grid, i), j) * array.nth(array.nth(grid, i+1), j+1) * array.nth(array.nth(grid, i+2), j+2) * array.nth(array.nth(grid, i+3), j+3)
|
||||
let d2: i64 = array.nth(array.nth(grid, i), N-j-1) * array.nth(array.nth(grid, i+1), N-j-2) * array.nth(array.nth(grid, i+2), N-j-3) * array.nth(array.nth(grid, i+3), N-j-4)
|
||||
h : i64 = array.nth(array.nth(grid, i), j) * array.nth(array.nth(grid, i), j+1) * array.nth(array.nth(grid, i), j+2) * array.nth(array.nth(grid, i), j+3)
|
||||
v : i64 = array.nth(array.nth(grid, j), i) * array.nth(array.nth(grid, j+1), i) * array.nth(array.nth(grid, j+2), i) * array.nth(array.nth(grid, j+3), i)
|
||||
d1 : i64 = array.nth(array.nth(grid, i), j) * array.nth(array.nth(grid, i+1), j+1) * array.nth(array.nth(grid, i+2), j+2) * array.nth(array.nth(grid, i+3), j+3)
|
||||
d2 : i64 = array.nth(array.nth(grid, i), N-j-1) * array.nth(array.nth(grid, i+1), N-j-2) * array.nth(array.nth(grid, i+2), N-j-3) * array.nth(array.nth(grid, i+3), N-j-4)
|
||||
out = math.max(out, math.max(h, math.max(v, math.max(d1, d2))))
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func num_divisors[n: i64] : i64
|
||||
let end = math.isqrt(n)
|
||||
end := math.isqrt(n)
|
||||
|
||||
let out = 0
|
||||
out := 0
|
||||
for i in 1..end+1
|
||||
if n % i == 0
|
||||
out = out + 2
|
||||
@@ -11,8 +11,8 @@ func num_divisors[n: i64] : i64
|
||||
return out
|
||||
|
||||
func main[] : i64
|
||||
let n = 0
|
||||
let i = 1
|
||||
n := 0
|
||||
i := 1
|
||||
while true
|
||||
n = n + i
|
||||
if num_divisors(n) > 500
|
||||
|
||||
@@ -4,18 +4,18 @@ func collatz_num[n: i64] : i64
|
||||
return n * 3 + 1
|
||||
|
||||
func collatz_seq[n: i64]: i64
|
||||
let i = 1
|
||||
i := 1
|
||||
while n != 1
|
||||
n = collatz_num(n)
|
||||
i = i + 1
|
||||
return i
|
||||
|
||||
func main[] : i64
|
||||
let max = 0
|
||||
let max_index = 0
|
||||
max := 0
|
||||
max_index := 0
|
||||
|
||||
for i in 1..1000000
|
||||
let seq = collatz_seq(i)
|
||||
seq := collatz_seq(i)
|
||||
if seq > max
|
||||
max = seq
|
||||
max_index = i
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : i64
|
||||
let n = 40
|
||||
let r = 20
|
||||
let out = 1
|
||||
n := 40
|
||||
r := 20
|
||||
out := 1
|
||||
|
||||
for i in 1..r+1
|
||||
out = out * (n - (r - i)) / i
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
func main[] : i64
|
||||
let n = []
|
||||
n := []
|
||||
array.push(n, 1)
|
||||
|
||||
for j in 0..1000
|
||||
let carry = 0
|
||||
carry := 0
|
||||
for i in 0..n->size
|
||||
let tmp: i64 = array.nth(n, i) * 2 + carry
|
||||
tmp : i64 = array.nth(n, i) * 2 + carry
|
||||
array.set(n, i, tmp % 10)
|
||||
carry = tmp / 10
|
||||
while carry > 0
|
||||
array.push(n, carry % 10)
|
||||
carry = carry / 10
|
||||
|
||||
let sum = 0
|
||||
sum := 0
|
||||
for i in 0..n->size
|
||||
sum = sum + array.nth(n, i)
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
func main[] : i64
|
||||
let s1 = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
|
||||
let s2 = [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
|
||||
let s3 = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]
|
||||
s1 := [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
|
||||
s2 := [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
|
||||
s3 := [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]
|
||||
|
||||
let sum = 0
|
||||
sum := 0
|
||||
|
||||
for i in 1..10
|
||||
sum = sum + array.nth(s1, i)
|
||||
|
||||
@@ -2,13 +2,13 @@ func findmax[triangle: Array, row: i64, col: i64] : i64
|
||||
if row == 14
|
||||
return array.nth(array.nth(triangle, row), col)
|
||||
|
||||
let left = findmax(triangle, row + 1, col)
|
||||
let right = findmax(triangle, row + 1, col + 1)
|
||||
left := findmax(triangle, row + 1, col)
|
||||
right := findmax(triangle, row + 1, col + 1)
|
||||
|
||||
return array.nth(array.nth(triangle, row), col) + math.max(left, right)
|
||||
|
||||
func main[] : i64
|
||||
let triangle = []
|
||||
triangle := []
|
||||
array.push(triangle, [75])
|
||||
array.push(triangle, [95, 64])
|
||||
array.push(triangle, [17, 47, 82])
|
||||
|
||||
@@ -10,8 +10,8 @@ func days[y: i64, m: i64] : i64
|
||||
return 31
|
||||
|
||||
func main[] : i64
|
||||
let wday = 0
|
||||
let sun = 0
|
||||
wday := 0
|
||||
sun := 0
|
||||
|
||||
for year in 1901..2001
|
||||
for mon in 1..13
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func multiply[n: Array, x: i64] : void
|
||||
let carry = 0
|
||||
carry := 0
|
||||
for i in 0..n->size
|
||||
let prod: i64 = array.nth(n, i) * x + carry
|
||||
prod : i64 = array.nth(n, i) * x + carry
|
||||
array.set(n, i, prod % 10)
|
||||
carry = prod / 10
|
||||
while carry > 0
|
||||
@@ -9,12 +9,12 @@ func multiply[n: Array, x: i64] : void
|
||||
carry = carry / 10
|
||||
|
||||
func main[] : i64
|
||||
let n = [1]
|
||||
n := [1]
|
||||
|
||||
for i in 2..101
|
||||
multiply(n, i)
|
||||
|
||||
let sum = 0
|
||||
sum := 0
|
||||
for i in 0..n->size
|
||||
sum = sum + array.nth(n, i)
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
func divisors_sum[n: i64] : i64
|
||||
let k = n
|
||||
let sum = 1
|
||||
k := n
|
||||
sum := 1
|
||||
|
||||
for i in 2..k+1
|
||||
let p = 1
|
||||
p := 1
|
||||
while k % i == 0
|
||||
p = p * i
|
||||
k = k / i
|
||||
@@ -11,10 +11,10 @@ func divisors_sum[n: i64] : i64
|
||||
return sum - n
|
||||
|
||||
func main[] : i64
|
||||
let sum = 0
|
||||
sum := 0
|
||||
|
||||
for i in 2..10000
|
||||
let d = divisors_sum(i)
|
||||
d := divisors_sum(i)
|
||||
if i < d && i == divisors_sum(d)
|
||||
sum = sum + i + d
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func main[] : i64
|
||||
let arr = []
|
||||
arr := []
|
||||
for i in 0..10
|
||||
array.push(arr, math.abs(os.urandom_i64()) % 1000)
|
||||
|
||||
|
||||
@@ -18,8 +18,8 @@ const WHITE = 0xffffffff
|
||||
const RED = 0xff0000ff
|
||||
|
||||
func main[] : i64
|
||||
let x = 200
|
||||
let y = 200
|
||||
x := 200
|
||||
y := 200
|
||||
|
||||
InitWindow(800, 600, "Hello, World!")
|
||||
SetTargetFPS(60)
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
func rule110_step[state: Array] : Array
|
||||
let new_state = []
|
||||
new_state := []
|
||||
|
||||
for i in 0..state->size
|
||||
let left = false
|
||||
left := false
|
||||
if i - 1 >= 0
|
||||
left = array.nth(state, i - 1)
|
||||
let center: bool = array.nth(state, i)
|
||||
let right = false
|
||||
center : bool = array.nth(state, i)
|
||||
right := false
|
||||
if i + 1 < state->size
|
||||
right = array.nth(state, i + 1)
|
||||
|
||||
@@ -23,9 +23,9 @@ func print_state[state: Array]: void
|
||||
io.println("")
|
||||
|
||||
func main[] : i64
|
||||
let SIZE = 60
|
||||
SIZE := 60
|
||||
|
||||
let state = []
|
||||
state := []
|
||||
for i in 0..SIZE
|
||||
array.push(state, false)
|
||||
array.push(state, true)
|
||||
|
||||
@@ -11,9 +11,9 @@ extern sqlite3_column_text
|
||||
extern sqlite3_finalize
|
||||
|
||||
func main[] : i64
|
||||
let rc = 0
|
||||
let db = 0
|
||||
let stmt = 0
|
||||
rc := 0
|
||||
db := 0
|
||||
stmt := 0
|
||||
|
||||
rc = sqlite3_open("todo.db", ^db)
|
||||
if rc
|
||||
@@ -30,7 +30,7 @@ func main[] : i64
|
||||
io.println("0. Quit")
|
||||
io.print("\n> ")
|
||||
|
||||
let choice = io.read_line() |> str.parse_i64()
|
||||
choice := io.read_line() |> str.parse_i64()
|
||||
|
||||
if choice == 0
|
||||
break
|
||||
@@ -39,8 +39,8 @@ func main[] : i64
|
||||
sqlite3_prepare_v2(db, "SELECT * FROM todo", -1, ^stmt, 0)
|
||||
|
||||
while sqlite3_step(stmt) == 100
|
||||
let id = sqlite3_column_int(stmt, 0)
|
||||
let task = sqlite3_column_text(stmt, 1)
|
||||
id := sqlite3_column_int(stmt, 0)
|
||||
task := sqlite3_column_text(stmt, 1)
|
||||
|
||||
io.print_i64(id)
|
||||
io.print(" - ")
|
||||
@@ -49,7 +49,7 @@ func main[] : i64
|
||||
io.println("============")
|
||||
else if choice == 2
|
||||
io.print("\nEnter new task: ")
|
||||
let task = io.read_line() |> str.trim()
|
||||
task := io.read_line() |> str.trim()
|
||||
|
||||
sqlite3_prepare_v2(db, "INSERT INTO todo(task) VALUES (?);", -1, ^stmt, 0)
|
||||
sqlite3_bind_text(stmt, 1, task, -1, 0)
|
||||
@@ -59,7 +59,7 @@ func main[] : i64
|
||||
io.println("\nTask added\n")
|
||||
else if choice == 3
|
||||
io.print("\nEnter task id: ")
|
||||
let id = io.read_line() |> str.parse_i64()
|
||||
id := io.read_line() |> str.parse_i64()
|
||||
|
||||
sqlite3_prepare_v2(db, "DELETE FROM todo WHERE id = ?;", -1, ^stmt, 0)
|
||||
sqlite3_bind_int(stmt, 1, id, -1, 0)
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
func main[] : i64
|
||||
let s: i64 = must(net.listen?(net.pack_addr(127, 0, 0, 1), 8000))
|
||||
s : i64 = must(net.listen?(net.pack_addr(127, 0, 0, 1), 8000))
|
||||
|
||||
io.println("Listening on port 8000...")
|
||||
let resp = mem.alloc(60000)
|
||||
let addr = mem.alloc(16)
|
||||
|
||||
resp := mem.alloc(60000)
|
||||
addr := mem.alloc(16)
|
||||
while true
|
||||
let conn = net.accept(s, addr)
|
||||
conn := net.accept(s, addr)
|
||||
if conn < 0
|
||||
continue
|
||||
|
||||
@@ -14,6 +15,6 @@ func main[] : i64
|
||||
io.print_sized(resp, n)
|
||||
io.println("")
|
||||
|
||||
let n = net.read(conn, resp, 60000)
|
||||
n := net.read(conn, resp, 60000)
|
||||
net.send(conn, resp, n)
|
||||
net.close(conn)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
func main[] : i64
|
||||
let s: net.UDPSocket = must(net.create_udp_server?(net.pack_addr(127, 0, 0, 1), 8000))
|
||||
s : net.UDPSocket = must(net.create_udp_server?(net.pack_addr(127, 0, 0, 1), 8000))
|
||||
|
||||
io.println("Listening on port 8000...")
|
||||
while true
|
||||
let pkt = net.udp_receive(s, 60000)
|
||||
pkt := net.udp_receive(s, 60000)
|
||||
if pkt->size <= 0
|
||||
net.UDPPacket.free(pkt)
|
||||
continue
|
||||
|
||||
@@ -211,7 +211,7 @@ _builtin_environ:
|
||||
pub fn compile_stmt(&mut self, env: &mut Env, stmt: &Stmt) -> Result<(), ZernError> {
|
||||
match stmt {
|
||||
Stmt::Expression(expr) => self.compile_expr(env, expr)?,
|
||||
Stmt::Let {
|
||||
Stmt::Declare {
|
||||
name,
|
||||
var_type,
|
||||
initializer,
|
||||
|
||||
@@ -17,7 +17,7 @@ pub enum Params {
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Stmt {
|
||||
Expression(Expr),
|
||||
Let {
|
||||
Declare {
|
||||
name: Token,
|
||||
var_type: Option<Token>,
|
||||
initializer: Expr,
|
||||
@@ -182,12 +182,8 @@ impl Parser {
|
||||
);
|
||||
}
|
||||
|
||||
if self.match_token(&[TokenType::KeywordLet]) {
|
||||
self.let_declaration()
|
||||
} else {
|
||||
self.statement()
|
||||
}
|
||||
}
|
||||
|
||||
fn func_declaration(&mut self, exported: bool) -> Result<Stmt, ZernError> {
|
||||
let name = self.consume(TokenType::Identifier, "expected function name")?;
|
||||
@@ -256,25 +252,6 @@ impl Parser {
|
||||
Ok(Stmt::Struct { name, fields })
|
||||
}
|
||||
|
||||
fn let_declaration(&mut self) -> Result<Stmt, ZernError> {
|
||||
let name = self.consume(TokenType::Identifier, "expected variable name")?;
|
||||
|
||||
let var_type = if self.match_token(&[TokenType::Colon]) {
|
||||
let token = self.consume(TokenType::Identifier, "expected variable type")?;
|
||||
Some(token)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
self.consume(TokenType::Equal, "expected '=' after variable type")?;
|
||||
let initializer = self.expression()?;
|
||||
Ok(Stmt::Let {
|
||||
name,
|
||||
var_type,
|
||||
initializer,
|
||||
})
|
||||
}
|
||||
|
||||
fn const_declaration(&mut self) -> Result<Stmt, ZernError> {
|
||||
let name = self.consume(TokenType::Identifier, "expected const name")?;
|
||||
self.consume(TokenType::Equal, "expected '=' after const name")?;
|
||||
@@ -300,7 +277,25 @@ impl Parser {
|
||||
}
|
||||
|
||||
fn statement(&mut self) -> Result<Stmt, ZernError> {
|
||||
if self.match_token(&[TokenType::KeywordIf]) {
|
||||
if self.check_ahead(&TokenType::Colon) {
|
||||
let name = self.consume(TokenType::Identifier, "expected variable name")?;
|
||||
self.consume(TokenType::Colon, "expected ':'")?;
|
||||
|
||||
let var_type = if self.match_token(&[TokenType::Equal]) {
|
||||
None
|
||||
} else {
|
||||
let var_type = self.consume(TokenType::Identifier, "expected variable type")?;
|
||||
self.consume(TokenType::Equal, "expected '=' after varaible type")?;
|
||||
Some(var_type)
|
||||
};
|
||||
|
||||
let initializer = self.expression()?;
|
||||
Ok(Stmt::Declare {
|
||||
name,
|
||||
var_type,
|
||||
initializer,
|
||||
})
|
||||
} else if self.match_token(&[TokenType::KeywordIf]) {
|
||||
self.if_statement()
|
||||
} else if self.match_token(&[TokenType::KeywordWhile]) {
|
||||
self.while_statement()
|
||||
@@ -660,6 +655,14 @@ impl Parser {
|
||||
false
|
||||
}
|
||||
|
||||
fn check_ahead(&self, token_type: &TokenType) -> bool {
|
||||
if self.current + 1 >= self.tokens.len() {
|
||||
false
|
||||
} else {
|
||||
self.tokens[self.current + 1].token_type == *token_type
|
||||
}
|
||||
}
|
||||
|
||||
fn check(&self, token_type: &TokenType) -> bool {
|
||||
if self.eof() {
|
||||
false
|
||||
|
||||
@@ -6,18 +6,18 @@ const SO_REUSEADDR = 2
|
||||
|
||||
func net.listen?[packed_host: i64, port: i64] : i64
|
||||
err.clear()
|
||||
let s = _builtin_syscall(SYS_socket, AF_INET, SOCK_STREAM, 0)
|
||||
s := _builtin_syscall(SYS_socket, AF_INET, SOCK_STREAM, 0)
|
||||
if s < 0
|
||||
err.set(ERR_SYSCALL_FAILED, "net.listen?: failed to create a socket")
|
||||
return -1
|
||||
|
||||
let optval = 1
|
||||
optval := 1
|
||||
if _builtin_syscall(SYS_setsockopt, s, SOL_SOCKET, SO_REUSEADDR, ^optval, 8) < 0
|
||||
_builtin_syscall(SYS_close, s)
|
||||
err.set(ERR_SYSCALL_FAILED, "net.listen?: setsockopt() failed")
|
||||
return -1
|
||||
|
||||
let sa = mem.alloc(16)
|
||||
sa := mem.alloc(16)
|
||||
mem.zero(sa, 16)
|
||||
sa[0] = 2
|
||||
sa[1] = 0
|
||||
@@ -44,12 +44,12 @@ func net.listen?[packed_host: i64, port: i64] : i64
|
||||
|
||||
func net.connect?[packed_host: i64, port: i64] : i64
|
||||
err.clear()
|
||||
let s = _builtin_syscall(SYS_socket, AF_INET, SOCK_STREAM, 0)
|
||||
s := _builtin_syscall(SYS_socket, AF_INET, SOCK_STREAM, 0)
|
||||
if s < 0
|
||||
err.set(ERR_SYSCALL_FAILED, "net.connect?: failed to create a socket")
|
||||
return -1
|
||||
|
||||
let sa = mem.alloc(16)
|
||||
sa := mem.alloc(16)
|
||||
mem.zero(sa, 16)
|
||||
sa[0] = AF_INET
|
||||
sa[1] = 0
|
||||
@@ -91,7 +91,7 @@ struct net.UDPSocket
|
||||
|
||||
func net.create_udp_client?[packed_host: i64, port: i64] : net.UDPSocket
|
||||
err.clear()
|
||||
let s = new* net.UDPSocket
|
||||
s := new* net.UDPSocket
|
||||
|
||||
s->fd = _builtin_syscall(SYS_socket, AF_INET, SOCK_DGRAM, 0)
|
||||
if s->fd < 0
|
||||
@@ -113,7 +113,7 @@ func net.create_udp_client?[packed_host: i64, port: i64] : net.UDPSocket
|
||||
|
||||
func net.create_udp_server?[packed_host: i64, port: i64] : net.UDPSocket
|
||||
err.clear()
|
||||
let s = net.create_udp_client?(packed_host, port)
|
||||
s := net.create_udp_client?(packed_host, port)
|
||||
if err.check()
|
||||
return 0 as net.UDPSocket
|
||||
|
||||
@@ -138,12 +138,12 @@ func net.UDPPacket.free[pkt: net.UDPPacket] : void
|
||||
mem.free(pkt)
|
||||
|
||||
func net.udp_receive[s: net.UDPSocket, size: i64] : net.UDPPacket
|
||||
let pkt = new* net.UDPPacket
|
||||
pkt := new* net.UDPPacket
|
||||
pkt->data = mem.alloc(size)
|
||||
pkt->source_addr = mem.alloc(16)
|
||||
mem.zero(pkt->source_addr, 16)
|
||||
|
||||
let addrlen = 16
|
||||
addrlen := 16
|
||||
pkt->size = _builtin_syscall(SYS_recvfrom, s->fd, pkt->data, size, 0, pkt->source_addr, ^addrlen)
|
||||
return pkt
|
||||
|
||||
@@ -157,14 +157,14 @@ const DNS_CLASS_IN = 1
|
||||
const DNS_RECURSION_DESIRED = 256
|
||||
|
||||
func net.encode_dns_name[domain: str] : io.Buffer
|
||||
let domain_len = str.len(domain)
|
||||
let buf: io.Buffer = must(io.Buffer.alloc?(domain_len + 2))
|
||||
let out_pos = 0
|
||||
let part_start = 0
|
||||
let i = 0
|
||||
domain_len := str.len(domain)
|
||||
buf : io.Buffer = must(io.Buffer.alloc?(domain_len + 2))
|
||||
out_pos := 0
|
||||
part_start := 0
|
||||
i := 0
|
||||
while i <= domain_len
|
||||
if i == domain_len || domain[i] == '.'
|
||||
let part_len = i - part_start
|
||||
part_len := i - part_start
|
||||
buf->data[out_pos] = part_len & 0xff
|
||||
out_pos = out_pos + 1
|
||||
mem.copy(domain as ptr + part_start, buf->data + out_pos, part_len)
|
||||
@@ -176,11 +176,11 @@ func net.encode_dns_name[domain: str] : io.Buffer
|
||||
return buf
|
||||
|
||||
func net.build_dns_query[domain_name: str, record_type: i64] : io.Buffer
|
||||
let name = net.encode_dns_name(domain_name)
|
||||
let out: io.Buffer = must(io.Buffer.alloc?(12 + name->size + 4))
|
||||
name := net.encode_dns_name(domain_name)
|
||||
out : io.Buffer = must(io.Buffer.alloc?(12 + name->size + 4))
|
||||
|
||||
// header
|
||||
let id = math.abs(os.urandom_i64() % 65536)
|
||||
id := math.abs(os.urandom_i64() % 65536)
|
||||
mem.write16be(out->data + 0, id)
|
||||
mem.write16be(out->data + 2, DNS_RECURSION_DESIRED)
|
||||
mem.write16be(out->data + 4, 1) // num_questions
|
||||
@@ -199,21 +199,21 @@ func net.build_dns_query[domain_name: str, record_type: i64] : io.Buffer
|
||||
// TODO: dont resolve if its already an IP
|
||||
func net.resolve?[domain: str] : i64
|
||||
err.clear()
|
||||
let query = net.build_dns_query(domain, DNS_TYPE_A)
|
||||
query := net.build_dns_query(domain, DNS_TYPE_A)
|
||||
|
||||
// TODO: this probably shouldnt be hardcoded
|
||||
let s = net.create_udp_client?(net.pack_addr(1, 1, 1, 1), 53)
|
||||
s := net.create_udp_client?(net.pack_addr(1, 1, 1, 1), 53)
|
||||
if err.check()
|
||||
return -1
|
||||
|
||||
net.udp_send_to(s, s->addr, query->data, query->size)
|
||||
io.Buffer.free(query)
|
||||
let pkt = net.udp_receive(s, 1024)
|
||||
pkt := net.udp_receive(s, 1024)
|
||||
net.UDPSocket.close_and_free(s)
|
||||
|
||||
// TODO: do actual parsing
|
||||
|
||||
let pos = 12 // skip header (12 bytes)
|
||||
pos := 12 // skip header (12 bytes)
|
||||
|
||||
while pkt->data[pos] != 0
|
||||
pos = pos + pkt->data[pos] + 1 // skip question
|
||||
@@ -222,6 +222,6 @@ func net.resolve?[domain: str] : i64
|
||||
|
||||
pos = pos + 12 // skip name(2), type(2), class(2), ttl(4), rdlength(2)
|
||||
|
||||
let out = net.pack_addr(pkt->data[pos] as i64, pkt->data[pos+1] as i64, pkt->data[pos+2] as i64, pkt->data[pos+3] as i64)
|
||||
out := net.pack_addr(pkt->data[pos] as i64, pkt->data[pos+1] as i64, pkt->data[pos+2] as i64, pkt->data[pos+3] as i64)
|
||||
net.UDPPacket.free(pkt)
|
||||
return out
|
||||
|
||||
288
src/std/std.zr
288
src/std/std.zr
@@ -48,7 +48,7 @@ func mem._align[x: i64] : i64
|
||||
|
||||
func mem._split_block[blk: mem.Block, needed: i64] : void
|
||||
if blk->size >= needed + MEM_BLOCK_SIZE + 8
|
||||
let new_blk = (blk as ptr + MEM_BLOCK_SIZE + needed) as mem.Block
|
||||
new_blk := (blk as ptr + MEM_BLOCK_SIZE + needed) as mem.Block
|
||||
new_blk->size = blk->size - needed - MEM_BLOCK_SIZE
|
||||
new_blk->free = true
|
||||
new_blk->next = blk->next
|
||||
@@ -64,13 +64,13 @@ func mem._split_block[blk: mem.Block, needed: i64] : void
|
||||
|
||||
func mem._request_space?[size: i64] : mem.Block
|
||||
err.clear()
|
||||
let needed = size + MEM_BLOCK_SIZE
|
||||
let alloc_size = (needed + 4095) & -4096
|
||||
needed := size + MEM_BLOCK_SIZE
|
||||
alloc_size := (needed + 4095) & -4096
|
||||
|
||||
// PROT_READ | PROT_WRITE = 3
|
||||
// MAP_PRIVATE | MAP_ANONYMOUS = 34
|
||||
// fd = -1, offset = 0
|
||||
let blk = _builtin_syscall(SYS_mmap, 0, alloc_size, 3, 34, -1, 0) as mem.Block
|
||||
blk := _builtin_syscall(SYS_mmap, 0, alloc_size, 3, 34, -1, 0) as mem.Block
|
||||
if (blk as ptr as i64) == -1
|
||||
err.set(ERR_ALLOC_FAILED, "mem._request_space: mmap failed")
|
||||
return 0 as mem.Block
|
||||
@@ -80,7 +80,7 @@ func mem._request_space?[size: i64] : mem.Block
|
||||
blk->next = 0 as mem.Block
|
||||
blk->prev = 0 as mem.Block
|
||||
|
||||
let tail = mem.read64(_builtin_heap_tail()) as mem.Block
|
||||
tail := mem.read64(_builtin_heap_tail()) as mem.Block
|
||||
if tail as ptr
|
||||
tail->next = blk
|
||||
blk->prev = tail
|
||||
@@ -96,7 +96,7 @@ func mem.alloc?[size: i64] : ptr
|
||||
|
||||
size = mem._align(size)
|
||||
|
||||
let cur = mem.read64(_builtin_heap_head()) as mem.Block
|
||||
cur := mem.read64(_builtin_heap_head()) as mem.Block
|
||||
while cur as ptr
|
||||
if cur->free && cur->size >= size
|
||||
mem._split_block(cur, size)
|
||||
@@ -104,7 +104,7 @@ func mem.alloc?[size: i64] : ptr
|
||||
return cur as ptr + MEM_BLOCK_SIZE
|
||||
cur = cur->next
|
||||
|
||||
let blk = mem._request_space?(size)
|
||||
blk := mem._request_space?(size)
|
||||
if err.check()
|
||||
return 0 as ptr
|
||||
|
||||
@@ -119,15 +119,15 @@ func mem.alloc[size: i64] : ptr
|
||||
return must(mem.alloc?(size))
|
||||
|
||||
func mem.free[x: any] : void
|
||||
if !(x as ptr)
|
||||
if x == 0
|
||||
return 0
|
||||
|
||||
let blk = (x as ptr - MEM_BLOCK_SIZE) as mem.Block
|
||||
blk := (x as ptr - MEM_BLOCK_SIZE) as mem.Block
|
||||
blk->free = true
|
||||
|
||||
let next = blk->next
|
||||
next := blk->next
|
||||
if next as ptr && next->free
|
||||
let expected_next = (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block
|
||||
expected_next := (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block
|
||||
if expected_next as ptr == next as ptr
|
||||
blk->size = blk->size + MEM_BLOCK_SIZE + next->size
|
||||
blk->next = next->next
|
||||
@@ -136,9 +136,9 @@ func mem.free[x: any] : void
|
||||
if mem.read64(_builtin_heap_tail()) == next as ptr
|
||||
mem.write64(_builtin_heap_tail(), blk)
|
||||
|
||||
let prev = blk->prev
|
||||
prev := blk->prev
|
||||
if prev as ptr && prev->free
|
||||
let expected_blk = (prev as ptr + MEM_BLOCK_SIZE + prev->size) as mem.Block
|
||||
expected_blk := (prev as ptr + MEM_BLOCK_SIZE + prev->size) as mem.Block
|
||||
if expected_blk as ptr == blk as ptr
|
||||
prev->size = prev->size + MEM_BLOCK_SIZE + blk->size
|
||||
prev->next = blk->next
|
||||
@@ -148,7 +148,7 @@ func mem.free[x: any] : void
|
||||
mem.write64(_builtin_heap_tail(), prev)
|
||||
blk = prev
|
||||
|
||||
let block_total = blk->size + MEM_BLOCK_SIZE
|
||||
block_total := blk->size + MEM_BLOCK_SIZE
|
||||
if (blk as i64 & 4095) == 0 && (block_total & 4095) == 0
|
||||
if blk->prev as ptr
|
||||
blk->prev->next = blk->next
|
||||
@@ -175,16 +175,16 @@ func mem.realloc?[x: ptr, new_size: i64] : ptr
|
||||
|
||||
new_size = mem._align(new_size)
|
||||
|
||||
let blk = (x - MEM_BLOCK_SIZE) as mem.Block
|
||||
blk := (x - MEM_BLOCK_SIZE) as mem.Block
|
||||
if blk->size >= new_size
|
||||
mem._split_block(blk, new_size)
|
||||
return x
|
||||
|
||||
let next = blk->next
|
||||
let expected_next = (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block
|
||||
next := blk->next
|
||||
expected_next := (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block
|
||||
|
||||
if next as ptr && next->free && expected_next as ptr == next as ptr
|
||||
let combined = blk->size + MEM_BLOCK_SIZE + next->size
|
||||
combined := blk->size + MEM_BLOCK_SIZE + next->size
|
||||
if combined >= new_size
|
||||
blk->size = combined
|
||||
blk->next = next->next
|
||||
@@ -195,7 +195,7 @@ func mem.realloc?[x: ptr, new_size: i64] : ptr
|
||||
mem._split_block(blk, new_size)
|
||||
return x
|
||||
|
||||
let new_ptr = mem.alloc?(new_size)
|
||||
new_ptr := mem.alloc?(new_size)
|
||||
if err.check()
|
||||
return 0 as ptr
|
||||
|
||||
@@ -213,7 +213,7 @@ func mem.zero[x: ptr, size: i64] : void
|
||||
|
||||
func mem.copy[src: ptr, dst: ptr, n: i64] : void
|
||||
if dst > src
|
||||
let i = n - 1
|
||||
i := n - 1
|
||||
while i >= 0
|
||||
dst[i] = src[i]
|
||||
i = i - 1
|
||||
@@ -257,8 +257,8 @@ func mem.write64[x: ptr, d: any] : void
|
||||
_builtin_set64(x, d)
|
||||
|
||||
func io.printf[..] : void
|
||||
let s = _var_arg(0) as ptr
|
||||
let i = 1
|
||||
s := _var_arg(0) as ptr
|
||||
i := 1
|
||||
|
||||
while s[0]
|
||||
if s[0] == '%'
|
||||
@@ -304,29 +304,29 @@ func io.print_bool[x: bool] : void
|
||||
io.print("false")
|
||||
|
||||
func io.print_i64[x: i64] : void
|
||||
let s = str.from_i64(x)
|
||||
s := str.from_i64(x)
|
||||
io.print(s)
|
||||
mem.free(s)
|
||||
|
||||
func io.print_i64_hex[x: i64] : void
|
||||
let s = str.hex_from_i64(x)
|
||||
s := str.hex_from_i64(x)
|
||||
io.print(s)
|
||||
mem.free(s)
|
||||
|
||||
func io.println_i64[x: i64] : void
|
||||
let s = str.from_i64(x)
|
||||
s := str.from_i64(x)
|
||||
io.println(s)
|
||||
mem.free(s)
|
||||
|
||||
func io.read_char[] : u8
|
||||
let c = 0 as u8
|
||||
c := 0 as u8
|
||||
_builtin_syscall(SYS_read, 0, ^c, 1)
|
||||
return c
|
||||
|
||||
func io.read_line[]: str
|
||||
let MAX_SIZE = 60000
|
||||
let buffer = mem.alloc(MAX_SIZE + 1) as str
|
||||
let n = _builtin_syscall(SYS_read, 0, buffer, MAX_SIZE)
|
||||
MAX_SIZE := 60000
|
||||
buffer := mem.alloc(MAX_SIZE + 1) as str
|
||||
n := _builtin_syscall(SYS_read, 0, buffer, MAX_SIZE)
|
||||
if n < 0
|
||||
n = 0
|
||||
buffer[n] = 0
|
||||
@@ -339,7 +339,7 @@ struct io.Buffer
|
||||
|
||||
func io.Buffer.alloc?[size: i64] : io.Buffer
|
||||
err.clear()
|
||||
let buffer = new* io.Buffer
|
||||
buffer := new* io.Buffer
|
||||
buffer->size = size
|
||||
buffer->data = mem.alloc?(size)
|
||||
if err.check()
|
||||
@@ -358,34 +358,34 @@ const S_IFDIR = 0o040000
|
||||
const S_IFMT = 0o170000
|
||||
|
||||
func io.is_a_directory[path: str] : bool
|
||||
let st = mem.alloc(256) // it has 21 mixed-size fields so no struct for now
|
||||
st := mem.alloc(256) // it has 21 mixed-size fields so no struct for now
|
||||
|
||||
let rc = _builtin_syscall(SYS_newfstatat, -100, path, st, 0)
|
||||
rc := _builtin_syscall(SYS_newfstatat, -100, path, st, 0)
|
||||
if rc != 0
|
||||
mem.free(st)
|
||||
return false
|
||||
|
||||
let out: bool = (mem.read32(st + 24) & S_IFMT) == S_IFDIR
|
||||
out : bool = (mem.read32(st + 24) & S_IFMT) == S_IFDIR
|
||||
|
||||
mem.free(st)
|
||||
return out
|
||||
|
||||
func io.read_text_file?[path: str] : str
|
||||
err.clear()
|
||||
let fd = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
fd := _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
if fd < 0
|
||||
err.set(ERR_OPEN_FAILED, "io.read_text_file?: failed to open file")
|
||||
return 0 as str
|
||||
|
||||
let size = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
||||
size := _builtin_syscall(SYS_lseek, fd, 0, 2)
|
||||
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
||||
|
||||
let buffer = mem.alloc?(size + 1) as str
|
||||
buffer := mem.alloc?(size + 1) as str
|
||||
if err.check()
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
return 0 as str
|
||||
|
||||
let n = _builtin_syscall(SYS_read, fd, buffer, size)
|
||||
n := _builtin_syscall(SYS_read, fd, buffer, size)
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
|
||||
if n != size
|
||||
@@ -398,12 +398,12 @@ func io.read_text_file?[path: str] : str
|
||||
|
||||
func io.read_binary_file?[path: str] : io.Buffer
|
||||
err.clear()
|
||||
let fd = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
fd := _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
if fd < 0
|
||||
err.set(ERR_OPEN_FAILED, "io.read_binary_file?: failed to open file")
|
||||
return 0 as io.Buffer
|
||||
|
||||
let buf = new* io.Buffer
|
||||
buf := new* io.Buffer
|
||||
buf->size = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
||||
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
||||
|
||||
@@ -413,7 +413,7 @@ func io.read_binary_file?[path: str] : io.Buffer
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
return 0 as io.Buffer
|
||||
|
||||
let n = _builtin_syscall(SYS_read, fd, buf->data, buf->size)
|
||||
n := _builtin_syscall(SYS_read, fd, buf->data, buf->size)
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
|
||||
if n != buf->size
|
||||
@@ -428,31 +428,31 @@ func io.write_file?[path: str, content: str] : void
|
||||
|
||||
func io.write_binary_file?[path: str, content: ptr, size: i64] : void
|
||||
err.clear()
|
||||
let fd = _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644)
|
||||
fd := _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644)
|
||||
if fd < 0
|
||||
err.set(ERR_OPEN_FAILED, "io.write_binary_file?: failed to open file")
|
||||
return 0
|
||||
|
||||
let n = _builtin_syscall(SYS_write, fd, content, size)
|
||||
n := _builtin_syscall(SYS_write, fd, content, size)
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
if n != size
|
||||
err.set(ERR_WRITE_FAILED, "io.write_binary_file?: failed to write file")
|
||||
return 0
|
||||
|
||||
func str.len[s: str] : i64
|
||||
let i = 0
|
||||
i := 0
|
||||
while s[i]
|
||||
i = i + 1
|
||||
return i
|
||||
|
||||
func str.make_copy[s: str] : str
|
||||
let size = str.len(s) + 1
|
||||
let dup = mem.alloc(size) as str
|
||||
size := str.len(s) + 1
|
||||
dup := mem.alloc(size) as str
|
||||
mem.copy(s as ptr, dup as ptr, size)
|
||||
return dup
|
||||
|
||||
func str.equal[a: str, b: str] : bool
|
||||
let i = 0
|
||||
i := 0
|
||||
while a[i] != 0 && b[i] != 0
|
||||
if a[i] != b[i]
|
||||
return false
|
||||
@@ -481,9 +481,9 @@ func str.is_alphanumeric[x: u8] : bool
|
||||
return str.is_letter(x) || str.is_digit(x)
|
||||
|
||||
func str.concat[a: str, b: str] : str
|
||||
let a_len = str.len(a)
|
||||
let b_len = str.len(b)
|
||||
let out = mem.alloc(a_len + b_len + 1) as str
|
||||
a_len := str.len(a)
|
||||
b_len := str.len(b)
|
||||
out := mem.alloc(a_len + b_len + 1) as str
|
||||
mem.copy(a as ptr, out as ptr, a_len)
|
||||
mem.copy(b as ptr, out as ptr + a_len, b_len)
|
||||
out[a_len + b_len] = 0
|
||||
@@ -493,8 +493,8 @@ func str.contains[haystack: str, needle: str] : bool
|
||||
return str.find(haystack, needle) != -1
|
||||
|
||||
func str.find[haystack: str, needle: str] : i64
|
||||
let haystack_len = str.len(haystack)
|
||||
let needle_len = str.len(needle)
|
||||
haystack_len := str.len(haystack)
|
||||
needle_len := str.len(needle)
|
||||
|
||||
if needle_len == 0
|
||||
return 0
|
||||
@@ -503,7 +503,7 @@ func str.find[haystack: str, needle: str] : i64
|
||||
return -1
|
||||
|
||||
for i in 0..(haystack_len - needle_len + 1)
|
||||
let match = true
|
||||
match := true
|
||||
for j in 0..needle_len
|
||||
if haystack[i + j] != needle[j]
|
||||
match = false
|
||||
@@ -516,20 +516,20 @@ func str.substr[s: str, start: i64, length: i64] : str
|
||||
if start < 0 || length < 0 || start + length > str.len(s)
|
||||
panic("str.substr out of bounds")
|
||||
|
||||
let out = mem.alloc(length + 1) as str
|
||||
out := mem.alloc(length + 1) as str
|
||||
mem.copy(s as ptr + start, out as ptr, length)
|
||||
out[length] = 0
|
||||
return out
|
||||
|
||||
func str.trim[s: str] : str
|
||||
let len = str.len(s)
|
||||
len := str.len(s)
|
||||
if len == 0
|
||||
let out = mem.alloc(1) as str
|
||||
out := mem.alloc(1) as str
|
||||
out[0] = 0
|
||||
return out
|
||||
|
||||
let start = 0
|
||||
let end = len - 1
|
||||
start := 0
|
||||
end := len - 1
|
||||
|
||||
while start <= end && str.is_whitespace(s[start])
|
||||
start = start + 1
|
||||
@@ -540,9 +540,9 @@ func str.trim[s: str] : str
|
||||
return str.substr(s, start, end - start + 1)
|
||||
|
||||
func str.split[haystack: str, needle: str]: Array
|
||||
let haystack_len = str.len(haystack)
|
||||
let needle_len = str.len(needle)
|
||||
let result = []
|
||||
haystack_len := str.len(haystack)
|
||||
needle_len := str.len(needle)
|
||||
result := []
|
||||
|
||||
if !needle_len
|
||||
if !haystack_len
|
||||
@@ -552,11 +552,11 @@ func str.split[haystack: str, needle: str]: Array
|
||||
array.push(result, str.substr(haystack, i, 1))
|
||||
return result
|
||||
|
||||
let start = 0
|
||||
let i = 0
|
||||
start := 0
|
||||
i := 0
|
||||
while i < haystack_len
|
||||
if i <= haystack_len - needle_len
|
||||
let match = true
|
||||
match := true
|
||||
for j in 0..needle_len
|
||||
if haystack[i + j] != needle[j]
|
||||
match = false
|
||||
@@ -572,8 +572,8 @@ func str.split[haystack: str, needle: str]: Array
|
||||
return result
|
||||
|
||||
func str.reverse[s: str] : str
|
||||
let len = str.len(s)
|
||||
let out = mem.alloc(len + 1) as str
|
||||
len := str.len(s)
|
||||
out := mem.alloc(len + 1) as str
|
||||
|
||||
for i in 0..len
|
||||
out[i] = s[len - i - 1]
|
||||
@@ -582,19 +582,19 @@ func str.reverse[s: str] : str
|
||||
|
||||
func str.from_i64[n: i64] : str
|
||||
if n == 0
|
||||
let out = mem.alloc(2) as str
|
||||
out := mem.alloc(2) as str
|
||||
out[0] = '0'
|
||||
out[1] = 0
|
||||
return out
|
||||
|
||||
let neg: bool = n < 0
|
||||
neg : bool = n < 0
|
||||
if neg
|
||||
// negating MIN_I64 causes overflow
|
||||
if n == -9223372036854775808
|
||||
return str.make_copy("-9223372036854775808")
|
||||
n = -n
|
||||
let buf = mem.alloc(21) as str // enough to fit -MAX_I64
|
||||
let end = 20
|
||||
buf := mem.alloc(21) as str // enough to fit -MAX_I64
|
||||
end := 20
|
||||
buf[end] = 0
|
||||
end = end - 1
|
||||
while n > 0
|
||||
@@ -604,30 +604,30 @@ func str.from_i64[n: i64] : str
|
||||
if neg
|
||||
buf[end] = '-'
|
||||
end = end - 1
|
||||
let s = str.make_copy((buf as ptr + end + 1) as str)
|
||||
s := str.make_copy((buf as ptr + end + 1) as str)
|
||||
mem.free(buf)
|
||||
return s
|
||||
|
||||
func str.hex_from_i64[n: i64] : str
|
||||
let hex_chars = "0123456789abcdef"
|
||||
hex_chars := "0123456789abcdef"
|
||||
|
||||
if n == 0
|
||||
let out = mem.alloc(2) as str
|
||||
out := mem.alloc(2) as str
|
||||
out[0] = '0'
|
||||
out[1] = 0
|
||||
return out
|
||||
|
||||
let mask = (1 << 60) - 1
|
||||
let buf = mem.alloc(17) as str
|
||||
let len = 0
|
||||
mask := (1 << 60) - 1
|
||||
buf := mem.alloc(17) as str
|
||||
len := 0
|
||||
|
||||
while n != 0
|
||||
buf[len] = hex_chars[n & 15]
|
||||
n = (n >> 4) & mask
|
||||
len = len + 1
|
||||
|
||||
let out = mem.alloc(len + 1) as str
|
||||
let j = 0
|
||||
out := mem.alloc(len + 1) as str
|
||||
j := 0
|
||||
while j < len
|
||||
out[j] = buf[len - 1 - j]
|
||||
j = j + 1
|
||||
@@ -637,23 +637,23 @@ func str.hex_from_i64[n: i64] : str
|
||||
return out
|
||||
|
||||
func str.from_char[c: u8] : str
|
||||
let s = mem.alloc(2) as str
|
||||
s := mem.alloc(2) as str
|
||||
s[0] = c
|
||||
s[1] = 0
|
||||
return s
|
||||
|
||||
func str.parse_i64[s: str] : i64
|
||||
let len = str.len(s)
|
||||
let i = 0
|
||||
len := str.len(s)
|
||||
i := 0
|
||||
|
||||
let sign = 1
|
||||
sign := 1
|
||||
if i < len && s[i] == '-'
|
||||
sign = -1
|
||||
i = i + 1
|
||||
|
||||
let num = 0
|
||||
num := 0
|
||||
while i < len
|
||||
let d = s[i]
|
||||
d := s[i]
|
||||
if d < '0' || d > '9'
|
||||
break
|
||||
num = num * 10 + (d - '0')
|
||||
@@ -661,11 +661,11 @@ func str.parse_i64[s: str] : i64
|
||||
return num * sign
|
||||
|
||||
func str.hex_encode[s: str, s_len: i64] : str
|
||||
let hex_chars = "0123456789abcdef"
|
||||
let out = mem.alloc(s_len * 2 + 1) as str
|
||||
hex_chars := "0123456789abcdef"
|
||||
out := mem.alloc(s_len * 2 + 1) as str
|
||||
|
||||
for i in 0..s_len
|
||||
let b = s[i]
|
||||
b := s[i]
|
||||
out[i * 2] = hex_chars[(b >> 4) & 15]
|
||||
out[i * 2 + 1] = hex_chars[b & 15]
|
||||
|
||||
@@ -675,22 +675,22 @@ func str.hex_encode[s: str, s_len: i64] : str
|
||||
func str._hex_digit_to_int[d: u8] : u8
|
||||
if d >= '0' && d <= '9'
|
||||
return d - '0'
|
||||
let lower: u8 = d | 32
|
||||
lower : u8 = d | 32
|
||||
if lower >= 'a' && lower <= 'f'
|
||||
return lower - 'a' + 10
|
||||
panic("invalid hex digit passed to str._hex_digit_to_int")
|
||||
|
||||
func str.hex_decode[s: str] : str
|
||||
let s_len = str.len(s)
|
||||
s_len := str.len(s)
|
||||
if s_len % 2 != 0
|
||||
panic("invalid hex string passed to str.hex_decode")
|
||||
|
||||
let out_len = s_len / 2
|
||||
let out = mem.alloc(out_len + 1) as str
|
||||
out_len := s_len / 2
|
||||
out := mem.alloc(out_len + 1) as str
|
||||
|
||||
for i in 0..out_len
|
||||
let high = str._hex_digit_to_int(s[i * 2])
|
||||
let low = str._hex_digit_to_int(s[i * 2 + 1])
|
||||
high := str._hex_digit_to_int(s[i * 2])
|
||||
low := str._hex_digit_to_int(s[i * 2 + 1])
|
||||
out[i] = (high << 4) | low
|
||||
|
||||
out[out_len] = 0
|
||||
@@ -700,7 +700,7 @@ func math.gcd[a: i64, b: i64] : i64
|
||||
a = math.abs(a)
|
||||
b = math.abs(b)
|
||||
while b != 0
|
||||
let tmp = b
|
||||
tmp := b
|
||||
b = a % b
|
||||
a = tmp
|
||||
return a
|
||||
@@ -733,7 +733,7 @@ func math.sign[n: i64] : i64
|
||||
func math.pow[b: i64, e: i64] : i64
|
||||
if e < 0
|
||||
panic("negative exponent passed to math.pow")
|
||||
let out = 1
|
||||
out := 1
|
||||
for i in 0..e
|
||||
out = out * b
|
||||
return out
|
||||
@@ -747,8 +747,8 @@ func math.isqrt[n: i64] : i64
|
||||
if n == 0 || n == 1
|
||||
return n
|
||||
|
||||
let guess = n
|
||||
let next_guess = (guess + n / guess) / 2
|
||||
guess := n
|
||||
next_guess := (guess + n / guess) / 2
|
||||
|
||||
while next_guess < guess
|
||||
guess = next_guess
|
||||
@@ -764,7 +764,7 @@ func math.is_prime[n: i64]: bool
|
||||
if n % 2 == 0 || n % 3 == 0
|
||||
return false
|
||||
|
||||
let i = 5
|
||||
i := 5
|
||||
while i * i <= n
|
||||
if n % i == 0 || n % (i + 2) == 0
|
||||
return false
|
||||
@@ -780,7 +780,7 @@ func array.new[] : Array
|
||||
return new* Array
|
||||
|
||||
func array.new_preallocated_and_zeroed[size: i64] : Array
|
||||
let xs = new* Array
|
||||
xs := new* Array
|
||||
if size > 0
|
||||
xs->data = must(mem.realloc?(xs->data, size * 8)) as ptr
|
||||
mem.zero(xs->data, size * 8)
|
||||
@@ -800,7 +800,7 @@ func array.set[xs: Array, n: i64, x: any] : void
|
||||
|
||||
func array.push[xs: Array, x: any] : void
|
||||
if xs->size == xs->capacity
|
||||
let new_capacity = 4
|
||||
new_capacity := 4
|
||||
if xs->capacity != 0
|
||||
new_capacity = xs->capacity * 2
|
||||
xs->data = must(mem.realloc?(xs->data, new_capacity * 8))
|
||||
@@ -817,7 +817,7 @@ func array.free[xs: Array] : void
|
||||
func array.pop[xs: Array] : any
|
||||
if xs->size == 0
|
||||
panic("array.pop on empty array")
|
||||
let x: any = array.nth(xs, xs->size - 1)
|
||||
x : any = array.nth(xs, xs->size - 1)
|
||||
xs->size = xs->size - 1
|
||||
return x
|
||||
|
||||
@@ -825,13 +825,13 @@ func array.slice[xs: Array, start: i64, length: i64] : Array
|
||||
if start < 0 || length < 0 || start + length > xs->size
|
||||
panic("array.slice out of bounds")
|
||||
|
||||
let new_array = array.new_preallocated_and_zeroed(length)
|
||||
new_array := array.new_preallocated_and_zeroed(length)
|
||||
for i in 0..length
|
||||
array.set(new_array, i, array.nth(xs, start + i))
|
||||
return new_array
|
||||
|
||||
func array.concat[a: Array, b: Array] : Array
|
||||
let new_array = array.new_preallocated_and_zeroed(a->size + b->size)
|
||||
new_array := array.new_preallocated_and_zeroed(a->size + b->size)
|
||||
for i in 0..a->size
|
||||
array.set(new_array, i, array.nth(a, i))
|
||||
for i in 0..b->size
|
||||
@@ -849,13 +849,13 @@ struct HashMap._Node
|
||||
next: HashMap._Node
|
||||
|
||||
func HashMap.new[] : HashMap
|
||||
let map = new* HashMap
|
||||
map := new* HashMap
|
||||
map->table = array.new_preallocated_and_zeroed(HashMap._TABLE_SIZE)
|
||||
return map
|
||||
|
||||
func HashMap.insert[map: HashMap, key: str, value: any] : void
|
||||
let index = HashMap._djb2(key)
|
||||
let current: HashMap._Node = array.nth(map->table, index)
|
||||
index := HashMap._djb2(key)
|
||||
current : HashMap._Node = array.nth(map->table, index)
|
||||
|
||||
while current as ptr
|
||||
if str.equal(current->key, key)
|
||||
@@ -863,7 +863,7 @@ func HashMap.insert[map: HashMap, key: str, value: any] : void
|
||||
return 0
|
||||
current = current->next
|
||||
|
||||
let new_node = new* HashMap._Node
|
||||
new_node := new* HashMap._Node
|
||||
new_node->key = str.make_copy(key)
|
||||
new_node->value = value
|
||||
new_node->next = array.nth(map->table, index)
|
||||
@@ -871,8 +871,8 @@ func HashMap.insert[map: HashMap, key: str, value: any] : void
|
||||
|
||||
func HashMap.get?[map: HashMap, key: str] : any
|
||||
err.clear()
|
||||
let index = HashMap._djb2(key)
|
||||
let current: HashMap._Node = array.nth(map->table, index)
|
||||
index := HashMap._djb2(key)
|
||||
current : HashMap._Node = array.nth(map->table, index)
|
||||
|
||||
while current as ptr
|
||||
if str.equal(current->key, key)
|
||||
@@ -883,9 +883,9 @@ func HashMap.get?[map: HashMap, key: str] : any
|
||||
return 0
|
||||
|
||||
func HashMap.delete[map: HashMap, key: str] : void
|
||||
let index = HashMap._djb2(key)
|
||||
let current: HashMap._Node = array.nth(map->table, index)
|
||||
let prev = 0 as HashMap._Node
|
||||
index := HashMap._djb2(key)
|
||||
current : HashMap._Node = array.nth(map->table, index)
|
||||
prev := 0 as HashMap._Node
|
||||
|
||||
while current as ptr
|
||||
if str.equal(current->key, key)
|
||||
@@ -902,8 +902,8 @@ func HashMap.delete[map: HashMap, key: str] : void
|
||||
current = current->next
|
||||
|
||||
func HashMap._djb2[key: str] : i64
|
||||
let hash = 5381
|
||||
let key_len = str.len(key)
|
||||
hash := 5381
|
||||
key_len := str.len(key)
|
||||
for i in 0..key_len
|
||||
hash = ((hash << 5) + hash) + key[i]
|
||||
hash = (hash & 0x7fffffffffffffff) // prevent negative
|
||||
@@ -911,9 +911,9 @@ func HashMap._djb2[key: str] : i64
|
||||
|
||||
func HashMap.free[map: HashMap] : void
|
||||
for i in 0..HashMap._TABLE_SIZE
|
||||
let current: HashMap._Node = array.nth(map->table, i)
|
||||
current : HashMap._Node = array.nth(map->table, i)
|
||||
while current as ptr
|
||||
let tmp = current
|
||||
tmp := current
|
||||
current = current->next
|
||||
mem.free(tmp->key)
|
||||
mem.free(tmp)
|
||||
@@ -926,39 +926,39 @@ func alg.quicksort[arr: Array] : void
|
||||
|
||||
func alg._do_quicksort[arr: Array, low: i64, high: i64] : void
|
||||
if low < high
|
||||
let i = alg._partition(arr, low, high)
|
||||
i := alg._partition(arr, low, high)
|
||||
alg._do_quicksort(arr, low, i - 1)
|
||||
alg._do_quicksort(arr, i + 1, high)
|
||||
|
||||
func alg._partition[arr: Array, low: i64, high: i64] : i64
|
||||
let pivot: i64 = array.nth(arr, high)
|
||||
let i = low - 1
|
||||
pivot : i64 = array.nth(arr, high)
|
||||
i := low - 1
|
||||
for j in (low)..high
|
||||
if array.nth(arr, j) as i64 <= pivot
|
||||
i = i + 1
|
||||
let temp: i64 = array.nth(arr ,i)
|
||||
temp : i64 = array.nth(arr ,i)
|
||||
array.set(arr, i, array.nth(arr, j))
|
||||
array.set(arr, j, temp)
|
||||
let temp: i64 = array.nth(arr, i + 1)
|
||||
temp : i64 = array.nth(arr, i + 1)
|
||||
array.set(arr, i + 1, array.nth(arr, high))
|
||||
array.set(arr, high, temp)
|
||||
return i + 1
|
||||
|
||||
func alg.count[arr: Array, item: any] : i64
|
||||
let count = 0
|
||||
count := 0
|
||||
for i in 0..arr->size
|
||||
if array.nth(arr, i) == item
|
||||
count = count + 1
|
||||
return count
|
||||
|
||||
func alg.map[arr: Array, fn: fnptr] : Array
|
||||
let out = array.new_preallocated_and_zeroed(arr->size)
|
||||
out := array.new_preallocated_and_zeroed(arr->size)
|
||||
for i in 0..arr->size
|
||||
array.set(out, i, fn(array.nth(arr, i)))
|
||||
return out
|
||||
|
||||
func alg.filter[arr: Array, fn: fnptr] : Array
|
||||
let out = []
|
||||
out := []
|
||||
for i in 0..arr->size
|
||||
if fn(array.nth(arr, i))
|
||||
array.push(out, array.nth(arr, i))
|
||||
@@ -982,24 +982,24 @@ struct os.Timespec
|
||||
func os.sleep[ms: i64] : void
|
||||
if ms < 0
|
||||
panic("negative time passed to os.sleep")
|
||||
let req = new os.Timespec
|
||||
req := new os.Timespec
|
||||
req->tv_sec = ms / 1000
|
||||
req->tv_nsec = (ms % 1000) * 1000000
|
||||
_builtin_syscall(SYS_nanosleep, req, 0)
|
||||
|
||||
func os.urandom?[n: i64]: ptr
|
||||
err.clear()
|
||||
let buffer = mem.alloc(n)
|
||||
buffer := mem.alloc(n)
|
||||
if err.check()
|
||||
return 0 as ptr
|
||||
|
||||
let fd = _builtin_syscall(SYS_openat, -100, "/dev/urandom", 0, 0)
|
||||
fd := _builtin_syscall(SYS_openat, -100, "/dev/urandom", 0, 0)
|
||||
if fd < 0
|
||||
mem.free(buffer)
|
||||
err.set(ERR_OPEN_FAILED, "os.urandom?: failed to open /dev/urandom")
|
||||
return 0 as ptr
|
||||
|
||||
let bytes_read = _builtin_syscall(SYS_read, fd, buffer, n)
|
||||
bytes_read := _builtin_syscall(SYS_read, fd, buffer, n)
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
|
||||
if n != bytes_read
|
||||
@@ -1010,8 +1010,8 @@ func os.urandom?[n: i64]: ptr
|
||||
return buffer
|
||||
|
||||
func os.urandom_i64[]: i64
|
||||
let buffer: ptr = must(os.urandom?(8))
|
||||
let n = mem.read64(buffer)
|
||||
buffer : ptr = must(os.urandom?(8))
|
||||
n := mem.read64(buffer)
|
||||
mem.free(buffer)
|
||||
return n
|
||||
|
||||
@@ -1020,30 +1020,30 @@ struct os.Timeval
|
||||
tv_usec: i64
|
||||
|
||||
func os.time[] : i64
|
||||
let tv = new os.Timeval
|
||||
tv := new os.Timeval
|
||||
_builtin_syscall(SYS_gettimeofday, tv, 0)
|
||||
let out = tv->tv_sec * 1000 + tv->tv_usec / 1000
|
||||
out := tv->tv_sec * 1000 + tv->tv_usec / 1000
|
||||
return out
|
||||
|
||||
// voodoo magic
|
||||
func os.run_shell_command?[command: str] : i64
|
||||
err.clear()
|
||||
let pid = _builtin_syscall(SYS_fork)
|
||||
pid := _builtin_syscall(SYS_fork)
|
||||
if pid < 0
|
||||
err.set(ERR_SYSCALL_FAILED, "os.run_shell_command?: failed to fork")
|
||||
return -1
|
||||
|
||||
if pid == 0
|
||||
let argv = ["sh", "-c", command, 0] // gets freed after child process exits
|
||||
argv := ["sh", "-c", command, 0] // gets freed after child process exits
|
||||
_builtin_syscall(SYS_execve, "/bin/sh", argv->data, _builtin_environ())
|
||||
_builtin_syscall(SYS_exit, 1)
|
||||
else
|
||||
let status = 0
|
||||
let wp = _builtin_syscall(SYS_wait4, pid, ^status, 0, 0)
|
||||
status := 0
|
||||
wp := _builtin_syscall(SYS_wait4, pid, ^status, 0, 0)
|
||||
if wp < 0
|
||||
err.set(ERR_SYSCALL_FAILED, "os.run_shell_command?: wait() failed")
|
||||
return -1
|
||||
let st = status & 0xffffffff
|
||||
st := status & 0xffffffff
|
||||
|
||||
if (st & 0x7f) == 0
|
||||
return (st >> 8) & 0xff
|
||||
@@ -1052,15 +1052,15 @@ func os.run_shell_command?[command: str] : i64
|
||||
|
||||
func os.list_directory?[path: str] : Array
|
||||
err.clear()
|
||||
let fd = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
fd := _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
if fd < 0
|
||||
err.set(ERR_OPEN_FAILED, "os.list_directory?: failed to open dir")
|
||||
return 0 as Array
|
||||
|
||||
let files = []
|
||||
let buf = mem.alloc(1024)
|
||||
files := []
|
||||
buf := mem.alloc(1024)
|
||||
while true
|
||||
let n = _builtin_syscall(SYS_getdents64, fd, buf, 1024)
|
||||
n := _builtin_syscall(SYS_getdents64, fd, buf, 1024)
|
||||
if n < 0
|
||||
mem.free(buf)
|
||||
|
||||
@@ -1074,12 +1074,12 @@ func os.list_directory?[path: str] : Array
|
||||
else if n == 0
|
||||
break
|
||||
|
||||
let pos = 0
|
||||
pos := 0
|
||||
while pos < n
|
||||
let len = mem.read16(buf + pos + 16)
|
||||
let name: ptr = buf + pos + 19
|
||||
len := mem.read16(buf + pos + 16)
|
||||
name : ptr = buf + pos + 19
|
||||
if name[0]
|
||||
let skip = false
|
||||
skip := false
|
||||
// skip if name is exactly '.' or '..'
|
||||
if name[0] == '.'
|
||||
if name[1] == 0
|
||||
|
||||
@@ -40,7 +40,6 @@ pub enum TokenType {
|
||||
True,
|
||||
False,
|
||||
|
||||
KeywordLet,
|
||||
KeywordConst,
|
||||
KeywordIf,
|
||||
KeywordElse,
|
||||
@@ -357,7 +356,6 @@ impl Tokenizer {
|
||||
|
||||
let lexeme: String = self.source[self.start..self.current].iter().collect();
|
||||
self.add_token(match lexeme.as_str() {
|
||||
"let" => TokenType::KeywordLet,
|
||||
"const" => TokenType::KeywordConst,
|
||||
"if" => TokenType::KeywordIf,
|
||||
"else" => TokenType::KeywordElse,
|
||||
|
||||
@@ -89,7 +89,7 @@ impl<'a> TypeChecker<'a> {
|
||||
Stmt::Expression(expr) => {
|
||||
self.typecheck_expr(env, expr)?;
|
||||
}
|
||||
Stmt::Let {
|
||||
Stmt::Declare {
|
||||
name,
|
||||
var_type,
|
||||
initializer,
|
||||
|
||||
16
test.zr
16
test.zr
@@ -1,6 +1,6 @@
|
||||
func run_test[x: str] : void
|
||||
let build_blacklist = ["examples/puzzles", "examples/raylib.zr", "examples/chip8.zr", "examples/sqlite_todo.zr"]
|
||||
let run_blacklist = ["/aoc", "guess_number.zr", "tcp_server.zr", "udp_server.zr"]
|
||||
build_blacklist := ["examples/puzzles", "examples/raylib.zr", "examples/chip8.zr", "examples/sqlite_todo.zr"]
|
||||
run_blacklist := ["/aoc", "guess_number.zr", "tcp_server.zr", "udp_server.zr"]
|
||||
|
||||
for i in 0..build_blacklist->size
|
||||
if str.equal(x, array.nth(build_blacklist, i))
|
||||
@@ -9,10 +9,10 @@ func run_test[x: str] : void
|
||||
|
||||
io.printf("Building %s...\n", x)
|
||||
|
||||
let build_start_time = os.time()
|
||||
build_start_time := os.time()
|
||||
if must(os.run_shell_command?(str.concat("./target/release/zern ", x))) != 0
|
||||
os.exit(1)
|
||||
let build_end_time = os.time()
|
||||
build_end_time := os.time()
|
||||
|
||||
io.printf("%dms\n", build_end_time - build_start_time)
|
||||
|
||||
@@ -25,19 +25,19 @@ func run_test[x: str] : void
|
||||
|
||||
io.printf("Running %s...\n", x)
|
||||
|
||||
let run_cmd = "./out"
|
||||
run_cmd := "./out"
|
||||
if str.equal(x, "examples/curl.zr")
|
||||
run_cmd = str.concat(run_cmd, " http://example.com")
|
||||
|
||||
let run_start_time = os.time()
|
||||
run_start_time := os.time()
|
||||
if must(os.run_shell_command?(run_cmd)) != 0
|
||||
os.exit(1)
|
||||
let run_end_time = os.time()
|
||||
run_end_time := os.time()
|
||||
|
||||
io.printf("Running %s took %dms\n", x, run_end_time - run_start_time)
|
||||
|
||||
func run_directory[dir: str] : void
|
||||
let files: Array = must(os.list_directory?(dir))
|
||||
files : Array = must(os.list_directory?(dir))
|
||||
for i in 0..files->size
|
||||
run_test(str.concat(dir, array.nth(files, i)))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user