make types lowercase :)
This commit is contained in:
@@ -12,12 +12,12 @@ A very cool language
|
||||
|
||||
## Syntax
|
||||
```rust
|
||||
func main[] : I64
|
||||
let answer: I64 = math.abs(os.urandom()) % 100
|
||||
func main[] : i64
|
||||
let answer: i64 = math.abs(os.urandom()) % 100
|
||||
|
||||
while true
|
||||
io.println("Guess a number: ")
|
||||
let guess: I64 = io.read_line() |> str.trim() |> str.parse_i64()
|
||||
let guess: i64 = io.read_line() |> str.trim() |> str.parse_i64()
|
||||
|
||||
if guess == answer
|
||||
io.println("You win!")
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
func main[] : I64
|
||||
func main[] : i64
|
||||
// https://brainfuck.org/sierpinski.b
|
||||
let src: String = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
||||
let src_len: I64 = str.len(src)
|
||||
let i: I64 = 0
|
||||
let src: str = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
||||
let src_len: i64 = str.len(src)
|
||||
let i: i64 = 0
|
||||
|
||||
let memory: Ptr = mem.alloc(30000)
|
||||
let memory: ptr = mem.alloc(30000)
|
||||
mem.zero(memory, 30000)
|
||||
let p: I64 = 0
|
||||
let p: i64 = 0
|
||||
|
||||
while i < src_len
|
||||
let op: U8 = src[i]
|
||||
let op: u8 = 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: I64 = 0
|
||||
let opened: i64 = 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: I64 = 0
|
||||
let closed: i64 = 0
|
||||
while i >= 0 & !(src[i] == '[' & !closed)
|
||||
if src[i] == ']'
|
||||
closed = closed + 1
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
func main[argc: I64, argv: Ptr] : I64
|
||||
func main[argc: i64, argv: ptr] : i64
|
||||
if argc < 2
|
||||
dbg.panic("url missing")
|
||||
|
||||
let url: String = mem.read64(argv + 8)
|
||||
let url: str = mem.read64(argv + 8)
|
||||
|
||||
if str.len(url) <= 7
|
||||
dbg.panic("missing url scheme")
|
||||
@@ -10,25 +10,25 @@ func main[argc: I64, argv: Ptr] : I64
|
||||
if !str.equal(str.substr(url, 0, 7), "http://")
|
||||
dbg.panic("invalid url scheme")
|
||||
|
||||
let url_len: I64 = str.len(url)
|
||||
let host_start: I64 = 7
|
||||
let i: I64 = host_start
|
||||
let url_len: i64 = str.len(url)
|
||||
let host_start: i64 = 7
|
||||
let i: i64 = host_start
|
||||
while i < url_len
|
||||
if url[i] == '/'
|
||||
break
|
||||
i = i + 1
|
||||
|
||||
let host: String = str.substr(url, host_start, i - host_start)
|
||||
let path: String = "/"
|
||||
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)
|
||||
let s: i64 = net.connect(host, 80)
|
||||
if s < 0
|
||||
dbg.panic("failed to connect")
|
||||
|
||||
// very leaky
|
||||
let req: String = "GET "
|
||||
let req: str = "GET "
|
||||
req = str.concat(req, path)
|
||||
req = str.concat(req, " HTTP/1.0\r\nHost: ")
|
||||
req = str.concat(req, host)
|
||||
@@ -36,16 +36,16 @@ func main[argc: I64, argv: Ptr] : I64
|
||||
net.send(s, req, str.len(req))
|
||||
mem.free(req)
|
||||
|
||||
let header_buf: String = mem.alloc(8192)
|
||||
let header_size: I64 = 0
|
||||
let found: Bool = false
|
||||
let end_index: I64 = -1
|
||||
let header_buf: str = mem.alloc(8192)
|
||||
let header_size: i64 = 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)
|
||||
let n: i64 = net.read(s, header_buf + header_size, 8192 - header_size)
|
||||
if n <= 0
|
||||
break
|
||||
let current_size: I64 = header_size + n
|
||||
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
|
||||
@@ -59,9 +59,9 @@ func main[argc: I64, argv: Ptr] : I64
|
||||
io.print_sized(header_buf + end_index, header_size - end_index)
|
||||
mem.free(header_buf)
|
||||
|
||||
let buffer: Ptr = mem.alloc(4096)
|
||||
let buffer: ptr = mem.alloc(4096)
|
||||
while true
|
||||
let n: I64 = net.read(s, buffer, 4096)
|
||||
let n: i64 = net.read(s, buffer, 4096)
|
||||
if n <= 0
|
||||
break
|
||||
io.print_sized(buffer, n)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
func main[] : I64
|
||||
let a: I64 = 0
|
||||
let b: I64 = 1
|
||||
func main[] : i64
|
||||
let a: i64 = 0
|
||||
let b: i64 = 1
|
||||
|
||||
while a < 100000
|
||||
io.println_i64(a)
|
||||
let temp: I64 = b
|
||||
let temp: i64 = b
|
||||
b = a + b
|
||||
a = temp
|
||||
@@ -1,4 +1,4 @@
|
||||
func main[] : I64
|
||||
func main[] : i64
|
||||
for i in 1..40
|
||||
if i % 15 == 0
|
||||
io.println("FizzBuzz")
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
func main[] : I64
|
||||
let answer: I64 = math.abs(os.urandom()) % 100
|
||||
func main[] : i64
|
||||
let answer: i64 = math.abs(os.urandom()) % 100
|
||||
|
||||
while true
|
||||
io.println("Guess a number: ")
|
||||
let guess: I64 = io.read_line() |> str.trim() |> str.parse_i64()
|
||||
let guess: i64 = io.read_line() |> str.trim() |> str.parse_i64()
|
||||
|
||||
if guess == answer
|
||||
io.println("You win!")
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
func main[] : I64
|
||||
func main[] : i64
|
||||
io.println("Hello, World!")
|
||||
@@ -1,29 +1,29 @@
|
||||
func part1[l1: Array, l2: Array] : Void
|
||||
let out: I64 = 0
|
||||
func part1[l1: array, l2: array] : void
|
||||
let out: i64 = 0
|
||||
|
||||
for i in 0..array.size(l1)
|
||||
out = out + math.abs(array.nth(l1, i) - array.nth(l2, i))
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[l1: Array, l2: Array] : Void
|
||||
let out: I64 = 0
|
||||
func part2[l1: array, l2: array] : void
|
||||
let out: i64 = 0
|
||||
|
||||
for i in 0..array.size(l1)
|
||||
out = out + array.nth(l1, i) * alg.count(l2, array.nth(l1, i))
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : I64
|
||||
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||
func main[] : i64
|
||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||
|
||||
let l1: Array = []
|
||||
let l2: Array = []
|
||||
let l1: array = []
|
||||
let l2: array = []
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
let line: String = 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(l2, str.parse_i64(array.nth(parts, 1)))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func check[report: Array] : Bool
|
||||
let increasing: Bool = array.nth(report, 0) < array.nth(report, 1)
|
||||
func check[report: array] : bool
|
||||
let increasing: bool = array.nth(report, 0) < array.nth(report, 1)
|
||||
|
||||
for i in 0..array.size(report)-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: I64 = math.abs(array.nth(report, i) - array.nth(report, i + 1))
|
||||
let diff: i64 = 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: I64 = 0
|
||||
func part1[data: array] : void
|
||||
let out: i64 = 0
|
||||
|
||||
for i in 0..array.size(data)
|
||||
if check(array.nth(data, i))
|
||||
@@ -22,15 +22,15 @@ func part1[data: Array] : Void
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[data: Array] : Void
|
||||
let out: I64 = 0
|
||||
func part2[data: array] : void
|
||||
let out: i64 = 0
|
||||
|
||||
for i in 0..array.size(data)
|
||||
if check(array.nth(data, i))
|
||||
out = out + 1
|
||||
else
|
||||
for j in 0..array.size(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)))
|
||||
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)))
|
||||
|
||||
if check(sliced)
|
||||
out = out + 1
|
||||
@@ -38,14 +38,14 @@ func part2[data: Array] : Void
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : I64
|
||||
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||
func main[] : i64
|
||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||
|
||||
let data: Array = []
|
||||
let data: array = []
|
||||
for i in 0..array.size(lines)
|
||||
let line: String = 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)
|
||||
array.set(report, i, str.parse_i64(array.nth(report, i)))
|
||||
array.push(data, report)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
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))
|
||||
return false
|
||||
|
||||
@@ -13,8 +13,8 @@ func check[lines: Array, x: I64, y: I64, dx: I64, dy: I64] : Bool
|
||||
|
||||
return true
|
||||
|
||||
func part1[lines: Array] : Void
|
||||
let out: I64 = 0
|
||||
func part1[lines: array] : void
|
||||
let out: i64 = 0
|
||||
|
||||
for x in 0..array.size(lines)
|
||||
for y in 0..str.len(array.nth(lines, x))
|
||||
@@ -37,13 +37,13 @@ func part1[lines: Array] : Void
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[lines: Array] : Void
|
||||
let out: I64 = 0
|
||||
func part2[lines: array] : void
|
||||
let out: i64 = 0
|
||||
|
||||
for x in 1..array.size(lines)-1
|
||||
for y in 1..str.len(array.nth(lines, x))-1
|
||||
if array.nth(lines, x)[y] == 'A'
|
||||
let s: String = mem.alloc(5)
|
||||
let s: str = mem.alloc(5)
|
||||
str.set(s, 0, array.nth(lines, x - 1)[y - 1])
|
||||
str.set(s, 1, array.nth(lines, x + 1)[y - 1])
|
||||
str.set(s, 2, array.nth(lines, x + 1)[y + 1])
|
||||
@@ -55,8 +55,8 @@ func part2[lines: Array] : Void
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : I64
|
||||
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||
func main[] : i64
|
||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||
|
||||
part1(lines)
|
||||
part2(lines)
|
||||
@@ -1,69 +1,69 @@
|
||||
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)
|
||||
if array.nth(rules_left, k) == a & array.nth(rules_right, k) == b
|
||||
return true
|
||||
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 j in 0..i
|
||||
if rule_exists(rules_left, rules_right, array.nth(update, i), array.nth(update, j))
|
||||
return false
|
||||
return true
|
||||
|
||||
func sort_by_rules[update: Array, rules_left: Array, rules_right: Array] : Void
|
||||
let swapped: Bool = true
|
||||
func sort_by_rules[update: array, rules_left: array, rules_right: array] : void
|
||||
let swapped: bool = true
|
||||
|
||||
while swapped
|
||||
swapped = false
|
||||
for i in 0..array.size(update)-1
|
||||
let a: I64 = array.nth(update, i)
|
||||
let b: I64 = array.nth(update, i+1)
|
||||
let a: i64 = array.nth(update, i)
|
||||
let b: i64 = array.nth(update, i+1)
|
||||
if rule_exists(rules_left, rules_right, b, a)
|
||||
let tmp: I64 = a
|
||||
let tmp: i64 = 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: I64 = 0
|
||||
func part1[updates: array, rules_left: array, rules_right: array] : void
|
||||
let out: i64 = 0
|
||||
|
||||
for i in 0..array.size(updates)
|
||||
let update: Array = array.nth(updates, i)
|
||||
let update: array = array.nth(updates, i)
|
||||
if check(update, rules_left, rules_right)
|
||||
out = out + array.nth(update, array.size(update) / 2)
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[updates: Array, rules_left: Array, rules_right: Array] : Void
|
||||
let out: I64 = 0
|
||||
func part2[updates: array, rules_left: array, rules_right: array] : void
|
||||
let out: i64 = 0
|
||||
|
||||
for i in 0..array.size(updates)
|
||||
let update: Array = array.nth(updates, i)
|
||||
let 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, array.size(update) / 2)
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : I64
|
||||
let data: Array = io.read_file("input.txt") |> str.split("\n\n")
|
||||
func main[] : i64
|
||||
let data: array = io.read_file("input.txt") |> str.split("\n\n")
|
||||
|
||||
let rules_left: Array = []
|
||||
let rules_right: Array = []
|
||||
let rules_lines: Array = str.split(array.nth(data, 0), "\n")
|
||||
let rules_left: array = []
|
||||
let rules_right: array = []
|
||||
let rules_lines: array = str.split(array.nth(data, 0), "\n")
|
||||
for i in 0..array.size(rules_lines)
|
||||
let line: String = array.nth(rules_lines, i)
|
||||
let parts: Array = str.split(line, "|")
|
||||
let line: str = array.nth(rules_lines, i)
|
||||
let parts: array = 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: Array = []
|
||||
let updates_lines: Array = str.split(array.nth(data, 1), "\n")
|
||||
let updates: array = []
|
||||
let updates_lines: array = str.split(array.nth(data, 1), "\n")
|
||||
for i in 0..array.size(updates_lines)
|
||||
let line: String = array.nth(updates_lines, i)
|
||||
let xs: Array = str.split(line, ",")
|
||||
let line: str = array.nth(updates_lines, i)
|
||||
let xs: array = str.split(line, ",")
|
||||
|
||||
for i in 0..array.size(xs)
|
||||
array.set(xs, i, str.parse_i64(array.nth(xs, i)))
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
func concat[a: I64, b: I64] : I64
|
||||
let a_str: String = str.from_i64(a)
|
||||
let b_str: String = str.from_i64(b)
|
||||
let ab_str: String = str.concat(a_str, b_str)
|
||||
let out: I64 = str.parse_i64(ab_str)
|
||||
func concat[a: i64, b: i64] : i64
|
||||
let a_str: str = str.from_i64(a)
|
||||
let b_str: str = str.from_i64(b)
|
||||
let ab_str: str = str.concat(a_str, b_str)
|
||||
let out: i64 = str.parse_i64(ab_str)
|
||||
// without freeing the program works but leaks 2GB of memory :p
|
||||
mem.free(a_str)
|
||||
mem.free(b_str)
|
||||
mem.free(ab_str)
|
||||
return out
|
||||
|
||||
func solve[ops: Array, e: Array] : I64
|
||||
let n: I64 = array.size(array.nth(e, 1)) - 1
|
||||
let indices: Array = []
|
||||
func solve[ops: array, e: array] : i64
|
||||
let n: i64 = array.size(array.nth(e, 1)) - 1
|
||||
let indices: array = []
|
||||
for i in 0..n
|
||||
array.push(indices, 0)
|
||||
|
||||
while true
|
||||
let res: I64 = array.nth(array.nth(e, 1), 0)
|
||||
let res: i64 = array.nth(array.nth(e, 1), 0)
|
||||
for i in 0..n
|
||||
let op: String = array.nth(ops, array.nth(indices, i))
|
||||
let op: str = array.nth(ops, array.nth(indices, i))
|
||||
|
||||
if str.equal(op, "add")
|
||||
res = res + array.nth(array.nth(e, 1), i + 1)
|
||||
@@ -29,8 +29,8 @@ func solve[ops: Array, e: Array] : I64
|
||||
if res == array.nth(e, 0)
|
||||
return res
|
||||
|
||||
let done: Bool = true
|
||||
let i: I64 = n - 1
|
||||
let done: bool = true
|
||||
let i: i64 = n - 1
|
||||
|
||||
while i >= 0
|
||||
array.set(indices, i, array.nth(indices, i) + 1)
|
||||
@@ -43,31 +43,31 @@ func solve[ops: Array, e: Array] : I64
|
||||
if done
|
||||
return 0
|
||||
|
||||
func part1[equations: Array] : Void
|
||||
let out: I64 = 0
|
||||
func part1[equations: array] : void
|
||||
let out: i64 = 0
|
||||
|
||||
for i in 0..array.size(equations)
|
||||
out = out + solve(["add", "mul"], array.nth(equations, i))
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[equations: Array] : Void
|
||||
let out: I64 = 0
|
||||
func part2[equations: array] : void
|
||||
let out: i64 = 0
|
||||
|
||||
for i in 0..array.size(equations)
|
||||
out = out + solve(["add", "mul", "concat"], array.nth(equations, i))
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : I64
|
||||
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||
let equations: Array = []
|
||||
func main[] : i64
|
||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||
let equations: array = []
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
let line: String = array.nth(lines, i)
|
||||
let parts: Array = str.split(line, ": ")
|
||||
let line: str = array.nth(lines, i)
|
||||
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)
|
||||
array.set(xs, j, str.parse_i64(array.nth(xs, j)))
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
func part1[lines: Array] : Void
|
||||
let password: I64 = 0
|
||||
let dial: I64 = 50
|
||||
func part1[lines: array] : void
|
||||
let password: i64 = 0
|
||||
let dial: i64 = 50
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
let line: String = array.nth(lines, i)
|
||||
let dir: U8 = line[0]
|
||||
let n: I64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||
let line: str = array.nth(lines, i)
|
||||
let dir: u8 = line[0]
|
||||
let n: i64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||
|
||||
if dir == 'L'
|
||||
dial = dial - n
|
||||
@@ -21,14 +21,14 @@ func part1[lines: Array] : Void
|
||||
|
||||
io.println_i64(password)
|
||||
|
||||
func part2[lines: Array] : Void
|
||||
let password: I64 = 0
|
||||
let dial: I64 = 50
|
||||
func part2[lines: array] : void
|
||||
let password: i64 = 0
|
||||
let dial: i64 = 50
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
let line: String = array.nth(lines, i)
|
||||
let dir: U8 = line[0]
|
||||
let n: I64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||
let line: str = array.nth(lines, i)
|
||||
let dir: u8 = line[0]
|
||||
let n: i64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||
|
||||
if dir == 'L'
|
||||
for i in 0..n
|
||||
@@ -46,8 +46,8 @@ func part2[lines: Array] : Void
|
||||
|
||||
io.println_i64(password)
|
||||
|
||||
func main[] : I64
|
||||
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||
func main[] : i64
|
||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||
|
||||
part1(lines)
|
||||
part2(lines)
|
||||
@@ -1,20 +1,20 @@
|
||||
func part1_is_invalid_id[s: String] : Bool
|
||||
let len: I64 = str.len(s)
|
||||
func part1_is_invalid_id[s: str] : bool
|
||||
let len: i64 = str.len(s)
|
||||
if len % 2 != 0
|
||||
return false
|
||||
|
||||
let first: String = str.substr(s, 0, len / 2)
|
||||
let second: String = str.substr(s, len / 2, len / 2)
|
||||
let first: str = str.substr(s, 0, len / 2)
|
||||
let second: str = str.substr(s, len / 2, len / 2)
|
||||
|
||||
return str.equal(first, second)
|
||||
|
||||
func part1[ranges: Array] : Void
|
||||
let sum: I64 = 0
|
||||
func part1[ranges: array] : void
|
||||
let sum: i64 = 0
|
||||
|
||||
for i in 0..array.size(ranges)
|
||||
let parts: Array = array.nth(ranges, i) |> str.split("-")
|
||||
let start: I64 = array.nth(parts, 0) |> str.parse_i64()
|
||||
let end: I64 = array.nth(parts, 1) |> str.parse_i64()
|
||||
let parts: array = array.nth(ranges, i) |> str.split("-")
|
||||
let start: i64 = array.nth(parts, 0) |> str.parse_i64()
|
||||
let end: i64 = array.nth(parts, 1) |> str.parse_i64()
|
||||
|
||||
for n in (start)..end+1
|
||||
if part1_is_invalid_id(str.from_i64(n))
|
||||
@@ -23,29 +23,29 @@ func part1[ranges: Array] : Void
|
||||
io.println_i64(sum)
|
||||
|
||||
// had to cheat this one
|
||||
func part2_is_invalid_id[s: String] : Bool
|
||||
let len: I64 = str.len(s)
|
||||
func part2_is_invalid_id[s: str] : bool
|
||||
let len: i64 = str.len(s)
|
||||
if len < 2
|
||||
return false
|
||||
for div in 1..(len / 2 + 1)
|
||||
if len % div == 0
|
||||
let u: String = str.substr(s, 0, div)
|
||||
let is_repeat: Bool = true
|
||||
let u: str = str.substr(s, 0, div)
|
||||
let is_repeat: bool = true
|
||||
for k in 1..(len / div)
|
||||
let segment: String = str.substr(s, k * div, div)
|
||||
let segment: str = str.substr(s, k * div, div)
|
||||
if !str.equal(segment, u)
|
||||
is_repeat = false
|
||||
if is_repeat
|
||||
return true
|
||||
return false
|
||||
|
||||
func part2[ranges: Array] : Void
|
||||
let sum: I64 = 0
|
||||
func part2[ranges: array] : void
|
||||
let sum: i64 = 0
|
||||
|
||||
for i in 0..array.size(ranges)
|
||||
let parts: Array = array.nth(ranges, i) |> str.split("-")
|
||||
let start: I64 = array.nth(parts, 0) |> str.parse_i64()
|
||||
let end: I64 = array.nth(parts, 1) |> str.parse_i64()
|
||||
let parts: array = array.nth(ranges, i) |> str.split("-")
|
||||
let start: i64 = array.nth(parts, 0) |> str.parse_i64()
|
||||
let end: i64 = array.nth(parts, 1) |> str.parse_i64()
|
||||
|
||||
for n in (start)..end+1
|
||||
if part2_is_invalid_id(str.from_i64(n))
|
||||
@@ -53,8 +53,8 @@ func part2[ranges: Array] : Void
|
||||
|
||||
io.println_i64(sum)
|
||||
|
||||
func main[] : I64
|
||||
let ranges: Array = io.read_file("input.txt") |> str.split(",")
|
||||
func main[] : i64
|
||||
let ranges: array = io.read_file("input.txt") |> str.split(",")
|
||||
|
||||
part1(ranges)
|
||||
part2(ranges)
|
||||
@@ -1,17 +1,17 @@
|
||||
func part1[lines: Array] : Void
|
||||
let sum: I64 = 0
|
||||
func part1[lines: array] : void
|
||||
let sum: i64 = 0
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
let line: String = array.nth(lines, i)
|
||||
let line: str = array.nth(lines, i)
|
||||
|
||||
let largest: I64 = 0
|
||||
let largest: i64 = 0
|
||||
for j in 0..str.len(line)
|
||||
for k in (j+1)..str.len(line)
|
||||
let s: String = mem.alloc(3)
|
||||
let s: str = mem.alloc(3)
|
||||
str.set(s, 0, line[j])
|
||||
str.set(s, 1, line[k])
|
||||
str.set(s, 2, 0)
|
||||
let n: I64 = str.parse_i64(s)
|
||||
let n: i64 = str.parse_i64(s)
|
||||
if n > largest
|
||||
largest = n
|
||||
|
||||
@@ -19,13 +19,13 @@ func part1[lines: Array] : Void
|
||||
io.println_i64(sum)
|
||||
|
||||
// had to cheat this one
|
||||
func part2_rec[bank: String, start: I64, remaining: I64] : I64
|
||||
let largest: I64 = 0
|
||||
let largest_idx: I64 = start
|
||||
let len: I64 = str.len(bank)
|
||||
func part2_rec[bank: str, start: i64, remaining: i64] : i64
|
||||
let largest: i64 = 0
|
||||
let largest_idx: i64 = start
|
||||
let len: i64 = str.len(bank)
|
||||
|
||||
for i in (start)..(len-remaining+1)
|
||||
let v: I64 = bank[i] - '0'
|
||||
let v: i64 = bank[i] - '0'
|
||||
if v > largest
|
||||
largest = v
|
||||
largest_idx = i
|
||||
@@ -35,17 +35,17 @@ func part2_rec[bank: String, start: I64, remaining: I64] : I64
|
||||
else
|
||||
return largest
|
||||
|
||||
func part2[lines: Array] : Void
|
||||
let sum: I64 = 0
|
||||
func part2[lines: array] : void
|
||||
let sum: i64 = 0
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
let line: String = array.nth(lines, i)
|
||||
let line: str = array.nth(lines, i)
|
||||
|
||||
sum = sum + part2_rec(line, 0, 12)
|
||||
io.println_i64(sum)
|
||||
|
||||
func main[] : I64
|
||||
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||
func main[] : i64
|
||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||
|
||||
part1(lines)
|
||||
part2(lines)
|
||||
@@ -1,5 +1,5 @@
|
||||
func main[] : I64
|
||||
let sum: I64 = 0
|
||||
func main[] : i64
|
||||
let sum: i64 = 0
|
||||
|
||||
for i in 0..2000000
|
||||
if math.is_prime(i)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func num_divisors[n: I64] : I64
|
||||
let end: I64 = math.isqrt(n)
|
||||
func num_divisors[n: i64] : i64
|
||||
let end: i64 = math.isqrt(n)
|
||||
|
||||
let out: I64 = 0
|
||||
let out: i64 = 0
|
||||
for i in 1..end+1
|
||||
if n % i == 0
|
||||
out = out + 2
|
||||
@@ -10,9 +10,9 @@ func num_divisors[n: I64] : I64
|
||||
out = out - 1
|
||||
return out
|
||||
|
||||
func main[] : I64
|
||||
let n: I64 = 0
|
||||
let i: I64 = 1
|
||||
func main[] : i64
|
||||
let n: i64 = 0
|
||||
let i: i64 = 1
|
||||
while true
|
||||
n = n + i
|
||||
if num_divisors(n) > 500
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
func main[] : I64
|
||||
func main[] : i64
|
||||
// leaks a bit of memory but looks very cool
|
||||
37107287533 + 46376937677 + 74324986199 + 91942213363 + 23067588207 + 89261670696 + 28112879812 + 44274228917 + 47451445736 + 70386486105 + 62176457141 + 64906352462 + 92575867718 + 58203565325 + 80181199384 + 35398664372 + 86515506006 + 71693888707 + 54370070576 + 53282654108 + 36123272525 + 45876576172 + 17423706905 + 81142660418 + 51934325451 + 62467221648 + 15732444386 + 55037687525 + 18336384825 + 80386287592 + 78182833757 + 16726320100 + 48403098129 + 87086987551 + 59959406895 + 69793950679 + 41052684708 + 65378607361 + 35829035317 + 94953759765 + 88902802571 + 25267680276 + 36270218540 + 24074486908 + 91430288197 + 34413065578 + 23053081172 + 11487696932 + 63783299490 + 67720186971 + 95548255300 + 76085327132 + 37774242535 + 23701913275 + 29798860272 + 18495701454 + 38298203783 + 34829543829 + 40957953066 + 29746152185 + 41698116222 + 62467957194 + 23189706772 + 86188088225 + 11306739708 + 82959174767 + 97623331044 + 42846280183 + 55121603546 + 32238195734 + 75506164965 + 62177842752 + 32924185707 + 99518671430 + 73267460800 + 76841822524 + 97142617910 + 87783646182 + 10848802521 + 71329612474 + 62184073572 + 66627891981 + 60661826293 + 85786944089 + 66024396409 + 64913982680 + 16730939319 + 94809377245 + 78639167021 + 15368713711 + 40789923115 + 44889911501 + 41503128880 + 81234880673 + 82616570773 + 22918802058 + 77158542502 + 72107838435 + 20849603980 + 53503534226
|
||||
|> str.from_i64()
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
func collatz_num[n: I64] : I64
|
||||
func collatz_num[n: i64] : i64
|
||||
if n % 2 == 0
|
||||
return n / 2
|
||||
return n * 3 + 1
|
||||
|
||||
func collatz_seq[n: I64]: I64
|
||||
let i: I64 = 1
|
||||
func collatz_seq[n: i64]: i64
|
||||
let i: i64 = 1
|
||||
while n != 1
|
||||
n = collatz_num(n)
|
||||
i = i + 1
|
||||
return i
|
||||
|
||||
func main[] : I64
|
||||
let max: I64 = 0
|
||||
let max_index: I64 = 0
|
||||
func main[] : i64
|
||||
let max: i64 = 0
|
||||
let max_index: i64 = 0
|
||||
|
||||
for i in 1..1000000
|
||||
let seq: I64 = collatz_seq(i)
|
||||
let seq: i64 = collatz_seq(i)
|
||||
if seq > max
|
||||
max = seq
|
||||
max_index = i
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : I64
|
||||
let n: I64 = 40
|
||||
let r: I64 = 20
|
||||
let out: I64 = 1
|
||||
func main[] : i64
|
||||
let n: i64 = 40
|
||||
let r: i64 = 20
|
||||
let out: i64 = 1
|
||||
|
||||
for i in 1..r+1
|
||||
out = out * (n - (r - i)) / i
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func main[] : I64
|
||||
let sum: I64 = 0
|
||||
func main[] : i64
|
||||
let sum: i64 = 0
|
||||
|
||||
for i in 0..1000
|
||||
if i % 5 == 0 | i % 3 == 0
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
func main[] : I64
|
||||
let sum: I64 = 0
|
||||
let a: I64 = 0
|
||||
let b: I64 = 1
|
||||
func main[] : i64
|
||||
let sum: i64 = 0
|
||||
let a: i64 = 0
|
||||
let b: i64 = 1
|
||||
|
||||
while a < 4000000
|
||||
if a % 2 == 0
|
||||
sum = sum + a
|
||||
let temp: I64 = b
|
||||
let temp: i64 = b
|
||||
b = a + b
|
||||
a = temp
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
func main[] : I64
|
||||
let n: I64 = 600851475143
|
||||
let f: I64 = 2
|
||||
func main[] : i64
|
||||
let n: i64 = 600851475143
|
||||
let f: i64 = 2
|
||||
|
||||
while f * f <= n
|
||||
if n % f == 0
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
func main[] : I64
|
||||
let out: I64 = 0
|
||||
func main[] : i64
|
||||
let out: i64 = 0
|
||||
|
||||
for a in 500..1000
|
||||
for b in 500..1000
|
||||
if a * b > out
|
||||
let s: String = str.from_i64(a * b)
|
||||
let s_rev: String = str.reverse(s)
|
||||
let s: str = str.from_i64(a * b)
|
||||
let s_rev: str = str.reverse(s)
|
||||
if str.equal(s, s_rev)
|
||||
out = a * b
|
||||
mem.free(s)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func main[] : I64
|
||||
let out: I64 = 1
|
||||
func main[] : i64
|
||||
let out: i64 = 1
|
||||
|
||||
for i in 1..21
|
||||
out = math.lcm(out, i)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
func main[] : I64
|
||||
let sum_of_squares: I64 = 0
|
||||
func main[] : i64
|
||||
let sum_of_squares: i64 = 0
|
||||
for i in 1..101
|
||||
sum_of_squares = sum_of_squares + i * i
|
||||
|
||||
let square_of_sum: I64 = 0
|
||||
let square_of_sum: i64 = 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: I64 = 0
|
||||
func main[] : i64
|
||||
let found: i64 = 0
|
||||
|
||||
let i: I64 = 1
|
||||
let i: i64 = 1
|
||||
while true
|
||||
if math.is_prime(i)
|
||||
found = found + 1
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
func main[] : I64
|
||||
let n: String = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
|
||||
func main[] : i64
|
||||
let n: str = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
|
||||
|
||||
let out: I64 = 0
|
||||
let max: I64 = str.len(n) - 13
|
||||
let out: i64 = 0
|
||||
let max: i64 = str.len(n) - 13
|
||||
for i in 0..max
|
||||
let s: I64 = 1
|
||||
let j: I64 = 0
|
||||
let s: i64 = 1
|
||||
let j: i64 = 0
|
||||
while j < 13
|
||||
s = s * (n[i + j] - '0')
|
||||
j = j + 1
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : I64
|
||||
func main[] : i64
|
||||
for a in 1..1000
|
||||
for b in 1..1000
|
||||
let c: I64 = 1000 - b - a
|
||||
let c: i64 = 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 arr: Array = []
|
||||
func main[] : i64
|
||||
let arr: array = []
|
||||
for i in 0..10
|
||||
array.push(arr, math.abs(os.urandom() % 1000))
|
||||
|
||||
|
||||
@@ -10,9 +10,9 @@ extern CloseWindow
|
||||
extern DrawRectangle
|
||||
extern IsKeyDown
|
||||
|
||||
func main[] : I64
|
||||
let x: I64 = 200
|
||||
let y: I64 = 200
|
||||
func main[] : i64
|
||||
let x: i64 = 200
|
||||
let y: i64 = 200
|
||||
|
||||
InitWindow(800, 600, "Hello, World!")
|
||||
SetTargetFPS(60)
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
func rule110_step[state: Array] : Array
|
||||
let new_state: Array = []
|
||||
let state_len: I64 = array.size(state)
|
||||
func rule110_step[state: array] : array
|
||||
let new_state: array = []
|
||||
let state_len: i64 = array.size(state)
|
||||
|
||||
for i in 0..state_len
|
||||
let left: Bool = false
|
||||
let left: bool = false
|
||||
if i - 1 >= 0
|
||||
left = array.nth(state, i - 1)
|
||||
let center: Bool = array.nth(state, i)
|
||||
let right: Bool = false
|
||||
let center: bool = array.nth(state, i)
|
||||
let right: bool = false
|
||||
if i + 1 < state_len
|
||||
right = array.nth(state, i + 1)
|
||||
|
||||
@@ -15,7 +15,7 @@ func rule110_step[state: Array] : Array
|
||||
|
||||
return new_state
|
||||
|
||||
func print_state[state: Array]: Void
|
||||
func print_state[state: array]: void
|
||||
for i in 0..array.size(state)
|
||||
if array.nth(state, i)
|
||||
io.print_char('#')
|
||||
@@ -23,10 +23,10 @@ func print_state[state: Array]: Void
|
||||
io.print_char(' ')
|
||||
io.println("")
|
||||
|
||||
func main[] : I64
|
||||
let SIZE: I64 = 60
|
||||
func main[] : i64
|
||||
let SIZE: i64 = 60
|
||||
|
||||
let state: Array = []
|
||||
let state: array = []
|
||||
for i in 0..SIZE
|
||||
array.push(state, false)
|
||||
array.push(state, true)
|
||||
|
||||
@@ -10,10 +10,10 @@ extern sqlite3_column_int
|
||||
extern sqlite3_column_text
|
||||
extern sqlite3_finalize
|
||||
|
||||
func main[] : I64
|
||||
let rc: I64 = 0
|
||||
let db: I64 = 0
|
||||
let stmt: I64 = 0
|
||||
func main[] : i64
|
||||
let rc: i64 = 0
|
||||
let db: i64 = 0
|
||||
let stmt: i64 = 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: I64 = io.read_line() |> str.parse_i64()
|
||||
let choice: i64 = 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: I64 = sqlite3_column_int(stmt, 0)
|
||||
let task: String = sqlite3_column_text(stmt, 1)
|
||||
let id: i64 = sqlite3_column_int(stmt, 0)
|
||||
let task: str = 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: String = io.read_line() |> str.trim()
|
||||
let task: str = 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: I64 = io.read_line() |> str.parse_i64()
|
||||
let id: i64 = 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,13 +1,13 @@
|
||||
func main[] : I64
|
||||
let host: I64 = net.pack_addr(127, 0, 0, 1)
|
||||
let s: I64 = net.listen(host, 8000)
|
||||
func main[] : i64
|
||||
let host: i64 = net.pack_addr(127, 0, 0, 1)
|
||||
let s: i64 = net.listen(host, 8000)
|
||||
|
||||
let resp: String = mem.alloc(60000)
|
||||
let resp: str = mem.alloc(60000)
|
||||
while true
|
||||
let conn: I64 = net.accept(s)
|
||||
let conn: i64 = net.accept(s)
|
||||
if conn < 0
|
||||
continue
|
||||
|
||||
let n: I64 = net.read(conn, resp, 60000)
|
||||
let n: i64 = net.read(conn, resp, 60000)
|
||||
net.send(conn, resp, n)
|
||||
net.close(conn)
|
||||
@@ -12,23 +12,23 @@ extern XCreateGC
|
||||
extern XDefaultScreen
|
||||
extern XDrawString
|
||||
|
||||
func main[] : I64
|
||||
func main[] : i64
|
||||
|
||||
let dpy: Ptr = XOpenDisplay(0)
|
||||
let screen: Ptr = XDefaultScreen(dpy)
|
||||
let dpy: ptr = XOpenDisplay(0)
|
||||
let screen: ptr = XDefaultScreen(dpy)
|
||||
|
||||
let white: Ptr = XWhitePixel(dpy, screen)
|
||||
let black: Ptr = XBlackPixel(dpy, screen)
|
||||
let white: ptr = XWhitePixel(dpy, screen)
|
||||
let black: ptr = XBlackPixel(dpy, screen)
|
||||
|
||||
let win: Ptr = XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy), 0, 0, 200, 100, 0, black, white)
|
||||
let win: ptr = XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy), 0, 0, 200, 100, 0, black, white)
|
||||
|
||||
XSelectInput(dpy, win, 1 << 15)
|
||||
XMapWindow(dpy, win)
|
||||
|
||||
let gc: Ptr = XCreateGC(dpy, win, 0, 0)
|
||||
let gc: ptr = XCreateGC(dpy, win, 0, 0)
|
||||
XSetForeground(dpy, gc, black)
|
||||
|
||||
let ev: Ptr = mem.alloc(256)
|
||||
let ev: ptr = mem.alloc(256)
|
||||
|
||||
while true
|
||||
XNextEvent(dpy, ev)
|
||||
|
||||
@@ -69,8 +69,8 @@ impl Analyzer {
|
||||
body,
|
||||
exported: _,
|
||||
} => {
|
||||
if name.lexeme == "main" && return_type.lexeme != "I64" {
|
||||
return error!(&name.loc, "main must return I64");
|
||||
if name.lexeme == "main" && return_type.lexeme != "i64" {
|
||||
return error!(&name.loc, "main must return i64");
|
||||
}
|
||||
|
||||
self.analyze_stmt(body)?;
|
||||
|
||||
@@ -269,7 +269,7 @@ _builtin_environ:
|
||||
env.loop_end_label = self.label();
|
||||
|
||||
env.push_scope();
|
||||
let offset = env.define_var(var.lexeme, "I64".into());
|
||||
let offset = env.define_var(var.lexeme, "i64".into());
|
||||
|
||||
self.compile_expr(env, start)?;
|
||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
||||
|
||||
@@ -78,7 +78,7 @@ pub enum Expr {
|
||||
}
|
||||
|
||||
// TODO: currently they are all just 8 byte values
|
||||
static TYPES: [&str; 7] = ["Void", "U8", "I64", "String", "Bool", "Ptr", "Array"];
|
||||
static TYPES: [&str; 7] = ["void", "u8", "i64", "str", "bool", "ptr", "array"];
|
||||
|
||||
pub struct Parser {
|
||||
tokens: Vec<Token>,
|
||||
|
||||
344
src/std.zr
344
src/std.zr
@@ -3,136 +3,136 @@ extern realloc
|
||||
extern free
|
||||
extern gethostbyname
|
||||
|
||||
func dbg.panic[msg: String] : Void
|
||||
func dbg.panic[msg: str] : void
|
||||
io.print("PANIC: ")
|
||||
io.println(msg)
|
||||
0/0 // crashes program which is kinda better since you get a gdb backtrace
|
||||
os.exit(1)
|
||||
|
||||
func mem.alloc[x: I64] : Ptr
|
||||
func mem.alloc[x: i64] : ptr
|
||||
return malloc(x)
|
||||
|
||||
func mem.free[x: Ptr] : Void
|
||||
func mem.free[x: ptr] : void
|
||||
free(x)
|
||||
|
||||
func mem.zero[x: I64, size: I64] : Void
|
||||
func mem.zero[x: i64, size: i64] : void
|
||||
for i in 0..size
|
||||
mem.write8(x + i, 0)
|
||||
|
||||
func mem.read8[x: Ptr] : U8
|
||||
func mem.read8[x: ptr] : u8
|
||||
return _builtin_read8(x)
|
||||
|
||||
func mem.read16[x: Ptr] : I64
|
||||
func mem.read16[x: ptr] : i64
|
||||
return _builtin_read16(x)
|
||||
|
||||
func mem.read32[x: Ptr] : I64
|
||||
func mem.read32[x: ptr] : i64
|
||||
return _builtin_read32(x)
|
||||
|
||||
func mem.read64[x: Ptr] : I64
|
||||
func mem.read64[x: ptr] : i64
|
||||
return _builtin_read64(x)
|
||||
|
||||
func mem.write8[x: Ptr, d: U8] : Void
|
||||
func mem.write8[x: ptr, d: u8] : void
|
||||
_builtin_set8(x, d)
|
||||
|
||||
func mem.write64[x: Ptr, d: I64] : Void
|
||||
func mem.write64[x: ptr, d: i64] : void
|
||||
_builtin_set64(x, d)
|
||||
|
||||
func io.print[x: String] : Void
|
||||
func io.print[x: str] : void
|
||||
_builtin_syscall(1, 1, x, str.len(x))
|
||||
|
||||
func io.println[x: String] : Void
|
||||
func io.println[x: str] : void
|
||||
io.print(x)
|
||||
io.print("\n")
|
||||
|
||||
func io.print_sized[x: String, size: I64] : Void
|
||||
func io.print_sized[x: str, size: i64] : void
|
||||
_builtin_syscall(1, 1, x, size)
|
||||
|
||||
func io.print_char[x: U8] : Void
|
||||
let s: String = mem.alloc(1)
|
||||
func io.print_char[x: u8] : void
|
||||
let s: str = mem.alloc(1)
|
||||
str.set(s, 0, x)
|
||||
_builtin_syscall(1, 1, s, 1)
|
||||
mem.free(s)
|
||||
|
||||
func io.print_i64[x: I64] : Void
|
||||
let s: String = str.from_i64(x)
|
||||
func io.print_i64[x: i64] : void
|
||||
let s: str = str.from_i64(x)
|
||||
io.print(s)
|
||||
mem.free(s)
|
||||
|
||||
func io.println_i64[x: I64] : Void
|
||||
let s: String = str.from_i64(x)
|
||||
func io.println_i64[x: i64] : void
|
||||
let s: str = str.from_i64(x)
|
||||
io.println(s)
|
||||
mem.free(s)
|
||||
|
||||
func io.read_char[] : U8
|
||||
let s: String = mem.alloc(1)
|
||||
func io.read_char[] : u8
|
||||
let s: str = mem.alloc(1)
|
||||
str.set(s, 0, 0)
|
||||
_builtin_syscall(0, 0, s, 1)
|
||||
let c: U8 = s[0]
|
||||
let c: u8 = s[0]
|
||||
mem.free(s)
|
||||
return c
|
||||
|
||||
func io.read_line[]: String
|
||||
let MAX_SIZE: I64 = 60000
|
||||
let buffer: String = mem.alloc(MAX_SIZE + 1)
|
||||
let n: I64 = _builtin_syscall(0, 0, buffer, MAX_SIZE)
|
||||
func io.read_line[]: str
|
||||
let MAX_SIZE: i64 = 60000
|
||||
let buffer: str = mem.alloc(MAX_SIZE + 1)
|
||||
let n: i64 = _builtin_syscall(0, 0, buffer, MAX_SIZE)
|
||||
if n < 0
|
||||
return ""
|
||||
str.set(buffer, n, 0)
|
||||
return buffer
|
||||
|
||||
func io.read_file[path: String]: String
|
||||
let fd: I64 = _builtin_syscall(257, -100, path, 0, 0) // openat
|
||||
func io.read_file[path: str]: str
|
||||
let fd: i64 = _builtin_syscall(257, -100, path, 0, 0) // openat
|
||||
if fd <= 0
|
||||
dbg.panic("failed to open file")
|
||||
|
||||
let size: I64 = _builtin_syscall(8, fd, 0, 2) // lseek to the end
|
||||
let size: i64 = _builtin_syscall(8, fd, 0, 2) // lseek to the end
|
||||
_builtin_syscall(8, fd, 0, 0) // lseek back to start
|
||||
|
||||
let buffer: String = mem.alloc(size + 1)
|
||||
let n: I64 = _builtin_syscall(0, fd, buffer, size) // read
|
||||
let buffer: str = mem.alloc(size + 1)
|
||||
let n: i64 = _builtin_syscall(0, fd, buffer, size) // read
|
||||
str.set(buffer, n, 0)
|
||||
_builtin_syscall(3, fd) // close
|
||||
return buffer
|
||||
|
||||
func io.write_file[path: String, content: String] : Void
|
||||
let fd: Ptr = _builtin_syscall(257, -100, path, 0x241, 0o644) // openat
|
||||
func io.write_file[path: str, content: str] : void
|
||||
let fd: ptr = _builtin_syscall(257, -100, path, 0x241, 0o644) // openat
|
||||
if fd < 0
|
||||
dbg.panic("failed to open file")
|
||||
|
||||
_builtin_syscall(1, fd, content, str.len(content)) // write
|
||||
_builtin_syscall(3, fd) // close
|
||||
|
||||
func str.len[s: String] : I64
|
||||
let i: I64 = 0
|
||||
func str.len[s: str] : i64
|
||||
let i: i64 = 0
|
||||
while mem.read8(s + i)
|
||||
i = i + 1
|
||||
return i
|
||||
|
||||
func str.copy[s: String] : String
|
||||
let size: I64 = str.len(s) + 1
|
||||
let dup: String = mem.alloc(size)
|
||||
func str.copy[s: str] : str
|
||||
let size: i64 = str.len(s) + 1
|
||||
let dup: str = mem.alloc(size)
|
||||
for i in 0..size
|
||||
str.set(dup, i, s[i])
|
||||
return dup
|
||||
|
||||
func str.set[s: String, n: I64, c: U8] : Void
|
||||
func str.set[s: str, n: i64, c: u8] : void
|
||||
mem.write8(s + n, c)
|
||||
|
||||
func str.equal[a: String, b: String] : Bool
|
||||
let i: I64 = 0
|
||||
func str.equal[a: str, b: str] : bool
|
||||
let i: i64 = 0
|
||||
while a[i] != 0 & b[i] != 0
|
||||
if a[i] != b[i]
|
||||
return false
|
||||
i = i + 1
|
||||
return a[i] == b[i]
|
||||
|
||||
func str.is_whitespace[x: U8] : Bool
|
||||
func str.is_whitespace[x: u8] : bool
|
||||
return x == ' ' | x == 10 | x == 13 | x == 9
|
||||
|
||||
func str.concat[a: String, b: String] : String
|
||||
let a_len: I64 = str.len(a)
|
||||
let b_len: I64 = str.len(b)
|
||||
let out: String = mem.alloc(a_len + b_len + 1)
|
||||
func str.concat[a: str, b: str] : str
|
||||
let a_len: i64 = str.len(a)
|
||||
let b_len: i64 = str.len(b)
|
||||
let out: str = mem.alloc(a_len + b_len + 1)
|
||||
for i in 0..a_len
|
||||
str.set(out, i, a[i])
|
||||
for i in 0..b_len
|
||||
@@ -140,26 +140,26 @@ func str.concat[a: String, b: String] : String
|
||||
str.set(out, a_len + b_len, 0)
|
||||
return out
|
||||
|
||||
func str.find[s: String, c: U8] : I64
|
||||
let s_len: I64 = str.len(s)
|
||||
func str.find[s: str, c: u8] : i64
|
||||
let s_len: i64 = str.len(s)
|
||||
for i in 0..s_len
|
||||
if s[i] == c
|
||||
return i
|
||||
return -1
|
||||
|
||||
func str.substr[s: String, start: I64, length: I64] : String
|
||||
func str.substr[s: str, start: i64, length: i64] : str
|
||||
if start < 0 | length < 0 | start + length > str.len(s)
|
||||
dbg.panic("str.substr out of bounds")
|
||||
|
||||
let out: String = mem.alloc(length + 1)
|
||||
let out: str = mem.alloc(length + 1)
|
||||
for i in 0..length
|
||||
str.set(out, i, s[start + i])
|
||||
str.set(out, length, 0)
|
||||
return out
|
||||
|
||||
func str.trim[s: String] : String
|
||||
let start: I64 = 0
|
||||
let end: I64 = str.len(s) - 1
|
||||
func str.trim[s: str] : str
|
||||
let start: i64 = 0
|
||||
let end: i64 = str.len(s) - 1
|
||||
|
||||
while start <= end & str.is_whitespace(s[start])
|
||||
start = start + 1
|
||||
@@ -169,10 +169,10 @@ func str.trim[s: String] : String
|
||||
|
||||
return str.substr(s, start, end - start + 1)
|
||||
|
||||
func str.split[haystack: String, needle: String]: Array
|
||||
let haystack_len: I64 = str.len(haystack)
|
||||
let needle_len: I64 = str.len(needle)
|
||||
let result: Array = []
|
||||
func str.split[haystack: str, needle: str]: array
|
||||
let haystack_len: i64 = str.len(haystack)
|
||||
let needle_len: i64 = str.len(needle)
|
||||
let result: array = []
|
||||
|
||||
if !needle_len
|
||||
if !haystack_len
|
||||
@@ -182,11 +182,11 @@ func str.split[haystack: String, needle: String]: Array
|
||||
array.push(result, str.substr(haystack, i, 1))
|
||||
return result
|
||||
|
||||
let start: I64 = 0
|
||||
let i: I64 = 0
|
||||
let start: i64 = 0
|
||||
let i: i64 = 0
|
||||
while i < haystack_len
|
||||
if i <= haystack_len - needle_len
|
||||
let match: Bool = true
|
||||
let match: bool = true
|
||||
for j in 0..needle_len
|
||||
if haystack[i + j] != needle[j]
|
||||
match = false
|
||||
@@ -201,9 +201,9 @@ func str.split[haystack: String, needle: String]: Array
|
||||
array.push(result, str.substr(haystack, start, haystack_len - start))
|
||||
return result
|
||||
|
||||
func str.reverse[s: String] : String
|
||||
let len: I64 = str.len(s)
|
||||
let out: String = mem.alloc(len + 1)
|
||||
func str.reverse[s: str] : str
|
||||
let len: i64 = str.len(s)
|
||||
let out: str = mem.alloc(len + 1)
|
||||
|
||||
for i in 0..len
|
||||
str.set(out, i, s[len - i - 1])
|
||||
@@ -211,20 +211,20 @@ func str.reverse[s: String] : String
|
||||
return out
|
||||
|
||||
// not sure this covers all wacky edge cases
|
||||
func str.from_i64[n: I64] : String
|
||||
func str.from_i64[n: i64] : str
|
||||
if n == 0
|
||||
let s: String = mem.alloc(2)
|
||||
let s: str = mem.alloc(2)
|
||||
str.set(s, 0, '0')
|
||||
str.set(s, 1, 0)
|
||||
return s
|
||||
|
||||
let neg: Bool = n < 0
|
||||
let neg: bool = n < 0
|
||||
if neg
|
||||
n = -n
|
||||
let buf: String = mem.alloc(21) // enough to fit -MAX_I64
|
||||
let i: I64 = 0
|
||||
let buf: str = mem.alloc(21) // enough to fit -MAX_I64
|
||||
let i: i64 = 0
|
||||
while n > 0
|
||||
let d: U8 = n % 10
|
||||
let d: u8 = n % 10
|
||||
str.set(buf, i, '0' + d)
|
||||
n = n / 10
|
||||
i = i + 1
|
||||
@@ -232,43 +232,43 @@ func str.from_i64[n: I64] : String
|
||||
str.set(buf, i, '-')
|
||||
i = i + 1
|
||||
str.set(buf, i, 0)
|
||||
let s: String = str.reverse(buf)
|
||||
let s: str = str.reverse(buf)
|
||||
mem.free(buf)
|
||||
return s
|
||||
|
||||
func str.from_char[c: U8] : String
|
||||
let s: String = mem.alloc(2)
|
||||
func str.from_char[c: u8] : str
|
||||
let s: str = mem.alloc(2)
|
||||
str.set(s, 0, c)
|
||||
str.set(s, 1, 0)
|
||||
return s
|
||||
|
||||
func str.parse_i64[s: String] : I64
|
||||
let len: I64 = str.len(s)
|
||||
let i: I64 = 0
|
||||
func str.parse_i64[s: str] : i64
|
||||
let len: i64 = str.len(s)
|
||||
let i: i64 = 0
|
||||
|
||||
let sign: I64 = 1
|
||||
let sign: i64 = 1
|
||||
if i < len & s[i] == '-'
|
||||
sign = -1
|
||||
i = i + 1
|
||||
|
||||
let num: I64 = 0
|
||||
let num: i64 = 0
|
||||
while i < len
|
||||
let d: U8 = s[i]
|
||||
let d: u8 = s[i]
|
||||
if d < '0' | d > '9'
|
||||
break
|
||||
num = num * 10 + (d - '0')
|
||||
i = i + 1
|
||||
return num * sign
|
||||
|
||||
func str.hex_encode[s: String] : String
|
||||
let hex_chars: String = "0123456789abcdef"
|
||||
let s_len: I64 = str.len(s)
|
||||
let j: I64 = 0
|
||||
let out: String = mem.alloc(s_len * 2 + 1)
|
||||
func str.hex_encode[s: str] : str
|
||||
let hex_chars: str = "0123456789abcdef"
|
||||
let s_len: i64 = str.len(s)
|
||||
let j: i64 = 0
|
||||
let out: str = mem.alloc(s_len * 2 + 1)
|
||||
|
||||
for i in 0..s_len
|
||||
let high: U8 = (s[i] >> 4) & 15
|
||||
let low: U8 = s[i] & 15
|
||||
let high: u8 = (s[i] >> 4) & 15
|
||||
let low: u8 = s[i] & 15
|
||||
str.set(out, j, hex_chars[high])
|
||||
str.set(out, j + 1, hex_chars[low])
|
||||
j = j + 2
|
||||
@@ -276,18 +276,18 @@ func str.hex_encode[s: String] : String
|
||||
str.set(out, j, 0)
|
||||
return out
|
||||
|
||||
func str._hex_digit_to_int[d: U8] : I64
|
||||
func str._hex_digit_to_int[d: u8] : i64
|
||||
if d >= 'a' & d <= 'f'
|
||||
return d - 'a' + 10
|
||||
if d >= 'A' & d <= 'F'
|
||||
return d - 'A' + 10
|
||||
return d - '0'
|
||||
|
||||
func str.hex_decode[s: String] : String
|
||||
let s_len: I64 = str.len(s)
|
||||
let i: I64 = 0
|
||||
let j: I64 = 0
|
||||
let out: String = mem.alloc(s_len / 2 + 1)
|
||||
func str.hex_decode[s: str] : str
|
||||
let s_len: i64 = str.len(s)
|
||||
let i: i64 = 0
|
||||
let j: i64 = 0
|
||||
let out: str = mem.alloc(s_len / 2 + 1)
|
||||
|
||||
while i < s_len
|
||||
str.set(out, j, str._hex_digit_to_int(s[i]) * 16 + str._hex_digit_to_int(s[i + 1]))
|
||||
@@ -297,45 +297,45 @@ func str.hex_decode[s: String] : String
|
||||
str.set(out, j, 0)
|
||||
return out
|
||||
|
||||
func math.gcd[a: I64, b: I64] : I64
|
||||
func math.gcd[a: i64, b: i64] : i64
|
||||
while b != 0
|
||||
let tmp: I64 = b
|
||||
let tmp: i64 = b
|
||||
b = a % b
|
||||
a = tmp
|
||||
return a
|
||||
|
||||
func math.min[a: I64, b: I64] : I64
|
||||
func math.min[a: i64, b: i64] : i64
|
||||
if a < b
|
||||
return a
|
||||
return b
|
||||
|
||||
func math.max[a: I64, b: I64] : I64
|
||||
func math.max[a: i64, b: i64] : i64
|
||||
if a > b
|
||||
return a
|
||||
return b
|
||||
|
||||
func math.abs[n: I64] : I64
|
||||
func math.abs[n: i64] : i64
|
||||
if n < 0
|
||||
return -n
|
||||
return n
|
||||
|
||||
func math.pow[b: I64, e: I64] : I64
|
||||
let out: I64 = 1
|
||||
func math.pow[b: i64, e: i64] : i64
|
||||
let out: i64 = 1
|
||||
for i in 0..e
|
||||
out = out * b
|
||||
return out
|
||||
|
||||
func math.lcm[a: I64, b: I64] : I64
|
||||
func math.lcm[a: i64, b: i64] : i64
|
||||
return (a * b) / math.gcd(a, b)
|
||||
|
||||
func math.isqrt[n: I64] : I64
|
||||
func math.isqrt[n: i64] : i64
|
||||
if n < 0
|
||||
dbg.panic("negative number passed to math.isqrt")
|
||||
if n == 0 | n == 1
|
||||
return n
|
||||
|
||||
let guess: I64 = n
|
||||
let next_guess: I64 = (guess + n / guess) / 2
|
||||
let guess: i64 = n
|
||||
let next_guess: i64 = (guess + n / guess) / 2
|
||||
|
||||
while next_guess < guess
|
||||
guess = next_guess
|
||||
@@ -343,7 +343,7 @@ func math.isqrt[n: I64] : I64
|
||||
|
||||
return guess
|
||||
|
||||
func math.is_prime[n: I64]: Bool
|
||||
func math.is_prime[n: i64]: bool
|
||||
if n <= 1
|
||||
return false
|
||||
if n == 2 | n == 3
|
||||
@@ -351,42 +351,42 @@ func math.is_prime[n: I64]: Bool
|
||||
if n % 2 == 0 | n % 3 == 0
|
||||
return false
|
||||
|
||||
let i: I64 = 5
|
||||
let i: i64 = 5
|
||||
while i * i <= n
|
||||
if n % i == 0 | n % (i + 2) == 0
|
||||
return false
|
||||
i = i + 6
|
||||
return true
|
||||
|
||||
func array.new[] : Array
|
||||
func array.new[] : array
|
||||
// [ 8 bytes - ptr to data ] [ 8 bytes - size ] [ 8 bytes - capacity ]
|
||||
let arr: Ptr = mem.alloc(24)
|
||||
let arr: ptr = mem.alloc(24)
|
||||
mem.zero(arr, 24)
|
||||
return arr
|
||||
|
||||
func array.nth[xs: Array, n: I64] : I64
|
||||
func array.nth[xs: array, n: i64] : i64
|
||||
if n < 0 | n >= array.size(xs)
|
||||
dbg.panic("array.nth out of bounds")
|
||||
return array.nth_unchecked(xs, n)
|
||||
|
||||
func array.nth_unchecked[xs: Array, n: I64] : I64
|
||||
let data: Ptr = mem.read64(xs)
|
||||
func array.nth_unchecked[xs: array, n: i64] : i64
|
||||
let data: ptr = mem.read64(xs)
|
||||
return mem.read64(data + n * 8)
|
||||
|
||||
func array.set[xs: Array, n: I64, x: I64] : Void
|
||||
let data: Ptr = mem.read64(xs)
|
||||
func array.set[xs: array, n: i64, x: i64] : void
|
||||
let data: ptr = mem.read64(xs)
|
||||
mem.write64(data + n * 8, x)
|
||||
|
||||
func array.push[xs: Array, x: I64] : Void
|
||||
let data: Ptr = mem.read64(xs)
|
||||
let capacity: I64 = mem.read64(xs + 8)
|
||||
let size: I64 = mem.read64(xs + 16)
|
||||
func array.push[xs: array, x: i64] : void
|
||||
let data: ptr = mem.read64(xs)
|
||||
let capacity: i64 = mem.read64(xs + 8)
|
||||
let size: i64 = mem.read64(xs + 16)
|
||||
|
||||
if size == capacity
|
||||
let new_capacity: I64 = 4
|
||||
let new_capacity: i64 = 4
|
||||
if capacity != 0
|
||||
new_capacity = capacity * 2
|
||||
let new_data: Ptr = realloc(data, new_capacity * 8)
|
||||
let new_data: ptr = realloc(data, new_capacity * 8)
|
||||
mem.write64(xs, new_data)
|
||||
mem.write64(xs + 8, new_capacity)
|
||||
data = new_data
|
||||
@@ -394,95 +394,95 @@ func array.push[xs: Array, x: I64] : Void
|
||||
mem.write64(data + size * 8, x)
|
||||
mem.write64(xs + 16, size + 1)
|
||||
|
||||
func array.size[xs: Array] : I64
|
||||
func array.size[xs: array] : i64
|
||||
return mem.read64(xs + 16)
|
||||
|
||||
func array.free[xs: Array] : Void
|
||||
let data: Ptr = mem.read64(xs)
|
||||
func array.free[xs: array] : void
|
||||
let data: ptr = mem.read64(xs)
|
||||
if data != 0
|
||||
mem.free(data)
|
||||
mem.free(xs)
|
||||
|
||||
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)
|
||||
dbg.panic("array.slice out of bounds")
|
||||
|
||||
let new_array: Array = []
|
||||
let new_array: array = []
|
||||
for i in 0..length
|
||||
array.push(new_array, array.nth(xs, start + i))
|
||||
return new_array
|
||||
|
||||
func array.concat[a: Array, b: Array] : Array
|
||||
let new_array: Array = []
|
||||
func array.concat[a: array, b: array] : array
|
||||
let new_array: array = []
|
||||
for i in 0..array.size(a)
|
||||
array.push(new_array, array.nth(a, i))
|
||||
for i in 0..array.size(b)
|
||||
array.push(new_array, array.nth(b, i))
|
||||
return new_array
|
||||
|
||||
func alg.quicksort[arr: Array] : Void
|
||||
func alg.quicksort[arr: array] : void
|
||||
alg._do_quicksort(arr, 0, array.size(arr) - 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
|
||||
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, i + 1, high)
|
||||
|
||||
func alg._partition[arr: Array, low: I64, high: I64] : I64
|
||||
let pivot: I64 = array.nth(arr, high)
|
||||
let i: I64 = low - 1
|
||||
func alg._partition[arr: array, low: i64, high: i64] : i64
|
||||
let pivot: i64 = array.nth(arr, high)
|
||||
let i: i64 = low - 1
|
||||
for j in (low)..high
|
||||
if array.nth(arr, j) <= pivot
|
||||
i = i + 1
|
||||
let temp: I64 = array.nth(arr ,i)
|
||||
let 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)
|
||||
let 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: I64] : I64
|
||||
let count: I64 = 0
|
||||
let size: I64 = array.size(arr)
|
||||
func alg.count[arr: array, item: i64] : i64
|
||||
let count: i64 = 0
|
||||
let size: i64 = array.size(arr)
|
||||
for i in 0..size
|
||||
if array.nth(arr, i) == item
|
||||
count = count + 1
|
||||
return count
|
||||
|
||||
func os.exit[code: I64] : Void
|
||||
func os.exit[code: i64] : void
|
||||
_builtin_syscall(60, code)
|
||||
|
||||
func os.urandom[]: I64
|
||||
let n: I64 = 0
|
||||
let fd: I64 = _builtin_syscall(257, -100, "/dev/urandom", 0, 0) // openat
|
||||
func os.urandom[]: i64
|
||||
let n: i64 = 0
|
||||
let fd: i64 = _builtin_syscall(257, -100, "/dev/urandom", 0, 0) // openat
|
||||
_builtin_syscall(0, fd, @n, 8) // read
|
||||
_builtin_syscall(3, fd) // close
|
||||
return n
|
||||
|
||||
func os.time[] : I64
|
||||
let tv: Ptr = mem.alloc(16)
|
||||
func os.time[] : i64
|
||||
let tv: ptr = mem.alloc(16)
|
||||
_builtin_syscall(96, tv, 0) // gettimeofday
|
||||
let seconds: I64 = mem.read64(tv)
|
||||
let microseconds: I64 = mem.read64(tv + 8)
|
||||
let seconds: i64 = mem.read64(tv)
|
||||
let microseconds: i64 = mem.read64(tv + 8)
|
||||
mem.free(tv)
|
||||
return seconds * 1000 + microseconds / 1000
|
||||
|
||||
// voodoo magic
|
||||
func os.shell[command: String] : I64
|
||||
let pid: I64 = _builtin_syscall(57) // fork
|
||||
func os.shell[command: str] : i64
|
||||
let pid: i64 = _builtin_syscall(57) // fork
|
||||
if pid == 0
|
||||
let argv: Array = ["sh", "-c", command, 0]
|
||||
let argv: array = ["sh", "-c", command, 0]
|
||||
_builtin_syscall(59, "/bin/sh", mem.read64(argv), _builtin_environ()) // execve
|
||||
_builtin_syscall(60, 1) // exit
|
||||
else
|
||||
let status: Ptr = mem.alloc(4)
|
||||
let wp: I64 = _builtin_syscall(61, pid, status, 0, 0) // waitpid
|
||||
let status: ptr = mem.alloc(4)
|
||||
let wp: i64 = _builtin_syscall(61, pid, status, 0, 0) // waitpid
|
||||
if wp == -1
|
||||
mem.free(status)
|
||||
return -1
|
||||
let st: I64 = mem.read32(status)
|
||||
let st: i64 = mem.read32(status)
|
||||
mem.free(status)
|
||||
|
||||
if (st & 0x7f) == 0
|
||||
@@ -490,24 +490,24 @@ func os.shell[command: String] : I64
|
||||
else
|
||||
return -(st & 0x7f)
|
||||
|
||||
func os.listdir[path: String] : Array
|
||||
let fd: I64 = _builtin_syscall(257, -100, path, 0, 0) // openat
|
||||
func os.listdir[path: str] : array
|
||||
let fd: i64 = _builtin_syscall(257, -100, path, 0, 0) // openat
|
||||
if fd < 0
|
||||
return []
|
||||
|
||||
let files: Array = []
|
||||
let buf: Ptr = mem.alloc(1024)
|
||||
let files: array = []
|
||||
let buf: ptr = mem.alloc(1024)
|
||||
while true
|
||||
let n: I64 = _builtin_syscall(217, fd, buf, 1024) // getdents64
|
||||
let n: i64 = _builtin_syscall(217, fd, buf, 1024) // getdents64
|
||||
if n <= 0
|
||||
break
|
||||
|
||||
let pos: I64 = 0
|
||||
let pos: i64 = 0
|
||||
while pos < n
|
||||
let len: I64 = mem.read16(buf + pos + 16)
|
||||
let name: String = buf + pos + 19
|
||||
let len: i64 = mem.read16(buf + pos + 16)
|
||||
let name: str = buf + pos + 19
|
||||
if name[0]
|
||||
let skip: Bool = false
|
||||
let skip: bool = false
|
||||
// skip if name is exactly '.' or '..'
|
||||
if name[0] == '.'
|
||||
if name[1] == 0
|
||||
@@ -523,17 +523,17 @@ func os.listdir[path: String] : Array
|
||||
_builtin_syscall(3, fd) // close
|
||||
return files
|
||||
|
||||
func net.listen[packed_host: I64, port: I64] : I64
|
||||
let s: I64 = _builtin_syscall(41, 2, 1, 0) // socket
|
||||
func net.listen[packed_host: i64, port: i64] : i64
|
||||
let s: i64 = _builtin_syscall(41, 2, 1, 0) // socket
|
||||
if s < 0
|
||||
return -1
|
||||
|
||||
let optval: I64 = 1
|
||||
let optval: i64 = 1
|
||||
if _builtin_syscall(54, s, 1, 2, @optval, 8) < 0 // setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
||||
_builtin_syscall(3, s) // close
|
||||
return -1
|
||||
|
||||
let sa: Ptr = mem.alloc(16)
|
||||
let sa: ptr = mem.alloc(16)
|
||||
mem.zero(sa, 16)
|
||||
mem.write8(sa + 0, 2)
|
||||
mem.write8(sa + 1, 0)
|
||||
@@ -556,18 +556,18 @@ func net.listen[packed_host: I64, port: I64] : I64
|
||||
|
||||
return s
|
||||
|
||||
func net.connect[host: String, port: I64] : I64
|
||||
let he: Ptr = gethostbyname(host)
|
||||
func net.connect[host: str, port: i64] : i64
|
||||
let he: ptr = gethostbyname(host)
|
||||
if he == 0
|
||||
return -1
|
||||
|
||||
let ip_ptr: Ptr = mem.read64(mem.read64(he + 24))
|
||||
let ip_ptr: ptr = mem.read64(mem.read64(he + 24))
|
||||
|
||||
let s: I64 = _builtin_syscall(41, 2, 1, 0) // socket
|
||||
let s: i64 = _builtin_syscall(41, 2, 1, 0) // socket
|
||||
if s < 0
|
||||
return -1
|
||||
|
||||
let sa: Ptr = mem.alloc(16)
|
||||
let sa: ptr = mem.alloc(16)
|
||||
mem.zero(sa, 16)
|
||||
mem.write8(sa + 0, 2)
|
||||
mem.write8(sa + 2, (port >> 8) & 255)
|
||||
@@ -585,17 +585,17 @@ func net.connect[host: String, port: I64] : I64
|
||||
mem.free(sa)
|
||||
return s
|
||||
|
||||
func net.accept[s: I64] : I64
|
||||
func net.accept[s: i64] : i64
|
||||
return _builtin_syscall(43, s, 0, 0)
|
||||
|
||||
func net.send[s: I64, data: String, size: I64] : Void
|
||||
func net.send[s: i64, data: str, size: i64] : void
|
||||
_builtin_syscall(44, s, data, size, 0, 0, 0)
|
||||
|
||||
func net.read[s: I64, buffer: Ptr, size: I64] : I64
|
||||
func net.read[s: i64, buffer: ptr, size: i64] : i64
|
||||
return _builtin_syscall(0, s, buffer, size)
|
||||
|
||||
func net.close[s: I64] : Void
|
||||
func net.close[s: i64] : void
|
||||
_builtin_syscall(3, s)
|
||||
|
||||
func net.pack_addr[a: I64, b: I64, c: I64, d: I64] : I64
|
||||
func net.pack_addr[a: i64, b: i64, c: i64, d: i64] : i64
|
||||
return (a << 24) | (b << 16) | (c << 8) | d
|
||||
|
||||
16
test.zr
16
test.zr
@@ -1,4 +1,4 @@
|
||||
func run_test[x: String] : Void
|
||||
func run_test[x: str] : void
|
||||
if str.equal(x, "puzzles") | str.equal(x, "raylib.zr") | str.equal(x, "x11.zr") | str.equal(x, "sqlite_todo.zr")
|
||||
io.print("\033[93mSkipping ")
|
||||
io.print(x)
|
||||
@@ -8,12 +8,12 @@ func run_test[x: String] : Void
|
||||
io.print("\033[93mBuilding ")
|
||||
io.print(x)
|
||||
io.print("...\033[0m ")
|
||||
let cmd: String = str.concat("./target/release/zern examples/", x)
|
||||
let cmd: str = str.concat("./target/release/zern examples/", x)
|
||||
|
||||
let build_start_time: I64 = os.time()
|
||||
let build_start_time: i64 = os.time()
|
||||
if os.shell(cmd) != 0
|
||||
os.exit(1)
|
||||
let build_end_time: I64 = os.time()
|
||||
let build_end_time: i64 = os.time()
|
||||
|
||||
mem.free(cmd)
|
||||
io.print_i64(build_end_time - build_start_time)
|
||||
@@ -29,14 +29,14 @@ func run_test[x: String] : Void
|
||||
io.print(x)
|
||||
io.println("...\033[0m")
|
||||
|
||||
let run_start_time: I64 = os.time()
|
||||
let run_start_time: i64 = os.time()
|
||||
if str.equal(x, "curl.zr")
|
||||
if os.shell("./out http://example.com") != 0
|
||||
os.exit(1)
|
||||
else
|
||||
if os.shell("./out") != 0
|
||||
os.exit(1)
|
||||
let run_end_time: I64 = os.time()
|
||||
let run_end_time: i64 = os.time()
|
||||
|
||||
io.print("\033[93mRunning ")
|
||||
io.print(x)
|
||||
@@ -44,10 +44,10 @@ func run_test[x: String] : Void
|
||||
io.print_i64(run_end_time - run_start_time)
|
||||
io.println("ms")
|
||||
|
||||
func main[] : I64
|
||||
func main[] : i64
|
||||
os.shell("cargo build --release")
|
||||
|
||||
let files: Array = os.listdir("examples/")
|
||||
let files: array = os.listdir("examples/")
|
||||
for i in 0..array.size(files)
|
||||
run_test(array.nth(files, i))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user