Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d0a4fb7293 | |||
| 65913868b0 | |||
| 420f296ca5 | |||
| c603d10012 | |||
| b78223fae7 | |||
| abbe43a42c | |||
| 9a60518e0e | |||
| 20499a8ee0 | |||
| 55a8844699 | |||
| db2e639cc8 | |||
| 270386da95 | |||
| 07b46a987b | |||
| 781c35d484 | |||
| c527aceecd | |||
| 5682318915 | |||
| 3be891c7cc | |||
| e447d4d7cd | |||
| ebc887fb5b | |||
| 7c23e57ec0 | |||
| d55ca11658 | |||
| a259d204be | |||
| 0863094635 | |||
| 7f93599f34 | |||
| ada570c84e | |||
| fdcf7eca37 | |||
| fbf28748c7 | |||
| daf9079ca4 | |||
| 9f39f627ad | |||
| 7855e5b092 | |||
| b24bfc0241 | |||
| 73cd71c8e4 | |||
| 06c979f177 | |||
| ce54d1b560 | |||
| 7cffd73406 | |||
| 5a41163ca1 | |||
| 552a404d73 | |||
| 852c463532 |
37
README.md
37
README.md
@@ -5,23 +5,46 @@ A very cool language
|
|||||||
## Features
|
## Features
|
||||||
* Clean indentation-based syntax
|
* Clean indentation-based syntax
|
||||||
* Compiles to x86_64 Assembly
|
* Compiles to x86_64 Assembly
|
||||||
|
* Pretty big [standard library](https://github.com/antpiasecki/zern/tree/main/src/std)
|
||||||
|
* Produces tiny static executables (~30KB with musl)
|
||||||
|
* ~~No libc required~~ (SOON; still used for memory allocation and DNS resolution)
|
||||||
* Sometimes works
|
* Sometimes works
|
||||||
* Has the pipe operator
|
* Has the pipe operator
|
||||||
|
|
||||||
## Syntax
|
## Syntax
|
||||||
```rust
|
```rust
|
||||||
func main[] : I64
|
func main[] : i64
|
||||||
let answer: I64 = math.abs(math.urandom()) % 100
|
let answer: i64 = math.abs(os.urandom_i64()) % 100
|
||||||
|
|
||||||
while true
|
while true
|
||||||
io.print("Guess a number: ")
|
io.println("Guess a number: ")
|
||||||
let guess: I64 = io.read_stdin() |> str.trim() |> str.parse_i64()
|
let guess: i64 = io.read_line() |> str.trim() |> str.parse_i64()
|
||||||
|
|
||||||
if guess == answer
|
if guess == answer
|
||||||
io.print("You win!")
|
io.println("You win!")
|
||||||
break
|
break
|
||||||
else if guess < answer
|
else if guess < answer
|
||||||
io.print("Too low!")
|
io.println("Too low!")
|
||||||
else
|
else
|
||||||
io.print("Too high!")
|
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
|
||||||
|
zern -m -r hello.zr
|
||||||
```
|
```
|
||||||
@@ -1,46 +1,46 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
// https://brainfuck.org/sierpinski.b
|
// https://brainfuck.org/sierpinski.b
|
||||||
let src: String = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
let src: str = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
||||||
let src_len: I64 = str.len(src)
|
let src_len: i64 = str.len(src)
|
||||||
let i: I64 = 0
|
let i = 0
|
||||||
|
|
||||||
let memory: Ptr = mem.alloc(30000)
|
let memory: ptr = mem.alloc(30000)
|
||||||
mem.zero(memory, 30000)
|
mem.zero(memory, 30000)
|
||||||
let p: I64 = 0
|
let p = 0
|
||||||
|
|
||||||
while i < src_len
|
while i < src_len
|
||||||
let op: U8 = str.nth(src, i)
|
let op: u8 = src[i]
|
||||||
|
|
||||||
if op == '>'
|
if op == '>'
|
||||||
p = p + 1
|
p = p + 1
|
||||||
else if op == '<'
|
else if op == '<'
|
||||||
p = p - 1
|
p = p - 1
|
||||||
else if op == '+'
|
else if op == '+'
|
||||||
str.set(memory, p, mem.read8(memory + p)+1)
|
str.set(memory, p, memory[p] + 1)
|
||||||
else if op == '-'
|
else if op == '-'
|
||||||
str.set(memory, p, mem.read8(memory + p)-1)
|
str.set(memory, p, memory[p] - 1)
|
||||||
else if op == '.'
|
else if op == '.'
|
||||||
c.printf("%c", mem.read8(memory + p))
|
io.print_char(memory[p])
|
||||||
else if op == ','
|
else if op == ','
|
||||||
str.set(memory, p, c.getchar())
|
str.set(memory, p, io.read_char())
|
||||||
else if op == '['
|
else if op == '['
|
||||||
if !mem.read8(memory + p)
|
if !memory[p]
|
||||||
i = i + 1
|
i = i + 1
|
||||||
let opened: I64 = 0
|
let opened = 0
|
||||||
while i < src_len & !(str.nth(src, i) == ']' & !opened)
|
while i < src_len && !(src[i] == ']' && !opened)
|
||||||
if str.nth(src, i) == '['
|
if src[i] == '['
|
||||||
opened = opened + 1
|
opened = opened + 1
|
||||||
else if str.nth(src, i) == ']'
|
else if src[i] == ']'
|
||||||
opened = opened - 1
|
opened = opened - 1
|
||||||
i = i + 1
|
i = i + 1
|
||||||
else if op == ']'
|
else if op == ']'
|
||||||
if mem.read8(memory + p)
|
if memory[p]
|
||||||
i = i - 1
|
i = i - 1
|
||||||
let closed: I64 = 0
|
let closed = 0
|
||||||
while i >= 0 & !(str.nth(src, i) == '[' & !closed)
|
while i >= 0 && !(src[i] == '[' && !closed)
|
||||||
if str.nth(src, i) == ']'
|
if src[i] == ']'
|
||||||
closed = closed + 1
|
closed = closed + 1
|
||||||
else if str.nth(src, i) == '['
|
else if src[i] == '['
|
||||||
closed = closed - 1
|
closed = closed - 1
|
||||||
i = i - 1
|
i = i - 1
|
||||||
|
|
||||||
|
|||||||
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))
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
func main[argc: I64, argv: Ptr] : I64
|
func main[argc: i64, argv: ptr] : i64
|
||||||
if argc < 2
|
if argc < 2
|
||||||
dbg.panic("url missing")
|
dbg.panic("url missing")
|
||||||
|
|
||||||
let url: String = mem.read64(argv + 8)
|
let url: str = mem.read64(argv + 8)
|
||||||
|
|
||||||
if str.len(url) <= 7
|
if str.len(url) <= 7
|
||||||
dbg.panic("missing url scheme")
|
dbg.panic("missing url scheme")
|
||||||
@@ -10,41 +10,45 @@ func main[argc: I64, argv: Ptr] : I64
|
|||||||
if !str.equal(str.substr(url, 0, 7), "http://")
|
if !str.equal(str.substr(url, 0, 7), "http://")
|
||||||
dbg.panic("invalid url scheme")
|
dbg.panic("invalid url scheme")
|
||||||
|
|
||||||
let url_len: I64 = str.len(url)
|
let url_len: i64 = str.len(url)
|
||||||
let host_start: I64 = 7
|
let host_start = 7
|
||||||
let i: I64 = host_start
|
let i: i64 = host_start
|
||||||
while i < url_len
|
while i < url_len
|
||||||
if str.nth(url, i) == '/'
|
if url[i] == '/'
|
||||||
break
|
break
|
||||||
i = i + 1
|
i = i + 1
|
||||||
|
|
||||||
let host: String = str.substr(url, host_start, i - host_start)
|
let host: str = str.substr(url, host_start, i - host_start)
|
||||||
let path: String = "/"
|
let path: str = "/"
|
||||||
if i < url_len
|
if i < url_len
|
||||||
path = str.substr(url, i, url_len - i)
|
path = str.substr(url, i, url_len - i)
|
||||||
|
|
||||||
let s: I64 = net.connect(host, 80)
|
let s: i64 = net.connect(host, 80)
|
||||||
if s < 0
|
if s < 0
|
||||||
dbg.panic("failed to connect")
|
dbg.panic("failed to connect")
|
||||||
|
|
||||||
let req: String = mem.alloc(2048)
|
// very leaky
|
||||||
c.snprintf(req, 2048, "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", path, host)
|
let req: str = "GET "
|
||||||
c.send(s, req, str.len(req), 0)
|
req = str.concat(req, path)
|
||||||
|
req = str.concat(req, " HTTP/1.0\r\nHost: ")
|
||||||
|
req = str.concat(req, host)
|
||||||
|
req = str.concat(req, "\r\nConnection: close\r\n\r\n")
|
||||||
|
net.send(s, req, str.len(req))
|
||||||
mem.free(req)
|
mem.free(req)
|
||||||
|
|
||||||
let header_buf: String = mem.alloc(8192)
|
let header_buf: str = mem.alloc(8192)
|
||||||
let header_size: I64 = 0
|
let header_size = 0
|
||||||
let found: Bool = false
|
let found: bool = false
|
||||||
let end_index: I64 = -1
|
let end_index: i64 = -1
|
||||||
|
|
||||||
while !found & header_size < 8192
|
while !found && header_size < 8192
|
||||||
let n: I64 = c.read(s, header_buf + header_size, 8192 - header_size)
|
let n: i64 = net.read(s, header_buf + header_size, 8192 - header_size)
|
||||||
if n <= 0
|
if n <= 0
|
||||||
break
|
break
|
||||||
let current_size: I64 = header_size + n
|
let current_size: i64 = header_size + n
|
||||||
i = 0
|
i = 0
|
||||||
while i <= current_size - 4
|
while i <= current_size - 4
|
||||||
if str.nth(header_buf, i) == 13 & str.nth(header_buf, i+1) == 10 & str.nth(header_buf, i+2) == 13 & str.nth(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
|
found = true
|
||||||
end_index = i + 4
|
end_index = i + 4
|
||||||
break
|
break
|
||||||
@@ -52,15 +56,15 @@ func main[argc: I64, argv: Ptr] : I64
|
|||||||
header_size = current_size
|
header_size = current_size
|
||||||
|
|
||||||
if end_index < header_size
|
if end_index < header_size
|
||||||
c.write(1, header_buf + end_index, header_size - end_index)
|
io.print_sized(header_buf + end_index, header_size - end_index)
|
||||||
mem.free(header_buf)
|
mem.free(header_buf)
|
||||||
|
|
||||||
let buffer: Ptr = mem.alloc(4096)
|
let buffer: ptr = mem.alloc(4096)
|
||||||
while true
|
while true
|
||||||
let n: I64 = c.read(s, buffer, 4096)
|
let n: i64 = net.read(s, buffer, 4096)
|
||||||
if n <= 0
|
if n <= 0
|
||||||
break
|
break
|
||||||
c.write(1, buffer, n)
|
io.print_sized(buffer, n)
|
||||||
mem.free(buffer)
|
mem.free(buffer)
|
||||||
|
|
||||||
c.close(s)
|
net.close(s)
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
func main[] : I64
|
|
||||||
let sum: I64 = 0
|
|
||||||
|
|
||||||
for i in 0..1000
|
|
||||||
if i % 5 == 0 | i % 3 == 0
|
|
||||||
sum = sum + i
|
|
||||||
io.print_i64(sum)
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
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
|
|
||||||
|
|
||||||
io.print_i64(out)
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
func main[] : I64
|
|
||||||
let sum: I64 = 0
|
|
||||||
let a: I64 = 0
|
|
||||||
let b: I64 = 1
|
|
||||||
|
|
||||||
while a < 4000000
|
|
||||||
if a % 2 == 0
|
|
||||||
sum = sum + a
|
|
||||||
let temp: I64 = b
|
|
||||||
b = a + b
|
|
||||||
a = temp
|
|
||||||
|
|
||||||
io.print_i64(sum)
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
func main[] : I64
|
|
||||||
let out: I64 = 1
|
|
||||||
|
|
||||||
for i in 1..21
|
|
||||||
out = math.lcm(out, i)
|
|
||||||
io.print_i64(out)
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
func main[] : I64
|
|
||||||
let n: String = "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 * (str.nth(n, i + j) - '0')
|
|
||||||
j = j + 1
|
|
||||||
if s > out
|
|
||||||
out = s
|
|
||||||
io.print_i64(out)
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
let a: I64 = 0
|
let a = 0
|
||||||
let b: I64 = 1
|
let b = 1
|
||||||
|
|
||||||
while a < 100000
|
while a < 100000
|
||||||
io.print_i64(a)
|
io.println_i64(a)
|
||||||
let temp: I64 = b
|
let temp: i64 = b
|
||||||
b = a + b
|
b = a + b
|
||||||
a = temp
|
a = temp
|
||||||
10
examples/fizzbuzz.zr
Normal file
10
examples/fizzbuzz.zr
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
func main[] : i64
|
||||||
|
for i in 1..40
|
||||||
|
if i % 15 == 0
|
||||||
|
io.println("FizzBuzz")
|
||||||
|
else if i % 5 == 0
|
||||||
|
io.println("Buzz")
|
||||||
|
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,14 +1,14 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
let answer: I64 = math.abs(math.urandom()) % 100
|
let answer: i64 = math.abs(os.urandom_i64()) % 100
|
||||||
|
|
||||||
while true
|
while true
|
||||||
io.print("Guess a number: ")
|
io.println("Guess a number: ")
|
||||||
let guess: I64 = io.read_stdin() |> str.trim() |> str.parse_i64()
|
let guess: i64 = io.read_line() |> str.trim() |> str.parse_i64()
|
||||||
|
|
||||||
if guess == answer
|
if guess == answer
|
||||||
io.print("You win!")
|
io.println("You win!")
|
||||||
break
|
break
|
||||||
else if guess < answer
|
else if guess < answer
|
||||||
io.print("Too low!")
|
io.println("Too low!")
|
||||||
else
|
else
|
||||||
io.print("Too high!")
|
io.println("Too high!")
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
io.print("Hello, World!")
|
io.println("Hello, World!")
|
||||||
35
examples/puzzles/aoc2024_01.zr
Normal file
35
examples/puzzles/aoc2024_01.zr
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
func part1[l1: array, l2: array] : void
|
||||||
|
let out = 0
|
||||||
|
|
||||||
|
for i in 0..array.size(l1)
|
||||||
|
out = out + math.abs(array.nth(l1, i) - array.nth(l2, i))
|
||||||
|
|
||||||
|
io.println_i64(out)
|
||||||
|
|
||||||
|
func part2[l1: array, l2: array] : void
|
||||||
|
let out = 0
|
||||||
|
|
||||||
|
for i in 0..array.size(l1)
|
||||||
|
out = out + array.nth(l1, i) * alg.count(l2, array.nth(l1, i))
|
||||||
|
|
||||||
|
io.println_i64(out)
|
||||||
|
|
||||||
|
func main[] : i64
|
||||||
|
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||||
|
|
||||||
|
let l1: array = []
|
||||||
|
let l2: array = []
|
||||||
|
|
||||||
|
for i in 0..array.size(lines)
|
||||||
|
let line: str = array.nth(lines, i)
|
||||||
|
|
||||||
|
let parts: array = str.split(line, " ")
|
||||||
|
|
||||||
|
array.push(l1, str.parse_i64(array.nth(parts, 0)))
|
||||||
|
array.push(l2, str.parse_i64(array.nth(parts, 1)))
|
||||||
|
|
||||||
|
alg.quicksort(l1)
|
||||||
|
alg.quicksort(l2)
|
||||||
|
|
||||||
|
part1(l1, l2)
|
||||||
|
part2(l1, l2)
|
||||||
54
examples/puzzles/aoc2024_02.zr
Normal file
54
examples/puzzles/aoc2024_02.zr
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
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
|
||||||
|
return false
|
||||||
|
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
|
||||||
|
return false
|
||||||
|
|
||||||
|
return true
|
||||||
|
|
||||||
|
func part1[data: array] : void
|
||||||
|
let out = 0
|
||||||
|
|
||||||
|
for i in 0..array.size(data)
|
||||||
|
if check(array.nth(data, i))
|
||||||
|
out = out + 1
|
||||||
|
|
||||||
|
io.println_i64(out)
|
||||||
|
|
||||||
|
func part2[data: array] : void
|
||||||
|
let out = 0
|
||||||
|
|
||||||
|
for i in 0..array.size(data)
|
||||||
|
if check(array.nth(data, i))
|
||||||
|
out = out + 1
|
||||||
|
else
|
||||||
|
for j in 0..array.size(array.nth(data, i))
|
||||||
|
let sliced: array = array.concat(array.slice(array.nth(data, i), 0, j), array.slice(array.nth(data, i), j + 1, array.size(array.nth(data, i)) - (j + 1)))
|
||||||
|
|
||||||
|
if check(sliced)
|
||||||
|
out = out + 1
|
||||||
|
break
|
||||||
|
|
||||||
|
io.println_i64(out)
|
||||||
|
|
||||||
|
func main[] : i64
|
||||||
|
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||||
|
|
||||||
|
let data: array = []
|
||||||
|
for i in 0..array.size(lines)
|
||||||
|
let line: str = array.nth(lines, i)
|
||||||
|
|
||||||
|
let report: array = str.split(line, " ")
|
||||||
|
for i in 0..array.size(report)
|
||||||
|
array.set(report, i, str.parse_i64(array.nth(report, i)))
|
||||||
|
array.push(data, report)
|
||||||
|
|
||||||
|
part1(data)
|
||||||
|
part2(data)
|
||||||
62
examples/puzzles/aoc2024_04.zr
Normal file
62
examples/puzzles/aoc2024_04.zr
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
func check[lines: array, x: i64, y: i64, dx: i64, dy: i64] : bool
|
||||||
|
if x + dx * 3 < 0 || x + dx * 3 >= array.size(lines) || y + dy * 3 < 0 || y + dy * 3 >= str.len(array.nth(lines, 0))
|
||||||
|
return false
|
||||||
|
|
||||||
|
if array.nth(lines, x)[y] != 'X'
|
||||||
|
return false
|
||||||
|
if array.nth(lines, x + dx)[y + dy] != 'M'
|
||||||
|
return false
|
||||||
|
if array.nth(lines, x + dx * 2)[y + dy * 2] != 'A'
|
||||||
|
return false
|
||||||
|
if array.nth(lines, x + dx * 3)[y + dy * 3] != 'S'
|
||||||
|
return false
|
||||||
|
|
||||||
|
return true
|
||||||
|
|
||||||
|
func part1[lines: array] : void
|
||||||
|
let out = 0
|
||||||
|
|
||||||
|
for x in 0..array.size(lines)
|
||||||
|
for y in 0..str.len(array.nth(lines, x))
|
||||||
|
if check(lines, x, y, 0, 1)
|
||||||
|
out = out + 1
|
||||||
|
if check(lines, x, y, 0, -1)
|
||||||
|
out = out + 1
|
||||||
|
if check(lines, x, y, 1, 0)
|
||||||
|
out = out + 1
|
||||||
|
if check(lines, x, y, -1, 0)
|
||||||
|
out = out + 1
|
||||||
|
if check(lines, x, y, 1, 1)
|
||||||
|
out = out + 1
|
||||||
|
if check(lines, x, y, -1, -1)
|
||||||
|
out = out + 1
|
||||||
|
if check(lines, x, y, 1, -1)
|
||||||
|
out = out + 1
|
||||||
|
if check(lines, x, y, -1, 1)
|
||||||
|
out = out + 1
|
||||||
|
|
||||||
|
io.println_i64(out)
|
||||||
|
|
||||||
|
func part2[lines: array] : void
|
||||||
|
let out = 0
|
||||||
|
|
||||||
|
for x in 1..array.size(lines)-1
|
||||||
|
for y in 1..str.len(array.nth(lines, x))-1
|
||||||
|
if array.nth(lines, x)[y] == 'A'
|
||||||
|
let s: str = mem.alloc(5)
|
||||||
|
str.set(s, 0, array.nth(lines, x - 1)[y - 1])
|
||||||
|
str.set(s, 1, array.nth(lines, x + 1)[y - 1])
|
||||||
|
str.set(s, 2, array.nth(lines, x + 1)[y + 1])
|
||||||
|
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")
|
||||||
|
out = out + 1
|
||||||
|
|
||||||
|
io.println_i64(out)
|
||||||
|
|
||||||
|
func main[] : i64
|
||||||
|
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||||
|
|
||||||
|
part1(lines)
|
||||||
|
part2(lines)
|
||||||
73
examples/puzzles/aoc2024_05.zr
Normal file
73
examples/puzzles/aoc2024_05.zr
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
func rule_exists[rules_left: array, rules_right: array, a: i64, b: i64] : bool
|
||||||
|
for k in 0..array.size(rules_left)
|
||||||
|
if array.nth(rules_left, k) == a && array.nth(rules_right, k) == b
|
||||||
|
return true
|
||||||
|
return false
|
||||||
|
|
||||||
|
func check[update: array, rules_left: array, rules_right: array] : bool
|
||||||
|
for i in 0..array.size(update)
|
||||||
|
for j in 0..i
|
||||||
|
if rule_exists(rules_left, rules_right, array.nth(update, i), array.nth(update, j))
|
||||||
|
return false
|
||||||
|
return true
|
||||||
|
|
||||||
|
func sort_by_rules[update: array, rules_left: array, rules_right: array] : void
|
||||||
|
let swapped: bool = true
|
||||||
|
|
||||||
|
while swapped
|
||||||
|
swapped = false
|
||||||
|
for i in 0..array.size(update)-1
|
||||||
|
let a: i64 = array.nth(update, i)
|
||||||
|
let b: i64 = array.nth(update, i+1)
|
||||||
|
if rule_exists(rules_left, rules_right, b, a)
|
||||||
|
let tmp: i64 = a
|
||||||
|
array.set(update, i, b)
|
||||||
|
array.set(update, i+1, tmp)
|
||||||
|
swapped = true
|
||||||
|
|
||||||
|
func part1[updates: array, rules_left: array, rules_right: array] : void
|
||||||
|
let out = 0
|
||||||
|
|
||||||
|
for i in 0..array.size(updates)
|
||||||
|
let update: array = array.nth(updates, i)
|
||||||
|
if check(update, rules_left, rules_right)
|
||||||
|
out = out + array.nth(update, array.size(update) / 2)
|
||||||
|
|
||||||
|
io.println_i64(out)
|
||||||
|
|
||||||
|
func part2[updates: array, rules_left: array, rules_right: array] : void
|
||||||
|
let out = 0
|
||||||
|
|
||||||
|
for i in 0..array.size(updates)
|
||||||
|
let update: array = array.nth(updates, i)
|
||||||
|
if !check(update, rules_left, rules_right)
|
||||||
|
sort_by_rules(update, rules_left, rules_right)
|
||||||
|
out = out + array.nth(update, array.size(update) / 2)
|
||||||
|
|
||||||
|
io.println_i64(out)
|
||||||
|
|
||||||
|
func main[] : i64
|
||||||
|
let data: array = io.read_file("input.txt") |> str.split("\n\n")
|
||||||
|
|
||||||
|
let rules_left: array = []
|
||||||
|
let rules_right: array = []
|
||||||
|
let rules_lines: array = str.split(array.nth(data, 0), "\n")
|
||||||
|
for i in 0..array.size(rules_lines)
|
||||||
|
let line: str = array.nth(rules_lines, i)
|
||||||
|
let parts: array = str.split(line, "|")
|
||||||
|
|
||||||
|
array.push(rules_left, str.parse_i64(array.nth(parts, 0)))
|
||||||
|
array.push(rules_right, str.parse_i64(array.nth(parts, 1)))
|
||||||
|
|
||||||
|
let updates: array = []
|
||||||
|
let updates_lines: array = str.split(array.nth(data, 1), "\n")
|
||||||
|
for i in 0..array.size(updates_lines)
|
||||||
|
let line: str = array.nth(updates_lines, i)
|
||||||
|
let xs: array = str.split(line, ",")
|
||||||
|
|
||||||
|
for i in 0..array.size(xs)
|
||||||
|
array.set(xs, i, str.parse_i64(array.nth(xs, i)))
|
||||||
|
array.push(updates, xs)
|
||||||
|
|
||||||
|
part1(updates, rules_left, rules_right)
|
||||||
|
part2(updates, rules_left, rules_right)
|
||||||
77
examples/puzzles/aoc2024_07.zr
Normal file
77
examples/puzzles/aoc2024_07.zr
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
func concat[a: i64, b: i64] : i64
|
||||||
|
let a_str: str = str.from_i64(a)
|
||||||
|
let b_str: str = str.from_i64(b)
|
||||||
|
let ab_str: str = str.concat(a_str, b_str)
|
||||||
|
let out: i64 = str.parse_i64(ab_str)
|
||||||
|
// without freeing the program works but leaks 2GB of memory :p
|
||||||
|
mem.free(a_str)
|
||||||
|
mem.free(b_str)
|
||||||
|
mem.free(ab_str)
|
||||||
|
return out
|
||||||
|
|
||||||
|
func solve[ops: array, e: array] : i64
|
||||||
|
let n: i64 = array.size(array.nth(e, 1)) - 1
|
||||||
|
let indices: array = []
|
||||||
|
for i in 0..n
|
||||||
|
array.push(indices, 0)
|
||||||
|
|
||||||
|
while true
|
||||||
|
let res: i64 = array.nth(array.nth(e, 1), 0)
|
||||||
|
for i in 0..n
|
||||||
|
let op: str = array.nth(ops, array.nth(indices, i))
|
||||||
|
|
||||||
|
if str.equal(op, "add")
|
||||||
|
res = res + array.nth(array.nth(e, 1), i + 1)
|
||||||
|
else if str.equal(op, "mul")
|
||||||
|
res = res * array.nth(array.nth(e, 1), i + 1)
|
||||||
|
else if str.equal(op, "concat")
|
||||||
|
res = concat(res, array.nth(array.nth(e, 1), i + 1))
|
||||||
|
if res == array.nth(e, 0)
|
||||||
|
return res
|
||||||
|
|
||||||
|
let done: bool = true
|
||||||
|
let i: i64 = n - 1
|
||||||
|
|
||||||
|
while i >= 0
|
||||||
|
array.set(indices, i, array.nth(indices, i) + 1)
|
||||||
|
if array.nth(indices, i) < array.size(ops)
|
||||||
|
done = false
|
||||||
|
break
|
||||||
|
array.set(indices, i, 0)
|
||||||
|
i = i - 1
|
||||||
|
|
||||||
|
if done
|
||||||
|
return 0
|
||||||
|
|
||||||
|
func part1[equations: array] : void
|
||||||
|
let out = 0
|
||||||
|
|
||||||
|
for i in 0..array.size(equations)
|
||||||
|
out = out + solve(["add", "mul"], array.nth(equations, i))
|
||||||
|
|
||||||
|
io.println_i64(out)
|
||||||
|
|
||||||
|
func part2[equations: array] : void
|
||||||
|
let out = 0
|
||||||
|
|
||||||
|
for i in 0..array.size(equations)
|
||||||
|
out = out + solve(["add", "mul", "concat"], array.nth(equations, i))
|
||||||
|
|
||||||
|
io.println_i64(out)
|
||||||
|
|
||||||
|
func main[] : i64
|
||||||
|
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||||
|
let equations: array = []
|
||||||
|
|
||||||
|
for i in 0..array.size(lines)
|
||||||
|
let line: str = array.nth(lines, i)
|
||||||
|
let parts: array = str.split(line, ": ")
|
||||||
|
|
||||||
|
let xs: array = str.split(array.nth(parts, 1), " ")
|
||||||
|
for j in 0..array.size(xs)
|
||||||
|
array.set(xs, j, str.parse_i64(array.nth(xs, j)))
|
||||||
|
|
||||||
|
array.push(equations, [str.parse_i64(array.nth(parts, 0)), xs])
|
||||||
|
|
||||||
|
part1(equations)
|
||||||
|
part2(equations)
|
||||||
53
examples/puzzles/aoc2025_01.zr
Normal file
53
examples/puzzles/aoc2025_01.zr
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
func part1[lines: array] : void
|
||||||
|
let password = 0
|
||||||
|
let dial = 50
|
||||||
|
|
||||||
|
for i in 0..array.size(lines)
|
||||||
|
let line: str = array.nth(lines, i)
|
||||||
|
let dir: u8 = line[0]
|
||||||
|
let n: i64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||||
|
|
||||||
|
if dir == 'L'
|
||||||
|
dial = dial - n
|
||||||
|
while dial < 0
|
||||||
|
dial = 100 + dial
|
||||||
|
else
|
||||||
|
dial = dial + n
|
||||||
|
while dial >= 100
|
||||||
|
dial = dial - 100
|
||||||
|
|
||||||
|
if dial == 0
|
||||||
|
password = password + 1
|
||||||
|
|
||||||
|
io.println_i64(password)
|
||||||
|
|
||||||
|
func part2[lines: array] : void
|
||||||
|
let password = 0
|
||||||
|
let dial = 50
|
||||||
|
|
||||||
|
for i in 0..array.size(lines)
|
||||||
|
let line: str = array.nth(lines, i)
|
||||||
|
let dir: u8 = line[0]
|
||||||
|
let n: i64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||||
|
|
||||||
|
if dir == 'L'
|
||||||
|
for i in 0..n
|
||||||
|
dial = dial - 1
|
||||||
|
if dial == 0
|
||||||
|
password = password + 1
|
||||||
|
if dial == -1
|
||||||
|
dial = 99
|
||||||
|
else
|
||||||
|
for i in 0..n
|
||||||
|
dial = dial + 1
|
||||||
|
if dial == 100
|
||||||
|
dial = 0
|
||||||
|
password = password + 1
|
||||||
|
|
||||||
|
io.println_i64(password)
|
||||||
|
|
||||||
|
func main[] : i64
|
||||||
|
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||||
|
|
||||||
|
part1(lines)
|
||||||
|
part2(lines)
|
||||||
60
examples/puzzles/aoc2025_02.zr
Normal file
60
examples/puzzles/aoc2025_02.zr
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
func part1_is_invalid_id[s: str] : bool
|
||||||
|
let len: i64 = str.len(s)
|
||||||
|
if len % 2 != 0
|
||||||
|
return false
|
||||||
|
|
||||||
|
let first: str = str.substr(s, 0, len / 2)
|
||||||
|
let second: str = str.substr(s, len / 2, len / 2)
|
||||||
|
|
||||||
|
return str.equal(first, second)
|
||||||
|
|
||||||
|
func part1[ranges: array] : void
|
||||||
|
let sum = 0
|
||||||
|
|
||||||
|
for i in 0..array.size(ranges)
|
||||||
|
let parts: array = array.nth(ranges, i) |> str.split("-")
|
||||||
|
let start: i64 = array.nth(parts, 0) |> str.parse_i64()
|
||||||
|
let end: i64 = array.nth(parts, 1) |> str.parse_i64()
|
||||||
|
|
||||||
|
for n in (start)..end+1
|
||||||
|
if part1_is_invalid_id(str.from_i64(n))
|
||||||
|
sum = sum + n
|
||||||
|
|
||||||
|
io.println_i64(sum)
|
||||||
|
|
||||||
|
// had to cheat this one
|
||||||
|
func part2_is_invalid_id[s: str] : bool
|
||||||
|
let len: i64 = str.len(s)
|
||||||
|
if len < 2
|
||||||
|
return false
|
||||||
|
for div in 1..(len / 2 + 1)
|
||||||
|
if len % div == 0
|
||||||
|
let u: str = str.substr(s, 0, div)
|
||||||
|
let is_repeat: bool = true
|
||||||
|
for k in 1..(len / div)
|
||||||
|
let segment: str = str.substr(s, k * div, div)
|
||||||
|
if !str.equal(segment, u)
|
||||||
|
is_repeat = false
|
||||||
|
if is_repeat
|
||||||
|
return true
|
||||||
|
return false
|
||||||
|
|
||||||
|
func part2[ranges: array] : void
|
||||||
|
let sum = 0
|
||||||
|
|
||||||
|
for i in 0..array.size(ranges)
|
||||||
|
let parts: array = array.nth(ranges, i) |> str.split("-")
|
||||||
|
let start: i64 = array.nth(parts, 0) |> str.parse_i64()
|
||||||
|
let end: i64 = array.nth(parts, 1) |> str.parse_i64()
|
||||||
|
|
||||||
|
for n in (start)..end+1
|
||||||
|
if part2_is_invalid_id(str.from_i64(n))
|
||||||
|
sum = sum + n
|
||||||
|
|
||||||
|
io.println_i64(sum)
|
||||||
|
|
||||||
|
func main[] : i64
|
||||||
|
let ranges: array = io.read_file("input.txt") |> str.split(",")
|
||||||
|
|
||||||
|
part1(ranges)
|
||||||
|
part2(ranges)
|
||||||
51
examples/puzzles/aoc2025_03.zr
Normal file
51
examples/puzzles/aoc2025_03.zr
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
func part1[lines: array] : void
|
||||||
|
let sum = 0
|
||||||
|
|
||||||
|
for i in 0..array.size(lines)
|
||||||
|
let line: str = array.nth(lines, i)
|
||||||
|
|
||||||
|
let largest = 0
|
||||||
|
for j in 0..str.len(line)
|
||||||
|
for k in (j+1)..str.len(line)
|
||||||
|
let s: str = mem.alloc(3)
|
||||||
|
str.set(s, 0, line[j])
|
||||||
|
str.set(s, 1, line[k])
|
||||||
|
str.set(s, 2, 0)
|
||||||
|
let n: i64 = str.parse_i64(s)
|
||||||
|
if n > largest
|
||||||
|
largest = n
|
||||||
|
|
||||||
|
sum = sum + largest
|
||||||
|
io.println_i64(sum)
|
||||||
|
|
||||||
|
// had to cheat this one
|
||||||
|
func part2_rec[bank: str, start: i64, remaining: i64] : i64
|
||||||
|
let largest = 0
|
||||||
|
let largest_idx: i64 = start
|
||||||
|
let len: i64 = str.len(bank)
|
||||||
|
|
||||||
|
for i in (start)..(len-remaining+1)
|
||||||
|
let v: i64 = bank[i] - '0'
|
||||||
|
if v > largest
|
||||||
|
largest = v
|
||||||
|
largest_idx = i
|
||||||
|
|
||||||
|
if remaining > 1
|
||||||
|
return largest * math.pow(10, remaining - 1) + part2_rec(bank, largest_idx + 1, remaining - 1)
|
||||||
|
else
|
||||||
|
return largest
|
||||||
|
|
||||||
|
func part2[lines: array] : void
|
||||||
|
let sum = 0
|
||||||
|
|
||||||
|
for i in 0..array.size(lines)
|
||||||
|
let line: str = array.nth(lines, i)
|
||||||
|
|
||||||
|
sum = sum + part2_rec(line, 0, 12)
|
||||||
|
io.println_i64(sum)
|
||||||
|
|
||||||
|
func main[] : i64
|
||||||
|
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||||
|
|
||||||
|
part1(lines)
|
||||||
|
part2(lines)
|
||||||
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)
|
||||||
7
examples/puzzles/euler_01.zr
Normal file
7
examples/puzzles/euler_01.zr
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
func main[] : i64
|
||||||
|
let sum = 0
|
||||||
|
|
||||||
|
for i in 0..1000
|
||||||
|
if i % 5 == 0 || i % 3 == 0
|
||||||
|
sum = sum + i
|
||||||
|
io.println_i64(sum)
|
||||||
13
examples/puzzles/euler_02.zr
Normal file
13
examples/puzzles/euler_02.zr
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
func main[] : i64
|
||||||
|
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,6 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
let n: I64 = 600851475143
|
let n = 600851475143
|
||||||
let f: I64 = 2
|
let f = 2
|
||||||
|
|
||||||
while f * f <= n
|
while f * f <= n
|
||||||
if n % f == 0
|
if n % f == 0
|
||||||
@@ -8,4 +8,4 @@ func main[] : I64
|
|||||||
else
|
else
|
||||||
f = f + 1
|
f = f + 1
|
||||||
|
|
||||||
io.print_i64(n)
|
io.println_i64(n)
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
let out: I64 = 0
|
let out = 0
|
||||||
|
|
||||||
for a in 500..1000
|
for a in 500..1000
|
||||||
for b in 500..1000
|
for b in 500..1000
|
||||||
if a * b > out
|
if a * b > out
|
||||||
let s: String = str.from_i64(a * b)
|
let s: str = str.from_i64(a * b)
|
||||||
let s_rev: String = str.reverse(s)
|
let s_rev: str = str.reverse(s)
|
||||||
if str.equal(s, s_rev)
|
if str.equal(s, s_rev)
|
||||||
out = a * b
|
out = a * b
|
||||||
mem.free(s)
|
mem.free(s)
|
||||||
mem.free(s_rev)
|
mem.free(s_rev)
|
||||||
io.print_i64(out)
|
io.println_i64(out)
|
||||||
6
examples/puzzles/euler_05.zr
Normal file
6
examples/puzzles/euler_05.zr
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
func main[] : i64
|
||||||
|
let out = 1
|
||||||
|
|
||||||
|
for i in 1..21
|
||||||
|
out = math.lcm(out, i)
|
||||||
|
io.println_i64(out)
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
let sum_of_squares: I64 = 0
|
let sum_of_squares = 0
|
||||||
for i in 1..101
|
for i in 1..101
|
||||||
sum_of_squares = sum_of_squares + i * i
|
sum_of_squares = sum_of_squares + i * i
|
||||||
|
|
||||||
let square_of_sum: I64 = 0
|
let square_of_sum = 0
|
||||||
for i in 1..101
|
for i in 1..101
|
||||||
square_of_sum = square_of_sum + i
|
square_of_sum = square_of_sum + i
|
||||||
square_of_sum = square_of_sum * square_of_sum
|
square_of_sum = square_of_sum * square_of_sum
|
||||||
|
|
||||||
io.print_i64(square_of_sum - sum_of_squares)
|
io.println_i64(square_of_sum - sum_of_squares)
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
let found: I64 = 0
|
let found = 0
|
||||||
|
|
||||||
let i: I64 = 1
|
let i = 1
|
||||||
while true
|
while true
|
||||||
if math.is_prime(i)
|
if math.is_prime(i)
|
||||||
found = found + 1
|
found = found + 1
|
||||||
if found == 10001
|
if found == 10001
|
||||||
io.print_i64(i)
|
io.println_i64(i)
|
||||||
break
|
break
|
||||||
i = i + 1
|
i = i + 1
|
||||||
14
examples/puzzles/euler_08.zr
Normal file
14
examples/puzzles/euler_08.zr
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
func main[] : i64
|
||||||
|
let n: str = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
|
||||||
|
|
||||||
|
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,7 +1,7 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
for a in 1..1000
|
for a in 1..1000
|
||||||
for b in 1..1000
|
for b in 1..1000
|
||||||
let c: I64 = 1000 - b - a
|
let c: i64 = 1000 - b - a
|
||||||
if a * a + b * b == c * c
|
if a * a + b * b == c * c
|
||||||
io.print_i64(a * b * c)
|
io.println_i64(a * b * c)
|
||||||
return 0
|
return 0
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
let sum: I64 = 0
|
let sum = 0
|
||||||
|
|
||||||
for i in 0..2000000
|
for i in 0..2000000
|
||||||
if math.is_prime(i)
|
if math.is_prime(i)
|
||||||
sum = sum + i
|
sum = sum + i
|
||||||
io.print_i64(sum)
|
io.println_i64(sum)
|
||||||
36
examples/puzzles/euler_11.zr
Normal file
36
examples/puzzles/euler_11.zr
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
func main[] : i64
|
||||||
|
let N = 20
|
||||||
|
|
||||||
|
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])
|
||||||
|
|
||||||
|
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,7 +1,7 @@
|
|||||||
func num_divisors[n: I64] : I64
|
func num_divisors[n: i64] : i64
|
||||||
let end: I64 = math.isqrt(n)
|
let end: i64 = math.isqrt(n)
|
||||||
|
|
||||||
let out: I64 = 0
|
let out = 0
|
||||||
for i in 1..end+1
|
for i in 1..end+1
|
||||||
if n % i == 0
|
if n % i == 0
|
||||||
out = out + 2
|
out = out + 2
|
||||||
@@ -10,12 +10,12 @@ func num_divisors[n: I64] : I64
|
|||||||
out = out - 1
|
out = out - 1
|
||||||
return out
|
return out
|
||||||
|
|
||||||
func main[] : I64
|
func main[] : i64
|
||||||
let n: I64 = 0
|
let n = 0
|
||||||
let i: I64 = 1
|
let i = 1
|
||||||
while true
|
while true
|
||||||
n = n + i
|
n = n + i
|
||||||
if num_divisors(n) > 500
|
if num_divisors(n) > 500
|
||||||
io.print_i64(n)
|
io.println_i64(n)
|
||||||
break
|
break
|
||||||
i = i + 1
|
i = i + 1
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
// leaks a bit of memory but looks very cool
|
// 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
|
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.from_i64()
|
||||||
|> str.substr(0, 10)
|
|> str.substr(0, 10)
|
||||||
|> io.print()
|
|> io.println()
|
||||||
@@ -1,22 +1,22 @@
|
|||||||
func collatz_num[n: I64] : I64
|
func collatz_num[n: i64] : i64
|
||||||
if n % 2 == 0
|
if n % 2 == 0
|
||||||
return n / 2
|
return n / 2
|
||||||
return n * 3 + 1
|
return n * 3 + 1
|
||||||
|
|
||||||
func collatz_seq[n: I64]: I64
|
func collatz_seq[n: i64]: i64
|
||||||
let i: I64 = 1
|
let i = 1
|
||||||
while n != 1
|
while n != 1
|
||||||
n = collatz_num(n)
|
n = collatz_num(n)
|
||||||
i = i + 1
|
i = i + 1
|
||||||
return i
|
return i
|
||||||
|
|
||||||
func main[] : I64
|
func main[] : i64
|
||||||
let max: I64 = 0
|
let max = 0
|
||||||
let max_index: I64 = 0
|
let max_index = 0
|
||||||
|
|
||||||
for i in 1..1000000
|
for i in 1..1000000
|
||||||
let seq: I64 = collatz_seq(i)
|
let seq: i64 = collatz_seq(i)
|
||||||
if seq > max
|
if seq > max
|
||||||
max = seq
|
max = seq
|
||||||
max_index = i
|
max_index = i
|
||||||
io.print_i64(max_index)
|
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,15 +1,15 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
let arr: Array = []
|
let arr: array = []
|
||||||
for i in 0..10
|
for i in 0..10
|
||||||
array.push(arr, math.abs(math.urandom() % 1000))
|
array.push(arr, math.abs(os.urandom_i64()) % 1000)
|
||||||
|
|
||||||
for i in 0..array.size(arr)
|
for i in 0..array.size(arr)
|
||||||
io.print_i64(arr[i])
|
io.println_i64(array.nth(arr, i))
|
||||||
io.print("------------")
|
io.println("------------")
|
||||||
|
|
||||||
alg.quicksort(arr)
|
alg.quicksort(arr)
|
||||||
|
|
||||||
for i in 0..array.size(arr)
|
for i in 0..array.size(arr)
|
||||||
io.print_i64(arr[i])
|
io.println_i64(array.nth(arr, i))
|
||||||
|
|
||||||
array.free(arr)
|
array.free(arr)
|
||||||
@@ -1,42 +1,34 @@
|
|||||||
// musl doesnt like dlopen, needs to be compiled with -m
|
// needs to be compiled with -m -C="/usr/local/lib/libraylib.a -lm"
|
||||||
|
extern InitWindow
|
||||||
|
extern SetTargetFPS
|
||||||
|
extern WindowShouldClose
|
||||||
|
extern BeginDrawing
|
||||||
|
extern EndDrawing
|
||||||
|
extern ClearBackground
|
||||||
|
extern CloseWindow
|
||||||
|
extern DrawRectangle
|
||||||
|
extern IsKeyDown
|
||||||
|
|
||||||
func main[] : I64
|
func main[] : i64
|
||||||
let rl: Ptr = c.dlopen("libraylib.so", 2)
|
let x = 200
|
||||||
|
let y = 200
|
||||||
|
|
||||||
let rl.InitWindow: Ptr = c.dlsym(rl, "InitWindow")
|
InitWindow(800, 600, "Hello, World!")
|
||||||
let rl.SetTargetFPS: Ptr = c.dlsym(rl, "SetTargetFPS")
|
SetTargetFPS(60)
|
||||||
let rl.WindowShouldClose: Ptr = c.dlsym(rl, "WindowShouldClose")
|
|
||||||
let rl.BeginDrawing: Ptr = c.dlsym(rl, "BeginDrawing")
|
|
||||||
let rl.EndDrawing: Ptr = c.dlsym(rl, "EndDrawing")
|
|
||||||
let rl.ClearBackground: Ptr = c.dlsym(rl, "ClearBackground")
|
|
||||||
let rl.CloseWindow: Ptr = c.dlsym(rl, "CloseWindow")
|
|
||||||
let rl.DrawRectangle: Ptr = c.dlsym(rl, "DrawRectangle")
|
|
||||||
let rl.IsKeyDown: Ptr = c.dlsym(rl, "IsKeyDown")
|
|
||||||
|
|
||||||
let rl.KEY_W: I64 = 87
|
while !WindowShouldClose()
|
||||||
let rl.KEY_S: I64 = 83
|
if IsKeyDown(87) & 255 // W
|
||||||
let rl.KEY_A: I64 = 65
|
|
||||||
let rl.KEY_D: I64 = 68
|
|
||||||
|
|
||||||
let x: I64 = 200
|
|
||||||
let y: I64 = 200
|
|
||||||
|
|
||||||
rl.InitWindow(800, 600, "Hello, World!")
|
|
||||||
rl.SetTargetFPS(60)
|
|
||||||
|
|
||||||
while !rl.WindowShouldClose()
|
|
||||||
if rl.IsKeyDown(rl.KEY_W) & 255
|
|
||||||
y = y - 10
|
y = y - 10
|
||||||
if rl.IsKeyDown(rl.KEY_S) & 255
|
if IsKeyDown(83) & 255 // S
|
||||||
y = y + 10
|
y = y + 10
|
||||||
if rl.IsKeyDown(rl.KEY_A) & 255
|
if IsKeyDown(65) & 255 // A
|
||||||
x = x - 10
|
x = x - 10
|
||||||
if rl.IsKeyDown(rl.KEY_D) & 255
|
if IsKeyDown(68) & 255 // D
|
||||||
x = x + 10
|
x = x + 10
|
||||||
|
|
||||||
rl.BeginDrawing()
|
BeginDrawing()
|
||||||
rl.ClearBackground(0xffffffff)
|
ClearBackground(0xffffffff)
|
||||||
rl.DrawRectangle(x, y, 100, 100, 0xff0000ff)
|
DrawRectangle(x, y, 100, 100, 0xff0000ff)
|
||||||
rl.EndDrawing()
|
EndDrawing()
|
||||||
|
|
||||||
rl.CloseWindow()
|
CloseWindow()
|
||||||
@@ -1,32 +1,32 @@
|
|||||||
func rule110_step[state: Array] : Array
|
func rule110_step[state: array] : array
|
||||||
let new_state: Array = []
|
let new_state: array = []
|
||||||
let state_len: I64 = array.size(state)
|
let state_len: i64 = array.size(state)
|
||||||
|
|
||||||
for i in 0..state_len
|
for i in 0..state_len
|
||||||
let left: Bool = false
|
let left: bool = false
|
||||||
if i - 1 >= 0
|
if i - 1 >= 0
|
||||||
left = state[i-1]
|
left = array.nth(state, i - 1)
|
||||||
let center: Bool = state[i]
|
let center: bool = array.nth(state, i)
|
||||||
let right: Bool = false
|
let right: bool = false
|
||||||
if i + 1 < state_len
|
if i + 1 < state_len
|
||||||
right = state[i+1]
|
right = array.nth(state, i + 1)
|
||||||
|
|
||||||
array.push(new_state, !((!left & !center & !right) | (left & !center & !right) | (left & center & right)))
|
array.push(new_state, !((!left && !center && !right) || (left && !center && !right) || (left && center && right)))
|
||||||
|
|
||||||
return new_state
|
return new_state
|
||||||
|
|
||||||
func print_state[state: Array]: Void
|
func print_state[state: array]: void
|
||||||
for i in 0..array.size(state)
|
for i in 0..array.size(state)
|
||||||
if state[i]
|
if array.nth(state, i)
|
||||||
c.putchar('#')
|
io.print_char('#')
|
||||||
else
|
else
|
||||||
c.putchar(' ')
|
io.print_char(' ')
|
||||||
io.print("")
|
io.println("")
|
||||||
|
|
||||||
func main[] : I64
|
func main[] : i64
|
||||||
let SIZE: I64 = 60
|
let SIZE = 60
|
||||||
|
|
||||||
let state: Array = []
|
let state: array = []
|
||||||
for i in 0..SIZE
|
for i in 0..SIZE
|
||||||
array.push(state, false)
|
array.push(state, false)
|
||||||
array.push(state, true)
|
array.push(state, true)
|
||||||
|
|||||||
71
examples/sqlite_todo.zr
Normal file
71
examples/sqlite_todo.zr
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
// needs to be compiled with -m -C="-lsqlite3"
|
||||||
|
extern sqlite3_open
|
||||||
|
extern sqlite3_exec
|
||||||
|
extern sqlite3_prepare_v2
|
||||||
|
extern sqlite3_bind_int
|
||||||
|
extern sqlite3_bind_text
|
||||||
|
extern sqlite3_step
|
||||||
|
extern sqlite3_errmsg
|
||||||
|
extern sqlite3_column_int
|
||||||
|
extern sqlite3_column_text
|
||||||
|
extern sqlite3_finalize
|
||||||
|
|
||||||
|
func main[] : i64
|
||||||
|
let rc = 0
|
||||||
|
let db = 0
|
||||||
|
let stmt = 0
|
||||||
|
|
||||||
|
rc = sqlite3_open("todo.db", ^db)
|
||||||
|
if rc
|
||||||
|
dbg.panic("failed to open db")
|
||||||
|
|
||||||
|
rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS todo(id INTEGER PRIMARY KEY AUTOINCREMENT, task TEXT NOT NULL);", 0, 0, 0)
|
||||||
|
if rc
|
||||||
|
dbg.panic(sqlite3_errmsg(db))
|
||||||
|
|
||||||
|
while true
|
||||||
|
io.println("1. List tasks")
|
||||||
|
io.println("2. Add task")
|
||||||
|
io.println("3. Delete task")
|
||||||
|
io.println("0. Quit")
|
||||||
|
io.print("\n> ")
|
||||||
|
|
||||||
|
let choice: i64 = io.read_line() |> str.parse_i64()
|
||||||
|
|
||||||
|
if choice == 0
|
||||||
|
break
|
||||||
|
else if choice == 1
|
||||||
|
io.println("============")
|
||||||
|
sqlite3_prepare_v2(db, "SELECT * FROM todo", -1, ^stmt, 0)
|
||||||
|
|
||||||
|
while sqlite3_step(stmt) == 100
|
||||||
|
let id: i64 = sqlite3_column_int(stmt, 0)
|
||||||
|
let task: str = sqlite3_column_text(stmt, 1)
|
||||||
|
|
||||||
|
io.print_i64(id)
|
||||||
|
io.print(" - ")
|
||||||
|
io.println(task)
|
||||||
|
|
||||||
|
io.println("============")
|
||||||
|
else if choice == 2
|
||||||
|
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_bind_text(stmt, 1, task, -1, 0)
|
||||||
|
if sqlite3_step(stmt) != 101
|
||||||
|
dbg.panic(sqlite3_errmsg(db))
|
||||||
|
|
||||||
|
io.println("\nTask added\n")
|
||||||
|
else if choice == 3
|
||||||
|
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_bind_int(stmt, 1, id, -1, 0)
|
||||||
|
if sqlite3_step(stmt) != 101
|
||||||
|
dbg.panic(sqlite3_errmsg(db))
|
||||||
|
|
||||||
|
io.println("\nTask deleted\n")
|
||||||
|
|
||||||
|
sqlite3_finalize(stmt)
|
||||||
@@ -1,12 +1,13 @@
|
|||||||
func main[] : I64
|
func main[] : i64
|
||||||
let s: I64 = net.listen(8000)
|
let host: i64 = net.pack_addr(127, 0, 0, 1)
|
||||||
|
let s: i64 = net.listen(host, 8000)
|
||||||
|
|
||||||
let resp: String = mem.alloc(60000)
|
let resp: str = mem.alloc(60000)
|
||||||
while true
|
while true
|
||||||
let conn: I64 = c.accept(s, 0, 0)
|
let conn: i64 = net.accept(s)
|
||||||
if conn < 0
|
if conn < 0
|
||||||
continue
|
continue
|
||||||
|
|
||||||
let n: I64 = c.read(conn, resp, 60000)
|
let n: i64 = net.read(conn, resp, 60000)
|
||||||
c.send(conn, resp, n, 0)
|
net.send(conn, resp, n)
|
||||||
c.close(conn)
|
net.close(conn)
|
||||||
36
examples/x11.zr
Normal file
36
examples/x11.zr
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
// needs to be compiled with -m -C="-lX11"
|
||||||
|
extern XOpenDisplay
|
||||||
|
extern XDefaultRootWindow
|
||||||
|
extern XCreateSimpleWindow
|
||||||
|
extern XMapWindow
|
||||||
|
extern XSelectInput
|
||||||
|
extern XNextEvent
|
||||||
|
extern XBlackPixel
|
||||||
|
extern XWhitePixel
|
||||||
|
extern XSetForeground
|
||||||
|
extern XCreateGC
|
||||||
|
extern XDefaultScreen
|
||||||
|
extern XDrawString
|
||||||
|
|
||||||
|
func main[] : i64
|
||||||
|
|
||||||
|
let dpy: ptr = XOpenDisplay(0)
|
||||||
|
let screen: ptr = XDefaultScreen(dpy)
|
||||||
|
|
||||||
|
let white: ptr = XWhitePixel(dpy, screen)
|
||||||
|
let black: ptr = XBlackPixel(dpy, screen)
|
||||||
|
|
||||||
|
let win: ptr = XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy), 0, 0, 200, 100, 0, black, white)
|
||||||
|
|
||||||
|
XSelectInput(dpy, win, 1 << 15)
|
||||||
|
XMapWindow(dpy, win)
|
||||||
|
|
||||||
|
let gc: ptr = XCreateGC(dpy, win, 0, 0)
|
||||||
|
XSetForeground(dpy, gc, black)
|
||||||
|
|
||||||
|
let ev: ptr = mem.alloc(256)
|
||||||
|
|
||||||
|
while true
|
||||||
|
XNextEvent(dpy, ev)
|
||||||
|
if ev[0] == 12
|
||||||
|
XDrawString(dpy, win, gc, 20, 50, "Hello, World!", 13)
|
||||||
@@ -6,13 +6,15 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub struct Analyzer {
|
pub struct Analyzer {
|
||||||
pub functions: HashMap<String, usize>,
|
pub functions: HashMap<String, i32>,
|
||||||
|
pub constants: HashMap<String, u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Analyzer {
|
impl Analyzer {
|
||||||
pub fn new() -> Analyzer {
|
pub fn new() -> Analyzer {
|
||||||
Analyzer {
|
Analyzer {
|
||||||
functions: HashMap::new(),
|
functions: HashMap::new(),
|
||||||
|
constants: HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,12 +24,14 @@ impl Analyzer {
|
|||||||
params,
|
params,
|
||||||
return_type: _,
|
return_type: _,
|
||||||
body: _,
|
body: _,
|
||||||
|
exported: _,
|
||||||
} = stmt
|
} = stmt
|
||||||
{
|
{
|
||||||
if self.functions.contains_key(&name.lexeme) {
|
if self.functions.contains_key(&name.lexeme) {
|
||||||
return error!(name.loc, format!("tried to redefine '{}'", name.lexeme));
|
return error!(name.loc, format!("tried to redefine '{}'", name.lexeme));
|
||||||
}
|
}
|
||||||
self.functions.insert(name.lexeme.clone(), params.len());
|
self.functions
|
||||||
|
.insert(name.lexeme.clone(), params.len() as i32);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -42,6 +46,18 @@ impl Analyzer {
|
|||||||
} => {
|
} => {
|
||||||
self.analyze_expr(initializer)?;
|
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) => {
|
Stmt::Block(statements) => {
|
||||||
for stmt in statements {
|
for stmt in statements {
|
||||||
self.analyze_stmt(stmt)?;
|
self.analyze_stmt(stmt)?;
|
||||||
@@ -65,9 +81,10 @@ impl Analyzer {
|
|||||||
params: _,
|
params: _,
|
||||||
return_type,
|
return_type,
|
||||||
body,
|
body,
|
||||||
|
exported: _,
|
||||||
} => {
|
} => {
|
||||||
if name.lexeme == "main" && return_type.lexeme != "I64" {
|
if name.lexeme == "main" && return_type.lexeme != "i64" {
|
||||||
return error!(&name.loc, "main must return I64");
|
return error!(&name.loc, "main must return i64");
|
||||||
}
|
}
|
||||||
|
|
||||||
self.analyze_stmt(body)?;
|
self.analyze_stmt(body)?;
|
||||||
@@ -87,6 +104,12 @@ impl Analyzer {
|
|||||||
}
|
}
|
||||||
Stmt::Break => {}
|
Stmt::Break => {}
|
||||||
Stmt::Continue => {}
|
Stmt::Continue => {}
|
||||||
|
Stmt::Extern(name) => {
|
||||||
|
if self.functions.contains_key(&name.lexeme) {
|
||||||
|
return error!(name.loc, format!("tried to redefine '{}'", name.lexeme));
|
||||||
|
}
|
||||||
|
self.functions.insert(name.lexeme.clone(), -1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -97,6 +120,10 @@ impl Analyzer {
|
|||||||
self.analyze_expr(left)?;
|
self.analyze_expr(left)?;
|
||||||
self.analyze_expr(right)?;
|
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::Grouping(expr) => self.analyze_expr(expr)?,
|
||||||
Expr::Literal(_) => {}
|
Expr::Literal(_) => {}
|
||||||
Expr::Unary { op: _, right } => {
|
Expr::Unary { op: _, right } => {
|
||||||
@@ -111,20 +138,31 @@ impl Analyzer {
|
|||||||
paren,
|
paren,
|
||||||
args,
|
args,
|
||||||
} => {
|
} => {
|
||||||
let callee = match callee.as_ref() {
|
if let Expr::Variable(callee_name) = *callee.clone() {
|
||||||
Expr::Variable(name) => name.lexeme.clone(),
|
if callee_name.lexeme.starts_with("_builtin_")
|
||||||
_ => return error!(&paren.loc, "tried to call a non-constant expression"),
|
|| self.functions.contains_key(&callee_name.lexeme)
|
||||||
};
|
{
|
||||||
|
// its a function (defined/builtin/extern)
|
||||||
if let Some(arity) = self.functions.get(&callee) {
|
if let Some(arity) = self.functions.get(&callee_name.lexeme) {
|
||||||
if *arity != args.len() {
|
if *arity >= 0 && *arity != args.len() as i32 {
|
||||||
return error!(
|
return error!(
|
||||||
&paren.loc,
|
&paren.loc,
|
||||||
format!("expected {} arguments, got {}", arity, args.len())
|
format!("expected {} arguments, got {}", arity, args.len())
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
} else if !callee_name.lexeme.starts_with("_builtin_") {
|
||||||
|
return error!(
|
||||||
|
&paren.loc,
|
||||||
|
format!("undefined function: {}", callee_name.lexeme)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// its a variable containing function address
|
||||||
|
self.analyze_expr(callee)?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO: cant error here since we dont analyze externs/builtins
|
// its an expression that evalutes to function address
|
||||||
|
self.analyze_expr(callee)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
for arg in args {
|
for arg in args {
|
||||||
@@ -140,6 +178,9 @@ impl Analyzer {
|
|||||||
self.analyze_expr(expr)?;
|
self.analyze_expr(expr)?;
|
||||||
self.analyze_expr(index)?;
|
self.analyze_expr(index)?;
|
||||||
}
|
}
|
||||||
|
Expr::AddrOf { op: _, expr } => {
|
||||||
|
self.analyze_expr(expr)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
use std::{collections::HashMap, fmt::Write};
|
use std::{collections::HashMap, fmt::Write};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
analyzer::Analyzer,
|
||||||
parser::{Expr, Stmt},
|
parser::{Expr, Stmt},
|
||||||
tokenizer::{TokenType, ZernError, error},
|
tokenizer::{TokenType, ZernError, error},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Var {
|
pub struct Var {
|
||||||
// pub var_type: String,
|
|
||||||
pub stack_offset: usize,
|
pub stack_offset: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -15,6 +15,7 @@ pub struct Env {
|
|||||||
next_offset: usize,
|
next_offset: usize,
|
||||||
loop_begin_label: String,
|
loop_begin_label: String,
|
||||||
loop_end_label: String,
|
loop_end_label: String,
|
||||||
|
loop_continue_label: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Env {
|
impl Env {
|
||||||
@@ -24,6 +25,7 @@ impl Env {
|
|||||||
next_offset: 8,
|
next_offset: 8,
|
||||||
loop_begin_label: String::new(),
|
loop_begin_label: String::new(),
|
||||||
loop_end_label: String::new(),
|
loop_end_label: String::new(),
|
||||||
|
loop_continue_label: String::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,20 +67,22 @@ macro_rules! emit {
|
|||||||
|
|
||||||
static REGISTERS: [&str; 6] = ["rdi", "rsi", "rdx", "rcx", "r8", "r9"];
|
static REGISTERS: [&str; 6] = ["rdi", "rsi", "rdx", "rcx", "r8", "r9"];
|
||||||
|
|
||||||
pub struct CodegenX86_64 {
|
pub struct CodegenX86_64<'a> {
|
||||||
output: String,
|
output: String,
|
||||||
data_section: String,
|
data_section: String,
|
||||||
label_counter: usize,
|
label_counter: usize,
|
||||||
data_counter: usize,
|
data_counter: usize,
|
||||||
|
pub analyzer: &'a mut Analyzer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodegenX86_64 {
|
impl<'a> CodegenX86_64<'a> {
|
||||||
pub fn new() -> CodegenX86_64 {
|
pub fn new(analyzer: &'a mut Analyzer) -> CodegenX86_64<'a> {
|
||||||
CodegenX86_64 {
|
CodegenX86_64 {
|
||||||
output: String::new(),
|
output: String::new(),
|
||||||
data_section: String::new(),
|
data_section: String::new(),
|
||||||
label_counter: 0,
|
label_counter: 0,
|
||||||
data_counter: 1,
|
data_counter: 1,
|
||||||
|
analyzer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,11 +92,7 @@ impl CodegenX86_64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_output(&self) -> String {
|
pub fn get_output(&self) -> String {
|
||||||
format!(
|
format!("section .data\n{}{}", self.data_section, self.output)
|
||||||
"section .data
|
|
||||||
{}{}",
|
|
||||||
self.data_section, self.output
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn emit_prologue(&mut self) -> Result<(), ZernError> {
|
pub fn emit_prologue(&mut self) -> Result<(), ZernError> {
|
||||||
@@ -101,26 +101,6 @@ impl CodegenX86_64 {
|
|||||||
"section .note.GNU-stack
|
"section .note.GNU-stack
|
||||||
db 0
|
db 0
|
||||||
|
|
||||||
section .text
|
|
||||||
"
|
|
||||||
);
|
|
||||||
|
|
||||||
// take that rustfmt
|
|
||||||
for name in "malloc,memset,realloc,free,puts,putchar,printf,snprintf,strtol,strlen,strcmp,strcat,strcpy,strncpy,fopen,fseek,ftell,fread,fwrite,fclose,rewind,system,opendir,readdir,closedir,exit,gettimeofday,connect,socket,send,write,read,close,bind,listen,accept,getchar,gethostbyname,dlopen,dlsym".split(",")
|
|
||||||
{
|
|
||||||
emit!(&mut self.output, "extern {}", name);
|
|
||||||
emit!(&mut self.output, "c.{} equ {}", name, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
emit!(
|
|
||||||
&mut self.output,
|
|
||||||
"
|
|
||||||
section .text._builtin_read8
|
|
||||||
_builtin_read8:
|
|
||||||
xor rax, rax
|
|
||||||
mov al, byte [rdi]
|
|
||||||
ret
|
|
||||||
|
|
||||||
section .text._builtin_read64
|
section .text._builtin_read64
|
||||||
_builtin_read64:
|
_builtin_read64:
|
||||||
mov rax, qword [rdi]
|
mov rax, qword [rdi]
|
||||||
@@ -135,12 +115,30 @@ section .text._builtin_set64
|
|||||||
_builtin_set64:
|
_builtin_set64:
|
||||||
mov [rdi], rsi
|
mov [rdi], rsi
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
section .text._builtin_syscall
|
||||||
|
_builtin_syscall:
|
||||||
|
mov rax, rdi
|
||||||
|
mov rdi, rsi
|
||||||
|
mov rsi, rdx
|
||||||
|
mov rdx, rcx
|
||||||
|
mov r10, r8
|
||||||
|
mov r8, r9
|
||||||
|
mov r9, [rsp+8]
|
||||||
|
syscall
|
||||||
|
ret
|
||||||
|
|
||||||
|
section .text._builtin_environ
|
||||||
|
_builtin_environ:
|
||||||
|
extern environ
|
||||||
|
mov rax, [rel environ]
|
||||||
|
ret
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
Ok(())
|
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 {
|
match stmt {
|
||||||
Stmt::Expression(expr) => self.compile_expr(env, expr)?,
|
Stmt::Expression(expr) => self.compile_expr(env, expr)?,
|
||||||
Stmt::Let {
|
Stmt::Let {
|
||||||
@@ -156,10 +154,27 @@ _builtin_set64:
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)?;
|
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);
|
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
||||||
}
|
}
|
||||||
|
Stmt::Const { name: _, value: _ } => {
|
||||||
|
// handled in the analyzer
|
||||||
|
}
|
||||||
Stmt::Block(statements) => {
|
Stmt::Block(statements) => {
|
||||||
env.push_scope();
|
env.push_scope();
|
||||||
for stmt in statements {
|
for stmt in statements {
|
||||||
@@ -178,36 +193,40 @@ _builtin_set64:
|
|||||||
self.compile_expr(env, condition)?;
|
self.compile_expr(env, condition)?;
|
||||||
emit!(&mut self.output, " test rax, rax");
|
emit!(&mut self.output, " test rax, rax");
|
||||||
emit!(&mut self.output, " je {}", else_label);
|
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, " jmp {}", end_label);
|
||||||
emit!(&mut self.output, "{}:", else_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);
|
emit!(&mut self.output, "{}:", end_label);
|
||||||
}
|
}
|
||||||
Stmt::While { condition, body } => {
|
Stmt::While { condition, body } => {
|
||||||
let old_loop_begin_label = env.loop_begin_label.clone();
|
let old_loop_begin_label = env.loop_begin_label.clone();
|
||||||
let old_loop_end_label = env.loop_end_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_begin_label = self.label();
|
||||||
env.loop_end_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);
|
emit!(&mut self.output, "{}:", env.loop_begin_label);
|
||||||
self.compile_expr(env, condition)?;
|
self.compile_expr(env, condition)?;
|
||||||
emit!(&mut self.output, " test rax, rax");
|
emit!(&mut self.output, " test rax, rax");
|
||||||
emit!(&mut self.output, " je {}", env.loop_end_label);
|
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, " jmp {}", env.loop_begin_label);
|
||||||
emit!(&mut self.output, "{}:", env.loop_end_label);
|
emit!(&mut self.output, "{}:", env.loop_end_label);
|
||||||
|
|
||||||
env.loop_begin_label = old_loop_begin_label;
|
env.loop_begin_label = old_loop_begin_label;
|
||||||
env.loop_end_label = old_loop_end_label;
|
env.loop_end_label = old_loop_end_label;
|
||||||
|
env.loop_continue_label = old_loop_continue_label;
|
||||||
}
|
}
|
||||||
Stmt::Function {
|
Stmt::Function {
|
||||||
name,
|
name,
|
||||||
params,
|
params,
|
||||||
return_type: _,
|
return_type: _,
|
||||||
body,
|
body,
|
||||||
|
exported,
|
||||||
} => {
|
} => {
|
||||||
if name.lexeme == "main" {
|
if *exported || name.lexeme == "main" {
|
||||||
emit!(&mut self.output, "global {}", name.lexeme);
|
emit!(&mut self.output, "global {}", name.lexeme);
|
||||||
}
|
}
|
||||||
emit!(&mut self.output, "section .text.{}", name.lexeme);
|
emit!(&mut self.output, "section .text.{}", name.lexeme);
|
||||||
@@ -219,20 +238,29 @@ _builtin_set64:
|
|||||||
for (i, param) in params.iter().enumerate() {
|
for (i, param) in params.iter().enumerate() {
|
||||||
let offset = env
|
let offset = env
|
||||||
.define_var(param.var_name.lexeme.clone(), param.var_type.lexeme.clone());
|
.define_var(param.var_name.lexeme.clone(), param.var_type.lexeme.clone());
|
||||||
let reg = match REGISTERS.get(i) {
|
if let Some(reg) = REGISTERS.get(i) {
|
||||||
Some(x) => x,
|
emit!(&mut self.output, " mov QWORD [rbp-{}], {}", offset, reg);
|
||||||
None => return error!(&name.loc, "only up to 6 params allowed"),
|
} else {
|
||||||
};
|
let stack_offset = 16 + 8 * (i - REGISTERS.len());
|
||||||
emit!(&mut self.output, " mov QWORD [rbp-{}], {}", offset, reg);
|
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 returning null
|
// fallback to null
|
||||||
emit!(&mut self.output, " mov rax, 0");
|
// very hacky but works
|
||||||
emit!(&mut self.output, " mov rsp, rbp");
|
if !self.output.trim_end().ends_with(" ret") {
|
||||||
emit!(&mut self.output, " pop rbp");
|
emit!(&mut self.output, " mov rax, 0");
|
||||||
emit!(&mut self.output, " ret");
|
emit!(&mut self.output, " mov rsp, rbp");
|
||||||
|
emit!(&mut self.output, " pop rbp");
|
||||||
|
emit!(&mut self.output, " ret");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Stmt::Return(expr) => {
|
Stmt::Return(expr) => {
|
||||||
self.compile_expr(env, expr)?;
|
self.compile_expr(env, expr)?;
|
||||||
@@ -248,11 +276,13 @@ _builtin_set64:
|
|||||||
} => {
|
} => {
|
||||||
let old_loop_begin_label = env.loop_begin_label.clone();
|
let old_loop_begin_label = env.loop_begin_label.clone();
|
||||||
let old_loop_end_label = env.loop_end_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_begin_label = self.label();
|
||||||
env.loop_end_label = self.label();
|
env.loop_end_label = self.label();
|
||||||
|
env.loop_continue_label = self.label();
|
||||||
|
|
||||||
env.push_scope();
|
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)?;
|
self.compile_expr(env, start)?;
|
||||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
||||||
@@ -263,7 +293,8 @@ _builtin_set64:
|
|||||||
emit!(&mut self.output, " pop rcx");
|
emit!(&mut self.output, " pop rcx");
|
||||||
emit!(&mut self.output, " cmp rcx, rax");
|
emit!(&mut self.output, " cmp rcx, rax");
|
||||||
emit!(&mut self.output, " jge {}", env.loop_end_label);
|
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, " mov rax, QWORD [rbp-{}]", offset);
|
||||||
emit!(&mut self.output, " add rax, 1");
|
emit!(&mut self.output, " add rax, 1");
|
||||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
||||||
@@ -273,24 +304,27 @@ _builtin_set64:
|
|||||||
|
|
||||||
env.loop_begin_label = old_loop_begin_label;
|
env.loop_begin_label = old_loop_begin_label;
|
||||||
env.loop_end_label = old_loop_end_label;
|
env.loop_end_label = old_loop_end_label;
|
||||||
|
env.loop_continue_label = old_loop_continue_label;
|
||||||
}
|
}
|
||||||
Stmt::Break => {
|
Stmt::Break => {
|
||||||
emit!(&mut self.output, " jmp {}", env.loop_end_label);
|
emit!(&mut self.output, " jmp {}", env.loop_end_label);
|
||||||
}
|
}
|
||||||
Stmt::Continue => {
|
Stmt::Continue => {
|
||||||
// TODO: skips incrementing when used in a for loop
|
emit!(&mut self.output, " jmp {}", env.loop_continue_label);
|
||||||
emit!(&mut self.output, " jmp {}", env.loop_begin_label);
|
}
|
||||||
|
Stmt::Extern(name) => {
|
||||||
|
emit!(&mut self.output, "extern {}", name.lexeme);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
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 {
|
match expr {
|
||||||
Expr::Binary { left, op, right } => {
|
Expr::Binary { left, op, right } => {
|
||||||
self.compile_expr(env, *left)?;
|
self.compile_expr(env, left)?;
|
||||||
emit!(&mut self.output, " push rax");
|
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, " mov rbx, rax");
|
||||||
emit!(&mut self.output, " pop rax");
|
emit!(&mut self.output, " pop rax");
|
||||||
|
|
||||||
@@ -363,7 +397,26 @@ _builtin_set64:
|
|||||||
_ => unreachable!(),
|
_ => 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 {
|
Expr::Literal(token) => match token.token_type {
|
||||||
TokenType::Number => {
|
TokenType::Number => {
|
||||||
emit!(&mut self.output, " mov rax, {}", token.lexeme);
|
emit!(&mut self.output, " mov rax, {}", token.lexeme);
|
||||||
@@ -408,7 +461,7 @@ _builtin_set64:
|
|||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
},
|
},
|
||||||
Expr::Unary { op, right } => {
|
Expr::Unary { op, right } => {
|
||||||
self.compile_expr(env, *right)?;
|
self.compile_expr(env, right)?;
|
||||||
match op.token_type {
|
match op.token_type {
|
||||||
TokenType::Minus => {
|
TokenType::Minus => {
|
||||||
emit!(&mut self.output, " neg rax");
|
emit!(&mut self.output, " neg rax");
|
||||||
@@ -422,21 +475,32 @@ _builtin_set64:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expr::Variable(name) => {
|
Expr::Variable(name) => {
|
||||||
// TODO: move to analyzer
|
if self.analyzer.constants.contains_key(&name.lexeme) {
|
||||||
let var = match env.get_var(&name.lexeme) {
|
emit!(
|
||||||
Some(x) => x,
|
&mut self.output,
|
||||||
None => {
|
" mov rax, {}",
|
||||||
return error!(name.loc, format!("undefined variable: {}", &name.lexeme));
|
self.analyzer.constants[&name.lexeme]
|
||||||
}
|
);
|
||||||
};
|
} else {
|
||||||
emit!(
|
// TODO: move to analyzer
|
||||||
&mut self.output,
|
let var = match env.get_var(&name.lexeme) {
|
||||||
" mov rax, QWORD [rbp-{}]",
|
Some(x) => x,
|
||||||
var.stack_offset,
|
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 } => {
|
Expr::Assign { name, value } => {
|
||||||
self.compile_expr(env, *value)?;
|
self.compile_expr(env, value)?;
|
||||||
|
|
||||||
// TODO: move to analyzer
|
// TODO: move to analyzer
|
||||||
let var = match env.get_var(&name.lexeme) {
|
let var = match env.get_var(&name.lexeme) {
|
||||||
@@ -453,40 +517,64 @@ _builtin_set64:
|
|||||||
}
|
}
|
||||||
Expr::Call {
|
Expr::Call {
|
||||||
callee,
|
callee,
|
||||||
paren,
|
paren: _,
|
||||||
args,
|
args,
|
||||||
} => {
|
} => {
|
||||||
let callee = match *callee {
|
for arg in args {
|
||||||
Expr::Variable(name) => name.lexeme,
|
self.compile_expr(env, arg)?;
|
||||||
_ => return error!(&paren.loc, "tried to call a non-constant expression"),
|
|
||||||
};
|
|
||||||
|
|
||||||
for arg in &args {
|
|
||||||
self.compile_expr(env, arg.clone())?;
|
|
||||||
emit!(&mut self.output, " push rax");
|
emit!(&mut self.output, " push rax");
|
||||||
}
|
}
|
||||||
|
|
||||||
for i in (0..args.len()).rev() {
|
let arg_count = args.len();
|
||||||
let reg = match REGISTERS.get(i) {
|
if arg_count <= 6 {
|
||||||
Some(x) => x,
|
for i in (0..arg_count).rev() {
|
||||||
None => return error!(&paren.loc, "only up to 6 args allowed"),
|
emit!(&mut self.output, " pop {}", REGISTERS[i]);
|
||||||
};
|
}
|
||||||
emit!(&mut self.output, " pop {}", reg);
|
} else {
|
||||||
}
|
for (i, reg) in REGISTERS.iter().enumerate() {
|
||||||
|
let offset = 8 * (arg_count - 1 - i);
|
||||||
match env.get_var(&callee) {
|
|
||||||
Some(var) => {
|
|
||||||
emit!(
|
emit!(
|
||||||
&mut self.output,
|
&mut self.output,
|
||||||
" mov rax, QWORD [rbp-{}]",
|
" mov {}, QWORD [rsp + {}]",
|
||||||
var.stack_offset,
|
reg,
|
||||||
|
offset
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
let num_stack = arg_count - 6;
|
||||||
|
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 + 8 * i
|
||||||
|
);
|
||||||
|
emit!(&mut self.output, " push rax");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Expr::Variable(callee_name) = &**callee {
|
||||||
|
if callee_name.lexeme.starts_with("_builtin_")
|
||||||
|
|| self.analyzer.functions.contains_key(&callee_name.lexeme)
|
||||||
|
{
|
||||||
|
// its a function (defined/builtin/extern)
|
||||||
|
emit!(&mut self.output, " call {}", callee_name.lexeme);
|
||||||
|
} else {
|
||||||
|
// its a variable containing function address
|
||||||
|
self.compile_expr(env, callee)?;
|
||||||
emit!(&mut self.output, " call rax");
|
emit!(&mut self.output, " call rax");
|
||||||
}
|
}
|
||||||
None => {
|
} else {
|
||||||
emit!(&mut self.output, " call {}", callee);
|
// its an expression that evalutes to function address
|
||||||
}
|
self.compile_expr(env, callee)?;
|
||||||
};
|
emit!(&mut self.output, " call rax");
|
||||||
|
}
|
||||||
|
|
||||||
|
if arg_count > 6 {
|
||||||
|
let num_stack = arg_count - 6;
|
||||||
|
emit!(&mut self.output, " add rsp, {}", 8 * num_stack);
|
||||||
|
emit!(&mut self.output, " add rsp, {}", 8 * arg_count);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Expr::ArrayLiteral(exprs) => {
|
Expr::ArrayLiteral(exprs) => {
|
||||||
emit!(&mut self.output, " call array.new");
|
emit!(&mut self.output, " call array.new");
|
||||||
@@ -502,13 +590,38 @@ _builtin_set64:
|
|||||||
emit!(&mut self.output, " pop rax");
|
emit!(&mut self.output, " pop rax");
|
||||||
}
|
}
|
||||||
Expr::Index { expr, index } => {
|
Expr::Index { expr, index } => {
|
||||||
self.compile_expr(env, *expr)?;
|
self.compile_expr(env, expr)?;
|
||||||
emit!(&mut self.output, " push rax");
|
emit!(&mut self.output, " push rax");
|
||||||
self.compile_expr(env, *index)?;
|
self.compile_expr(env, index)?;
|
||||||
emit!(&mut self.output, " pop rbx");
|
emit!(&mut self.output, " pop rbx");
|
||||||
emit!(&mut self.output, " mov rbx, [rbx]");
|
emit!(&mut self.output, " add rax, rbx");
|
||||||
emit!(&mut self.output, " mov rax, [rbx + rax*8]");
|
emit!(&mut self.output, " movzx rax, BYTE [rax]");
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
|
} else {
|
||||||
|
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,
|
||||||
|
" lea rax, QWORD [rbp-{}]",
|
||||||
|
var.stack_offset,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return error!(&op.loc, "can only take address of variables and functions");
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
29
src/main.rs
29
src/main.rs
@@ -14,7 +14,6 @@ use tokenizer::ZernError;
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
fn compile_file_to(
|
fn compile_file_to(
|
||||||
analyzer: &mut analyzer::Analyzer,
|
|
||||||
codegen: &mut codegen_x86_64::CodegenX86_64,
|
codegen: &mut codegen_x86_64::CodegenX86_64,
|
||||||
filename: &str,
|
filename: &str,
|
||||||
source: String,
|
source: String,
|
||||||
@@ -26,15 +25,16 @@ fn compile_file_to(
|
|||||||
let statements = parser.parse()?;
|
let statements = parser.parse()?;
|
||||||
|
|
||||||
for stmt in &statements {
|
for stmt in &statements {
|
||||||
analyzer.register_function(stmt)?;
|
codegen.analyzer.register_function(stmt)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
for stmt in &statements {
|
for stmt in &statements {
|
||||||
analyzer.analyze_stmt(stmt)?;
|
codegen.analyzer.analyze_stmt(stmt)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
for stmt in statements {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -62,22 +62,27 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
|
|||||||
let filename = Path::new(&args.path).file_name().unwrap().to_str().unwrap();
|
let filename = Path::new(&args.path).file_name().unwrap().to_str().unwrap();
|
||||||
|
|
||||||
let mut analyzer = analyzer::Analyzer::new();
|
let mut analyzer = analyzer::Analyzer::new();
|
||||||
let mut codegen = codegen_x86_64::CodegenX86_64::new();
|
let mut codegen = codegen_x86_64::CodegenX86_64::new(&mut analyzer);
|
||||||
codegen.emit_prologue()?;
|
codegen.emit_prologue()?;
|
||||||
compile_file_to(
|
compile_file_to(
|
||||||
&mut analyzer,
|
|
||||||
&mut codegen,
|
&mut codegen,
|
||||||
"std.zr",
|
"syscalls.zr",
|
||||||
include_str!("std.zr").into(),
|
include_str!("std/syscalls.zr").into(),
|
||||||
)?;
|
)?;
|
||||||
compile_file_to(&mut analyzer, &mut codegen, filename, source)?;
|
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 {
|
if !args.output_asm {
|
||||||
fs::write(format!("{}.s", args.out), codegen.get_output()).unwrap();
|
fs::write(format!("{}.s", args.out), codegen.get_output()).unwrap();
|
||||||
|
|
||||||
run_command(format!("nasm -f elf64 -o {}.o {}.s", args.out, args.out));
|
run_command(format!("nasm -f elf64 -o {}.o {}.s", args.out, args.out));
|
||||||
|
|
||||||
if args.no_musl {
|
if args.use_glibc {
|
||||||
run_command(format!(
|
run_command(format!(
|
||||||
"gcc -no-pie -o {} {}.o -flto -Wl,--gc-sections {}",
|
"gcc -no-pie -o {} {}.o -flto -Wl,--gc-sections {}",
|
||||||
args.out, args.out, args.cflags
|
args.out, args.out, args.cflags
|
||||||
@@ -118,8 +123,8 @@ struct Args {
|
|||||||
#[arg(short = 'r', help = "Run the compiled executable")]
|
#[arg(short = 'r', help = "Run the compiled executable")]
|
||||||
run_exe: bool,
|
run_exe: bool,
|
||||||
|
|
||||||
#[arg(short = 'm', help = "Don't use musl")]
|
#[arg(short = 'm', help = "Use glibc instead of musl")]
|
||||||
no_musl: bool,
|
use_glibc: bool,
|
||||||
|
|
||||||
#[arg(short = 'C', default_value = "", help = "Extra flags to pass to gcc")]
|
#[arg(short = 'C', default_value = "", help = "Extra flags to pass to gcc")]
|
||||||
cflags: String,
|
cflags: String,
|
||||||
|
|||||||
101
src/parser.rs
101
src/parser.rs
@@ -11,9 +11,13 @@ pub enum Stmt {
|
|||||||
Expression(Expr),
|
Expression(Expr),
|
||||||
Let {
|
Let {
|
||||||
name: Token,
|
name: Token,
|
||||||
var_type: Token,
|
var_type: Option<Token>,
|
||||||
initializer: Expr,
|
initializer: Expr,
|
||||||
},
|
},
|
||||||
|
Const {
|
||||||
|
name: Token,
|
||||||
|
value: Token,
|
||||||
|
},
|
||||||
Block(Vec<Stmt>),
|
Block(Vec<Stmt>),
|
||||||
If {
|
If {
|
||||||
condition: Expr,
|
condition: Expr,
|
||||||
@@ -35,10 +39,12 @@ pub enum Stmt {
|
|||||||
params: Vec<Param>,
|
params: Vec<Param>,
|
||||||
return_type: Token,
|
return_type: Token,
|
||||||
body: Box<Stmt>,
|
body: Box<Stmt>,
|
||||||
|
exported: bool,
|
||||||
},
|
},
|
||||||
Return(Expr),
|
Return(Expr),
|
||||||
Break,
|
Break,
|
||||||
Continue,
|
Continue,
|
||||||
|
Extern(Token),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -48,6 +54,11 @@ pub enum Expr {
|
|||||||
op: Token,
|
op: Token,
|
||||||
right: Box<Expr>,
|
right: Box<Expr>,
|
||||||
},
|
},
|
||||||
|
Logical {
|
||||||
|
left: Box<Expr>,
|
||||||
|
op: Token,
|
||||||
|
right: Box<Expr>,
|
||||||
|
},
|
||||||
Grouping(Box<Expr>),
|
Grouping(Box<Expr>),
|
||||||
Literal(Token),
|
Literal(Token),
|
||||||
Unary {
|
Unary {
|
||||||
@@ -69,10 +80,14 @@ pub enum Expr {
|
|||||||
expr: Box<Expr>,
|
expr: Box<Expr>,
|
||||||
index: Box<Expr>,
|
index: Box<Expr>,
|
||||||
},
|
},
|
||||||
|
AddrOf {
|
||||||
|
op: Token,
|
||||||
|
expr: Box<Expr>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: currently they are all just 8 byte values
|
// TODO: currently they are all just 8 byte values
|
||||||
static TYPES: [&str; 7] = ["Void", "U8", "I64", "String", "Bool", "Ptr", "Array"];
|
static TYPES: [&str; 7] = ["void", "u8", "i64", "str", "bool", "ptr", "array"];
|
||||||
|
|
||||||
pub struct Parser {
|
pub struct Parser {
|
||||||
tokens: Vec<Token>,
|
tokens: Vec<Token>,
|
||||||
@@ -100,7 +115,17 @@ impl Parser {
|
|||||||
fn declaration(&mut self) -> Result<Stmt, ZernError> {
|
fn declaration(&mut self) -> Result<Stmt, ZernError> {
|
||||||
if !self.is_inside_function {
|
if !self.is_inside_function {
|
||||||
if self.match_token(&[TokenType::KeywordFunc]) {
|
if self.match_token(&[TokenType::KeywordFunc]) {
|
||||||
return self.func_declaration();
|
return self.func_declaration(false);
|
||||||
|
}
|
||||||
|
if self.match_token(&[TokenType::KeywordExport]) {
|
||||||
|
self.consume(TokenType::KeywordFunc, "expected 'func' after 'export'")?;
|
||||||
|
return self.func_declaration(true);
|
||||||
|
}
|
||||||
|
if self.match_token(&[TokenType::KeywordExtern]) {
|
||||||
|
return self.extern_declaration();
|
||||||
|
}
|
||||||
|
if self.match_token(&[TokenType::KeywordConst]) {
|
||||||
|
return self.const_declaration();
|
||||||
}
|
}
|
||||||
return error!(
|
return error!(
|
||||||
self.peek().loc,
|
self.peek().loc,
|
||||||
@@ -115,7 +140,7 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn func_declaration(&mut self) -> Result<Stmt, ZernError> {
|
fn func_declaration(&mut self, exported: bool) -> Result<Stmt, ZernError> {
|
||||||
let name = self.consume(TokenType::Identifier, "expected function name")?;
|
let name = self.consume(TokenType::Identifier, "expected function name")?;
|
||||||
self.consume(TokenType::LeftBracket, "expected '[' after function name")?;
|
self.consume(TokenType::LeftBracket, "expected '[' after function name")?;
|
||||||
|
|
||||||
@@ -153,17 +178,22 @@ impl Parser {
|
|||||||
params,
|
params,
|
||||||
return_type,
|
return_type,
|
||||||
body,
|
body,
|
||||||
|
exported,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn let_declaration(&mut self) -> Result<Stmt, ZernError> {
|
fn let_declaration(&mut self) -> Result<Stmt, ZernError> {
|
||||||
let name = self.consume(TokenType::Identifier, "expected variable name")?;
|
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")?;
|
let var_type = if self.match_token(&[TokenType::Colon]) {
|
||||||
if !TYPES.contains(&var_type.lexeme.as_str()) {
|
let token = self.consume(TokenType::Identifier, "expected variable type")?;
|
||||||
return error!(&name.loc, format!("unknown type: {}", var_type.lexeme));
|
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")?;
|
self.consume(TokenType::Equal, "expected '=' after variable type")?;
|
||||||
let initializer = self.expression()?;
|
let initializer = self.expression()?;
|
||||||
@@ -174,6 +204,19 @@ 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")?,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
fn block(&mut self) -> Result<Stmt, ZernError> {
|
fn block(&mut self) -> Result<Stmt, ZernError> {
|
||||||
self.consume(TokenType::Indent, "expected an indent")?;
|
self.consume(TokenType::Indent, "expected an indent")?;
|
||||||
|
|
||||||
@@ -271,7 +314,7 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn pipe(&mut self) -> Result<Expr, ZernError> {
|
fn pipe(&mut self) -> Result<Expr, ZernError> {
|
||||||
let mut expr = self.logical_or()?;
|
let mut expr = self.or_and()?;
|
||||||
|
|
||||||
while self.match_token(&[TokenType::Pipe]) {
|
while self.match_token(&[TokenType::Pipe]) {
|
||||||
let pipe = self.previous().clone();
|
let pipe = self.previous().clone();
|
||||||
@@ -300,29 +343,13 @@ impl Parser {
|
|||||||
Ok(expr)
|
Ok(expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn logical_or(&mut self) -> Result<Expr, ZernError> {
|
fn or_and(&mut self) -> Result<Expr, ZernError> {
|
||||||
let mut expr = self.logical_and()?;
|
|
||||||
|
|
||||||
while self.match_token(&[TokenType::BitOr]) {
|
|
||||||
let op = self.previous().clone();
|
|
||||||
let right = self.logical_and()?;
|
|
||||||
expr = Expr::Binary {
|
|
||||||
left: Box::new(expr),
|
|
||||||
op,
|
|
||||||
right: Box::new(right),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(expr)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn logical_and(&mut self) -> Result<Expr, ZernError> {
|
|
||||||
let mut expr = self.equality()?;
|
let mut expr = self.equality()?;
|
||||||
|
|
||||||
while self.match_token(&[TokenType::BitAnd]) {
|
while self.match_token(&[TokenType::LogicalOr, TokenType::LogicalAnd]) {
|
||||||
let op = self.previous().clone();
|
let op = self.previous().clone();
|
||||||
let right = self.equality()?;
|
let right = self.equality()?;
|
||||||
expr = Expr::Binary {
|
expr = Expr::Logical {
|
||||||
left: Box::new(expr),
|
left: Box::new(expr),
|
||||||
op,
|
op,
|
||||||
right: Box::new(right),
|
right: Box::new(right),
|
||||||
@@ -372,7 +399,13 @@ impl Parser {
|
|||||||
fn term(&mut self) -> Result<Expr, ZernError> {
|
fn term(&mut self) -> Result<Expr, ZernError> {
|
||||||
let mut expr = self.factor()?;
|
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 op = self.previous().clone();
|
||||||
let right = self.factor()?;
|
let right = self.factor()?;
|
||||||
expr = Expr::Binary {
|
expr = Expr::Binary {
|
||||||
@@ -408,6 +441,14 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn unary(&mut self) -> Result<Expr, ZernError> {
|
fn unary(&mut self) -> Result<Expr, ZernError> {
|
||||||
|
if self.match_token(&[TokenType::Xor]) {
|
||||||
|
let op = self.previous().clone();
|
||||||
|
let right = self.unary()?;
|
||||||
|
return Ok(Expr::AddrOf {
|
||||||
|
op,
|
||||||
|
expr: Box::new(right),
|
||||||
|
});
|
||||||
|
}
|
||||||
if self.match_token(&[TokenType::Bang, TokenType::Minus]) {
|
if self.match_token(&[TokenType::Bang, TokenType::Minus]) {
|
||||||
let op = self.previous().clone();
|
let op = self.previous().clone();
|
||||||
let right = self.unary()?;
|
let right = self.unary()?;
|
||||||
|
|||||||
424
src/std.zr
424
src/std.zr
@@ -1,424 +0,0 @@
|
|||||||
func dbg.panic[msg: String] : Void
|
|
||||||
c.printf("PANIC: %s\n", msg)
|
|
||||||
c.exit(1)
|
|
||||||
|
|
||||||
func mem.alloc[x: I64] : Ptr
|
|
||||||
return c.malloc(x)
|
|
||||||
|
|
||||||
func mem.free[x: Ptr] : Void
|
|
||||||
c.free(x)
|
|
||||||
|
|
||||||
func mem.zero[x: I64, size: I64] : Void
|
|
||||||
c.memset(x, 0, size)
|
|
||||||
|
|
||||||
func mem.read8[x: Ptr] : U8
|
|
||||||
return _builtin_read8(x)
|
|
||||||
|
|
||||||
func mem.read64[x: Ptr] : I64
|
|
||||||
return _builtin_read64(x)
|
|
||||||
|
|
||||||
func mem.write8[x: Ptr, d: U8] : Void
|
|
||||||
_builtin_set8(x, d)
|
|
||||||
|
|
||||||
func mem.write64[x: Ptr, d: I64] : Void
|
|
||||||
_builtin_set64(x, d)
|
|
||||||
|
|
||||||
func io.print[x: String] : Void
|
|
||||||
c.puts(x)
|
|
||||||
|
|
||||||
func io.print_i64[x: I64] : Void
|
|
||||||
c.printf("%ld\n", x)
|
|
||||||
|
|
||||||
func io.read_stdin[]: String
|
|
||||||
let buffer: String = mem.alloc(1025)
|
|
||||||
let n: I64 = c.read(0, buffer, 1024)
|
|
||||||
if n < 0
|
|
||||||
return ""
|
|
||||||
str.set(buffer, n, 0)
|
|
||||||
return buffer
|
|
||||||
|
|
||||||
func io.read_file[path: String]: String
|
|
||||||
let file: Ptr = c.fopen(path, "rb")
|
|
||||||
if !file
|
|
||||||
dbg.panic("failed to open file")
|
|
||||||
|
|
||||||
c.fseek(file, 0, 2)
|
|
||||||
let size: I64 = c.ftell(file)
|
|
||||||
c.rewind(file)
|
|
||||||
|
|
||||||
let buffer: String = mem.alloc(size + 1)
|
|
||||||
|
|
||||||
// TODO: check if works with huge files
|
|
||||||
let n: I64 = c.fread(buffer, 1, size, file)
|
|
||||||
str.set(buffer, n, 0)
|
|
||||||
c.fclose(file)
|
|
||||||
return buffer
|
|
||||||
|
|
||||||
func io.write_file[path: String, content: String] : Void
|
|
||||||
let file: Ptr = c.fopen(path, "wb")
|
|
||||||
if !file
|
|
||||||
dbg.panic("failed to open file")
|
|
||||||
|
|
||||||
c.fwrite(content, 1, str.len(content), file)
|
|
||||||
c.fclose(file)
|
|
||||||
|
|
||||||
func str.len[s: String] : I64
|
|
||||||
return c.strlen(s)
|
|
||||||
|
|
||||||
func str.nth[s: String, n: I64] : U8
|
|
||||||
return mem.read8(s + n)
|
|
||||||
|
|
||||||
func str.copy[s: String] : String
|
|
||||||
let size: I64 = str.len(s) + 1
|
|
||||||
let dup: String = mem.alloc(size)
|
|
||||||
for i in 0..size+1
|
|
||||||
str.set(dup, i, str.nth(s, i))
|
|
||||||
return dup
|
|
||||||
|
|
||||||
func str.set[s: String, n: I64, c: U8] : Void
|
|
||||||
mem.write8(s+n, c)
|
|
||||||
|
|
||||||
func str.equal[a: String, b: String] : Bool
|
|
||||||
return c.strcmp(a, b) == 0
|
|
||||||
|
|
||||||
func str.is_whitespace[x: U8] : Bool
|
|
||||||
return x == ' ' | x == 10 | x == 13 | x == 9
|
|
||||||
|
|
||||||
func str.concat[a: String, b: String] : String
|
|
||||||
let out: String = mem.alloc(str.len(a) + str.len(b) + 1)
|
|
||||||
c.strcpy(out, a)
|
|
||||||
c.strcat(out, b)
|
|
||||||
return out
|
|
||||||
|
|
||||||
func str.find[s: String, c: U8] : I64
|
|
||||||
let s_len: I64 = str.len(s)
|
|
||||||
for i in 0..s_len
|
|
||||||
if str.nth(s, i) == c
|
|
||||||
return i
|
|
||||||
return -1
|
|
||||||
|
|
||||||
func str.substr[s: String, start: I64, length: I64] : String
|
|
||||||
if start < 0 | length < 0 | start + length > str.len(s)
|
|
||||||
dbg.panic("str.substr out of bounds")
|
|
||||||
|
|
||||||
let out: String = mem.alloc(length + 1)
|
|
||||||
c.strncpy(out, s + start, length)
|
|
||||||
str.set(out, length, 0)
|
|
||||||
return out
|
|
||||||
|
|
||||||
func str.trim[s: String] : String
|
|
||||||
let start: I64 = 0
|
|
||||||
let end: I64 = str.len(s) - 1
|
|
||||||
|
|
||||||
while start <= end & str.is_whitespace(str.nth(s, start))
|
|
||||||
start = start + 1
|
|
||||||
|
|
||||||
while end >= start & str.is_whitespace(str.nth(s, end))
|
|
||||||
end = end - 1
|
|
||||||
|
|
||||||
return str.substr(s, start, end - start + 1)
|
|
||||||
|
|
||||||
func str.split[haystack: String, needle: String]: Array
|
|
||||||
let haystack_len: I64 = str.len(haystack)
|
|
||||||
let needle_len: I64 = str.len(needle)
|
|
||||||
let result: Array = []
|
|
||||||
|
|
||||||
if !needle_len
|
|
||||||
if !haystack_len
|
|
||||||
return result
|
|
||||||
else
|
|
||||||
for i in 0..haystack_len
|
|
||||||
array.push(result, str.substr(haystack, i, 1))
|
|
||||||
return result
|
|
||||||
|
|
||||||
let start: I64 = 0
|
|
||||||
let i: I64 = 0
|
|
||||||
while i < haystack_len
|
|
||||||
if i <= haystack_len - needle_len
|
|
||||||
let match: Bool = true
|
|
||||||
for j in 0..needle_len
|
|
||||||
if str.nth(haystack, i+j) != str.nth(needle, j)
|
|
||||||
match = false
|
|
||||||
break
|
|
||||||
if match
|
|
||||||
array.push(result, str.substr(haystack, start, i - start))
|
|
||||||
start = i + needle_len
|
|
||||||
i = i + needle_len
|
|
||||||
continue
|
|
||||||
i = i + 1
|
|
||||||
|
|
||||||
array.push(result, str.substr(haystack, start, haystack_len - start))
|
|
||||||
return result
|
|
||||||
|
|
||||||
func str.reverse[s: String] : String
|
|
||||||
let len: I64 = str.len(s)
|
|
||||||
let out: String = mem.alloc(len + 1)
|
|
||||||
|
|
||||||
for i in 0..len
|
|
||||||
str.set(out, i, str.nth(s, len - i - 1))
|
|
||||||
str.set(out, len, 0)
|
|
||||||
return out
|
|
||||||
|
|
||||||
func str.from_i64[n: I64] : String
|
|
||||||
let x: String = mem.alloc(21)
|
|
||||||
c.snprintf(x, 21, "%ld", n)
|
|
||||||
return x
|
|
||||||
|
|
||||||
func str.parse_i64[s: String] : I64
|
|
||||||
return c.strtol(s, 0, 0)
|
|
||||||
|
|
||||||
func str.hex_encode[s: String] : String
|
|
||||||
let hex_chars: String = "0123456789abcdef"
|
|
||||||
let s_len: I64 = str.len(s)
|
|
||||||
let j: I64 = 0
|
|
||||||
let out: String = mem.alloc(s_len*2+1)
|
|
||||||
|
|
||||||
for i in 0..s_len
|
|
||||||
let high: U8 = (str.nth(s, i) >> 4) & 15
|
|
||||||
let low: U8 = str.nth(s, i) & 15
|
|
||||||
str.set(out, j, str.nth(hex_chars, high))
|
|
||||||
str.set(out, j+1, str.nth(hex_chars, low))
|
|
||||||
j = j + 2
|
|
||||||
|
|
||||||
str.set(out, j, 0)
|
|
||||||
return out
|
|
||||||
|
|
||||||
func str._from_hex_digit[d: U8] : I64
|
|
||||||
if d >= 'a' & d <= 'f'
|
|
||||||
return d - 'a' + 10
|
|
||||||
if d >= 'A' & d <= 'F'
|
|
||||||
return d - 'A' + 10
|
|
||||||
return d - '0'
|
|
||||||
|
|
||||||
func str.hex_decode[s: String] : String
|
|
||||||
let s_len: I64 = str.len(s)
|
|
||||||
let i: I64 = 0
|
|
||||||
let j: I64 = 0
|
|
||||||
let out: String = mem.alloc(s_len/2+1)
|
|
||||||
|
|
||||||
while i < s_len
|
|
||||||
str.set(out, j, str._from_hex_digit(str.nth(s, i)) * 16 + str._from_hex_digit(str.nth(s, i+1)))
|
|
||||||
i = i + 2
|
|
||||||
j = j + 1
|
|
||||||
|
|
||||||
str.set(out, j, 0)
|
|
||||||
return out
|
|
||||||
|
|
||||||
func math.gcd[a: I64, b: I64] : I64
|
|
||||||
while b != 0
|
|
||||||
let tmp: I64 = b
|
|
||||||
b = a % b
|
|
||||||
a = tmp
|
|
||||||
return a
|
|
||||||
|
|
||||||
func math.min[a: I64, b: I64] : I64
|
|
||||||
if a < b
|
|
||||||
return a
|
|
||||||
return b
|
|
||||||
|
|
||||||
func math.max[a: I64, b: I64] : I64
|
|
||||||
if a > b
|
|
||||||
return a
|
|
||||||
return b
|
|
||||||
|
|
||||||
func math.abs[n: I64] : I64
|
|
||||||
if n < 0
|
|
||||||
return -n
|
|
||||||
return n
|
|
||||||
|
|
||||||
func math.pow[b: I64, e: I64] : I64
|
|
||||||
let out: I64 = 1
|
|
||||||
for i in 0..e
|
|
||||||
out = out * b
|
|
||||||
return out
|
|
||||||
|
|
||||||
func math.lcm[a: I64, b: I64] : I64
|
|
||||||
return (a * b) / math.gcd(a, b)
|
|
||||||
|
|
||||||
func math.isqrt[n: I64] : I64
|
|
||||||
if n < 0
|
|
||||||
return -1
|
|
||||||
if n == 0 | n == 1
|
|
||||||
return n
|
|
||||||
|
|
||||||
let guess: I64 = n
|
|
||||||
let next_guess: I64 = (guess + n / guess) / 2
|
|
||||||
|
|
||||||
while next_guess < guess
|
|
||||||
guess = next_guess
|
|
||||||
next_guess = (guess + n / guess) / 2
|
|
||||||
|
|
||||||
return guess
|
|
||||||
|
|
||||||
func math.is_prime[n: I64]: Bool
|
|
||||||
if n <= 1
|
|
||||||
return false
|
|
||||||
if n == 2 | n == 3
|
|
||||||
return true
|
|
||||||
if n % 2 == 0 | n % 3 == 0
|
|
||||||
return false
|
|
||||||
|
|
||||||
let i: I64 = 5
|
|
||||||
while i * i <= n
|
|
||||||
if n % i == 0 | n % (i + 2) == 0
|
|
||||||
return false
|
|
||||||
i = i + 6
|
|
||||||
return true
|
|
||||||
|
|
||||||
func math.urandom[]: I64
|
|
||||||
let buffer: Ptr = mem.alloc(8)
|
|
||||||
let file: Ptr = c.fopen("/dev/urandom", "rb")
|
|
||||||
c.fread(buffer, 8, 1, file)
|
|
||||||
c.fclose(file)
|
|
||||||
let n: I64 = mem.read64(buffer)
|
|
||||||
mem.free(buffer)
|
|
||||||
return n
|
|
||||||
|
|
||||||
func array.new[] : Array
|
|
||||||
let arr: Ptr = mem.alloc(24)
|
|
||||||
mem.zero(arr, 24)
|
|
||||||
return arr
|
|
||||||
|
|
||||||
func array.nth[xs: Array, n: I64] : I64
|
|
||||||
// this probably should be implemented in the codegen
|
|
||||||
if n < 0 | n >= array.size(xs)
|
|
||||||
dbg.panic("array.nth out of bounds")
|
|
||||||
return xs[n]
|
|
||||||
|
|
||||||
func array.set[xs: Array, n: I64, x: I64] : Void
|
|
||||||
let data: Ptr = mem.read64(xs)
|
|
||||||
mem.write64(data+n*8, x)
|
|
||||||
|
|
||||||
func array.push[xs: Array, x: I64] : Void
|
|
||||||
let data: Ptr = mem.read64(xs)
|
|
||||||
let capacity: I64 = mem.read64(xs+8)
|
|
||||||
let size: I64 = mem.read64(xs+16)
|
|
||||||
|
|
||||||
if size == capacity
|
|
||||||
let new_capacity: I64 = 4
|
|
||||||
if capacity != 0
|
|
||||||
new_capacity = capacity * 2
|
|
||||||
let new_data: Ptr = c.realloc(data, new_capacity * 8)
|
|
||||||
mem.write64(xs, new_data)
|
|
||||||
mem.write64(xs+8, new_capacity)
|
|
||||||
data = new_data
|
|
||||||
|
|
||||||
mem.write64(data+size*8, x)
|
|
||||||
mem.write64(xs+16, size + 1)
|
|
||||||
|
|
||||||
func array.size[xs: Array] : I64
|
|
||||||
return mem.read64(xs+16)
|
|
||||||
|
|
||||||
func array.free[xs: Array] : Void
|
|
||||||
let data: Ptr = mem.read64(xs)
|
|
||||||
if data != 0
|
|
||||||
mem.free(data)
|
|
||||||
mem.free(xs)
|
|
||||||
|
|
||||||
func alg.quicksort[arr: Array] : Void
|
|
||||||
alg._do_quicksort(arr, 0, array.size(arr)-1)
|
|
||||||
|
|
||||||
func alg._do_quicksort[arr: Array, low: I64, high: I64] : Void
|
|
||||||
if low < high
|
|
||||||
let i: I64 = alg._partition(arr, low, high)
|
|
||||||
alg._do_quicksort(arr, low, i - 1)
|
|
||||||
alg._do_quicksort(arr, i + 1, high)
|
|
||||||
|
|
||||||
func alg._partition[arr: Array, low: I64, high: I64] : I64
|
|
||||||
let pivot: I64 = arr[high]
|
|
||||||
let i: I64 = low - 1
|
|
||||||
for j in (low)..high
|
|
||||||
if arr[j] <= pivot
|
|
||||||
i = i + 1
|
|
||||||
let temp: I64 = arr[i]
|
|
||||||
array.set(arr, i, arr[j])
|
|
||||||
array.set(arr, j, temp)
|
|
||||||
let temp: I64 = arr[i + 1]
|
|
||||||
array.set(arr, i + 1, arr[high])
|
|
||||||
array.set(arr, high, temp)
|
|
||||||
return i + 1
|
|
||||||
|
|
||||||
func os.exit[code: I64] : Void
|
|
||||||
c.exit(code)
|
|
||||||
|
|
||||||
func os.time[] : I64
|
|
||||||
let tv: Ptr = mem.alloc(16)
|
|
||||||
c.gettimeofday(tv, 0)
|
|
||||||
let seconds: I64 = mem.read64(tv)
|
|
||||||
let microseconds: I64 = mem.read64(tv+8)
|
|
||||||
mem.free(tv)
|
|
||||||
return seconds * 1000 + microseconds / 1000
|
|
||||||
|
|
||||||
func os.listdir[path: String] : Array
|
|
||||||
let dir: Ptr = c.opendir(path)
|
|
||||||
let files: Array = []
|
|
||||||
|
|
||||||
while true
|
|
||||||
let entry: Ptr = c.readdir(dir)
|
|
||||||
if entry == 0
|
|
||||||
break
|
|
||||||
|
|
||||||
let skip: Bool = false
|
|
||||||
if mem.read8(entry + 19) == '.'
|
|
||||||
if mem.read8(entry + 20) == 0
|
|
||||||
skip = true
|
|
||||||
else if mem.read8(entry + 20) == '.'
|
|
||||||
if mem.read8(entry + 21) == 0
|
|
||||||
skip = true
|
|
||||||
|
|
||||||
if !skip
|
|
||||||
array.push(files, str.copy(entry + 19))
|
|
||||||
c.closedir(dir)
|
|
||||||
return files
|
|
||||||
|
|
||||||
func net.listen[port: I64] : I64
|
|
||||||
let s: I64 = c.socket(2, 1, 0)
|
|
||||||
if s < 0
|
|
||||||
return -1
|
|
||||||
|
|
||||||
let sa: Ptr = mem.alloc(16)
|
|
||||||
mem.zero(sa, 16)
|
|
||||||
mem.write8(sa + 0, 2)
|
|
||||||
mem.write8(sa + 1, 0)
|
|
||||||
mem.write8(sa + 2, (port >> 8) & 255)
|
|
||||||
mem.write8(sa + 3, port & 255)
|
|
||||||
|
|
||||||
if c.bind(s, sa, 16) < 0
|
|
||||||
c.close(s)
|
|
||||||
return -1
|
|
||||||
mem.free(sa)
|
|
||||||
|
|
||||||
if c.listen(s, 1) < 0
|
|
||||||
c.close(s)
|
|
||||||
return -1
|
|
||||||
|
|
||||||
return s
|
|
||||||
|
|
||||||
func net.connect[host: String, port: I64] : I64
|
|
||||||
let he: Ptr = c.gethostbyname(host)
|
|
||||||
if he == 0
|
|
||||||
return -1
|
|
||||||
|
|
||||||
let ip_ptr: Ptr = mem.read64(mem.read64(he + 24))
|
|
||||||
|
|
||||||
let s: I64 = c.socket(2, 1, 0)
|
|
||||||
if s < 0
|
|
||||||
return -1
|
|
||||||
|
|
||||||
let sa: Ptr = mem.alloc(16)
|
|
||||||
mem.zero(sa, 16)
|
|
||||||
mem.write8(sa + 0, 2)
|
|
||||||
mem.write8(sa + 2, (port >> 8) & 255)
|
|
||||||
mem.write8(sa + 3, port & 255)
|
|
||||||
mem.write8(sa + 4, mem.read8(ip_ptr + 0))
|
|
||||||
mem.write8(sa + 5, mem.read8(ip_ptr + 1))
|
|
||||||
mem.write8(sa + 6, mem.read8(ip_ptr + 2))
|
|
||||||
mem.write8(sa + 7, mem.read8(ip_ptr + 3))
|
|
||||||
|
|
||||||
if c.connect(s, sa, 16) < 0
|
|
||||||
mem.free(sa)
|
|
||||||
c.close(s)
|
|
||||||
return -1
|
|
||||||
|
|
||||||
mem.free(sa)
|
|
||||||
return s
|
|
||||||
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
|
||||||
668
src/std/std.zr
Normal file
668
src/std/std.zr
Normal file
@@ -0,0 +1,668 @@
|
|||||||
|
extern malloc
|
||||||
|
extern realloc
|
||||||
|
extern free
|
||||||
|
extern gethostbyname
|
||||||
|
|
||||||
|
func dbg.panic[msg: str] : void
|
||||||
|
io.print("PANIC: ")
|
||||||
|
io.println(msg)
|
||||||
|
0/0 // crashes program which is kinda better since you get a gdb backtrace
|
||||||
|
os.exit(1)
|
||||||
|
|
||||||
|
func mem.alloc[x: i64] : ptr
|
||||||
|
return malloc(x)
|
||||||
|
|
||||||
|
func mem.free[x: ptr] : void
|
||||||
|
free(x)
|
||||||
|
|
||||||
|
func mem.zero[x: i64, size: i64] : void
|
||||||
|
for i in 0..size
|
||||||
|
mem.write8(x + i, 0)
|
||||||
|
|
||||||
|
func mem.read8[x: ptr] : u8
|
||||||
|
return x[0]
|
||||||
|
|
||||||
|
func mem.read16[x: ptr] : i64
|
||||||
|
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)
|
||||||
|
|
||||||
|
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(SYS_write, 1, x, size)
|
||||||
|
|
||||||
|
func io.print[x: str] : void
|
||||||
|
io.print_sized(x, str.len(x))
|
||||||
|
|
||||||
|
func io.println[x: str] : void
|
||||||
|
io.print(x)
|
||||||
|
io.print("\n")
|
||||||
|
|
||||||
|
func io.print_char[x: u8] : void
|
||||||
|
io.print_sized(^x, 1)
|
||||||
|
|
||||||
|
func io.print_i64[x: i64] : void
|
||||||
|
let s: str = str.from_i64(x)
|
||||||
|
io.print(s)
|
||||||
|
mem.free(s)
|
||||||
|
|
||||||
|
func io.println_i64[x: i64] : void
|
||||||
|
let s: str = str.from_i64(x)
|
||||||
|
io.println(s)
|
||||||
|
mem.free(s)
|
||||||
|
|
||||||
|
func io.read_char[] : u8
|
||||||
|
let c: u8 = 0
|
||||||
|
_builtin_syscall(SYS_read, 0, ^c, 1)
|
||||||
|
return c
|
||||||
|
|
||||||
|
func io.read_line[]: str
|
||||||
|
let MAX_SIZE = 60000
|
||||||
|
let buffer: str = mem.alloc(MAX_SIZE + 1)
|
||||||
|
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(SYS_openat, -100, path, 0, 0)
|
||||||
|
if fd <= 0
|
||||||
|
dbg.panic("failed to open file")
|
||||||
|
|
||||||
|
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(SYS_read, fd, buffer, size)
|
||||||
|
str.set(buffer, n, 0)
|
||||||
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
return buffer
|
||||||
|
|
||||||
|
func io.write_file[path: str, content: str] : void
|
||||||
|
let fd: ptr = _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644)
|
||||||
|
if fd < 0
|
||||||
|
dbg.panic("failed to open file")
|
||||||
|
|
||||||
|
_builtin_syscall(SYS_write, fd, content, str.len(content))
|
||||||
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
|
||||||
|
func str.len[s: str] : i64
|
||||||
|
let i = 0
|
||||||
|
while s[i]
|
||||||
|
i = i + 1
|
||||||
|
return i
|
||||||
|
|
||||||
|
func str.copy[s: str] : str
|
||||||
|
let size: i64 = str.len(s) + 1
|
||||||
|
let dup: str = mem.alloc(size)
|
||||||
|
for i in 0..size
|
||||||
|
str.set(dup, i, s[i])
|
||||||
|
return dup
|
||||||
|
|
||||||
|
func str.set[s: str, n: i64, c: u8] : void
|
||||||
|
mem.write8(s + n, c)
|
||||||
|
|
||||||
|
func str.equal[a: str, b: str] : bool
|
||||||
|
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
|
||||||
|
|
||||||
|
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)
|
||||||
|
let b_len: i64 = str.len(b)
|
||||||
|
let out: str = mem.alloc(a_len + b_len + 1)
|
||||||
|
for i in 0..a_len
|
||||||
|
str.set(out, i, a[i])
|
||||||
|
for i in 0..b_len
|
||||||
|
str.set(out, a_len + i, b[i])
|
||||||
|
str.set(out, a_len + b_len, 0)
|
||||||
|
return out
|
||||||
|
|
||||||
|
func str.find[haystack: str, needle: str] : i64
|
||||||
|
let haystack_len: i64 = str.len(haystack)
|
||||||
|
let needle_len: i64 = str.len(needle)
|
||||||
|
|
||||||
|
if needle_len == 0
|
||||||
|
return 0
|
||||||
|
|
||||||
|
for i in 0..(haystack_len - needle_len + 1)
|
||||||
|
let match: bool = true
|
||||||
|
for j in 0..needle_len
|
||||||
|
if haystack[i + j] != needle[j]
|
||||||
|
match = false
|
||||||
|
break
|
||||||
|
if match
|
||||||
|
return i
|
||||||
|
return -1
|
||||||
|
|
||||||
|
func str.substr[s: str, start: i64, length: i64] : str
|
||||||
|
if start < 0 || length < 0 || start + length > str.len(s)
|
||||||
|
dbg.panic("str.substr out of bounds")
|
||||||
|
|
||||||
|
let out: str = mem.alloc(length + 1)
|
||||||
|
for i in 0..length
|
||||||
|
str.set(out, i, s[start + i])
|
||||||
|
str.set(out, length, 0)
|
||||||
|
return out
|
||||||
|
|
||||||
|
func str.trim[s: str] : str
|
||||||
|
let len: i64 = str.len(s)
|
||||||
|
if len == 0
|
||||||
|
return ""
|
||||||
|
|
||||||
|
let start = 0
|
||||||
|
let end: i64 = len - 1
|
||||||
|
|
||||||
|
while start <= end && str.is_whitespace(s[start])
|
||||||
|
start = start + 1
|
||||||
|
|
||||||
|
while end >= start && str.is_whitespace(s[end])
|
||||||
|
end = end - 1
|
||||||
|
|
||||||
|
return str.substr(s, start, end - start + 1)
|
||||||
|
|
||||||
|
func str.split[haystack: str, needle: str]: array
|
||||||
|
let haystack_len: i64 = str.len(haystack)
|
||||||
|
let needle_len: i64 = str.len(needle)
|
||||||
|
let result: array = []
|
||||||
|
|
||||||
|
if !needle_len
|
||||||
|
if !haystack_len
|
||||||
|
return result
|
||||||
|
else
|
||||||
|
for i in 0..haystack_len
|
||||||
|
array.push(result, str.substr(haystack, i, 1))
|
||||||
|
return result
|
||||||
|
|
||||||
|
let start = 0
|
||||||
|
let i = 0
|
||||||
|
while i < haystack_len
|
||||||
|
if i <= haystack_len - needle_len
|
||||||
|
let match: bool = true
|
||||||
|
for j in 0..needle_len
|
||||||
|
if haystack[i + j] != needle[j]
|
||||||
|
match = false
|
||||||
|
break
|
||||||
|
if match
|
||||||
|
array.push(result, str.substr(haystack, start, i - start))
|
||||||
|
start = i + needle_len
|
||||||
|
i = i + needle_len
|
||||||
|
continue
|
||||||
|
i = i + 1
|
||||||
|
|
||||||
|
array.push(result, str.substr(haystack, start, haystack_len - start))
|
||||||
|
return result
|
||||||
|
|
||||||
|
func str.reverse[s: str] : str
|
||||||
|
let len: i64 = str.len(s)
|
||||||
|
let out: str = mem.alloc(len + 1)
|
||||||
|
|
||||||
|
for i in 0..len
|
||||||
|
str.set(out, i, s[len - i - 1])
|
||||||
|
str.set(out, len, 0)
|
||||||
|
return out
|
||||||
|
|
||||||
|
// not sure this covers all wacky edge cases
|
||||||
|
func str.from_i64[n: i64] : str
|
||||||
|
if n == 0
|
||||||
|
return str.copy("0")
|
||||||
|
|
||||||
|
let neg: bool = n < 0
|
||||||
|
if neg
|
||||||
|
n = -n
|
||||||
|
let buf: str = mem.alloc(21) // enough to fit -MAX_I64
|
||||||
|
let i = 0
|
||||||
|
while n > 0
|
||||||
|
let d: u8 = n % 10
|
||||||
|
str.set(buf, i, '0' + d)
|
||||||
|
n = n / 10
|
||||||
|
i = i + 1
|
||||||
|
if neg
|
||||||
|
str.set(buf, i, '-')
|
||||||
|
i = i + 1
|
||||||
|
str.set(buf, i, 0)
|
||||||
|
let s: str = str.reverse(buf)
|
||||||
|
mem.free(buf)
|
||||||
|
return s
|
||||||
|
|
||||||
|
func str.from_char[c: u8] : str
|
||||||
|
let s: str = mem.alloc(2)
|
||||||
|
str.set(s, 0, c)
|
||||||
|
str.set(s, 1, 0)
|
||||||
|
return s
|
||||||
|
|
||||||
|
func str.parse_i64[s: str] : i64
|
||||||
|
let len: i64 = str.len(s)
|
||||||
|
let i = 0
|
||||||
|
|
||||||
|
let sign = 1
|
||||||
|
if i < len && s[i] == '-'
|
||||||
|
sign = -1
|
||||||
|
i = i + 1
|
||||||
|
|
||||||
|
let num = 0
|
||||||
|
while i < len
|
||||||
|
let d: u8 = s[i]
|
||||||
|
if d < '0' || d > '9'
|
||||||
|
break
|
||||||
|
num = num * 10 + (d - '0')
|
||||||
|
i = i + 1
|
||||||
|
return num * sign
|
||||||
|
|
||||||
|
func str.hex_encode[s: str, s_len: i64] : str
|
||||||
|
let hex_chars: str = "0123456789abcdef"
|
||||||
|
let j = 0
|
||||||
|
let out: str = mem.alloc(s_len * 2 + 1)
|
||||||
|
|
||||||
|
for i in 0..s_len
|
||||||
|
let high: u8 = (s[i] >> 4) & 15
|
||||||
|
let low: u8 = s[i] & 15
|
||||||
|
str.set(out, j, hex_chars[high])
|
||||||
|
str.set(out, j + 1, hex_chars[low])
|
||||||
|
j = j + 2
|
||||||
|
|
||||||
|
str.set(out, j, 0)
|
||||||
|
return out
|
||||||
|
|
||||||
|
func str._hex_digit_to_int[d: u8] : i64
|
||||||
|
if d >= 'a' && d <= 'f'
|
||||||
|
return d - 'a' + 10
|
||||||
|
if d >= 'A' && d <= 'F'
|
||||||
|
return d - 'A' + 10
|
||||||
|
return d - '0'
|
||||||
|
|
||||||
|
func str.hex_decode[s: str] : str
|
||||||
|
let s_len: i64 = str.len(s)
|
||||||
|
let i = 0
|
||||||
|
let j = 0
|
||||||
|
let out: str = mem.alloc(s_len / 2 + 1)
|
||||||
|
|
||||||
|
while i < s_len
|
||||||
|
str.set(out, j, str._hex_digit_to_int(s[i]) * 16 + str._hex_digit_to_int(s[i + 1]))
|
||||||
|
i = i + 2
|
||||||
|
j = j + 1
|
||||||
|
|
||||||
|
str.set(out, j, 0)
|
||||||
|
return out
|
||||||
|
|
||||||
|
func math.gcd[a: i64, b: i64] : i64
|
||||||
|
while b != 0
|
||||||
|
let tmp: i64 = b
|
||||||
|
b = a % b
|
||||||
|
a = tmp
|
||||||
|
return a
|
||||||
|
|
||||||
|
func math.min[a: i64, b: i64] : i64
|
||||||
|
if a < b
|
||||||
|
return a
|
||||||
|
return b
|
||||||
|
|
||||||
|
func math.max[a: i64, b: i64] : i64
|
||||||
|
if a > b
|
||||||
|
return a
|
||||||
|
return b
|
||||||
|
|
||||||
|
func math.abs[n: i64] : i64
|
||||||
|
if n < 0
|
||||||
|
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 = 1
|
||||||
|
for i in 0..e
|
||||||
|
out = out * b
|
||||||
|
return out
|
||||||
|
|
||||||
|
func math.lcm[a: i64, b: i64] : i64
|
||||||
|
return (a * b) / math.gcd(a, b)
|
||||||
|
|
||||||
|
func math.isqrt[n: i64] : i64
|
||||||
|
if n < 0
|
||||||
|
dbg.panic("negative number passed to math.isqrt")
|
||||||
|
if n == 0 || n == 1
|
||||||
|
return n
|
||||||
|
|
||||||
|
let guess: i64 = n
|
||||||
|
let next_guess: i64 = (guess + n / guess) / 2
|
||||||
|
|
||||||
|
while next_guess < guess
|
||||||
|
guess = next_guess
|
||||||
|
next_guess = (guess + n / guess) / 2
|
||||||
|
|
||||||
|
return guess
|
||||||
|
|
||||||
|
func math.is_prime[n: i64]: bool
|
||||||
|
if n <= 1
|
||||||
|
return false
|
||||||
|
if n == 2 || n == 3
|
||||||
|
return true
|
||||||
|
if n % 2 == 0 || n % 3 == 0
|
||||||
|
return false
|
||||||
|
|
||||||
|
let i = 5
|
||||||
|
while i * i <= n
|
||||||
|
if n % i == 0 || n % (i + 2) == 0
|
||||||
|
return false
|
||||||
|
i = i + 6
|
||||||
|
return true
|
||||||
|
|
||||||
|
func array.new[] : array
|
||||||
|
// [ 8 bytes - ptr to data ] [ 8 bytes - size ] [ 8 bytes - capacity ]
|
||||||
|
let arr: ptr = mem.alloc(24)
|
||||||
|
mem.zero(arr, 24)
|
||||||
|
return arr
|
||||||
|
|
||||||
|
func array.nth[xs: array, n: i64] : i64
|
||||||
|
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.set out of bounds")
|
||||||
|
let data: ptr = mem.read64(xs)
|
||||||
|
mem.write64(data + n * 8, x)
|
||||||
|
|
||||||
|
func array.push[xs: array, x: i64] : void
|
||||||
|
let data: ptr = mem.read64(xs)
|
||||||
|
let capacity: i64 = mem.read64(xs + 8)
|
||||||
|
let size: i64 = mem.read64(xs + 16)
|
||||||
|
|
||||||
|
if size == capacity
|
||||||
|
let new_capacity = 4
|
||||||
|
if capacity != 0
|
||||||
|
new_capacity = capacity * 2
|
||||||
|
let new_data: ptr = realloc(data, new_capacity * 8)
|
||||||
|
mem.write64(xs, new_data)
|
||||||
|
mem.write64(xs + 8, new_capacity)
|
||||||
|
data = new_data
|
||||||
|
|
||||||
|
mem.write64(data + size * 8, x)
|
||||||
|
mem.write64(xs + 16, size + 1)
|
||||||
|
|
||||||
|
func array.size[xs: array] : i64
|
||||||
|
return mem.read64(xs + 16)
|
||||||
|
|
||||||
|
func array.free[xs: array] : void
|
||||||
|
let data: ptr = mem.read64(xs)
|
||||||
|
if data != 0
|
||||||
|
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)
|
||||||
|
dbg.panic("array.slice out of bounds")
|
||||||
|
|
||||||
|
let new_array: array = []
|
||||||
|
for i in 0..length
|
||||||
|
array.push(new_array, array.nth(xs, start + i))
|
||||||
|
return new_array
|
||||||
|
|
||||||
|
func array.concat[a: array, b: array] : array
|
||||||
|
let new_array: array = []
|
||||||
|
for i in 0..array.size(a)
|
||||||
|
array.push(new_array, array.nth(a, i))
|
||||||
|
for i in 0..array.size(b)
|
||||||
|
array.push(new_array, array.nth(b, i))
|
||||||
|
return new_array
|
||||||
|
|
||||||
|
func alg.quicksort[arr: array] : void
|
||||||
|
alg._do_quicksort(arr, 0, array.size(arr) - 1)
|
||||||
|
|
||||||
|
func alg._do_quicksort[arr: array, low: i64, high: i64] : void
|
||||||
|
if low < high
|
||||||
|
let i: i64 = alg._partition(arr, low, high)
|
||||||
|
alg._do_quicksort(arr, low, i - 1)
|
||||||
|
alg._do_quicksort(arr, i + 1, high)
|
||||||
|
|
||||||
|
func alg._partition[arr: array, low: i64, high: i64] : i64
|
||||||
|
let pivot: i64 = array.nth(arr, high)
|
||||||
|
let i: i64 = low - 1
|
||||||
|
for j in (low)..high
|
||||||
|
if array.nth(arr, j) <= pivot
|
||||||
|
i = i + 1
|
||||||
|
let temp: i64 = array.nth(arr ,i)
|
||||||
|
array.set(arr, i, array.nth(arr, j))
|
||||||
|
array.set(arr, j, temp)
|
||||||
|
let temp: i64 = array.nth(arr, i + 1)
|
||||||
|
array.set(arr, i + 1, array.nth(arr, high))
|
||||||
|
array.set(arr, high, temp)
|
||||||
|
return i + 1
|
||||||
|
|
||||||
|
func alg.count[arr: array, item: i64] : i64
|
||||||
|
let count = 0
|
||||||
|
let size: i64 = array.size(arr)
|
||||||
|
for i in 0..size
|
||||||
|
if array.nth(arr, i) == item
|
||||||
|
count = count + 1
|
||||||
|
return count
|
||||||
|
|
||||||
|
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 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(SYS_gettimeofday, tv, 0)
|
||||||
|
let seconds: i64 = mem.read64(tv)
|
||||||
|
let microseconds: i64 = mem.read64(tv + 8)
|
||||||
|
mem.free(tv)
|
||||||
|
return seconds * 1000 + microseconds / 1000
|
||||||
|
|
||||||
|
// voodoo magic
|
||||||
|
func os.shell[command: str] : i64
|
||||||
|
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(SYS_execve, "/bin/sh", mem.read64(argv), _builtin_environ())
|
||||||
|
_builtin_syscall(SYS_exit, 1)
|
||||||
|
else
|
||||||
|
let status = 0
|
||||||
|
let wp: i64 = _builtin_syscall(SYS_wait4, pid, ^status, 0, 0)
|
||||||
|
if wp == -1
|
||||||
|
return -1
|
||||||
|
let st: i64 = status & 0xffffffff
|
||||||
|
|
||||||
|
if (st & 0x7f) == 0
|
||||||
|
return (st >> 8) & 0xff
|
||||||
|
else
|
||||||
|
return -(st & 0x7f)
|
||||||
|
|
||||||
|
func os.listdir[path: str] : array
|
||||||
|
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(SYS_getdents64, fd, buf, 1024)
|
||||||
|
if n <= 0
|
||||||
|
break
|
||||||
|
|
||||||
|
let pos = 0
|
||||||
|
while pos < n
|
||||||
|
let len: i64 = mem.read16(buf + pos + 16)
|
||||||
|
let name: str = buf + pos + 19
|
||||||
|
if name[0]
|
||||||
|
let skip: bool = false
|
||||||
|
// skip if name is exactly '.' or '..'
|
||||||
|
if name[0] == '.'
|
||||||
|
if name[1] == 0
|
||||||
|
skip = true
|
||||||
|
else if name[1] == '.'
|
||||||
|
if name[2] == 0
|
||||||
|
skip = true
|
||||||
|
if !skip
|
||||||
|
array.push(files, str.copy(name))
|
||||||
|
pos = pos + len
|
||||||
|
|
||||||
|
mem.free(buf)
|
||||||
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
return files
|
||||||
|
|
||||||
|
func net.listen[packed_host: i64, port: i64] : i64
|
||||||
|
let s: i64 = _builtin_syscall(SYS_socket, 2, 1, 0)
|
||||||
|
if s < 0
|
||||||
|
return -1
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
mem.zero(sa, 16)
|
||||||
|
mem.write8(sa + 0, 2)
|
||||||
|
mem.write8(sa + 1, 0)
|
||||||
|
mem.write8(sa + 2, (port >> 8) & 255)
|
||||||
|
mem.write8(sa + 3, port & 255)
|
||||||
|
mem.write8(sa + 4, (packed_host >> 24) & 255)
|
||||||
|
mem.write8(sa + 5, (packed_host >> 16) & 255)
|
||||||
|
mem.write8(sa + 6, (packed_host >> 8) & 255)
|
||||||
|
mem.write8(sa + 7, packed_host & 255)
|
||||||
|
|
||||||
|
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(SYS_listen, s, 1) < 0
|
||||||
|
_builtin_syscall(SYS_close, s)
|
||||||
|
return -1
|
||||||
|
|
||||||
|
return s
|
||||||
|
|
||||||
|
func net.connect[host: str, port: i64] : i64
|
||||||
|
let he: ptr = gethostbyname(host)
|
||||||
|
if he == 0
|
||||||
|
return -1
|
||||||
|
|
||||||
|
let ip_ptr: ptr = mem.read64(mem.read64(he + 24))
|
||||||
|
|
||||||
|
let s: i64 = _builtin_syscall(SYS_socket, 2, 1, 0)
|
||||||
|
if s < 0
|
||||||
|
return -1
|
||||||
|
|
||||||
|
let sa: ptr = mem.alloc(16)
|
||||||
|
mem.zero(sa, 16)
|
||||||
|
mem.write8(sa + 0, 2)
|
||||||
|
mem.write8(sa + 2, (port >> 8) & 255)
|
||||||
|
mem.write8(sa + 3, port & 255)
|
||||||
|
mem.write8(sa + 4, ip_ptr[0])
|
||||||
|
mem.write8(sa + 5, ip_ptr[1])
|
||||||
|
mem.write8(sa + 6, ip_ptr[2])
|
||||||
|
mem.write8(sa + 7, ip_ptr[3])
|
||||||
|
|
||||||
|
if _builtin_syscall(SYS_connect, s, sa, 16) < 0
|
||||||
|
mem.free(sa)
|
||||||
|
_builtin_syscall(SYS_close, s)
|
||||||
|
return -1
|
||||||
|
|
||||||
|
mem.free(sa)
|
||||||
|
return s
|
||||||
|
|
||||||
|
func net.accept[s: i64] : i64
|
||||||
|
return _builtin_syscall(SYS_accept, s, 0, 0)
|
||||||
|
|
||||||
|
func net.send[s: i64, data: str, size: i64] : void
|
||||||
|
_builtin_syscall(SYS_sendto, s, data, size, 0, 0, 0)
|
||||||
|
|
||||||
|
func net.read[s: i64, buffer: ptr, size: i64] : i64
|
||||||
|
return _builtin_syscall(SYS_read, s, buffer, size)
|
||||||
|
|
||||||
|
func net.close[s: i64] : void
|
||||||
|
_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
|
||||||
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,6 +17,8 @@ pub enum TokenType {
|
|||||||
Colon,
|
Colon,
|
||||||
BitAnd,
|
BitAnd,
|
||||||
BitOr,
|
BitOr,
|
||||||
|
LogicalAnd,
|
||||||
|
LogicalOr,
|
||||||
Pipe,
|
Pipe,
|
||||||
DoubleDot,
|
DoubleDot,
|
||||||
ShiftLeft,
|
ShiftLeft,
|
||||||
@@ -38,6 +40,7 @@ pub enum TokenType {
|
|||||||
False,
|
False,
|
||||||
|
|
||||||
KeywordLet,
|
KeywordLet,
|
||||||
|
KeywordConst,
|
||||||
KeywordIf,
|
KeywordIf,
|
||||||
KeywordElse,
|
KeywordElse,
|
||||||
KeywordWhile,
|
KeywordWhile,
|
||||||
@@ -47,6 +50,8 @@ pub enum TokenType {
|
|||||||
KeywordReturn,
|
KeywordReturn,
|
||||||
KeywordBreak,
|
KeywordBreak,
|
||||||
KeywordContinue,
|
KeywordContinue,
|
||||||
|
KeywordExtern,
|
||||||
|
KeywordExport,
|
||||||
|
|
||||||
Indent,
|
Indent,
|
||||||
Dedent,
|
Dedent,
|
||||||
@@ -168,10 +173,18 @@ impl Tokenizer {
|
|||||||
self.add_token(TokenType::Slash)
|
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('>') {
|
if self.match_char('>') {
|
||||||
self.add_token(TokenType::Pipe);
|
self.add_token(TokenType::Pipe);
|
||||||
|
} else if self.match_char('|') {
|
||||||
|
self.add_token(TokenType::LogicalOr);
|
||||||
} else {
|
} else {
|
||||||
self.add_token(TokenType::BitOr);
|
self.add_token(TokenType::BitOr);
|
||||||
}
|
}
|
||||||
@@ -210,6 +223,9 @@ impl Tokenizer {
|
|||||||
}
|
}
|
||||||
// TODO: escape sequences
|
// TODO: escape sequences
|
||||||
'\'' => {
|
'\'' => {
|
||||||
|
if self.eof() {
|
||||||
|
return error!(self.loc, "unterminated char literal");
|
||||||
|
}
|
||||||
self.advance();
|
self.advance();
|
||||||
if !self.match_char('\'') {
|
if !self.match_char('\'') {
|
||||||
return error!(self.loc, "expected ' after char literal");
|
return error!(self.loc, "expected ' after char literal");
|
||||||
@@ -298,20 +314,14 @@ impl Tokenizer {
|
|||||||
while self.peek().is_ascii_hexdigit() {
|
while self.peek().is_ascii_hexdigit() {
|
||||||
self.advance();
|
self.advance();
|
||||||
}
|
}
|
||||||
|
} else if self.match_char('o') {
|
||||||
|
while matches!(self.peek(), '0'..='7') {
|
||||||
|
self.advance();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
while self.peek().is_ascii_digit() {
|
while self.peek().is_ascii_digit() {
|
||||||
self.advance();
|
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);
|
self.add_token(TokenType::Number);
|
||||||
@@ -325,6 +335,7 @@ impl Tokenizer {
|
|||||||
let lexeme: String = self.source[self.start..self.current].iter().collect();
|
let lexeme: String = self.source[self.start..self.current].iter().collect();
|
||||||
self.add_token(match lexeme.as_str() {
|
self.add_token(match lexeme.as_str() {
|
||||||
"let" => TokenType::KeywordLet,
|
"let" => TokenType::KeywordLet,
|
||||||
|
"const" => TokenType::KeywordConst,
|
||||||
"if" => TokenType::KeywordIf,
|
"if" => TokenType::KeywordIf,
|
||||||
"else" => TokenType::KeywordElse,
|
"else" => TokenType::KeywordElse,
|
||||||
"while" => TokenType::KeywordWhile,
|
"while" => TokenType::KeywordWhile,
|
||||||
@@ -334,6 +345,8 @@ impl Tokenizer {
|
|||||||
"return" => TokenType::KeywordReturn,
|
"return" => TokenType::KeywordReturn,
|
||||||
"break" => TokenType::KeywordBreak,
|
"break" => TokenType::KeywordBreak,
|
||||||
"continue" => TokenType::KeywordContinue,
|
"continue" => TokenType::KeywordContinue,
|
||||||
|
"extern" => TokenType::KeywordExtern,
|
||||||
|
"export" => TokenType::KeywordExport,
|
||||||
"true" => TokenType::True,
|
"true" => TokenType::True,
|
||||||
"false" => TokenType::False,
|
"false" => TokenType::False,
|
||||||
_ => TokenType::Identifier,
|
_ => TokenType::Identifier,
|
||||||
|
|||||||
79
test.zr
79
test.zr
@@ -1,34 +1,63 @@
|
|||||||
func run_test[x: String] : Void
|
func run_test[x: str] : void
|
||||||
c.printf("\033[93mBuilding %s...\033[0m", x)
|
let build_blacklist: array = ["examples/puzzles", "examples/raylib.zr", "examples/x11.zr", "examples/sqlite_todo.zr"]
|
||||||
let cmd: String = str.concat("./target/release/zern examples/", x)
|
let run_blacklist: array = ["/aoc", "guess_number.zr", "tcp_server.zr"]
|
||||||
|
|
||||||
let build_start_time: I64 = os.time()
|
for i in 0..array.size(build_blacklist)
|
||||||
if c.system(cmd) != 0
|
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 build_start_time: i64 = os.time()
|
||||||
|
if os.shell(str.concat("./target/release/zern ", x)) != 0
|
||||||
os.exit(1)
|
os.exit(1)
|
||||||
let build_end_time: I64 = os.time()
|
let build_end_time: i64 = os.time()
|
||||||
|
|
||||||
mem.free(cmd)
|
io.print_i64(build_end_time - build_start_time)
|
||||||
c.printf(" %ldms\n", build_end_time - build_start_time)
|
io.println("ms")
|
||||||
|
|
||||||
if str.equal(x, "guess_number.zr") | str.equal(x, "tcp_server.zr") | str.equal(x, "raylib.zr")
|
for i in 0..array.size(run_blacklist)
|
||||||
c.printf("\033[93mSkipping %s...\033[0m\n", x)
|
if str.find(x, array.nth(run_blacklist, i)) != -1
|
||||||
else
|
io.print("\033[93mSkipping ")
|
||||||
let run_start_time: I64 = os.time()
|
io.print(x)
|
||||||
if str.equal(x, "curl.zr")
|
io.println("...\033[0m")
|
||||||
if c.system("./out http://example.com") != 0
|
return 0
|
||||||
os.exit(1)
|
|
||||||
else
|
|
||||||
if c.system("./out") != 0
|
|
||||||
os.exit(1)
|
|
||||||
let run_end_time: I64 = os.time()
|
|
||||||
|
|
||||||
c.printf("\033[93mRunning %s...\033[0m %ldms\n", x, run_end_time - run_start_time)
|
io.print("\033[95mRunning ")
|
||||||
|
io.print(x)
|
||||||
|
io.println("...\033[0m")
|
||||||
|
|
||||||
func main[] : I64
|
let run_cmd: str = "./out"
|
||||||
c.system("cargo build --release")
|
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 files: Array = os.listdir("examples/")
|
let run_start_time: i64 = os.time()
|
||||||
|
if os.shell(run_cmd) != 0
|
||||||
|
os.exit(1)
|
||||||
|
let run_end_time: i64 = os.time()
|
||||||
|
|
||||||
|
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 run_directory[dir: str] : void
|
||||||
|
let files: array = os.listdir(dir)
|
||||||
for i in 0..array.size(files)
|
for i in 0..array.size(files)
|
||||||
run_test(files[i])
|
run_test(str.concat(dir, array.nth(files, i)))
|
||||||
|
|
||||||
array.free(files)
|
array.free(files)
|
||||||
|
|
||||||
|
func main[] : i64
|
||||||
|
os.shell("cargo build --release")
|
||||||
|
|
||||||
|
run_directory("examples/")
|
||||||
|
run_directory("examples/puzzles/")
|
||||||
Reference in New Issue
Block a user