Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d0a4fb7293 | |||
| 65913868b0 | |||
| 420f296ca5 | |||
| c603d10012 | |||
| b78223fae7 | |||
| abbe43a42c | |||
| 9a60518e0e | |||
| 20499a8ee0 | |||
| 55a8844699 | |||
| db2e639cc8 | |||
| 270386da95 | |||
| 07b46a987b | |||
| 781c35d484 | |||
| c527aceecd | |||
| 5682318915 | |||
| 3be891c7cc |
19
README.md
19
README.md
@@ -5,15 +5,16 @@ A very cool language
|
||||
## Features
|
||||
* Clean indentation-based syntax
|
||||
* Compiles to x86_64 Assembly
|
||||
* ~~No libc required~~ (SOON; still used for memory allocation and DNS resolution)
|
||||
* Pretty big [standard library](https://github.com/antpiasecki/zern/tree/main/src/std)
|
||||
* Produces tiny static executables (~30KB with musl)
|
||||
* ~~No libc required~~ (SOON; still used for memory allocation and DNS resolution)
|
||||
* Sometimes works
|
||||
* Has the pipe operator
|
||||
|
||||
## Syntax
|
||||
```rust
|
||||
func main[] : i64
|
||||
let answer: i64 = math.abs(os.urandom()) % 100
|
||||
let answer: i64 = math.abs(os.urandom_i64()) % 100
|
||||
|
||||
while true
|
||||
io.println("Guess a number: ")
|
||||
@@ -28,6 +29,20 @@ func main[] : i64
|
||||
io.println("Too high!")
|
||||
```
|
||||
|
||||
```rust
|
||||
func square[x: i64] : i64
|
||||
return x * x
|
||||
|
||||
func sum[a: i64, b: i64] : i64
|
||||
return a + b
|
||||
|
||||
func main[] : i64
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
|
||||
|> alg.map(^square)
|
||||
|> alg.reduce(^sum, 0)
|
||||
|> io.println_i64()
|
||||
```
|
||||
|
||||
## Quickstart
|
||||
```
|
||||
cargo install --git https://github.com/antpiasecki/zern
|
||||
|
||||
@@ -2,11 +2,11 @@ func main[] : i64
|
||||
// https://brainfuck.org/sierpinski.b
|
||||
let src: str = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
||||
let src_len: i64 = str.len(src)
|
||||
let i: i64 = 0
|
||||
let i = 0
|
||||
|
||||
let memory: ptr = mem.alloc(30000)
|
||||
mem.zero(memory, 30000)
|
||||
let p: i64 = 0
|
||||
let p = 0
|
||||
|
||||
while i < src_len
|
||||
let op: u8 = src[i]
|
||||
@@ -26,8 +26,8 @@ func main[] : i64
|
||||
else if op == '['
|
||||
if !memory[p]
|
||||
i = i + 1
|
||||
let opened: i64 = 0
|
||||
while i < src_len & !(src[i] == ']' & !opened)
|
||||
let opened = 0
|
||||
while i < src_len && !(src[i] == ']' && !opened)
|
||||
if src[i] == '['
|
||||
opened = opened + 1
|
||||
else if src[i] == ']'
|
||||
@@ -36,8 +36,8 @@ func main[] : i64
|
||||
else if op == ']'
|
||||
if memory[p]
|
||||
i = i - 1
|
||||
let closed: i64 = 0
|
||||
while i >= 0 & !(src[i] == '[' & !closed)
|
||||
let closed = 0
|
||||
while i >= 0 && !(src[i] == '[' && !closed)
|
||||
if src[i] == ']'
|
||||
closed = closed + 1
|
||||
else if src[i] == '['
|
||||
|
||||
59
examples/crypto.zr
Normal file
59
examples/crypto.zr
Normal file
@@ -0,0 +1,59 @@
|
||||
func main[] : i64
|
||||
// Blake2b
|
||||
let out: ptr = crypto.blake2b.hash(32, 0, 0, "Hello, World!", 13)
|
||||
io.print("Blake2b-256: ")
|
||||
io.println(str.hex_encode(out, 32))
|
||||
|
||||
io.print("Blake2b-512: ")
|
||||
out = crypto.blake2b.hash(64, 0, 0, "Hello, World!", 13)
|
||||
io.println(str.hex_encode(out, 64))
|
||||
|
||||
// XChaCha20
|
||||
let key: ptr = str.hex_decode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
|
||||
let nonce: ptr = str.hex_decode("000102030405060708090a0b0c0d0e0f1011121314151617")
|
||||
|
||||
let input: str = "Hello, World!"
|
||||
let input_len: i64 = str.len(input)
|
||||
|
||||
let ciphertext: ptr = crypto.xchacha20.xor(key, nonce, input, input_len)
|
||||
io.println(str.hex_encode(ciphertext, input_len))
|
||||
|
||||
// X25519
|
||||
let scalar: ptr = str.hex_decode("a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4")
|
||||
let point: ptr = str.hex_decode("e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c")
|
||||
let expected: ptr = str.hex_decode("c3da55379de9c6908e94ea4df28d084f32eccf03491c71f754b4075577a28552")
|
||||
|
||||
out = crypto.x25519.scalarmult(scalar, point)
|
||||
|
||||
io.print("Computed: ")
|
||||
io.println(str.hex_encode(out, 32))
|
||||
io.print("Expected: ")
|
||||
io.println(str.hex_encode(expected, 32))
|
||||
|
||||
let base_point: ptr = mem.alloc(32)
|
||||
mem.zero(base_point, 32)
|
||||
mem.write8(base_point, 9)
|
||||
|
||||
let alice_private: ptr = str.hex_decode("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a")
|
||||
io.print("A_priv: ")
|
||||
io.println(str.hex_encode(alice_private, 32))
|
||||
|
||||
let alice_public: ptr = crypto.x25519.scalarmult(alice_private, base_point)
|
||||
io.print("A_pub: ")
|
||||
io.println(str.hex_encode(alice_public, 32))
|
||||
|
||||
let bob_private: ptr = str.hex_decode("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6dbddb79b1732920165")
|
||||
io.print("B_priv: ")
|
||||
io.println(str.hex_encode(bob_private, 32))
|
||||
|
||||
let bob_public: ptr = crypto.x25519.scalarmult(bob_private, base_point)
|
||||
io.print("B_pub: ")
|
||||
io.println(str.hex_encode(bob_public, 32))
|
||||
|
||||
let alice_shared: ptr = crypto.x25519.scalarmult(alice_private, bob_public)
|
||||
io.print("A_shared: ")
|
||||
io.println(str.hex_encode(alice_shared, 32))
|
||||
|
||||
let bob_shared: ptr = crypto.x25519.scalarmult(bob_private, alice_public)
|
||||
io.print("B_shared: ")
|
||||
io.println(str.hex_encode(bob_shared, 32))
|
||||
@@ -11,7 +11,7 @@ func main[argc: i64, argv: ptr] : i64
|
||||
dbg.panic("invalid url scheme")
|
||||
|
||||
let url_len: i64 = str.len(url)
|
||||
let host_start: i64 = 7
|
||||
let host_start = 7
|
||||
let i: i64 = host_start
|
||||
while i < url_len
|
||||
if url[i] == '/'
|
||||
@@ -37,18 +37,18 @@ func main[argc: i64, argv: ptr] : i64
|
||||
mem.free(req)
|
||||
|
||||
let header_buf: str = mem.alloc(8192)
|
||||
let header_size: i64 = 0
|
||||
let header_size = 0
|
||||
let found: bool = false
|
||||
let end_index: i64 = -1
|
||||
|
||||
while !found & header_size < 8192
|
||||
while !found && header_size < 8192
|
||||
let n: i64 = net.read(s, header_buf + header_size, 8192 - header_size)
|
||||
if n <= 0
|
||||
break
|
||||
let current_size: i64 = header_size + n
|
||||
i = 0
|
||||
while i <= current_size - 4
|
||||
if header_buf[i] == 13 & header_buf[i + 1] == 10 & header_buf[i + 2] == 13 & header_buf[i + 3] == 10
|
||||
if header_buf[i] == 13 && header_buf[i + 1] == 10 && header_buf[i + 2] == 13 && header_buf[i + 3] == 10
|
||||
found = true
|
||||
end_index = i + 4
|
||||
break
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
func main[] : i64
|
||||
let a: i64 = 0
|
||||
let b: i64 = 1
|
||||
let a = 0
|
||||
let b = 1
|
||||
|
||||
while a < 100000
|
||||
io.println_i64(a)
|
||||
|
||||
@@ -4,7 +4,7 @@ func main[] : i64
|
||||
io.println("FizzBuzz")
|
||||
else if i % 5 == 0
|
||||
io.println("Buzz")
|
||||
else if i %3 == 0
|
||||
else if i % 3 == 0
|
||||
io.println("Fizz")
|
||||
else
|
||||
io.println_i64(i)
|
||||
11
examples/functional.zr
Normal file
11
examples/functional.zr
Normal file
@@ -0,0 +1,11 @@
|
||||
func square[x: i64] : i64
|
||||
return x * x
|
||||
|
||||
func sum[a: i64, b: i64] : i64
|
||||
return a + b
|
||||
|
||||
func main[] : i64
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
|
||||
|> alg.map(^square)
|
||||
|> alg.reduce(^sum, 0)
|
||||
|> io.println_i64()
|
||||
@@ -1,5 +1,5 @@
|
||||
func main[] : i64
|
||||
let answer: i64 = math.abs(os.urandom()) % 100
|
||||
let answer: i64 = math.abs(os.urandom_i64()) % 100
|
||||
|
||||
while true
|
||||
io.println("Guess a number: ")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func part1[l1: array, l2: array] : void
|
||||
let out: i64 = 0
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(l1)
|
||||
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: i64 = 0
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(l1)
|
||||
out = out + array.nth(l1, i) * alg.count(l2, array.nth(l1, i))
|
||||
|
||||
@@ -2,19 +2,19 @@ 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
|
||||
if array.nth(report, i) > array.nth(report, i + 1) && increasing
|
||||
return false
|
||||
if array.nth(report, i) < array.nth(report, i + 1) & !increasing
|
||||
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))
|
||||
if diff < 1 | diff > 3
|
||||
if diff < 1 || diff > 3
|
||||
return false
|
||||
|
||||
return true
|
||||
|
||||
func part1[data: array] : void
|
||||
let out: i64 = 0
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(data)
|
||||
if check(array.nth(data, i))
|
||||
@@ -23,7 +23,7 @@ func part1[data: array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[data: array] : void
|
||||
let out: i64 = 0
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(data)
|
||||
if check(array.nth(data, i))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func check[lines: array, x: i64, y: i64, dx: i64, dy: i64] : bool
|
||||
if x + dx * 3 < 0 | x + dx * 3 >= array.size(lines) | y + dy * 3 < 0 | y + dy * 3 >= str.len(array.nth(lines, 0))
|
||||
if x + dx * 3 < 0 || x + dx * 3 >= array.size(lines) || y + dy * 3 < 0 || y + dy * 3 >= str.len(array.nth(lines, 0))
|
||||
return false
|
||||
|
||||
if array.nth(lines, x)[y] != 'X'
|
||||
@@ -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: i64 = 0
|
||||
let out = 0
|
||||
|
||||
for x in 0..array.size(lines)
|
||||
for y in 0..str.len(array.nth(lines, x))
|
||||
@@ -38,7 +38,7 @@ func part1[lines: array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[lines: array] : void
|
||||
let out: i64 = 0
|
||||
let out = 0
|
||||
|
||||
for x in 1..array.size(lines)-1
|
||||
for y in 1..str.len(array.nth(lines, x))-1
|
||||
@@ -50,7 +50,7 @@ func part2[lines: array] : void
|
||||
str.set(s, 3, array.nth(lines, x - 1)[y + 1])
|
||||
str.set(s, 4, 0)
|
||||
|
||||
if str.equal(s, "MSSM") | str.equal(s, "SMMS") | str.equal(s, "MMSS") | str.equal(s, "SSMM")
|
||||
if str.equal(s, "MSSM") || str.equal(s, "SMMS") || str.equal(s, "MMSS") || str.equal(s, "SSMM")
|
||||
out = out + 1
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
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
|
||||
if array.nth(rules_left, k) == a && array.nth(rules_right, k) == b
|
||||
return true
|
||||
return false
|
||||
|
||||
@@ -26,7 +26,7 @@ func sort_by_rules[update: array, rules_left: array, rules_right: array] : void
|
||||
swapped = true
|
||||
|
||||
func part1[updates: array, rules_left: array, rules_right: array] : void
|
||||
let out: i64 = 0
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(updates)
|
||||
let update: array = array.nth(updates, i)
|
||||
@@ -36,7 +36,7 @@ func part1[updates: array, rules_left: array, rules_right: array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[updates: array, rules_left: array, rules_right: array] : void
|
||||
let out: i64 = 0
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(updates)
|
||||
let update: array = array.nth(updates, i)
|
||||
|
||||
@@ -44,7 +44,7 @@ func solve[ops: array, e: array] : i64
|
||||
return 0
|
||||
|
||||
func part1[equations: array] : void
|
||||
let out: i64 = 0
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(equations)
|
||||
out = out + solve(["add", "mul"], array.nth(equations, i))
|
||||
@@ -52,7 +52,7 @@ func part1[equations: array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[equations: array] : void
|
||||
let out: i64 = 0
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(equations)
|
||||
out = out + solve(["add", "mul", "concat"], array.nth(equations, i))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
func part1[lines: array] : void
|
||||
let password: i64 = 0
|
||||
let dial: i64 = 50
|
||||
let password = 0
|
||||
let dial = 50
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
let line: str = array.nth(lines, i)
|
||||
@@ -22,8 +22,8 @@ func part1[lines: array] : void
|
||||
io.println_i64(password)
|
||||
|
||||
func part2[lines: array] : void
|
||||
let password: i64 = 0
|
||||
let dial: i64 = 50
|
||||
let password = 0
|
||||
let dial = 50
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
let line: str = array.nth(lines, i)
|
||||
|
||||
@@ -9,7 +9,7 @@ func part1_is_invalid_id[s: str] : bool
|
||||
return str.equal(first, second)
|
||||
|
||||
func part1[ranges: array] : void
|
||||
let sum: i64 = 0
|
||||
let sum = 0
|
||||
|
||||
for i in 0..array.size(ranges)
|
||||
let parts: array = array.nth(ranges, i) |> str.split("-")
|
||||
@@ -40,7 +40,7 @@ func part2_is_invalid_id[s: str] : bool
|
||||
return false
|
||||
|
||||
func part2[ranges: array] : void
|
||||
let sum: i64 = 0
|
||||
let sum = 0
|
||||
|
||||
for i in 0..array.size(ranges)
|
||||
let parts: array = array.nth(ranges, i) |> str.split("-")
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
func part1[lines: array] : void
|
||||
let sum: i64 = 0
|
||||
let sum = 0
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
let line: str = array.nth(lines, i)
|
||||
|
||||
let largest: i64 = 0
|
||||
let largest = 0
|
||||
for j in 0..str.len(line)
|
||||
for k in (j+1)..str.len(line)
|
||||
let s: str = mem.alloc(3)
|
||||
@@ -20,7 +20,7 @@ func part1[lines: array] : void
|
||||
|
||||
// had to cheat this one
|
||||
func part2_rec[bank: str, start: i64, remaining: i64] : i64
|
||||
let largest: i64 = 0
|
||||
let largest = 0
|
||||
let largest_idx: i64 = start
|
||||
let len: i64 = str.len(bank)
|
||||
|
||||
@@ -36,7 +36,7 @@ func part2_rec[bank: str, start: i64, remaining: i64] : i64
|
||||
return largest
|
||||
|
||||
func part2[lines: array] : void
|
||||
let sum: i64 = 0
|
||||
let sum = 0
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
let line: str = array.nth(lines, i)
|
||||
|
||||
8
examples/puzzles/euler_00.zr
Normal file
8
examples/puzzles/euler_00.zr
Normal file
@@ -0,0 +1,8 @@
|
||||
func main[] : i64
|
||||
let sum = 0
|
||||
|
||||
for i in 0..266000
|
||||
if i % 2 != 0
|
||||
sum = sum + i * i
|
||||
|
||||
io.println_i64(sum)
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : i64
|
||||
let sum: i64 = 0
|
||||
let sum = 0
|
||||
|
||||
for i in 0..2000000
|
||||
if math.is_prime(i)
|
||||
for i in 0..1000
|
||||
if i % 5 == 0 || i % 3 == 0
|
||||
sum = sum + i
|
||||
io.println_i64(sum)
|
||||
@@ -1,21 +1,13 @@
|
||||
func num_divisors[n: i64] : i64
|
||||
let end: i64 = math.isqrt(n)
|
||||
|
||||
let out: i64 = 0
|
||||
for i in 1..end+1
|
||||
if n % i == 0
|
||||
out = out + 2
|
||||
|
||||
if end * end == n
|
||||
out = out - 1
|
||||
return out
|
||||
|
||||
func main[] : i64
|
||||
let n: i64 = 0
|
||||
let i: i64 = 1
|
||||
while true
|
||||
n = n + i
|
||||
if num_divisors(n) > 500
|
||||
io.println_i64(n)
|
||||
break
|
||||
i = i + 1
|
||||
let sum = 0
|
||||
let a = 0
|
||||
let b = 1
|
||||
|
||||
while a < 4000000
|
||||
if a % 2 == 0
|
||||
sum = sum + a
|
||||
let temp: i64 = b
|
||||
b = a + b
|
||||
a = temp
|
||||
|
||||
io.println_i64(sum)
|
||||
@@ -1,6 +1,11 @@
|
||||
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()
|
||||
|> str.substr(0, 10)
|
||||
|> io.println()
|
||||
let n = 600851475143
|
||||
let f = 2
|
||||
|
||||
while f * f <= n
|
||||
if n % f == 0
|
||||
n = n / f
|
||||
else
|
||||
f = f + 1
|
||||
|
||||
io.println_i64(n)
|
||||
@@ -1,22 +1,13 @@
|
||||
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
|
||||
while n != 1
|
||||
n = collatz_num(n)
|
||||
i = i + 1
|
||||
return i
|
||||
|
||||
func main[] : i64
|
||||
let max: i64 = 0
|
||||
let max_index: i64 = 0
|
||||
let out = 0
|
||||
|
||||
for i in 1..1000000
|
||||
let seq: i64 = collatz_seq(i)
|
||||
if seq > max
|
||||
max = seq
|
||||
max_index = i
|
||||
io.println_i64(max_index)
|
||||
for a in 500..1000
|
||||
for b in 500..1000
|
||||
if a * b > out
|
||||
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)
|
||||
mem.free(s_rev)
|
||||
io.println_i64(out)
|
||||
@@ -1,9 +1,6 @@
|
||||
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
|
||||
let out = 1
|
||||
|
||||
for i in 1..21
|
||||
out = math.lcm(out, i)
|
||||
io.println_i64(out)
|
||||
@@ -1,7 +1,11 @@
|
||||
func main[] : i64
|
||||
let sum: i64 = 0
|
||||
let sum_of_squares = 0
|
||||
for i in 1..101
|
||||
sum_of_squares = sum_of_squares + i * i
|
||||
|
||||
for i in 0..1000
|
||||
if i % 5 == 0 | i % 3 == 0
|
||||
sum = sum + i
|
||||
io.println_i64(sum)
|
||||
let 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
|
||||
|
||||
io.println_i64(square_of_sum - sum_of_squares)
|
||||
@@ -1,13 +1,11 @@
|
||||
func main[] : i64
|
||||
let sum: i64 = 0
|
||||
let a: i64 = 0
|
||||
let b: i64 = 1
|
||||
let found = 0
|
||||
|
||||
while a < 4000000
|
||||
if a % 2 == 0
|
||||
sum = sum + a
|
||||
let temp: i64 = b
|
||||
b = a + b
|
||||
a = temp
|
||||
|
||||
io.println_i64(sum)
|
||||
let i = 1
|
||||
while true
|
||||
if math.is_prime(i)
|
||||
found = found + 1
|
||||
if found == 10001
|
||||
io.println_i64(i)
|
||||
break
|
||||
i = i + 1
|
||||
@@ -1,11 +1,14 @@
|
||||
func main[] : i64
|
||||
let n: i64 = 600851475143
|
||||
let f: i64 = 2
|
||||
let n: str = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
|
||||
|
||||
while f * f <= n
|
||||
if n % f == 0
|
||||
n = n / f
|
||||
else
|
||||
f = f + 1
|
||||
|
||||
io.println_i64(n)
|
||||
let out = 0
|
||||
let max: i64 = str.len(n) - 13
|
||||
for i in 0..max
|
||||
let s = 1
|
||||
let j = 0
|
||||
while j < 13
|
||||
s = s * (n[i + j] - '0')
|
||||
j = j + 1
|
||||
if s > out
|
||||
out = s
|
||||
io.println_i64(out)
|
||||
@@ -1,13 +1,7 @@
|
||||
func main[] : i64
|
||||
let out: i64 = 0
|
||||
|
||||
for a in 500..1000
|
||||
for b in 500..1000
|
||||
if a * b > out
|
||||
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)
|
||||
mem.free(s_rev)
|
||||
io.println_i64(out)
|
||||
for a in 1..1000
|
||||
for b in 1..1000
|
||||
let c: i64 = 1000 - b - a
|
||||
if a * a + b * b == c * c
|
||||
io.println_i64(a * b * c)
|
||||
return 0
|
||||
@@ -1,6 +1,7 @@
|
||||
func main[] : i64
|
||||
let out: i64 = 1
|
||||
let sum = 0
|
||||
|
||||
for i in 1..21
|
||||
out = math.lcm(out, i)
|
||||
io.println_i64(out)
|
||||
for i in 0..2000000
|
||||
if math.is_prime(i)
|
||||
sum = sum + i
|
||||
io.println_i64(sum)
|
||||
@@ -1,11 +1,36 @@
|
||||
func main[] : i64
|
||||
let sum_of_squares: i64 = 0
|
||||
for i in 1..101
|
||||
sum_of_squares = sum_of_squares + i * i
|
||||
let N = 20
|
||||
|
||||
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
|
||||
let grid: array = []
|
||||
array.push(grid, [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8])
|
||||
array.push(grid, [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0])
|
||||
array.push(grid, [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65])
|
||||
array.push(grid, [52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91])
|
||||
array.push(grid, [22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80])
|
||||
array.push(grid, [24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50])
|
||||
array.push(grid, [32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70])
|
||||
array.push(grid, [67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21])
|
||||
array.push(grid, [24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72])
|
||||
array.push(grid, [21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95])
|
||||
array.push(grid, [78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92])
|
||||
array.push(grid, [16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57])
|
||||
array.push(grid, [86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58])
|
||||
array.push(grid, [19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40])
|
||||
array.push(grid, [4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66])
|
||||
array.push(grid, [88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69])
|
||||
array.push(grid, [4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36])
|
||||
array.push(grid, [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16])
|
||||
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])
|
||||
|
||||
io.println_i64(square_of_sum - sum_of_squares)
|
||||
let 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)
|
||||
out = math.max(out, math.max(h, math.max(v, math.max(d1, d2))))
|
||||
|
||||
io.println_i64(out)
|
||||
@@ -1,11 +1,21 @@
|
||||
func main[] : i64
|
||||
let found: i64 = 0
|
||||
func num_divisors[n: i64] : i64
|
||||
let end: i64 = math.isqrt(n)
|
||||
|
||||
let i: i64 = 1
|
||||
let out = 0
|
||||
for i in 1..end+1
|
||||
if n % i == 0
|
||||
out = out + 2
|
||||
|
||||
if end * end == n
|
||||
out = out - 1
|
||||
return out
|
||||
|
||||
func main[] : i64
|
||||
let n = 0
|
||||
let i = 1
|
||||
while true
|
||||
if math.is_prime(i)
|
||||
found = found + 1
|
||||
if found == 10001
|
||||
io.println_i64(i)
|
||||
break
|
||||
n = n + i
|
||||
if num_divisors(n) > 500
|
||||
io.println_i64(n)
|
||||
break
|
||||
i = i + 1
|
||||
@@ -1,14 +1,6 @@
|
||||
func main[] : i64
|
||||
let n: str = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
|
||||
|
||||
let out: i64 = 0
|
||||
let max: i64 = str.len(n) - 13
|
||||
for i in 0..max
|
||||
let s: i64 = 1
|
||||
let j: i64 = 0
|
||||
while j < 13
|
||||
s = s * (n[i + j] - '0')
|
||||
j = j + 1
|
||||
if s > out
|
||||
out = s
|
||||
io.println_i64(out)
|
||||
// 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()
|
||||
|> str.substr(0, 10)
|
||||
|> io.println()
|
||||
@@ -1,7 +1,22 @@
|
||||
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 = 1
|
||||
while n != 1
|
||||
n = collatz_num(n)
|
||||
i = i + 1
|
||||
return i
|
||||
|
||||
func main[] : i64
|
||||
for a in 1..1000
|
||||
for b in 1..1000
|
||||
let c: i64 = 1000 - b - a
|
||||
if a * a + b * b == c * c
|
||||
io.println_i64(a * b * c)
|
||||
return 0
|
||||
let max = 0
|
||||
let max_index = 0
|
||||
|
||||
for i in 1..1000000
|
||||
let seq: i64 = collatz_seq(i)
|
||||
if seq > max
|
||||
max = seq
|
||||
max_index = i
|
||||
io.println_i64(max_index)
|
||||
9
examples/puzzles/euler_15.zr
Normal file
9
examples/puzzles/euler_15.zr
Normal file
@@ -0,0 +1,9 @@
|
||||
func main[] : i64
|
||||
let n = 40
|
||||
let r = 20
|
||||
let out = 1
|
||||
|
||||
for i in 1..r+1
|
||||
out = out * (n - (r - i)) / i
|
||||
|
||||
io.println_i64(out)
|
||||
19
examples/puzzles/euler_16.zr
Normal file
19
examples/puzzles/euler_16.zr
Normal file
@@ -0,0 +1,19 @@
|
||||
func main[] : i64
|
||||
let n: array = []
|
||||
array.push(n, 1)
|
||||
|
||||
for j in 0..1000
|
||||
let carry = 0
|
||||
for i in 0..array.size(n)
|
||||
let 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
|
||||
for i in 0..array.size(n)
|
||||
sum = sum + array.nth(n, i)
|
||||
|
||||
io.println_i64(sum)
|
||||
26
examples/puzzles/euler_17.zr
Normal file
26
examples/puzzles/euler_17.zr
Normal file
@@ -0,0 +1,26 @@
|
||||
func main[] : i64
|
||||
let s1: array = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
|
||||
let s2: array = [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
|
||||
let s3: array = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]
|
||||
|
||||
let sum = 0
|
||||
|
||||
for i in 1..10
|
||||
sum = sum + array.nth(s1, i)
|
||||
for i in 0..10
|
||||
sum = sum + array.nth(s2, i)
|
||||
for i in 20..100
|
||||
sum = sum + array.nth(s3, i / 10) + array.nth(s1, i % 10)
|
||||
|
||||
for i in 1..10
|
||||
sum = sum + array.nth(s1, i) + 7
|
||||
for j in 1..10
|
||||
sum = sum + array.nth(s1, i) + 7 + 3 + array.nth(s1, j)
|
||||
for j in 0..10
|
||||
sum = sum + array.nth(s1, i) + 7 + 3 + array.nth(s2, j)
|
||||
for j in 20..100
|
||||
sum = sum + array.nth(s1, i) + 7 + 3 + array.nth(s3, j / 10) + array.nth(s1, j % 10)
|
||||
|
||||
sum = sum + array.nth(s1, 1) + 8
|
||||
|
||||
io.println_i64(sum)
|
||||
28
examples/puzzles/euler_18.zr
Normal file
28
examples/puzzles/euler_18.zr
Normal file
@@ -0,0 +1,28 @@
|
||||
func findmax[triangle: array, row: i64, col: i64] : i64
|
||||
if row == 14
|
||||
return array.nth(array.nth(triangle, row), col)
|
||||
|
||||
let left: i64 = findmax(triangle, row + 1, col)
|
||||
let right: i64 = findmax(triangle, row + 1, col + 1)
|
||||
|
||||
return array.nth(array.nth(triangle, row), col) + math.max(left, right)
|
||||
|
||||
func main[] : i64
|
||||
let triangle: array = []
|
||||
array.push(triangle, [75])
|
||||
array.push(triangle, [95, 64])
|
||||
array.push(triangle, [17, 47, 82])
|
||||
array.push(triangle, [18, 35, 87, 10])
|
||||
array.push(triangle, [20, 4, 82, 47, 65])
|
||||
array.push(triangle, [19, 1, 23, 75, 3, 34])
|
||||
array.push(triangle, [88, 2, 77, 73, 7, 63, 67])
|
||||
array.push(triangle, [99, 65, 4, 28, 6, 16, 70, 92])
|
||||
array.push(triangle, [41, 41, 26, 56, 83, 40, 80, 70, 33])
|
||||
array.push(triangle, [41, 48, 72, 33, 47, 32, 37, 16, 94, 29])
|
||||
array.push(triangle, [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14])
|
||||
array.push(triangle, [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57])
|
||||
array.push(triangle, [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48])
|
||||
array.push(triangle, [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31])
|
||||
array.push(triangle, [4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23])
|
||||
|
||||
io.println_i64(findmax(triangle, 0, 0))
|
||||
22
examples/puzzles/euler_19.zr
Normal file
22
examples/puzzles/euler_19.zr
Normal file
@@ -0,0 +1,22 @@
|
||||
func days[y: i64, m: i64] : i64
|
||||
if m == 2
|
||||
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
|
||||
return 29
|
||||
else
|
||||
return 28
|
||||
else if (m == 4) || (m == 6) || (m == 9) || (m == 11)
|
||||
return 30
|
||||
else
|
||||
return 31
|
||||
|
||||
func main[] : i64
|
||||
let wday = 0
|
||||
let sun = 0
|
||||
|
||||
for year in 1901..2001
|
||||
for mon in 1..13
|
||||
if wday == 5
|
||||
sun = sun + 1
|
||||
wday = (wday + days(year, mon)) % 7
|
||||
|
||||
io.println_i64(sun)
|
||||
21
examples/puzzles/euler_20.zr
Normal file
21
examples/puzzles/euler_20.zr
Normal file
@@ -0,0 +1,21 @@
|
||||
func multiply[n: array, x: i64] : void
|
||||
let carry = 0
|
||||
for i in 0..array.size(n)
|
||||
let prod: i64 = array.nth(n, i) * x + carry
|
||||
array.set(n, i, prod % 10)
|
||||
carry = prod / 10
|
||||
while carry > 0
|
||||
array.push(n, carry % 10)
|
||||
carry = carry / 10
|
||||
|
||||
func main[] : i64
|
||||
let n: array = [1]
|
||||
|
||||
for i in 2..101
|
||||
multiply(n, i)
|
||||
|
||||
let sum = 0
|
||||
for i in 0..array.size(n)
|
||||
sum = sum + array.nth(n, i)
|
||||
|
||||
io.println_i64(sum)
|
||||
21
examples/puzzles/euler_21.zr
Normal file
21
examples/puzzles/euler_21.zr
Normal file
@@ -0,0 +1,21 @@
|
||||
func divisors_sum[n: i64] : i64
|
||||
let k: i64 = n
|
||||
let sum = 1
|
||||
|
||||
for i in 2..k+1
|
||||
let p = 1
|
||||
while k % i == 0
|
||||
p = p * i
|
||||
k = k / i
|
||||
sum = sum * (p * i - 1) / (i - 1)
|
||||
return sum - n
|
||||
|
||||
func main[] : i64
|
||||
let sum = 0
|
||||
|
||||
for i in 2..10000
|
||||
let d: i64 = divisors_sum(i)
|
||||
if i < d && i == divisors_sum(d)
|
||||
sum = sum + i + d
|
||||
|
||||
io.println_i64(sum)
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : i64
|
||||
let arr: array = []
|
||||
for i in 0..10
|
||||
array.push(arr, math.abs(os.urandom() % 1000))
|
||||
array.push(arr, math.abs(os.urandom_i64()) % 1000)
|
||||
|
||||
for i in 0..array.size(arr)
|
||||
io.println_i64(array.nth(arr, i))
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// needs to be compiled with -m -C="/usr/local/lib/libraylib.a -lm"
|
||||
|
||||
extern InitWindow
|
||||
extern SetTargetFPS
|
||||
extern WindowShouldClose
|
||||
@@ -11,8 +10,8 @@ extern DrawRectangle
|
||||
extern IsKeyDown
|
||||
|
||||
func main[] : i64
|
||||
let x: i64 = 200
|
||||
let y: i64 = 200
|
||||
let x = 200
|
||||
let y = 200
|
||||
|
||||
InitWindow(800, 600, "Hello, World!")
|
||||
SetTargetFPS(60)
|
||||
|
||||
@@ -11,7 +11,7 @@ func rule110_step[state: array] : array
|
||||
if i + 1 < state_len
|
||||
right = array.nth(state, i + 1)
|
||||
|
||||
array.push(new_state, !((!left & !center & !right) | (left & !center & !right) | (left & center & right)))
|
||||
array.push(new_state, !((!left && !center && !right) || (left && !center && !right) || (left && center && right)))
|
||||
|
||||
return new_state
|
||||
|
||||
@@ -24,7 +24,7 @@ func print_state[state: array]: void
|
||||
io.println("")
|
||||
|
||||
func main[] : i64
|
||||
let SIZE: i64 = 60
|
||||
let SIZE = 60
|
||||
|
||||
let state: array = []
|
||||
for i in 0..SIZE
|
||||
|
||||
@@ -11,11 +11,11 @@ extern sqlite3_column_text
|
||||
extern sqlite3_finalize
|
||||
|
||||
func main[] : i64
|
||||
let rc: i64 = 0
|
||||
let db: i64 = 0
|
||||
let stmt: i64 = 0
|
||||
let rc = 0
|
||||
let db = 0
|
||||
let stmt = 0
|
||||
|
||||
rc = sqlite3_open("todo.db", @db)
|
||||
rc = sqlite3_open("todo.db", ^db)
|
||||
if rc
|
||||
dbg.panic("failed to open db")
|
||||
|
||||
@@ -36,7 +36,7 @@ func main[] : i64
|
||||
break
|
||||
else if choice == 1
|
||||
io.println("============")
|
||||
sqlite3_prepare_v2(db, "SELECT * FROM todo", -1, @stmt, 0)
|
||||
sqlite3_prepare_v2(db, "SELECT * FROM todo", -1, ^stmt, 0)
|
||||
|
||||
while sqlite3_step(stmt) == 100
|
||||
let id: i64 = sqlite3_column_int(stmt, 0)
|
||||
@@ -51,7 +51,7 @@ func main[] : i64
|
||||
io.print("\nEnter new task: ")
|
||||
let task: str = io.read_line() |> str.trim()
|
||||
|
||||
sqlite3_prepare_v2(db, "INSERT INTO todo(task) VALUES (?);", -1, @stmt, 0)
|
||||
sqlite3_prepare_v2(db, "INSERT INTO todo(task) VALUES (?);", -1, ^stmt, 0)
|
||||
sqlite3_bind_text(stmt, 1, task, -1, 0)
|
||||
if sqlite3_step(stmt) != 101
|
||||
dbg.panic(sqlite3_errmsg(db))
|
||||
@@ -61,7 +61,7 @@ func main[] : i64
|
||||
io.print("\nEnter task id: ")
|
||||
let id: i64 = io.read_line() |> str.parse_i64()
|
||||
|
||||
sqlite3_prepare_v2(db, "DELETE FROM todo WHERE id = ?;", -1, @stmt, 0)
|
||||
sqlite3_prepare_v2(db, "DELETE FROM todo WHERE id = ?;", -1, ^stmt, 0)
|
||||
sqlite3_bind_int(stmt, 1, id, -1, 0)
|
||||
if sqlite3_step(stmt) != 101
|
||||
dbg.panic(sqlite3_errmsg(db))
|
||||
|
||||
@@ -7,12 +7,14 @@ use crate::{
|
||||
|
||||
pub struct Analyzer {
|
||||
pub functions: HashMap<String, i32>,
|
||||
pub constants: HashMap<String, u64>,
|
||||
}
|
||||
|
||||
impl Analyzer {
|
||||
pub fn new() -> Analyzer {
|
||||
Analyzer {
|
||||
functions: HashMap::new(),
|
||||
constants: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +46,18 @@ impl Analyzer {
|
||||
} => {
|
||||
self.analyze_expr(initializer)?;
|
||||
}
|
||||
Stmt::Const { name, value } => {
|
||||
if self.constants.contains_key(&name.lexeme)
|
||||
|| self.functions.contains_key(&name.lexeme)
|
||||
{
|
||||
return error!(
|
||||
name.loc,
|
||||
format!("tried to redefine constant '{}'", name.lexeme)
|
||||
);
|
||||
}
|
||||
self.constants
|
||||
.insert(name.lexeme.clone(), value.lexeme.parse().unwrap());
|
||||
}
|
||||
Stmt::Block(statements) => {
|
||||
for stmt in statements {
|
||||
self.analyze_stmt(stmt)?;
|
||||
@@ -106,6 +120,10 @@ impl Analyzer {
|
||||
self.analyze_expr(left)?;
|
||||
self.analyze_expr(right)?;
|
||||
}
|
||||
Expr::Logical { left, op: _, right } => {
|
||||
self.analyze_expr(left)?;
|
||||
self.analyze_expr(right)?;
|
||||
}
|
||||
Expr::Grouping(expr) => self.analyze_expr(expr)?,
|
||||
Expr::Literal(_) => {}
|
||||
Expr::Unary { op: _, right } => {
|
||||
|
||||
@@ -15,6 +15,7 @@ pub struct Env {
|
||||
next_offset: usize,
|
||||
loop_begin_label: String,
|
||||
loop_end_label: String,
|
||||
loop_continue_label: String,
|
||||
}
|
||||
|
||||
impl Env {
|
||||
@@ -24,6 +25,7 @@ impl Env {
|
||||
next_offset: 8,
|
||||
loop_begin_label: String::new(),
|
||||
loop_end_label: String::new(),
|
||||
loop_continue_label: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,12 +101,6 @@ impl<'a> CodegenX86_64<'a> {
|
||||
"section .note.GNU-stack
|
||||
db 0
|
||||
|
||||
section .text._builtin_read8
|
||||
_builtin_read8:
|
||||
xor rax, rax
|
||||
mov al, byte [rdi]
|
||||
ret
|
||||
|
||||
section .text._builtin_read64
|
||||
_builtin_read64:
|
||||
mov rax, qword [rdi]
|
||||
@@ -142,7 +138,7 @@ _builtin_environ:
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn compile_stmt(&mut self, env: &mut Env, stmt: Stmt) -> Result<(), ZernError> {
|
||||
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 {
|
||||
@@ -158,10 +154,27 @@ _builtin_environ:
|
||||
);
|
||||
}
|
||||
|
||||
let var_type: String = match var_type {
|
||||
Some(t) => t.lexeme.clone(),
|
||||
None => match &initializer {
|
||||
Expr::Literal(token) => {
|
||||
if token.token_type == TokenType::Number {
|
||||
"i64".into()
|
||||
} else {
|
||||
return error!(&name.loc, "unable to infer variable type");
|
||||
}
|
||||
}
|
||||
_ => return error!(&name.loc, "unable to infer variable type"),
|
||||
},
|
||||
};
|
||||
|
||||
self.compile_expr(env, initializer)?;
|
||||
let offset = env.define_var(name.lexeme.clone(), var_type.lexeme);
|
||||
let offset = env.define_var(name.lexeme.clone(), var_type);
|
||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
||||
}
|
||||
Stmt::Const { name: _, value: _ } => {
|
||||
// handled in the analyzer
|
||||
}
|
||||
Stmt::Block(statements) => {
|
||||
env.push_scope();
|
||||
for stmt in statements {
|
||||
@@ -180,28 +193,31 @@ _builtin_environ:
|
||||
self.compile_expr(env, condition)?;
|
||||
emit!(&mut self.output, " test rax, rax");
|
||||
emit!(&mut self.output, " je {}", else_label);
|
||||
self.compile_stmt(env, *then_branch.clone())?;
|
||||
self.compile_stmt(env, then_branch)?;
|
||||
emit!(&mut self.output, " jmp {}", end_label);
|
||||
emit!(&mut self.output, "{}:", else_label);
|
||||
self.compile_stmt(env, *else_branch.clone())?;
|
||||
self.compile_stmt(env, else_branch)?;
|
||||
emit!(&mut self.output, "{}:", end_label);
|
||||
}
|
||||
Stmt::While { condition, body } => {
|
||||
let old_loop_begin_label = env.loop_begin_label.clone();
|
||||
let old_loop_end_label = env.loop_end_label.clone();
|
||||
let old_loop_continue_label = env.loop_continue_label.clone();
|
||||
env.loop_begin_label = self.label();
|
||||
env.loop_end_label = self.label();
|
||||
env.loop_continue_label = env.loop_begin_label.clone();
|
||||
|
||||
emit!(&mut self.output, "{}:", env.loop_begin_label);
|
||||
self.compile_expr(env, condition)?;
|
||||
emit!(&mut self.output, " test rax, rax");
|
||||
emit!(&mut self.output, " je {}", env.loop_end_label);
|
||||
self.compile_stmt(env, *body.clone())?;
|
||||
self.compile_stmt(env, body)?;
|
||||
emit!(&mut self.output, " jmp {}", env.loop_begin_label);
|
||||
emit!(&mut self.output, "{}:", env.loop_end_label);
|
||||
|
||||
env.loop_begin_label = old_loop_begin_label;
|
||||
env.loop_end_label = old_loop_end_label;
|
||||
env.loop_continue_label = old_loop_continue_label;
|
||||
}
|
||||
Stmt::Function {
|
||||
name,
|
||||
@@ -210,7 +226,7 @@ _builtin_environ:
|
||||
body,
|
||||
exported,
|
||||
} => {
|
||||
if exported || name.lexeme == "main" {
|
||||
if *exported || name.lexeme == "main" {
|
||||
emit!(&mut self.output, "global {}", name.lexeme);
|
||||
}
|
||||
emit!(&mut self.output, "section .text.{}", name.lexeme);
|
||||
@@ -222,14 +238,20 @@ _builtin_environ:
|
||||
for (i, param) in params.iter().enumerate() {
|
||||
let offset = env
|
||||
.define_var(param.var_name.lexeme.clone(), param.var_type.lexeme.clone());
|
||||
let reg = match REGISTERS.get(i) {
|
||||
Some(x) => x,
|
||||
None => return error!(&name.loc, "only up to 6 params allowed"),
|
||||
};
|
||||
emit!(&mut self.output, " mov QWORD [rbp-{}], {}", offset, reg);
|
||||
if let Some(reg) = REGISTERS.get(i) {
|
||||
emit!(&mut self.output, " mov QWORD [rbp-{}], {}", offset, reg);
|
||||
} else {
|
||||
let stack_offset = 16 + 8 * (i - REGISTERS.len());
|
||||
emit!(
|
||||
&mut self.output,
|
||||
" mov rax, QWORD [rbp+{}]",
|
||||
stack_offset
|
||||
);
|
||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
||||
}
|
||||
}
|
||||
|
||||
self.compile_stmt(env, *body)?;
|
||||
self.compile_stmt(env, body)?;
|
||||
|
||||
// fallback to null
|
||||
// very hacky but works
|
||||
@@ -254,11 +276,13 @@ _builtin_environ:
|
||||
} => {
|
||||
let old_loop_begin_label = env.loop_begin_label.clone();
|
||||
let old_loop_end_label = env.loop_end_label.clone();
|
||||
let old_loop_continue_label = env.loop_continue_label.clone();
|
||||
env.loop_begin_label = self.label();
|
||||
env.loop_end_label = self.label();
|
||||
env.loop_continue_label = self.label();
|
||||
|
||||
env.push_scope();
|
||||
let offset = env.define_var(var.lexeme, "i64".into());
|
||||
let offset = env.define_var(var.lexeme.clone(), "i64".into());
|
||||
|
||||
self.compile_expr(env, start)?;
|
||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
||||
@@ -269,7 +293,8 @@ _builtin_environ:
|
||||
emit!(&mut self.output, " pop rcx");
|
||||
emit!(&mut self.output, " cmp rcx, rax");
|
||||
emit!(&mut self.output, " jge {}", env.loop_end_label);
|
||||
self.compile_stmt(env, *body)?;
|
||||
self.compile_stmt(env, body)?;
|
||||
emit!(&mut self.output, "{}:", env.loop_continue_label);
|
||||
emit!(&mut self.output, " mov rax, QWORD [rbp-{}]", offset);
|
||||
emit!(&mut self.output, " add rax, 1");
|
||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
||||
@@ -279,13 +304,13 @@ _builtin_environ:
|
||||
|
||||
env.loop_begin_label = old_loop_begin_label;
|
||||
env.loop_end_label = old_loop_end_label;
|
||||
env.loop_continue_label = old_loop_continue_label;
|
||||
}
|
||||
Stmt::Break => {
|
||||
emit!(&mut self.output, " jmp {}", env.loop_end_label);
|
||||
}
|
||||
Stmt::Continue => {
|
||||
// TODO: skips incrementing when used in a for loop
|
||||
emit!(&mut self.output, " jmp {}", env.loop_begin_label);
|
||||
emit!(&mut self.output, " jmp {}", env.loop_continue_label);
|
||||
}
|
||||
Stmt::Extern(name) => {
|
||||
emit!(&mut self.output, "extern {}", name.lexeme);
|
||||
@@ -294,12 +319,12 @@ _builtin_environ:
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn compile_expr(&mut self, env: &mut Env, expr: Expr) -> Result<(), ZernError> {
|
||||
pub fn compile_expr(&mut self, env: &mut Env, expr: &Expr) -> Result<(), ZernError> {
|
||||
match expr {
|
||||
Expr::Binary { left, op, right } => {
|
||||
self.compile_expr(env, *left)?;
|
||||
self.compile_expr(env, left)?;
|
||||
emit!(&mut self.output, " push rax");
|
||||
self.compile_expr(env, *right)?;
|
||||
self.compile_expr(env, right)?;
|
||||
emit!(&mut self.output, " mov rbx, rax");
|
||||
emit!(&mut self.output, " pop rax");
|
||||
|
||||
@@ -372,7 +397,26 @@ _builtin_environ:
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
Expr::Grouping(expr) => self.compile_expr(env, *expr)?,
|
||||
Expr::Logical { left, op, right } => {
|
||||
let end_label = self.label();
|
||||
match op.token_type {
|
||||
TokenType::LogicalAnd => {
|
||||
self.compile_expr(env, left)?;
|
||||
emit!(&mut self.output, " test rax, rax");
|
||||
emit!(&mut self.output, " je {}", end_label);
|
||||
self.compile_expr(env, right)?;
|
||||
}
|
||||
TokenType::LogicalOr => {
|
||||
self.compile_expr(env, left)?;
|
||||
emit!(&mut self.output, " test rax, rax");
|
||||
emit!(&mut self.output, " jne {}", end_label);
|
||||
self.compile_expr(env, right)?;
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
emit!(&mut self.output, "{}:", end_label);
|
||||
}
|
||||
Expr::Grouping(expr) => self.compile_expr(env, expr)?,
|
||||
Expr::Literal(token) => match token.token_type {
|
||||
TokenType::Number => {
|
||||
emit!(&mut self.output, " mov rax, {}", token.lexeme);
|
||||
@@ -417,7 +461,7 @@ _builtin_environ:
|
||||
_ => unreachable!(),
|
||||
},
|
||||
Expr::Unary { op, right } => {
|
||||
self.compile_expr(env, *right)?;
|
||||
self.compile_expr(env, right)?;
|
||||
match op.token_type {
|
||||
TokenType::Minus => {
|
||||
emit!(&mut self.output, " neg rax");
|
||||
@@ -431,21 +475,32 @@ _builtin_environ:
|
||||
}
|
||||
}
|
||||
Expr::Variable(name) => {
|
||||
// TODO: move to analyzer
|
||||
let var = match env.get_var(&name.lexeme) {
|
||||
Some(x) => x,
|
||||
None => {
|
||||
return error!(name.loc, format!("undefined variable: {}", &name.lexeme));
|
||||
}
|
||||
};
|
||||
emit!(
|
||||
&mut self.output,
|
||||
" mov rax, QWORD [rbp-{}]",
|
||||
var.stack_offset,
|
||||
);
|
||||
if self.analyzer.constants.contains_key(&name.lexeme) {
|
||||
emit!(
|
||||
&mut self.output,
|
||||
" mov rax, {}",
|
||||
self.analyzer.constants[&name.lexeme]
|
||||
);
|
||||
} else {
|
||||
// TODO: move to analyzer
|
||||
let var = match env.get_var(&name.lexeme) {
|
||||
Some(x) => x,
|
||||
None => {
|
||||
return error!(
|
||||
name.loc,
|
||||
format!("undefined variable: {}", &name.lexeme)
|
||||
);
|
||||
}
|
||||
};
|
||||
emit!(
|
||||
&mut self.output,
|
||||
" mov rax, QWORD [rbp-{}]",
|
||||
var.stack_offset,
|
||||
);
|
||||
}
|
||||
}
|
||||
Expr::Assign { name, value } => {
|
||||
self.compile_expr(env, *value)?;
|
||||
self.compile_expr(env, value)?;
|
||||
|
||||
// TODO: move to analyzer
|
||||
let var = match env.get_var(&name.lexeme) {
|
||||
@@ -465,8 +520,8 @@ _builtin_environ:
|
||||
paren: _,
|
||||
args,
|
||||
} => {
|
||||
for arg in &args {
|
||||
self.compile_expr(env, arg.clone())?;
|
||||
for arg in args {
|
||||
self.compile_expr(env, arg)?;
|
||||
emit!(&mut self.output, " push rax");
|
||||
}
|
||||
|
||||
@@ -489,12 +544,16 @@ _builtin_environ:
|
||||
for i in 0..num_stack {
|
||||
let arg_idx = arg_count - 1 - i;
|
||||
let offset = 8 * (arg_count - 1 - arg_idx);
|
||||
emit!(&mut self.output, " mov rax, QWORD [rsp + {}]", offset);
|
||||
emit!(
|
||||
&mut self.output,
|
||||
" mov rax, QWORD [rsp + {}]",
|
||||
offset + 8 * i
|
||||
);
|
||||
emit!(&mut self.output, " push rax");
|
||||
}
|
||||
}
|
||||
|
||||
if let Expr::Variable(callee_name) = &*callee {
|
||||
if let Expr::Variable(callee_name) = &**callee {
|
||||
if callee_name.lexeme.starts_with("_builtin_")
|
||||
|| self.analyzer.functions.contains_key(&callee_name.lexeme)
|
||||
{
|
||||
@@ -502,12 +561,12 @@ _builtin_environ:
|
||||
emit!(&mut self.output, " call {}", callee_name.lexeme);
|
||||
} else {
|
||||
// its a variable containing function address
|
||||
self.compile_expr(env, *callee)?;
|
||||
self.compile_expr(env, callee)?;
|
||||
emit!(&mut self.output, " call rax");
|
||||
}
|
||||
} else {
|
||||
// its an expression that evalutes to function address
|
||||
self.compile_expr(env, *callee)?;
|
||||
self.compile_expr(env, callee)?;
|
||||
emit!(&mut self.output, " call rax");
|
||||
}
|
||||
|
||||
@@ -531,13 +590,14 @@ _builtin_environ:
|
||||
emit!(&mut self.output, " pop rax");
|
||||
}
|
||||
Expr::Index { expr, index } => {
|
||||
self.compile_expr(env, *expr)?;
|
||||
emit!(&mut self.output, " mov rdi, rax");
|
||||
self.compile_expr(env, *index)?;
|
||||
emit!(&mut self.output, " add rdi, rax");
|
||||
emit!(&mut self.output, " call _builtin_read8");
|
||||
self.compile_expr(env, expr)?;
|
||||
emit!(&mut self.output, " push rax");
|
||||
self.compile_expr(env, index)?;
|
||||
emit!(&mut self.output, " pop rbx");
|
||||
emit!(&mut self.output, " add rax, rbx");
|
||||
emit!(&mut self.output, " movzx rax, BYTE [rax]");
|
||||
}
|
||||
Expr::AddrOf { op, expr } => match *expr {
|
||||
Expr::AddrOf { op, expr } => match *expr.clone() {
|
||||
Expr::Variable(name) => {
|
||||
if self.analyzer.functions.contains_key(&name.lexeme) {
|
||||
emit!(&mut self.output, " mov rax, {}", name.lexeme);
|
||||
|
||||
21
src/main.rs
21
src/main.rs
@@ -33,7 +33,8 @@ fn compile_file_to(
|
||||
}
|
||||
|
||||
for stmt in statements {
|
||||
codegen.compile_stmt(&mut codegen_x86_64::Env::new(), stmt)?;
|
||||
// top level statements are all function/const/extern declarations so a new env for each
|
||||
codegen.compile_stmt(&mut codegen_x86_64::Env::new(), &stmt)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -63,7 +64,17 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
|
||||
let mut analyzer = analyzer::Analyzer::new();
|
||||
let mut codegen = codegen_x86_64::CodegenX86_64::new(&mut analyzer);
|
||||
codegen.emit_prologue()?;
|
||||
compile_file_to(&mut codegen, "std.zr", include_str!("std.zr").into())?;
|
||||
compile_file_to(
|
||||
&mut codegen,
|
||||
"syscalls.zr",
|
||||
include_str!("std/syscalls.zr").into(),
|
||||
)?;
|
||||
compile_file_to(&mut codegen, "std.zr", include_str!("std/std.zr").into())?;
|
||||
compile_file_to(
|
||||
&mut codegen,
|
||||
"crypto.zr",
|
||||
include_str!("std/crypto.zr").into(),
|
||||
)?;
|
||||
compile_file_to(&mut codegen, filename, source)?;
|
||||
|
||||
if !args.output_asm {
|
||||
@@ -71,7 +82,7 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
|
||||
|
||||
run_command(format!("nasm -f elf64 -o {}.o {}.s", args.out, args.out));
|
||||
|
||||
if args.no_musl {
|
||||
if args.use_glibc {
|
||||
run_command(format!(
|
||||
"gcc -no-pie -o {} {}.o -flto -Wl,--gc-sections {}",
|
||||
args.out, args.out, args.cflags
|
||||
@@ -112,8 +123,8 @@ struct Args {
|
||||
#[arg(short = 'r', help = "Run the compiled executable")]
|
||||
run_exe: bool,
|
||||
|
||||
#[arg(short = 'm', help = "Don't use musl")]
|
||||
no_musl: bool,
|
||||
#[arg(short = 'm', help = "Use glibc instead of musl")]
|
||||
use_glibc: bool,
|
||||
|
||||
#[arg(short = 'C', default_value = "", help = "Extra flags to pass to gcc")]
|
||||
cflags: String,
|
||||
|
||||
@@ -11,9 +11,13 @@ pub enum Stmt {
|
||||
Expression(Expr),
|
||||
Let {
|
||||
name: Token,
|
||||
var_type: Token,
|
||||
var_type: Option<Token>,
|
||||
initializer: Expr,
|
||||
},
|
||||
Const {
|
||||
name: Token,
|
||||
value: Token,
|
||||
},
|
||||
Block(Vec<Stmt>),
|
||||
If {
|
||||
condition: Expr,
|
||||
@@ -50,6 +54,11 @@ pub enum Expr {
|
||||
op: Token,
|
||||
right: Box<Expr>,
|
||||
},
|
||||
Logical {
|
||||
left: Box<Expr>,
|
||||
op: Token,
|
||||
right: Box<Expr>,
|
||||
},
|
||||
Grouping(Box<Expr>),
|
||||
Literal(Token),
|
||||
Unary {
|
||||
@@ -115,6 +124,9 @@ impl Parser {
|
||||
if self.match_token(&[TokenType::KeywordExtern]) {
|
||||
return self.extern_declaration();
|
||||
}
|
||||
if self.match_token(&[TokenType::KeywordConst]) {
|
||||
return self.const_declaration();
|
||||
}
|
||||
return error!(
|
||||
self.peek().loc,
|
||||
"statements not allowed outside function body"
|
||||
@@ -172,12 +184,16 @@ impl Parser {
|
||||
|
||||
fn let_declaration(&mut self) -> Result<Stmt, ZernError> {
|
||||
let name = self.consume(TokenType::Identifier, "expected variable name")?;
|
||||
self.consume(TokenType::Colon, "expected ':' after variable name")?;
|
||||
|
||||
let var_type = self.consume(TokenType::Identifier, "expected variable type")?;
|
||||
if !TYPES.contains(&var_type.lexeme.as_str()) {
|
||||
return error!(&name.loc, format!("unknown type: {}", var_type.lexeme));
|
||||
}
|
||||
let var_type = if self.match_token(&[TokenType::Colon]) {
|
||||
let token = self.consume(TokenType::Identifier, "expected variable type")?;
|
||||
if !TYPES.contains(&token.lexeme.as_str()) {
|
||||
return error!(&name.loc, format!("unknown type: {}", token.lexeme));
|
||||
}
|
||||
Some(token)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
self.consume(TokenType::Equal, "expected '=' after variable type")?;
|
||||
let initializer = self.expression()?;
|
||||
@@ -188,6 +204,13 @@ impl Parser {
|
||||
})
|
||||
}
|
||||
|
||||
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")?;
|
||||
let value = self.consume(TokenType::Number, "expected a number after '='")?;
|
||||
Ok(Stmt::Const { name, value })
|
||||
}
|
||||
|
||||
fn extern_declaration(&mut self) -> Result<Stmt, ZernError> {
|
||||
Ok(Stmt::Extern(
|
||||
self.consume(TokenType::Identifier, "expected extern name")?,
|
||||
@@ -323,10 +346,10 @@ impl Parser {
|
||||
fn or_and(&mut self) -> Result<Expr, ZernError> {
|
||||
let mut expr = self.equality()?;
|
||||
|
||||
while self.match_token(&[TokenType::BitOr, TokenType::BitAnd]) {
|
||||
while self.match_token(&[TokenType::LogicalOr, TokenType::LogicalAnd]) {
|
||||
let op = self.previous().clone();
|
||||
let right = self.equality()?;
|
||||
expr = Expr::Binary {
|
||||
expr = Expr::Logical {
|
||||
left: Box::new(expr),
|
||||
op,
|
||||
right: Box::new(right),
|
||||
@@ -376,7 +399,13 @@ impl Parser {
|
||||
fn term(&mut self) -> Result<Expr, ZernError> {
|
||||
let mut expr = self.factor()?;
|
||||
|
||||
while self.match_token(&[TokenType::Plus, TokenType::Minus, TokenType::Xor]) {
|
||||
while self.match_token(&[
|
||||
TokenType::Plus,
|
||||
TokenType::Minus,
|
||||
TokenType::Xor,
|
||||
TokenType::BitAnd,
|
||||
TokenType::BitOr,
|
||||
]) {
|
||||
let op = self.previous().clone();
|
||||
let right = self.factor()?;
|
||||
expr = Expr::Binary {
|
||||
@@ -412,7 +441,7 @@ impl Parser {
|
||||
}
|
||||
|
||||
fn unary(&mut self) -> Result<Expr, ZernError> {
|
||||
if self.match_token(&[TokenType::At]) {
|
||||
if self.match_token(&[TokenType::Xor]) {
|
||||
let op = self.previous().clone();
|
||||
let right = self.unary()?;
|
||||
return Ok(Expr::AddrOf {
|
||||
|
||||
406
src/std/crypto.zr
Normal file
406
src/std/crypto.zr
Normal file
@@ -0,0 +1,406 @@
|
||||
func crypto.rotl32[x: i64, r: i64] : i64
|
||||
return ((x << r) | (x >> (32 - r))) & 0xffffffff
|
||||
|
||||
func crypto.rotr64[x: i64, y: i64] : i64
|
||||
y = y & 63
|
||||
if y == 0
|
||||
return x
|
||||
let lhs_mask: i64 = ((1 << (64 - y)) - 1)
|
||||
let r: i64 = (x >> y) & lhs_mask
|
||||
let l: i64 = (x << (64 - y))
|
||||
return (r | l)
|
||||
|
||||
func crypto.blake2b._G[v: ptr, a: i64, b: i64, c: i64, d: i64, x: i64, y: i64] : void
|
||||
mem.write64(v + a * 8, mem.read64(v + a * 8) + mem.read64(v + b * 8) + x)
|
||||
mem.write64(v + d * 8, crypto.rotr64(mem.read64(v + d * 8) ^ mem.read64(v + a * 8), 32))
|
||||
mem.write64(v + c * 8, mem.read64(v + c * 8) + mem.read64(v + d * 8))
|
||||
mem.write64(v + b * 8, crypto.rotr64(mem.read64(v + b * 8) ^ mem.read64(v + c * 8), 24))
|
||||
mem.write64(v + a * 8, mem.read64(v + a * 8) + mem.read64(v + b * 8) + y)
|
||||
mem.write64(v + d * 8, crypto.rotr64(mem.read64(v + d * 8) ^ mem.read64(v + a * 8), 16))
|
||||
mem.write64(v + c * 8, mem.read64(v + c * 8) + mem.read64(v + d * 8))
|
||||
mem.write64(v + b * 8, crypto.rotr64(mem.read64(v + b * 8) ^ mem.read64(v + c * 8), 63))
|
||||
|
||||
func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, iv: array] : void
|
||||
let v: ptr = mem.alloc(16 * 8)
|
||||
let m: ptr = mem.alloc(16 * 8)
|
||||
// https://crypto.stackexchange.com/questions/108987/rationale-for-blake2-message-schedule
|
||||
let sigma: array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3,11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4,7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8,9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13,2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9,12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11,13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10,6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5,10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3]
|
||||
|
||||
for j in 0..8
|
||||
mem.write64(v + j * 8, mem.read64(h + j * 8))
|
||||
mem.write64(v + (j + 8) * 8, array.nth(iv, j))
|
||||
|
||||
mem.write64(v + 12 * 8, mem.read64(v + 12 * 8) ^ t0)
|
||||
mem.write64(v + 13 * 8, mem.read64(v + 13 * 8) ^ t1)
|
||||
if last
|
||||
mem.write64(v + 14 * 8, mem.read64(v + 14 * 8) ^ 0xffffffffffffffff)
|
||||
|
||||
for j in 0..16
|
||||
let w = 0
|
||||
for k in 0..8
|
||||
w = w | ((block[j * 8 + k] & 0xff) << (8 * k))
|
||||
mem.write64(m + j * 8, w)
|
||||
|
||||
for r in 0..12
|
||||
let base: i64 = r * 16
|
||||
crypto.blake2b._G(v, 0, 4, 8, 12, mem.read64(m + array.nth(sigma, base + 0) * 8), mem.read64(m + array.nth(sigma, base + 1) * 8))
|
||||
crypto.blake2b._G(v, 1, 5, 9, 13, mem.read64(m + array.nth(sigma, base + 2) * 8), mem.read64(m + array.nth(sigma, base + 3) * 8))
|
||||
crypto.blake2b._G(v, 2, 6, 10, 14, mem.read64(m + array.nth(sigma, base + 4) * 8), mem.read64(m + array.nth(sigma, base + 5) * 8))
|
||||
crypto.blake2b._G(v, 3, 7, 11, 15, mem.read64(m + array.nth(sigma, base + 6) * 8), mem.read64(m + array.nth(sigma, base + 7) * 8))
|
||||
crypto.blake2b._G(v, 0, 5, 10, 15, mem.read64(m + array.nth(sigma, base + 8) * 8), mem.read64(m + array.nth(sigma, base + 9) * 8))
|
||||
crypto.blake2b._G(v, 1, 6, 11, 12, mem.read64(m + array.nth(sigma, base + 10) * 8), mem.read64(m + array.nth(sigma, base + 11) * 8))
|
||||
crypto.blake2b._G(v, 2, 7, 8, 13, mem.read64(m + array.nth(sigma, base + 12) * 8), mem.read64(m + array.nth(sigma, base + 13) * 8))
|
||||
crypto.blake2b._G(v, 3, 4, 9, 14, mem.read64(m + array.nth(sigma, base + 14) * 8), mem.read64(m + array.nth(sigma, base + 15) * 8))
|
||||
|
||||
for j in 0..8
|
||||
mem.write64(h + j * 8, mem.read64(h + j * 8) ^ (mem.read64(v + j * 8) ^ mem.read64(v + (j + 8) * 8)))
|
||||
|
||||
mem.free(v)
|
||||
mem.free(m)
|
||||
array.free(sigma)
|
||||
|
||||
func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputlen: i64] : ptr
|
||||
if outlen == 0 || outlen > 64 || keylen > 64
|
||||
dbg.panic("invalid length passed to crypto.blake2b.hash")
|
||||
let out: ptr = mem.alloc(outlen)
|
||||
|
||||
// IV[i] = floor(2**w * frac(sqrt(prime(i+1)))), where prime(i)
|
||||
// is the i:th prime number ( 2, 3, 5, 7, 11, 13, 17, 19 )
|
||||
// and sqrt(x) is the square root of x.
|
||||
// https://www.ietf.org/rfc/rfc7693#section-2.6
|
||||
let iv: array = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179]
|
||||
|
||||
let h: ptr = mem.alloc(8 * 8)
|
||||
let t0 = 0
|
||||
let t1 = 0
|
||||
let block: ptr = mem.alloc(128)
|
||||
let c = 0
|
||||
|
||||
for i in 0..8
|
||||
mem.write64(h + i * 8, array.nth(iv, i))
|
||||
|
||||
mem.write64(h, mem.read64(h) ^ (0x01010000 ^ ((keylen << 8) ^ outlen)))
|
||||
|
||||
if keylen > 0
|
||||
for i in 0..keylen
|
||||
mem.write8(block + i, key[i])
|
||||
for i in (keylen)..128
|
||||
mem.write8(block + i, 0)
|
||||
c = 128
|
||||
else
|
||||
c = 0
|
||||
|
||||
for i in 0..inputlen
|
||||
if c == 128
|
||||
t0 = t0 + c
|
||||
if t0 < c
|
||||
t1 = t1 + 1
|
||||
crypto.blake2b._compress(h, block, t0, t1, 0, iv)
|
||||
c = 0
|
||||
mem.write8(block + c, input[i])
|
||||
c = c + 1
|
||||
|
||||
t0 = t0 + c
|
||||
if t0 < c
|
||||
t1 = t1 + 1
|
||||
|
||||
if c < 128
|
||||
for i in (c)..128
|
||||
mem.write8(block + i, 0)
|
||||
crypto.blake2b._compress(h, block, t0, t1, 1, iv)
|
||||
|
||||
for i in 0..outlen
|
||||
mem.write8(out + i, ((mem.read64(h + (i >> 3) * 8) >> (8 * (i & 7))) & 0xff))
|
||||
|
||||
mem.free(h)
|
||||
mem.free(block)
|
||||
array.free(iv)
|
||||
return out
|
||||
|
||||
func crypto.chacha20._quarter_round[state: ptr, a: i64, b: i64, c: i64, d: i64] : void
|
||||
let va: i64 = mem.read32(state + a * 4)
|
||||
let vb: i64 = mem.read32(state + b * 4)
|
||||
let vc: i64 = mem.read32(state + c * 4)
|
||||
let vd: i64 = mem.read32(state + d * 4)
|
||||
va = (va + vb) & 0xffffffff
|
||||
vd = vd ^ va
|
||||
vd = crypto.rotl32(vd, 16)
|
||||
vc = (vc + vd) & 0xffffffff
|
||||
vb = vb ^ vc
|
||||
vb = crypto.rotl32(vb, 12)
|
||||
va = (va + vb) & 0xffffffff
|
||||
vd = vd ^ va
|
||||
vd = crypto.rotl32(vd, 8)
|
||||
vc = (vc + vd) & 0xffffffff
|
||||
vb = vb ^ vc
|
||||
vb = crypto.rotl32(vb, 7)
|
||||
mem.write32(state + a * 4, va)
|
||||
mem.write32(state + b * 4, vb)
|
||||
mem.write32(state + c * 4, vc)
|
||||
mem.write32(state + d * 4, vd)
|
||||
|
||||
func crypto.xchacha20._permute[state: ptr] : void
|
||||
for i in 0..10
|
||||
crypto.chacha20._quarter_round(state, 0, 4, 8, 12)
|
||||
crypto.chacha20._quarter_round(state, 1, 5, 9, 13)
|
||||
crypto.chacha20._quarter_round(state, 2, 6, 10, 14)
|
||||
crypto.chacha20._quarter_round(state, 3, 7, 11, 15)
|
||||
crypto.chacha20._quarter_round(state, 0, 5, 10, 15)
|
||||
crypto.chacha20._quarter_round(state, 1, 6, 11, 12)
|
||||
crypto.chacha20._quarter_round(state, 2, 7, 8, 13)
|
||||
crypto.chacha20._quarter_round(state, 3, 4, 9, 14)
|
||||
|
||||
func crypto.xchacha20._block[key: ptr, nonce: ptr, blocknum: i64, out: ptr] : void
|
||||
let sigma: str = "expand 32-byte k"
|
||||
let state: ptr = mem.alloc(16 * 4)
|
||||
|
||||
mem.write32(state + 0, mem.read32(sigma + 0))
|
||||
mem.write32(state + 4, mem.read32(sigma + 4))
|
||||
mem.write32(state + 8, mem.read32(sigma + 8))
|
||||
mem.write32(state + 12, mem.read32(sigma + 12))
|
||||
|
||||
for i in 0..8
|
||||
mem.write32(state + (4 + i) * 4, mem.read32(key + i * 4))
|
||||
|
||||
mem.write32(state + 12 * 4, blocknum)
|
||||
mem.write32(state + 13 * 4, mem.read32(nonce + 0))
|
||||
mem.write32(state + 14 * 4, mem.read32(nonce + 4))
|
||||
mem.write32(state + 15 * 4, mem.read32(nonce + 8))
|
||||
|
||||
let working: ptr = mem.alloc(16 * 4)
|
||||
for i in 0..16
|
||||
mem.write32(working + i * 4, mem.read32(state + i * 4))
|
||||
|
||||
crypto.xchacha20._permute(working)
|
||||
|
||||
for i in 0..16
|
||||
let v: i64 = (mem.read32(working + i * 4) + mem.read32(state + i * 4)) & 0xffffffff
|
||||
mem.write32(out + i * 4, v)
|
||||
mem.free(working)
|
||||
mem.free(state)
|
||||
|
||||
func crypto.xchacha20._hchacha20[key: ptr, input: ptr, out32: ptr] : void
|
||||
let sigma: str = "expand 32-byte k"
|
||||
let state: ptr = mem.alloc(16 * 4)
|
||||
|
||||
mem.write32(state + 0, mem.read32(sigma + 0))
|
||||
mem.write32(state + 4, mem.read32(sigma + 4))
|
||||
mem.write32(state + 8, mem.read32(sigma + 8))
|
||||
mem.write32(state + 12, mem.read32(sigma + 12))
|
||||
|
||||
for i in 0..8
|
||||
mem.write32(state + (4 + i) * 4, mem.read32(key + i * 4))
|
||||
|
||||
for i in 0..4
|
||||
mem.write32(state + (12 + i) * 4, mem.read32(input + i * 4))
|
||||
|
||||
crypto.xchacha20._permute(state)
|
||||
|
||||
for i in 0..4
|
||||
mem.write32(out32 + i * 4, mem.read32(state + i * 4))
|
||||
for i in 0..4
|
||||
mem.write32(out32 + 16 + i * 4, mem.read32(state + (12 + i) * 4))
|
||||
mem.free(state)
|
||||
|
||||
func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void
|
||||
let subkey: ptr = mem.alloc(32)
|
||||
crypto.xchacha20._hchacha20(key, nonce, subkey)
|
||||
|
||||
let nonce12: ptr = mem.alloc(12)
|
||||
for i in 0..12
|
||||
mem.write8(nonce12 + i, 0)
|
||||
for i in 0..8
|
||||
mem.write8(nonce12 + 4 + i, nonce[16 + i])
|
||||
|
||||
let blocknum = 0
|
||||
let remaining: i64 = len
|
||||
let block: ptr = mem.alloc(64)
|
||||
|
||||
while remaining > 0
|
||||
crypto.xchacha20._block(subkey, nonce12, blocknum, block)
|
||||
let take = 64
|
||||
if remaining < 64
|
||||
take = remaining
|
||||
for i in 0..take
|
||||
mem.write8(out + (len - remaining) + i, block[i])
|
||||
remaining = remaining - take
|
||||
blocknum = blocknum + 1
|
||||
mem.free(block)
|
||||
mem.free(nonce12)
|
||||
mem.free(subkey)
|
||||
|
||||
func crypto.xchacha20.xor[key: ptr, nonce: ptr, input: ptr, len: i64] : ptr
|
||||
if len <= 0
|
||||
let out: ptr = mem.alloc(1)
|
||||
mem.write8(out, 0)
|
||||
return out
|
||||
let out: ptr = mem.alloc(len)
|
||||
let ks: ptr = mem.alloc(len)
|
||||
crypto.xchacha20._stream(key, nonce, ks, len)
|
||||
for i in 0..len
|
||||
mem.write8(out + i, input[i] ^ ks[i])
|
||||
mem.free(ks)
|
||||
return out
|
||||
|
||||
func crypto.x25519.carry[elem: ptr] : void
|
||||
for i in 0..16
|
||||
let carry: i64 = mem.read64(elem + i * 8) >> 16
|
||||
mem.write64(elem + i * 8, mem.read64(elem + i * 8) - (carry << 16))
|
||||
if i < 15
|
||||
mem.write64(elem + (i + 1) * 8, mem.read64(elem + (i + 1) * 8) + carry)
|
||||
else
|
||||
mem.write64(elem, mem.read64(elem) + 38 * carry)
|
||||
|
||||
func crypto.x25519.fadd[out: ptr, a: ptr, b: ptr] : void
|
||||
for i in 0..16
|
||||
mem.write64(out + i * 8, mem.read64(a + i * 8) + mem.read64(b + i * 8))
|
||||
|
||||
func crypto.x25519.fsub[out: ptr, a: ptr, b: ptr] : void
|
||||
for i in 0..16
|
||||
mem.write64(out + i * 8, mem.read64(a + i * 8) - mem.read64(b + i * 8))
|
||||
|
||||
func crypto.x25519.fmul[out: ptr, a: ptr, b: ptr] : void
|
||||
let product: ptr = mem.alloc(31 * 8)
|
||||
for i in 0..31
|
||||
mem.write64(product + i * 8, 0)
|
||||
for i in 0..16
|
||||
for j in 0..16
|
||||
mem.write64(product + (i + j) * 8, mem.read64(product + (i + j) * 8) + (mem.read64(a + i * 8) * mem.read64(b + j * 8)))
|
||||
for i in 0..15
|
||||
mem.write64(product + i * 8, mem.read64(product + i * 8) + 38 * mem.read64(product + (i + 16) * 8))
|
||||
for i in 0..16
|
||||
mem.write64(out + i * 8, mem.read64(product + i * 8))
|
||||
|
||||
crypto.x25519.carry(out)
|
||||
crypto.x25519.carry(out)
|
||||
mem.free(product)
|
||||
|
||||
func crypto.x25519.finverse[out: ptr, input: ptr] : void
|
||||
let c: ptr = mem.alloc(16 * 8)
|
||||
for i in 0..16
|
||||
mem.write64(c + i * 8, mem.read64(input + i * 8))
|
||||
|
||||
let i = 253
|
||||
while i >= 0
|
||||
crypto.x25519.fmul(c, c, c)
|
||||
if i != 2 && i != 4
|
||||
crypto.x25519.fmul(c, c, input)
|
||||
i = i - 1
|
||||
|
||||
for i in 0..16
|
||||
mem.write64(out + i * 8, mem.read64(c + i * 8))
|
||||
mem.free(c)
|
||||
|
||||
func crypto.x25519.swap[p: ptr, q: ptr, bit: i64] : void
|
||||
for i in 0..16
|
||||
let t: i64 = (-bit) & (mem.read64(p + i * 8) ^ mem.read64(q + i * 8))
|
||||
mem.write64(p + i * 8, mem.read64(p + i * 8) ^ t)
|
||||
mem.write64(q + i * 8, mem.read64(q + i * 8) ^ t)
|
||||
|
||||
func crypto.x25519.unpack[out: ptr, input: ptr] : void
|
||||
for i in 0..16
|
||||
mem.write64(out + i * 8, input[i * 2] + (input[i * 2 + 1] << 8))
|
||||
mem.write64(out + 8 * 15, mem.read64(out + 8 * 15) & 0x7fff)
|
||||
|
||||
func crypto.x25519.pack[out: ptr, input: ptr] : void
|
||||
let t: ptr = mem.alloc(16 * 8)
|
||||
for i in 0..16
|
||||
mem.write64(t + i * 8, mem.read64(input + i * 8))
|
||||
let m: ptr = mem.alloc(16 * 8)
|
||||
|
||||
crypto.x25519.carry(t)
|
||||
crypto.x25519.carry(t)
|
||||
crypto.x25519.carry(t)
|
||||
for j in 0..2
|
||||
mem.write64(m, mem.read64(t) - 0xffed)
|
||||
for i in 1..15
|
||||
mem.write64(m + i * 8, mem.read64(t + i * 8) - 0xffff - ((mem.read64(m + (i - 1) * 8) >> 16) & 1))
|
||||
mem.write64(m + (i - 1) * 8, mem.read64(m + (i - 1) * 8) & 0xffff)
|
||||
mem.write64(m + 15 * 8, mem.read64(t + 15 * 8) - 0x7fff - ((mem.read64(m + 14 * 8) >> 16) & 1))
|
||||
let carry: i64 = (mem.read64(m + 15 * 8) >> 16) & 1
|
||||
mem.write64(m + 14 * 8, mem.read64(m + 14 * 8) & 0xffff)
|
||||
crypto.x25519.swap(t, m, 1 - carry)
|
||||
|
||||
for i in 0..16
|
||||
let v: i64 = mem.read64(t + i * 8)
|
||||
mem.write8(out + i * 2, v & 0xff)
|
||||
mem.write8(out + i * 2 + 1, (v >> 8) & 0xff)
|
||||
|
||||
mem.free(t)
|
||||
mem.free(m)
|
||||
|
||||
func crypto.x25519.scalarmult[scalar: ptr, point: ptr] : ptr
|
||||
let clamped: ptr = mem.alloc(32)
|
||||
let a: ptr = mem.alloc(16 * 8)
|
||||
let b: ptr = mem.alloc(16 * 8)
|
||||
let c: ptr = mem.alloc(16 * 8)
|
||||
let d: ptr = mem.alloc(16 * 8)
|
||||
let e: ptr = mem.alloc(16 * 8)
|
||||
let f: ptr = mem.alloc(16 * 8)
|
||||
let x: ptr = mem.alloc(16 * 8)
|
||||
|
||||
let magic: ptr = mem.alloc(16 * 8)
|
||||
mem.zero(magic, 16 * 8)
|
||||
mem.write64(magic, 0xdb41) // 0xdb41 = 121665 = (486662 + 2)/4
|
||||
mem.write64(magic + 8, 1)
|
||||
|
||||
// copy and clamp scalar
|
||||
for i in 0..32
|
||||
mem.write8(clamped + i, scalar[i])
|
||||
mem.write8(clamped, clamped[0] & 0xf8)
|
||||
mem.write8(clamped + 31, (clamped[31] & 0x7f) | 0x40)
|
||||
|
||||
// load point
|
||||
crypto.x25519.unpack(x, point)
|
||||
|
||||
// initialize ladder state
|
||||
for i in 0..16
|
||||
mem.write64(a + i * 8, 0)
|
||||
mem.write64(b + i * 8, mem.read64(x + i * 8))
|
||||
mem.write64(c + i * 8, 0)
|
||||
mem.write64(d + i * 8, 0)
|
||||
mem.write64(a, 1)
|
||||
mem.write64(d, 1)
|
||||
|
||||
let i = 254
|
||||
while i >= 0
|
||||
let bit: i64 = (clamped[i >> 3] >> (i & 7)) & 1
|
||||
crypto.x25519.swap(a, b, bit)
|
||||
crypto.x25519.swap(c, d, bit)
|
||||
crypto.x25519.fadd(e, a, c)
|
||||
crypto.x25519.fsub(a, a, c)
|
||||
crypto.x25519.fadd(c, b, d)
|
||||
crypto.x25519.fsub(b, b, d)
|
||||
crypto.x25519.fmul(d, e, e)
|
||||
crypto.x25519.fmul(f, a, a)
|
||||
crypto.x25519.fmul(a, c, a)
|
||||
crypto.x25519.fmul(c, b, e)
|
||||
crypto.x25519.fadd(e, a, c)
|
||||
crypto.x25519.fsub(a, a, c)
|
||||
crypto.x25519.fmul(b, a, a)
|
||||
crypto.x25519.fsub(c, d, f)
|
||||
crypto.x25519.fmul(a, c, magic)
|
||||
crypto.x25519.fadd(a, a, d)
|
||||
crypto.x25519.fmul(c, c, a)
|
||||
crypto.x25519.fmul(a, d, f)
|
||||
crypto.x25519.fmul(d, b, x)
|
||||
crypto.x25519.fmul(b, e, e)
|
||||
crypto.x25519.swap(a, b, bit)
|
||||
crypto.x25519.swap(c, d, bit)
|
||||
i = i - 1
|
||||
|
||||
crypto.x25519.finverse(c, c)
|
||||
crypto.x25519.fmul(a, a, c)
|
||||
let out: ptr = mem.alloc(32)
|
||||
crypto.x25519.pack(out, a)
|
||||
|
||||
mem.free(clamped)
|
||||
mem.free(a)
|
||||
mem.free(b)
|
||||
mem.free(c)
|
||||
mem.free(d)
|
||||
mem.free(e)
|
||||
mem.free(f)
|
||||
mem.free(x)
|
||||
mem.free(magic)
|
||||
return out
|
||||
@@ -20,12 +20,13 @@ func mem.zero[x: i64, size: i64] : void
|
||||
mem.write8(x + i, 0)
|
||||
|
||||
func mem.read8[x: ptr] : u8
|
||||
return _builtin_read8(x)
|
||||
return x[0]
|
||||
|
||||
func mem.read16[x: ptr] : i64
|
||||
let low: i64 = mem.read8(x)
|
||||
let high: i64 = mem.read8(x + 1)
|
||||
return low | (high << 8)
|
||||
return x[0] | (x[1] << 8)
|
||||
|
||||
func mem.read32[x: ptr] : i64
|
||||
return x[0] | (x[1] << 8) | (x[2] << 16) | (x[3] << 24)
|
||||
|
||||
func mem.read64[x: ptr] : i64
|
||||
return _builtin_read64(x)
|
||||
@@ -33,11 +34,17 @@ func mem.read64[x: ptr] : i64
|
||||
func mem.write8[x: ptr, d: u8] : void
|
||||
_builtin_set8(x, d)
|
||||
|
||||
func mem.write32[x: ptr, d: i64] : void
|
||||
mem.write8(x, d & 0xff)
|
||||
mem.write8(x + 1, (d >> 8) & 0xff)
|
||||
mem.write8(x + 2, (d >> 16) & 0xff)
|
||||
mem.write8(x + 3, (d >> 24) & 0xff)
|
||||
|
||||
func mem.write64[x: ptr, d: i64] : void
|
||||
_builtin_set64(x, d)
|
||||
|
||||
func io.print_sized[x: str, size: i64] : void
|
||||
_builtin_syscall(1, 1, x, size) // write
|
||||
_builtin_syscall(SYS_write, 1, x, size)
|
||||
|
||||
func io.print[x: str] : void
|
||||
io.print_sized(x, str.len(x))
|
||||
@@ -47,7 +54,7 @@ func io.println[x: str] : void
|
||||
io.print("\n")
|
||||
|
||||
func io.print_char[x: u8] : void
|
||||
io.print_sized(@x, 1)
|
||||
io.print_sized(^x, 1)
|
||||
|
||||
func io.print_i64[x: i64] : void
|
||||
let s: str = str.from_i64(x)
|
||||
@@ -61,43 +68,43 @@ func io.println_i64[x: i64] : void
|
||||
|
||||
func io.read_char[] : u8
|
||||
let c: u8 = 0
|
||||
_builtin_syscall(0, 0, @c, 1) // read
|
||||
_builtin_syscall(SYS_read, 0, ^c, 1)
|
||||
return c
|
||||
|
||||
func io.read_line[]: str
|
||||
let MAX_SIZE: i64 = 60000
|
||||
let MAX_SIZE = 60000
|
||||
let buffer: str = mem.alloc(MAX_SIZE + 1)
|
||||
let n: i64 = _builtin_syscall(0, 0, buffer, MAX_SIZE) // read
|
||||
let n: i64 = _builtin_syscall(SYS_read, 0, buffer, MAX_SIZE)
|
||||
if n < 0
|
||||
return ""
|
||||
str.set(buffer, n, 0)
|
||||
return buffer
|
||||
|
||||
func io.read_file[path: str]: str
|
||||
let fd: i64 = _builtin_syscall(257, -100, path, 0, 0) // openat
|
||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
if fd <= 0
|
||||
dbg.panic("failed to open file")
|
||||
|
||||
let size: i64 = _builtin_syscall(8, fd, 0, 2) // lseek to the end
|
||||
_builtin_syscall(8, fd, 0, 0) // lseek back to start
|
||||
let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
||||
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
||||
|
||||
let buffer: str = mem.alloc(size + 1)
|
||||
let n: i64 = _builtin_syscall(0, fd, buffer, size) // read
|
||||
let n: i64 = _builtin_syscall(SYS_read, fd, buffer, size)
|
||||
str.set(buffer, n, 0)
|
||||
_builtin_syscall(3, fd) // close
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
return buffer
|
||||
|
||||
func io.write_file[path: str, content: str] : void
|
||||
let fd: ptr = _builtin_syscall(257, -100, path, 0x241, 0o644) // openat
|
||||
let fd: ptr = _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644)
|
||||
if fd < 0
|
||||
dbg.panic("failed to open file")
|
||||
|
||||
_builtin_syscall(1, fd, content, str.len(content)) // write
|
||||
_builtin_syscall(3, fd) // close
|
||||
_builtin_syscall(SYS_write, fd, content, str.len(content))
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
|
||||
func str.len[s: str] : i64
|
||||
let i: i64 = 0
|
||||
while mem.read8(s + i)
|
||||
let i = 0
|
||||
while s[i]
|
||||
i = i + 1
|
||||
return i
|
||||
|
||||
@@ -112,15 +119,33 @@ func str.set[s: str, n: i64, c: u8] : void
|
||||
mem.write8(s + n, c)
|
||||
|
||||
func str.equal[a: str, b: str] : bool
|
||||
let i: i64 = 0
|
||||
while a[i] != 0 & b[i] != 0
|
||||
let i = 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
|
||||
return x == ' ' | x == 10 | x == 13 | x == 9
|
||||
return x == ' ' || x == 10 || x == 13 || x == 9
|
||||
|
||||
func str.is_digit[x: u8] : bool
|
||||
return x >= '0' && x <= '9'
|
||||
|
||||
func str.is_hex_digit[x: u8] : bool
|
||||
return (x >= '0' && x <= '9') || (x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F')
|
||||
|
||||
func str.is_lowercase[x: u8] : bool
|
||||
return x >= 'a' && x <= 'z'
|
||||
|
||||
func str.is_uppercase[x: u8] : bool
|
||||
return x >= 'A' && x <= 'Z'
|
||||
|
||||
func str.is_letter[x: u8] : bool
|
||||
return str.is_uppercase(x) || str.is_lowercase(x)
|
||||
|
||||
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: i64 = str.len(a)
|
||||
@@ -151,7 +176,7 @@ func str.find[haystack: str, needle: str] : i64
|
||||
return -1
|
||||
|
||||
func str.substr[s: str, start: i64, length: i64] : str
|
||||
if start < 0 | length < 0 | start + length > str.len(s)
|
||||
if start < 0 || length < 0 || start + length > str.len(s)
|
||||
dbg.panic("str.substr out of bounds")
|
||||
|
||||
let out: str = mem.alloc(length + 1)
|
||||
@@ -165,13 +190,13 @@ func str.trim[s: str] : str
|
||||
if len == 0
|
||||
return ""
|
||||
|
||||
let start: i64 = 0
|
||||
let start = 0
|
||||
let end: i64 = len - 1
|
||||
|
||||
while start <= end & str.is_whitespace(s[start])
|
||||
while start <= end && str.is_whitespace(s[start])
|
||||
start = start + 1
|
||||
|
||||
while end >= start & str.is_whitespace(s[end])
|
||||
while end >= start && str.is_whitespace(s[end])
|
||||
end = end - 1
|
||||
|
||||
return str.substr(s, start, end - start + 1)
|
||||
@@ -189,8 +214,8 @@ func str.split[haystack: str, needle: str]: array
|
||||
array.push(result, str.substr(haystack, i, 1))
|
||||
return result
|
||||
|
||||
let start: i64 = 0
|
||||
let i: i64 = 0
|
||||
let start = 0
|
||||
let i = 0
|
||||
while i < haystack_len
|
||||
if i <= haystack_len - needle_len
|
||||
let match: bool = true
|
||||
@@ -226,7 +251,7 @@ func str.from_i64[n: i64] : str
|
||||
if neg
|
||||
n = -n
|
||||
let buf: str = mem.alloc(21) // enough to fit -MAX_I64
|
||||
let i: i64 = 0
|
||||
let i = 0
|
||||
while n > 0
|
||||
let d: u8 = n % 10
|
||||
str.set(buf, i, '0' + d)
|
||||
@@ -248,26 +273,25 @@ func str.from_char[c: u8] : str
|
||||
|
||||
func str.parse_i64[s: str] : i64
|
||||
let len: i64 = str.len(s)
|
||||
let i: i64 = 0
|
||||
let i = 0
|
||||
|
||||
let sign: i64 = 1
|
||||
if i < len & s[i] == '-'
|
||||
let sign = 1
|
||||
if i < len && s[i] == '-'
|
||||
sign = -1
|
||||
i = i + 1
|
||||
|
||||
let num: i64 = 0
|
||||
let num = 0
|
||||
while i < len
|
||||
let d: u8 = s[i]
|
||||
if d < '0' | d > '9'
|
||||
if d < '0' || d > '9'
|
||||
break
|
||||
num = num * 10 + (d - '0')
|
||||
i = i + 1
|
||||
return num * sign
|
||||
|
||||
func str.hex_encode[s: str] : str
|
||||
func str.hex_encode[s: str, s_len: i64] : str
|
||||
let hex_chars: str = "0123456789abcdef"
|
||||
let s_len: i64 = str.len(s)
|
||||
let j: i64 = 0
|
||||
let j = 0
|
||||
let out: str = mem.alloc(s_len * 2 + 1)
|
||||
|
||||
for i in 0..s_len
|
||||
@@ -281,16 +305,16 @@ func str.hex_encode[s: str] : str
|
||||
return out
|
||||
|
||||
func str._hex_digit_to_int[d: u8] : i64
|
||||
if d >= 'a' & d <= 'f'
|
||||
if d >= 'a' && d <= 'f'
|
||||
return d - 'a' + 10
|
||||
if d >= 'A' & d <= 'F'
|
||||
if d >= 'A' && d <= 'F'
|
||||
return d - 'A' + 10
|
||||
return d - '0'
|
||||
|
||||
func str.hex_decode[s: str] : str
|
||||
let s_len: i64 = str.len(s)
|
||||
let i: i64 = 0
|
||||
let j: i64 = 0
|
||||
let i = 0
|
||||
let j = 0
|
||||
let out: str = mem.alloc(s_len / 2 + 1)
|
||||
|
||||
while i < s_len
|
||||
@@ -323,8 +347,16 @@ func math.abs[n: i64] : i64
|
||||
return -n
|
||||
return n
|
||||
|
||||
func math.sign[n: i64] : i64
|
||||
if n < 0
|
||||
return -1
|
||||
else if n > 0
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
|
||||
func math.pow[b: i64, e: i64] : i64
|
||||
let out: i64 = 1
|
||||
let out = 1
|
||||
for i in 0..e
|
||||
out = out * b
|
||||
return out
|
||||
@@ -335,7 +367,7 @@ func math.lcm[a: i64, b: i64] : i64
|
||||
func math.isqrt[n: i64] : i64
|
||||
if n < 0
|
||||
dbg.panic("negative number passed to math.isqrt")
|
||||
if n == 0 | n == 1
|
||||
if n == 0 || n == 1
|
||||
return n
|
||||
|
||||
let guess: i64 = n
|
||||
@@ -350,14 +382,14 @@ func math.isqrt[n: i64] : i64
|
||||
func math.is_prime[n: i64]: bool
|
||||
if n <= 1
|
||||
return false
|
||||
if n == 2 | n == 3
|
||||
if n == 2 || n == 3
|
||||
return true
|
||||
if n % 2 == 0 | n % 3 == 0
|
||||
if n % 2 == 0 || n % 3 == 0
|
||||
return false
|
||||
|
||||
let i: i64 = 5
|
||||
let i = 5
|
||||
while i * i <= n
|
||||
if n % i == 0 | n % (i + 2) == 0
|
||||
if n % i == 0 || n % (i + 2) == 0
|
||||
return false
|
||||
i = i + 6
|
||||
return true
|
||||
@@ -369,14 +401,14 @@ func array.new[] : array
|
||||
return arr
|
||||
|
||||
func array.nth[xs: array, n: i64] : i64
|
||||
if n < 0 | n >= array.size(xs)
|
||||
if n < 0 || n >= array.size(xs)
|
||||
dbg.panic("array.nth out of bounds")
|
||||
let data: ptr = mem.read64(xs)
|
||||
return mem.read64(data + n * 8)
|
||||
|
||||
func array.set[xs: array, n: i64, x: i64] : void
|
||||
if n < 0 | n >= array.size(xs)
|
||||
dbg.panic("array.nth out of bounds")
|
||||
if n < 0 || n >= array.size(xs)
|
||||
dbg.panic("array.set out of bounds")
|
||||
let data: ptr = mem.read64(xs)
|
||||
mem.write64(data + n * 8, x)
|
||||
|
||||
@@ -386,7 +418,7 @@ func array.push[xs: array, x: i64] : void
|
||||
let size: i64 = mem.read64(xs + 16)
|
||||
|
||||
if size == capacity
|
||||
let new_capacity: i64 = 4
|
||||
let new_capacity = 4
|
||||
if capacity != 0
|
||||
new_capacity = capacity * 2
|
||||
let new_data: ptr = realloc(data, new_capacity * 8)
|
||||
@@ -406,8 +438,16 @@ func array.free[xs: array] : void
|
||||
mem.free(data)
|
||||
mem.free(xs)
|
||||
|
||||
func array.pop[xs: array] : i64
|
||||
let size: i64 = array.size(xs)
|
||||
if size == 0
|
||||
dbg.panic("array.pop on empty array")
|
||||
let x: i64 = array.nth(xs, size - 1)
|
||||
mem.write64(xs + 16, size - 1)
|
||||
return x
|
||||
|
||||
func array.slice[xs: array, start: i64, length: i64] : array
|
||||
if start < 0 | length < 0 | start + length > array.size(xs)
|
||||
if start < 0 || length < 0 || start + length > array.size(xs)
|
||||
dbg.panic("array.slice out of bounds")
|
||||
|
||||
let new_array: array = []
|
||||
@@ -447,26 +487,50 @@ func alg._partition[arr: array, low: i64, high: i64] : i64
|
||||
return i + 1
|
||||
|
||||
func alg.count[arr: array, item: i64] : i64
|
||||
let count: i64 = 0
|
||||
let count = 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
|
||||
_builtin_syscall(60, code)
|
||||
func alg.map[arr: array, fn: ptr] : array
|
||||
let out: array = []
|
||||
for i in 0..array.size(arr)
|
||||
array.push(out, fn(array.nth(arr, i)))
|
||||
return out
|
||||
|
||||
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
|
||||
func alg.filter[arr: array, fn: ptr] : array
|
||||
let out: array = []
|
||||
for i in 0..array.size(arr)
|
||||
if fn(array.nth(arr, i))
|
||||
array.push(out, array.nth(arr, i))
|
||||
return out
|
||||
|
||||
func alg.reduce[arr: array, fn: ptr, acc: i64] : i64
|
||||
for i in 0..array.size(arr)
|
||||
acc = fn(acc, array.nth(arr, i))
|
||||
return acc
|
||||
|
||||
func os.exit[code: i64] : void
|
||||
_builtin_syscall(SYS_exit, code)
|
||||
|
||||
func os.urandom[n: i64]: ptr
|
||||
let buffer: ptr = mem.alloc(n)
|
||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, "/dev/urandom", 0, 0)
|
||||
_builtin_syscall(SYS_read, fd, buffer, n)
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
return buffer
|
||||
|
||||
func os.urandom_i64[]: i64
|
||||
let buffer: ptr = os.urandom(8)
|
||||
let n: i64 = mem.read64(buffer)
|
||||
mem.free(buffer)
|
||||
return n
|
||||
|
||||
func os.time[] : i64
|
||||
let tv: ptr = mem.alloc(16)
|
||||
_builtin_syscall(96, tv, 0) // gettimeofday
|
||||
_builtin_syscall(SYS_gettimeofday, tv, 0)
|
||||
let seconds: i64 = mem.read64(tv)
|
||||
let microseconds: i64 = mem.read64(tv + 8)
|
||||
mem.free(tv)
|
||||
@@ -474,15 +538,15 @@ func os.time[] : i64
|
||||
|
||||
// voodoo magic
|
||||
func os.shell[command: str] : i64
|
||||
let pid: i64 = _builtin_syscall(57) // fork
|
||||
let pid: i64 = _builtin_syscall(SYS_fork)
|
||||
if pid == 0
|
||||
// leaky but not sure where can i free it
|
||||
let argv: array = ["sh", "-c", command, 0]
|
||||
_builtin_syscall(59, "/bin/sh", mem.read64(argv), _builtin_environ()) // execve
|
||||
_builtin_syscall(60, 1) // exit
|
||||
_builtin_syscall(SYS_execve, "/bin/sh", mem.read64(argv), _builtin_environ())
|
||||
_builtin_syscall(SYS_exit, 1)
|
||||
else
|
||||
let status: i64 = 0
|
||||
let wp: i64 = _builtin_syscall(61, pid, @status, 0, 0) // waitpid
|
||||
let status = 0
|
||||
let wp: i64 = _builtin_syscall(SYS_wait4, pid, ^status, 0, 0)
|
||||
if wp == -1
|
||||
return -1
|
||||
let st: i64 = status & 0xffffffff
|
||||
@@ -493,18 +557,18 @@ func os.shell[command: str] : i64
|
||||
return -(st & 0x7f)
|
||||
|
||||
func os.listdir[path: str] : array
|
||||
let fd: i64 = _builtin_syscall(257, -100, path, 0, 0) // openat
|
||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
if fd < 0
|
||||
return []
|
||||
|
||||
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(SYS_getdents64, fd, buf, 1024)
|
||||
if n <= 0
|
||||
break
|
||||
|
||||
let pos: i64 = 0
|
||||
let pos = 0
|
||||
while pos < n
|
||||
let len: i64 = mem.read16(buf + pos + 16)
|
||||
let name: str = buf + pos + 19
|
||||
@@ -522,17 +586,18 @@ func os.listdir[path: str] : array
|
||||
pos = pos + len
|
||||
|
||||
mem.free(buf)
|
||||
_builtin_syscall(3, fd) // close
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
return files
|
||||
|
||||
func net.listen[packed_host: i64, port: i64] : i64
|
||||
let s: i64 = _builtin_syscall(41, 2, 1, 0) // socket
|
||||
let s: i64 = _builtin_syscall(SYS_socket, 2, 1, 0)
|
||||
if s < 0
|
||||
return -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
|
||||
// setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
||||
let optval = 1
|
||||
if _builtin_syscall(SYS_setsockopt, s, 1, 2, ^optval, 8) < 0
|
||||
_builtin_syscall(SYS_close, s)
|
||||
return -1
|
||||
|
||||
let sa: ptr = mem.alloc(16)
|
||||
@@ -546,14 +611,14 @@ func net.listen[packed_host: i64, port: i64] : i64
|
||||
mem.write8(sa + 6, (packed_host >> 8) & 255)
|
||||
mem.write8(sa + 7, packed_host & 255)
|
||||
|
||||
if _builtin_syscall(49, s, sa, 16) < 0 // bind
|
||||
_builtin_syscall(3, s) // close
|
||||
if _builtin_syscall(SYS_bind, s, sa, 16) < 0
|
||||
_builtin_syscall(SYS_close, s)
|
||||
mem.free(sa)
|
||||
return -1
|
||||
mem.free(sa)
|
||||
|
||||
if _builtin_syscall(50, s, 1) < 0 // listen
|
||||
_builtin_syscall(3, s) // close
|
||||
if _builtin_syscall(SYS_listen, s, 1) < 0
|
||||
_builtin_syscall(SYS_close, s)
|
||||
return -1
|
||||
|
||||
return s
|
||||
@@ -565,7 +630,7 @@ func net.connect[host: str, port: i64] : i64
|
||||
|
||||
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(SYS_socket, 2, 1, 0)
|
||||
if s < 0
|
||||
return -1
|
||||
|
||||
@@ -579,25 +644,25 @@ func net.connect[host: str, port: i64] : i64
|
||||
mem.write8(sa + 6, ip_ptr[2])
|
||||
mem.write8(sa + 7, ip_ptr[3])
|
||||
|
||||
if _builtin_syscall(42, s, sa, 16) < 0 // connect
|
||||
if _builtin_syscall(SYS_connect, s, sa, 16) < 0
|
||||
mem.free(sa)
|
||||
_builtin_syscall(3, s) // close
|
||||
_builtin_syscall(SYS_close, s)
|
||||
return -1
|
||||
|
||||
mem.free(sa)
|
||||
return s
|
||||
|
||||
func net.accept[s: i64] : i64
|
||||
return _builtin_syscall(43, s, 0, 0)
|
||||
return _builtin_syscall(SYS_accept, s, 0, 0)
|
||||
|
||||
func net.send[s: i64, data: str, size: i64] : void
|
||||
_builtin_syscall(44, s, data, size, 0, 0, 0)
|
||||
_builtin_syscall(SYS_sendto, s, data, size, 0, 0, 0)
|
||||
|
||||
func net.read[s: i64, buffer: ptr, size: i64] : i64
|
||||
return _builtin_syscall(0, s, buffer, size)
|
||||
return _builtin_syscall(SYS_read, s, buffer, size)
|
||||
|
||||
func net.close[s: i64] : void
|
||||
_builtin_syscall(3, s)
|
||||
_builtin_syscall(SYS_close, s)
|
||||
|
||||
func net.pack_addr[a: i64, b: i64, c: i64, d: i64] : i64
|
||||
return (a << 24) | (b << 16) | (c << 8) | d
|
||||
return (a << 24) | (b << 16) | (c << 8) | d
|
||||
381
src/std/syscalls.zr
Normal file
381
src/std/syscalls.zr
Normal file
@@ -0,0 +1,381 @@
|
||||
// https://github.com/torvalds/linux/blob/19272b37aa4f83ca52bdf9c16d5d81bdd1354494/arch/x86/entry/syscalls/syscall_64.tbl
|
||||
const SYS_read = 0
|
||||
const SYS_write = 1
|
||||
const SYS_open = 2
|
||||
const SYS_close = 3
|
||||
const SYS_stat = 4
|
||||
const SYS_fstat = 5
|
||||
const SYS_lstat = 6
|
||||
const SYS_poll = 7
|
||||
const SYS_lseek = 8
|
||||
const SYS_mmap = 9
|
||||
const SYS_mprotect = 10
|
||||
const SYS_munmap = 11
|
||||
const SYS_brk = 12
|
||||
const SYS_rt_sigaction = 13
|
||||
const SYS_rt_sigprocmask = 14
|
||||
const SYS_rt_sigreturn = 15
|
||||
const SYS_ioctl = 16
|
||||
const SYS_pread64 = 17
|
||||
const SYS_pwrite64 = 18
|
||||
const SYS_readv = 19
|
||||
const SYS_writev = 20
|
||||
const SYS_access = 21
|
||||
const SYS_pipe = 22
|
||||
const SYS_select = 23
|
||||
const SYS_sched_yield = 24
|
||||
const SYS_mremap = 25
|
||||
const SYS_msync = 26
|
||||
const SYS_mincore = 27
|
||||
const SYS_madvise = 28
|
||||
const SYS_shmget = 29
|
||||
const SYS_shmat = 30
|
||||
const SYS_shmctl = 31
|
||||
const SYS_dup = 32
|
||||
const SYS_dup2 = 33
|
||||
const SYS_pause = 34
|
||||
const SYS_nanosleep = 35
|
||||
const SYS_getitimer = 36
|
||||
const SYS_alarm = 37
|
||||
const SYS_setitimer = 38
|
||||
const SYS_getpid = 39
|
||||
const SYS_sendfile = 40
|
||||
const SYS_socket = 41
|
||||
const SYS_connect = 42
|
||||
const SYS_accept = 43
|
||||
const SYS_sendto = 44
|
||||
const SYS_recvfrom = 45
|
||||
const SYS_sendmsg = 46
|
||||
const SYS_recvmsg = 47
|
||||
const SYS_shutdown = 48
|
||||
const SYS_bind = 49
|
||||
const SYS_listen = 50
|
||||
const SYS_getsockname = 51
|
||||
const SYS_getpeername = 52
|
||||
const SYS_socketpair = 53
|
||||
const SYS_setsockopt = 54
|
||||
const SYS_getsockopt = 55
|
||||
const SYS_clone = 56
|
||||
const SYS_fork = 57
|
||||
const SYS_vfork = 58
|
||||
const SYS_execve = 59
|
||||
const SYS_exit = 60
|
||||
const SYS_wait4 = 61
|
||||
const SYS_kill = 62
|
||||
const SYS_uname = 63
|
||||
const SYS_semget = 64
|
||||
const SYS_semop = 65
|
||||
const SYS_semctl = 66
|
||||
const SYS_shmdt = 67
|
||||
const SYS_msgget = 68
|
||||
const SYS_msgsnd = 69
|
||||
const SYS_msgrcv = 70
|
||||
const SYS_msgctl = 71
|
||||
const SYS_fcntl = 72
|
||||
const SYS_flock = 73
|
||||
const SYS_fsync = 74
|
||||
const SYS_fdatasync = 75
|
||||
const SYS_truncate = 76
|
||||
const SYS_ftruncate = 77
|
||||
const SYS_getdents = 78
|
||||
const SYS_getcwd = 79
|
||||
const SYS_chdir = 80
|
||||
const SYS_fchdir = 81
|
||||
const SYS_rename = 82
|
||||
const SYS_mkdir = 83
|
||||
const SYS_rmdir = 84
|
||||
const SYS_creat = 85
|
||||
const SYS_link = 86
|
||||
const SYS_unlink = 87
|
||||
const SYS_symlink = 88
|
||||
const SYS_readlink = 89
|
||||
const SYS_chmod = 90
|
||||
const SYS_fchmod = 91
|
||||
const SYS_chown = 92
|
||||
const SYS_fchown = 93
|
||||
const SYS_lchown = 94
|
||||
const SYS_umask = 95
|
||||
const SYS_gettimeofday = 96
|
||||
const SYS_getrlimit = 97
|
||||
const SYS_getrusage = 98
|
||||
const SYS_sysinfo = 99
|
||||
const SYS_times = 100
|
||||
const SYS_ptrace = 101
|
||||
const SYS_getuid = 102
|
||||
const SYS_syslog = 103
|
||||
const SYS_getgid = 104
|
||||
const SYS_setuid = 105
|
||||
const SYS_setgid = 106
|
||||
const SYS_geteuid = 107
|
||||
const SYS_getegid = 108
|
||||
const SYS_setpgid = 109
|
||||
const SYS_getppid = 110
|
||||
const SYS_getpgrp = 111
|
||||
const SYS_setsid = 112
|
||||
const SYS_setreuid = 113
|
||||
const SYS_setregid = 114
|
||||
const SYS_getgroups = 115
|
||||
const SYS_setgroups = 116
|
||||
const SYS_setresuid = 117
|
||||
const SYS_getresuid = 118
|
||||
const SYS_setresgid = 119
|
||||
const SYS_getresgid = 120
|
||||
const SYS_getpgid = 121
|
||||
const SYS_setfsuid = 122
|
||||
const SYS_setfsgid = 123
|
||||
const SYS_getsid = 124
|
||||
const SYS_capget = 125
|
||||
const SYS_capset = 126
|
||||
const SYS_rt_sigpending = 127
|
||||
const SYS_rt_sigtimedwait = 128
|
||||
const SYS_rt_sigqueueinfo = 129
|
||||
const SYS_rt_sigsuspend = 130
|
||||
const SYS_sigaltstack = 131
|
||||
const SYS_utime = 132
|
||||
const SYS_mknod = 133
|
||||
const SYS_uselib = 134
|
||||
const SYS_personality = 135
|
||||
const SYS_ustat = 136
|
||||
const SYS_statfs = 137
|
||||
const SYS_fstatfs = 138
|
||||
const SYS_sysfs = 139
|
||||
const SYS_getpriority = 140
|
||||
const SYS_setpriority = 141
|
||||
const SYS_sched_setparam = 142
|
||||
const SYS_sched_getparam = 143
|
||||
const SYS_sched_setscheduler = 144
|
||||
const SYS_sched_getscheduler = 145
|
||||
const SYS_sched_get_priority_max = 146
|
||||
const SYS_sched_get_priority_min = 147
|
||||
const SYS_sched_rr_get_interval = 148
|
||||
const SYS_mlock = 149
|
||||
const SYS_munlock = 150
|
||||
const SYS_mlockall = 151
|
||||
const SYS_munlockall = 152
|
||||
const SYS_vhangup = 153
|
||||
const SYS_modify_ldt = 154
|
||||
const SYS_pivot_root = 155
|
||||
const SYS__sysctl = 156
|
||||
const SYS_prctl = 157
|
||||
const SYS_arch_prctl = 158
|
||||
const SYS_adjtimex = 159
|
||||
const SYS_setrlimit = 160
|
||||
const SYS_chroot = 161
|
||||
const SYS_sync = 162
|
||||
const SYS_acct = 163
|
||||
const SYS_settimeofday = 164
|
||||
const SYS_mount = 165
|
||||
const SYS_umount2 = 166
|
||||
const SYS_swapon = 167
|
||||
const SYS_swapoff = 168
|
||||
const SYS_reboot = 169
|
||||
const SYS_sethostname = 170
|
||||
const SYS_setdomainname = 171
|
||||
const SYS_iopl = 172
|
||||
const SYS_ioperm = 173
|
||||
const SYS_create_module = 174
|
||||
const SYS_init_module = 175
|
||||
const SYS_delete_module = 176
|
||||
const SYS_get_kernel_syms = 177
|
||||
const SYS_query_module = 178
|
||||
const SYS_quotactl = 179
|
||||
const SYS_nfsservctl = 180
|
||||
const SYS_getpmsg = 181
|
||||
const SYS_putpmsg = 182
|
||||
const SYS_afs_syscall = 183
|
||||
const SYS_tuxcall = 184
|
||||
const SYS_security = 185
|
||||
const SYS_gettid = 186
|
||||
const SYS_readahead = 187
|
||||
const SYS_setxattr = 188
|
||||
const SYS_lsetxattr = 189
|
||||
const SYS_fsetxattr = 190
|
||||
const SYS_getxattr = 191
|
||||
const SYS_lgetxattr = 192
|
||||
const SYS_fgetxattr = 193
|
||||
const SYS_listxattr = 194
|
||||
const SYS_llistxattr = 195
|
||||
const SYS_flistxattr = 196
|
||||
const SYS_removexattr = 197
|
||||
const SYS_lremovexattr = 198
|
||||
const SYS_fremovexattr = 199
|
||||
const SYS_tkill = 200
|
||||
const SYS_time = 201
|
||||
const SYS_futex = 202
|
||||
const SYS_sched_setaffinity = 203
|
||||
const SYS_sched_getaffinity = 204
|
||||
const SYS_set_thread_area = 205
|
||||
const SYS_io_setup = 206
|
||||
const SYS_io_destroy = 207
|
||||
const SYS_io_getevents = 208
|
||||
const SYS_io_submit = 209
|
||||
const SYS_io_cancel = 210
|
||||
const SYS_get_thread_area = 211
|
||||
const SYS_lookup_dcookie = 212
|
||||
const SYS_epoll_create = 213
|
||||
const SYS_epoll_ctl_old = 214
|
||||
const SYS_epoll_wait_old = 215
|
||||
const SYS_remap_file_pages = 216
|
||||
const SYS_getdents64 = 217
|
||||
const SYS_set_tid_address = 218
|
||||
const SYS_restart_syscall = 219
|
||||
const SYS_semtimedop = 220
|
||||
const SYS_fadvise64 = 221
|
||||
const SYS_timer_create = 222
|
||||
const SYS_timer_settime = 223
|
||||
const SYS_timer_gettime = 224
|
||||
const SYS_timer_getoverrun = 225
|
||||
const SYS_timer_delete = 226
|
||||
const SYS_clock_settime = 227
|
||||
const SYS_clock_gettime = 228
|
||||
const SYS_clock_getres = 229
|
||||
const SYS_clock_nanosleep = 230
|
||||
const SYS_exit_group = 231
|
||||
const SYS_epoll_wait = 232
|
||||
const SYS_epoll_ctl = 233
|
||||
const SYS_tgkill = 234
|
||||
const SYS_utimes = 235
|
||||
const SYS_vserver = 236
|
||||
const SYS_mbind = 237
|
||||
const SYS_set_mempolicy = 238
|
||||
const SYS_get_mempolicy = 239
|
||||
const SYS_mq_open = 240
|
||||
const SYS_mq_unlink = 241
|
||||
const SYS_mq_timedsend = 242
|
||||
const SYS_mq_timedreceive = 243
|
||||
const SYS_mq_notify = 244
|
||||
const SYS_mq_getsetattr = 245
|
||||
const SYS_kexec_load = 246
|
||||
const SYS_waitid = 247
|
||||
const SYS_add_key = 248
|
||||
const SYS_request_key = 249
|
||||
const SYS_keyctl = 250
|
||||
const SYS_ioprio_set = 251
|
||||
const SYS_ioprio_get = 252
|
||||
const SYS_inotify_init = 253
|
||||
const SYS_inotify_add_watch = 254
|
||||
const SYS_inotify_rm_watch = 255
|
||||
const SYS_migrate_pages = 256
|
||||
const SYS_openat = 257
|
||||
const SYS_mkdirat = 258
|
||||
const SYS_mknodat = 259
|
||||
const SYS_fchownat = 260
|
||||
const SYS_futimesat = 261
|
||||
const SYS_newfstatat = 262
|
||||
const SYS_unlinkat = 263
|
||||
const SYS_renameat = 264
|
||||
const SYS_linkat = 265
|
||||
const SYS_symlinkat = 266
|
||||
const SYS_readlinkat = 267
|
||||
const SYS_fchmodat = 268
|
||||
const SYS_faccessat = 269
|
||||
const SYS_pselect6 = 270
|
||||
const SYS_ppoll = 271
|
||||
const SYS_unshare = 272
|
||||
const SYS_set_robust_list = 273
|
||||
const SYS_get_robust_list = 274
|
||||
const SYS_splice = 275
|
||||
const SYS_tee = 276
|
||||
const SYS_sync_file_range = 277
|
||||
const SYS_vmsplice = 278
|
||||
const SYS_move_pages = 279
|
||||
const SYS_utimensat = 280
|
||||
const SYS_epoll_pwait = 281
|
||||
const SYS_signalfd = 282
|
||||
const SYS_timerfd_create = 283
|
||||
const SYS_eventfd = 284
|
||||
const SYS_fallocate = 285
|
||||
const SYS_timerfd_settime = 286
|
||||
const SYS_timerfd_gettime = 287
|
||||
const SYS_accept4 = 288
|
||||
const SYS_signalfd4 = 289
|
||||
const SYS_eventfd2 = 290
|
||||
const SYS_epoll_create1 = 291
|
||||
const SYS_dup3 = 292
|
||||
const SYS_pipe2 = 293
|
||||
const SYS_inotify_init1 = 294
|
||||
const SYS_preadv = 295
|
||||
const SYS_pwritev = 296
|
||||
const SYS_rt_tgsigqueueinfo = 297
|
||||
const SYS_perf_event_open = 298
|
||||
const SYS_recvmmsg = 299
|
||||
const SYS_fanotify_init = 300
|
||||
const SYS_fanotify_mark = 301
|
||||
const SYS_prlimit64 = 302
|
||||
const SYS_name_to_handle_at = 303
|
||||
const SYS_open_by_handle_at = 304
|
||||
const SYS_clock_adjtime = 305
|
||||
const SYS_syncfs = 306
|
||||
const SYS_sendmmsg = 307
|
||||
const SYS_setns = 308
|
||||
const SYS_getcpu = 309
|
||||
const SYS_process_vm_readv = 310
|
||||
const SYS_process_vm_writev = 311
|
||||
const SYS_kcmp = 312
|
||||
const SYS_finit_module = 313
|
||||
const SYS_sched_setattr = 314
|
||||
const SYS_sched_getattr = 315
|
||||
const SYS_renameat2 = 316
|
||||
const SYS_seccomp = 317
|
||||
const SYS_getrandom = 318
|
||||
const SYS_memfd_create = 319
|
||||
const SYS_kexec_file_load = 320
|
||||
const SYS_bpf = 321
|
||||
const SYS_execveat = 322
|
||||
const SYS_userfaultfd = 323
|
||||
const SYS_membarrier = 324
|
||||
const SYS_mlock2 = 325
|
||||
const SYS_copy_file_range = 326
|
||||
const SYS_preadv2 = 327
|
||||
const SYS_pwritev2 = 328
|
||||
const SYS_pkey_mprotect = 329
|
||||
const SYS_pkey_alloc = 330
|
||||
const SYS_pkey_free = 331
|
||||
const SYS_statx = 332
|
||||
const SYS_io_pgetevents = 333
|
||||
const SYS_rseq = 334
|
||||
const SYS_uretprobe = 335
|
||||
const SYS_pidfd_send_signal = 424
|
||||
const SYS_io_uring_setup = 425
|
||||
const SYS_io_uring_enter = 426
|
||||
const SYS_io_uring_register = 427
|
||||
const SYS_open_tree = 428
|
||||
const SYS_move_mount = 429
|
||||
const SYS_fsopen = 430
|
||||
const SYS_fsconfig = 431
|
||||
const SYS_fsmount = 432
|
||||
const SYS_fspick = 433
|
||||
const SYS_pidfd_open = 434
|
||||
const SYS_clone3 = 435
|
||||
const SYS_close_range = 436
|
||||
const SYS_openat2 = 437
|
||||
const SYS_pidfd_getfd = 438
|
||||
const SYS_faccessat2 = 439
|
||||
const SYS_process_madvise = 440
|
||||
const SYS_epoll_pwait2 = 441
|
||||
const SYS_mount_setattr = 442
|
||||
const SYS_quotactl_fd = 443
|
||||
const SYS_landlock_create_ruleset = 444
|
||||
const SYS_landlock_add_rule = 445
|
||||
const SYS_landlock_restrict_self = 446
|
||||
const SYS_memfd_secret = 447
|
||||
const SYS_process_mrelease = 448
|
||||
const SYS_futex_waitv = 449
|
||||
const SYS_set_mempolicy_home_node = 450
|
||||
const SYS_cachestat = 451
|
||||
const SYS_fchmodat2 = 452
|
||||
const SYS_map_shadow_stack = 453
|
||||
const SYS_futex_wake = 454
|
||||
const SYS_futex_wait = 455
|
||||
const SYS_futex_requeue = 456
|
||||
const SYS_statmount = 457
|
||||
const SYS_listmount = 458
|
||||
const SYS_lsm_get_self_attr = 459
|
||||
const SYS_lsm_set_self_attr = 460
|
||||
const SYS_lsm_list_modules = 461
|
||||
const SYS_mseal = 462
|
||||
const SYS_setxattrat = 463
|
||||
const SYS_getxattrat = 464
|
||||
const SYS_listxattrat = 465
|
||||
const SYS_removexattrat = 466
|
||||
const SYS_open_tree_attr = 467
|
||||
@@ -17,8 +17,9 @@ pub enum TokenType {
|
||||
Colon,
|
||||
BitAnd,
|
||||
BitOr,
|
||||
LogicalAnd,
|
||||
LogicalOr,
|
||||
Pipe,
|
||||
At,
|
||||
DoubleDot,
|
||||
ShiftLeft,
|
||||
ShiftRight,
|
||||
@@ -39,6 +40,7 @@ pub enum TokenType {
|
||||
False,
|
||||
|
||||
KeywordLet,
|
||||
KeywordConst,
|
||||
KeywordIf,
|
||||
KeywordElse,
|
||||
KeywordWhile,
|
||||
@@ -155,7 +157,6 @@ impl Tokenizer {
|
||||
'%' => self.add_token(TokenType::Mod),
|
||||
'^' => self.add_token(TokenType::Xor),
|
||||
':' => self.add_token(TokenType::Colon),
|
||||
'@' => self.add_token(TokenType::At),
|
||||
'.' => {
|
||||
if self.match_char('.') {
|
||||
self.add_token(TokenType::DoubleDot)
|
||||
@@ -172,10 +173,18 @@ impl Tokenizer {
|
||||
self.add_token(TokenType::Slash)
|
||||
}
|
||||
}
|
||||
'&' => self.add_token(TokenType::BitAnd),
|
||||
'&' => {
|
||||
if self.match_char('&') {
|
||||
self.add_token(TokenType::LogicalAnd)
|
||||
} else {
|
||||
self.add_token(TokenType::BitAnd)
|
||||
}
|
||||
}
|
||||
'|' => {
|
||||
if self.match_char('>') {
|
||||
self.add_token(TokenType::Pipe);
|
||||
} else if self.match_char('|') {
|
||||
self.add_token(TokenType::LogicalOr);
|
||||
} else {
|
||||
self.add_token(TokenType::BitOr);
|
||||
}
|
||||
@@ -313,16 +322,6 @@ impl Tokenizer {
|
||||
while self.peek().is_ascii_digit() {
|
||||
self.advance();
|
||||
}
|
||||
|
||||
if self.peek() == '.'
|
||||
&& self.current + 1 < self.source.len()
|
||||
&& self.source[self.current + 1].is_ascii_digit()
|
||||
{
|
||||
self.advance();
|
||||
while self.peek().is_ascii_digit() {
|
||||
self.advance();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add_token(TokenType::Number);
|
||||
@@ -336,6 +335,7 @@ 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,
|
||||
"while" => TokenType::KeywordWhile,
|
||||
|
||||
67
test.zr
67
test.zr
@@ -1,60 +1,63 @@
|
||||
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)
|
||||
io.println("...\033[0m")
|
||||
return 0
|
||||
let build_blacklist: array = ["examples/puzzles", "examples/raylib.zr", "examples/x11.zr", "examples/sqlite_todo.zr"]
|
||||
let run_blacklist: array = ["/aoc", "guess_number.zr", "tcp_server.zr"]
|
||||
|
||||
io.print("\033[93mBuilding ")
|
||||
for i in 0..array.size(build_blacklist)
|
||||
if str.equal(x, array.nth(build_blacklist, i))
|
||||
io.print("\033[93mSkipping ")
|
||||
io.print(x)
|
||||
io.println("...\033[0m")
|
||||
return 0
|
||||
|
||||
io.print("\033[94mBuilding ")
|
||||
io.print(x)
|
||||
io.print("...\033[0m ")
|
||||
let cmd: str = str.concat("./target/release/zern examples/", x)
|
||||
|
||||
let build_start_time: i64 = os.time()
|
||||
if os.shell(cmd) != 0
|
||||
if os.shell(str.concat("./target/release/zern ", x)) != 0
|
||||
os.exit(1)
|
||||
let build_end_time: i64 = os.time()
|
||||
|
||||
mem.free(cmd)
|
||||
io.print_i64(build_end_time - build_start_time)
|
||||
io.println("ms")
|
||||
|
||||
if str.find(x, "/aoc") != -1 | str.equal(x, "guess_number.zr") | str.equal(x, "tcp_server.zr")
|
||||
io.print("\033[93mSkipping ")
|
||||
io.print(x)
|
||||
io.println("...\033[0m")
|
||||
return 0
|
||||
for i in 0..array.size(run_blacklist)
|
||||
if str.find(x, array.nth(run_blacklist, i)) != -1
|
||||
io.print("\033[93mSkipping ")
|
||||
io.print(x)
|
||||
io.println("...\033[0m")
|
||||
return 0
|
||||
|
||||
io.print("\033[93mRunning ")
|
||||
io.print("\033[95mRunning ")
|
||||
io.print(x)
|
||||
io.println("...\033[0m")
|
||||
|
||||
let run_cmd: str = "./out"
|
||||
if str.equal(x, "examples/curl.zr")
|
||||
run_cmd = str.concat(run_cmd, " http://example.com")
|
||||
else if str.equal(x, "examples/tokenizer.zr")
|
||||
run_cmd = str.concat(run_cmd, " test.zr")
|
||||
|
||||
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)
|
||||
if os.shell(run_cmd) != 0
|
||||
os.exit(1)
|
||||
let run_end_time: i64 = os.time()
|
||||
|
||||
io.print("\033[93mRunning ")
|
||||
io.print("\033[92mRunning ")
|
||||
io.print(x)
|
||||
io.print(" took\033[0m ")
|
||||
io.print_i64(run_end_time - run_start_time)
|
||||
io.println("ms")
|
||||
|
||||
func main[] : i64
|
||||
os.shell("cargo build --release")
|
||||
|
||||
let files: array = os.listdir("examples/")
|
||||
func run_directory[dir: str] : void
|
||||
let files: array = os.listdir(dir)
|
||||
for i in 0..array.size(files)
|
||||
run_test(array.nth(files, i))
|
||||
run_test(str.concat(dir, array.nth(files, i)))
|
||||
|
||||
array.free(files)
|
||||
|
||||
let puzzle_files: array = os.listdir("examples/puzzles/")
|
||||
for i in 0..array.size(puzzle_files)
|
||||
run_test(str.concat("puzzles/", array.nth(puzzle_files, i)))
|
||||
|
||||
array.free(puzzle_files)
|
||||
func main[] : i64
|
||||
os.shell("cargo build --release")
|
||||
|
||||
run_directory("examples/")
|
||||
run_directory("examples/puzzles/")
|
||||
Reference in New Issue
Block a user