Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e447d4d7cd | |||
| ebc887fb5b | |||
| 7c23e57ec0 | |||
| d55ca11658 | |||
| a259d204be | |||
| 0863094635 | |||
| 7f93599f34 | |||
| ada570c84e | |||
| fdcf7eca37 | |||
| fbf28748c7 | |||
| daf9079ca4 | |||
| 9f39f627ad | |||
| 7855e5b092 | |||
| b24bfc0241 | |||
| 73cd71c8e4 | |||
| 06c979f177 | |||
| ce54d1b560 | |||
| 7cffd73406 | |||
| 5a41163ca1 | |||
| 552a404d73 | |||
| 852c463532 | |||
| c1bd84464c | |||
| d9819476f8 | |||
| 2b316cbc16 | |||
| 1e98d9d706 | |||
| 46045af4fa | |||
| ca8ae6e110 | |||
| 3fd62c6083 | |||
| 6fc80626dc | |||
| 2c09d7bc21 |
32
README.md
32
README.md
@@ -2,20 +2,34 @@
|
||||
|
||||
A very cool language
|
||||
|
||||
## Huh?
|
||||
## Features
|
||||
* Clean indentation-based syntax
|
||||
* Compiles to x86_64 Assembly
|
||||
* Almost works
|
||||
* ~~No libc required~~ (SOON; still used for memory allocation and DNS resolution)
|
||||
* Produces tiny static executables (~30KB with musl)
|
||||
* Sometimes works
|
||||
* Has the pipe operator
|
||||
|
||||
## Syntax
|
||||
```rust
|
||||
func fib[n: I64] : I64
|
||||
if n <= 1
|
||||
return n
|
||||
return fib(n-2) + fib(n-1)
|
||||
func main[] : i64
|
||||
let answer: i64 = math.abs(os.urandom()) % 100
|
||||
|
||||
func main[] : I64
|
||||
for i in 0..20
|
||||
fib(i) |> str.from_i64() |> io.print()
|
||||
while true
|
||||
io.println("Guess a number: ")
|
||||
let guess: i64 = io.read_line() |> str.trim() |> str.parse_i64()
|
||||
|
||||
if guess == answer
|
||||
io.println("You win!")
|
||||
break
|
||||
else if guess < answer
|
||||
io.println("Too low!")
|
||||
else
|
||||
io.println("Too high!")
|
||||
```
|
||||
|
||||
## Quickstart
|
||||
```
|
||||
cargo install --git https://github.com/antpiasecki/zern
|
||||
zern -m -r hello.zr
|
||||
```
|
||||
@@ -1,46 +1,49 @@
|
||||
func main[] : I64
|
||||
func main[] : i64
|
||||
// https://brainfuck.org/sierpinski.b
|
||||
let src: String = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
||||
let src_len: I64 = c.strlen(src)
|
||||
let i: I64 = 0
|
||||
let src: str = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
||||
let src_len: i64 = str.len(src)
|
||||
let i: i64 = 0
|
||||
|
||||
let memory: Ptr = c.calloc(1, 30000)
|
||||
let p: I64 = 0
|
||||
let memory: ptr = mem.alloc(30000)
|
||||
mem.zero(memory, 30000)
|
||||
let p: i64 = 0
|
||||
|
||||
while i < src_len
|
||||
let op: U8 = str.nth(src, i)
|
||||
let op: u8 = src[i]
|
||||
|
||||
if op == '>'
|
||||
p = p + 1
|
||||
else if op == '<'
|
||||
p = p - 1
|
||||
else if op == '+'
|
||||
str.set(memory, p, str.nth(memory, p)+1)
|
||||
str.set(memory, p, memory[p] + 1)
|
||||
else if op == '-'
|
||||
str.set(memory, p, str.nth(memory, p)-1)
|
||||
str.set(memory, p, memory[p] - 1)
|
||||
else if op == '.'
|
||||
c.printf("%c", str.nth(memory, p))
|
||||
io.print_char(memory[p])
|
||||
else if op == ','
|
||||
str.set(memory, p, c.getchar())
|
||||
str.set(memory, p, io.read_char())
|
||||
else if op == '['
|
||||
if !str.nth(memory, p)
|
||||
if !memory[p]
|
||||
i = i + 1
|
||||
let opened: I64 = 0
|
||||
while i < src_len & !(str.nth(src, i) == ']' & !opened)
|
||||
if str.nth(src, i) == '['
|
||||
let opened: i64 = 0
|
||||
while i < src_len & !(src[i] == ']' & !opened)
|
||||
if src[i] == '['
|
||||
opened = opened + 1
|
||||
else if str.nth(src, i) == ']'
|
||||
else if src[i] == ']'
|
||||
opened = opened - 1
|
||||
i = i + 1
|
||||
else if op == ']'
|
||||
if str.nth(memory, p)
|
||||
if memory[p]
|
||||
i = i - 1
|
||||
let closed: I64 = 0
|
||||
while i >= 0 & !(str.nth(src, i) == '[' & !closed)
|
||||
if str.nth(src, i) == ']'
|
||||
let closed: i64 = 0
|
||||
while i >= 0 & !(src[i] == '[' & !closed)
|
||||
if src[i] == ']'
|
||||
closed = closed + 1
|
||||
else if str.nth(src, i) == '['
|
||||
else if src[i] == '['
|
||||
closed = closed - 1
|
||||
i = i - 1
|
||||
|
||||
i = i + 1
|
||||
|
||||
mem.free(memory)
|
||||
@@ -1,31 +1,54 @@
|
||||
func main[] : I64
|
||||
// TODO: parse url
|
||||
let host: String = "devernay.free.fr"
|
||||
let path: String = "/hacks/chip8/C8TECH10.HTM"
|
||||
func main[argc: i64, argv: ptr] : i64
|
||||
if argc < 2
|
||||
dbg.panic("url missing")
|
||||
|
||||
let s: I64 = net.connect(host, 80)
|
||||
let url: str = mem.read64(argv + 8)
|
||||
|
||||
if str.len(url) <= 7
|
||||
dbg.panic("missing url scheme")
|
||||
|
||||
if !str.equal(str.substr(url, 0, 7), "http://")
|
||||
dbg.panic("invalid url scheme")
|
||||
|
||||
let url_len: i64 = str.len(url)
|
||||
let host_start: i64 = 7
|
||||
let i: i64 = host_start
|
||||
while i < url_len
|
||||
if url[i] == '/'
|
||||
break
|
||||
i = i + 1
|
||||
|
||||
let host: str = str.substr(url, host_start, i - host_start)
|
||||
let path: str = "/"
|
||||
if i < url_len
|
||||
path = str.substr(url, i, url_len - i)
|
||||
|
||||
let s: i64 = net.connect(host, 80)
|
||||
if s < 0
|
||||
dbg.panic("failed to connect")
|
||||
|
||||
let req: String = c.malloc(2048)
|
||||
c.snprintf(req, 2048, "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", path, host)
|
||||
c.send(s, req, c.strlen(req), 0)
|
||||
c.free(req)
|
||||
// very leaky
|
||||
let req: str = "GET "
|
||||
req = str.concat(req, path)
|
||||
req = str.concat(req, " HTTP/1.0\r\nHost: ")
|
||||
req = str.concat(req, host)
|
||||
req = str.concat(req, "\r\nConnection: close\r\n\r\n")
|
||||
net.send(s, req, str.len(req))
|
||||
mem.free(req)
|
||||
|
||||
let header_buf: Ptr = c.malloc(8192)
|
||||
let header_size: I64 = 0
|
||||
let found: Bool = false
|
||||
let end_index: I64 = -1
|
||||
let header_buf: str = mem.alloc(8192)
|
||||
let header_size: i64 = 0
|
||||
let found: bool = false
|
||||
let end_index: i64 = -1
|
||||
|
||||
while !found & header_size < 8192
|
||||
let n: I64 = 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
|
||||
break
|
||||
let current_size: I64 = header_size + n
|
||||
let i: I64 = 0
|
||||
let current_size: i64 = header_size + n
|
||||
i = 0
|
||||
while i <= current_size - 4
|
||||
let p: Ptr = header_buf + i
|
||||
if str.nth(p, 0) == 13 & str.nth(p, 1) == 10 & str.nth(p, 2) == 13 & str.nth(p, 3) == 10
|
||||
if header_buf[i] == 13 & header_buf[i + 1] == 10 & header_buf[i + 2] == 13 & header_buf[i + 3] == 10
|
||||
found = true
|
||||
end_index = i + 4
|
||||
break
|
||||
@@ -33,15 +56,15 @@ func main[] : I64
|
||||
header_size = current_size
|
||||
|
||||
if end_index < header_size
|
||||
c.write(1, header_buf + end_index, header_size - end_index)
|
||||
c.free(header_buf)
|
||||
io.print_sized(header_buf + end_index, header_size - end_index)
|
||||
mem.free(header_buf)
|
||||
|
||||
let buffer: Ptr = c.malloc(4096)
|
||||
let buffer: ptr = mem.alloc(4096)
|
||||
while true
|
||||
let n: I64 = c.read(s, buffer, 4096)
|
||||
let n: i64 = net.read(s, buffer, 4096)
|
||||
if n <= 0
|
||||
break
|
||||
c.write(1, buffer, n)
|
||||
c.free(buffer)
|
||||
io.print_sized(buffer, n)
|
||||
mem.free(buffer)
|
||||
|
||||
c.close(s)
|
||||
net.close(s)
|
||||
@@ -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,13 +0,0 @@
|
||||
func main[] : I64
|
||||
let out: I64 = 0
|
||||
|
||||
for a in 500..1000
|
||||
for b in 500..1000
|
||||
if a * b > out
|
||||
let s: String = str.from_i64(a * b)
|
||||
let s_rev: String = str.reverse(s)
|
||||
if c.strcmp(s, s_rev) == 0
|
||||
out = a * b
|
||||
c.free(s)
|
||||
c.free(s_rev)
|
||||
io.print_i64(out)
|
||||
@@ -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 = c.strlen(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
|
||||
let a: I64 = 0
|
||||
let b: I64 = 1
|
||||
func main[] : i64
|
||||
let a: i64 = 0
|
||||
let b: i64 = 1
|
||||
|
||||
while a < 100000
|
||||
io.print_i64(a)
|
||||
let temp: I64 = b
|
||||
io.println_i64(a)
|
||||
let temp: i64 = b
|
||||
b = a + b
|
||||
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)
|
||||
@@ -1,14 +1,14 @@
|
||||
func main[] : I64
|
||||
let answer: I64 = math.abs(math.urandom()) % 100
|
||||
func main[] : i64
|
||||
let answer: i64 = math.abs(os.urandom()) % 100
|
||||
|
||||
while true
|
||||
io.print("Guess a number: ")
|
||||
let guess: I64 = io.read_stdin() |> str.trim() |> str.parse_i64()
|
||||
io.println("Guess a number: ")
|
||||
let guess: i64 = io.read_line() |> str.trim() |> str.parse_i64()
|
||||
|
||||
if guess == answer
|
||||
io.print("You win!")
|
||||
io.println("You win!")
|
||||
break
|
||||
else if guess < answer
|
||||
io.print("Too low!")
|
||||
io.println("Too low!")
|
||||
else
|
||||
io.print("Too high!")
|
||||
io.println("Too high!")
|
||||
@@ -1,2 +1,2 @@
|
||||
func main[] : I64
|
||||
io.print("Hello, World!")
|
||||
func main[] : i64
|
||||
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: i64 = 0
|
||||
|
||||
for i in 0..array.size(l1)
|
||||
out = out + math.abs(array.nth(l1, i) - array.nth(l2, i))
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[l1: array, l2: array] : void
|
||||
let out: i64 = 0
|
||||
|
||||
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: i64 = 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: i64 = 0
|
||||
|
||||
for i in 0..array.size(data)
|
||||
if check(array.nth(data, i))
|
||||
out = out + 1
|
||||
else
|
||||
for j in 0..array.size(array.nth(data, i))
|
||||
let sliced: array = array.concat(array.slice(array.nth(data, i), 0, j), array.slice(array.nth(data, i), j + 1, array.size(array.nth(data, i)) - (j + 1)))
|
||||
|
||||
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: i64 = 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: i64 = 0
|
||||
|
||||
for x in 1..array.size(lines)-1
|
||||
for y in 1..str.len(array.nth(lines, x))-1
|
||||
if array.nth(lines, x)[y] == 'A'
|
||||
let s: 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: i64 = 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: i64 = 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: i64 = 0
|
||||
|
||||
for i in 0..array.size(equations)
|
||||
out = out + solve(["add", "mul"], array.nth(equations, i))
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[equations: array] : void
|
||||
let out: i64 = 0
|
||||
|
||||
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: i64 = 0
|
||||
let dial: i64 = 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: i64 = 0
|
||||
let dial: i64 = 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: i64 = 0
|
||||
|
||||
for i in 0..array.size(ranges)
|
||||
let parts: array = array.nth(ranges, i) |> str.split("-")
|
||||
let start: i64 = array.nth(parts, 0) |> str.parse_i64()
|
||||
let end: i64 = array.nth(parts, 1) |> str.parse_i64()
|
||||
|
||||
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: i64 = 0
|
||||
|
||||
for i in 0..array.size(ranges)
|
||||
let parts: array = array.nth(ranges, i) |> str.split("-")
|
||||
let start: i64 = array.nth(parts, 0) |> str.parse_i64()
|
||||
let end: i64 = array.nth(parts, 1) |> str.parse_i64()
|
||||
|
||||
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: i64 = 0
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
let line: str = array.nth(lines, i)
|
||||
|
||||
let largest: i64 = 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: i64 = 0
|
||||
let largest_idx: i64 = start
|
||||
let len: i64 = str.len(bank)
|
||||
|
||||
for i in (start)..(len-remaining+1)
|
||||
let v: i64 = bank[i] - '0'
|
||||
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: i64 = 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)
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : I64
|
||||
let sum: I64 = 0
|
||||
func main[] : i64
|
||||
let sum: i64 = 0
|
||||
|
||||
for i in 0..2000000
|
||||
if math.is_prime(i)
|
||||
sum = sum + i
|
||||
io.print_i64(sum)
|
||||
io.println_i64(sum)
|
||||
@@ -1,7 +1,7 @@
|
||||
func num_divisors[n: I64] : I64
|
||||
let end: I64 = math.isqrt(n)
|
||||
func num_divisors[n: i64] : i64
|
||||
let end: i64 = math.isqrt(n)
|
||||
|
||||
let out: I64 = 0
|
||||
let out: i64 = 0
|
||||
for i in 1..end+1
|
||||
if n % i == 0
|
||||
out = out + 2
|
||||
@@ -10,12 +10,12 @@ func num_divisors[n: I64] : I64
|
||||
out = out - 1
|
||||
return out
|
||||
|
||||
func main[] : I64
|
||||
let n: I64 = 0
|
||||
let i: I64 = 1
|
||||
func main[] : i64
|
||||
let n: i64 = 0
|
||||
let i: i64 = 1
|
||||
while true
|
||||
n = n + i
|
||||
if num_divisors(n) > 500
|
||||
io.print_i64(n)
|
||||
io.println_i64(n)
|
||||
break
|
||||
i = i + 1
|
||||
@@ -1,5 +1,6 @@
|
||||
func main[] : I64
|
||||
func main[] : i64
|
||||
// leaks a bit of memory but looks very cool
|
||||
37107287533 + 46376937677 + 74324986199 + 91942213363 + 23067588207 + 89261670696 + 28112879812 + 44274228917 + 47451445736 + 70386486105 + 62176457141 + 64906352462 + 92575867718 + 58203565325 + 80181199384 + 35398664372 + 86515506006 + 71693888707 + 54370070576 + 53282654108 + 36123272525 + 45876576172 + 17423706905 + 81142660418 + 51934325451 + 62467221648 + 15732444386 + 55037687525 + 18336384825 + 80386287592 + 78182833757 + 16726320100 + 48403098129 + 87086987551 + 59959406895 + 69793950679 + 41052684708 + 65378607361 + 35829035317 + 94953759765 + 88902802571 + 25267680276 + 36270218540 + 24074486908 + 91430288197 + 34413065578 + 23053081172 + 11487696932 + 63783299490 + 67720186971 + 95548255300 + 76085327132 + 37774242535 + 23701913275 + 29798860272 + 18495701454 + 38298203783 + 34829543829 + 40957953066 + 29746152185 + 41698116222 + 62467957194 + 23189706772 + 86188088225 + 11306739708 + 82959174767 + 97623331044 + 42846280183 + 55121603546 + 32238195734 + 75506164965 + 62177842752 + 32924185707 + 99518671430 + 73267460800 + 76841822524 + 97142617910 + 87783646182 + 10848802521 + 71329612474 + 62184073572 + 66627891981 + 60661826293 + 85786944089 + 66024396409 + 64913982680 + 16730939319 + 94809377245 + 78639167021 + 15368713711 + 40789923115 + 44889911501 + 41503128880 + 81234880673 + 82616570773 + 22918802058 + 77158542502 + 72107838435 + 20849603980 + 53503534226
|
||||
|> str.from_i64()
|
||||
|> 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
|
||||
return n / 2
|
||||
return n * 3 + 1
|
||||
|
||||
func collatz_seq[n: I64]: I64
|
||||
let i: I64 = 1
|
||||
func collatz_seq[n: i64]: i64
|
||||
let i: i64 = 1
|
||||
while n != 1
|
||||
n = collatz_num(n)
|
||||
i = i + 1
|
||||
return i
|
||||
|
||||
func main[] : I64
|
||||
let max: I64 = 0
|
||||
let max_index: I64 = 0
|
||||
func main[] : i64
|
||||
let max: i64 = 0
|
||||
let max_index: i64 = 0
|
||||
|
||||
for i in 1..1000000
|
||||
let seq: I64 = collatz_seq(i)
|
||||
let seq: i64 = collatz_seq(i)
|
||||
if seq > max
|
||||
max = seq
|
||||
max_index = i
|
||||
io.print_i64(max_index)
|
||||
io.println_i64(max_index)
|
||||
9
examples/puzzles/euler_05.zr
Normal file
9
examples/puzzles/euler_05.zr
Normal file
@@ -0,0 +1,9 @@
|
||||
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.println_i64(out)
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : I64
|
||||
let sum: I64 = 0
|
||||
func main[] : i64
|
||||
let sum: i64 = 0
|
||||
|
||||
for i in 0..1000
|
||||
if i % 5 == 0 | i % 3 == 0
|
||||
sum = sum + i
|
||||
io.print_i64(sum)
|
||||
io.println_i64(sum)
|
||||
13
examples/puzzles/euler_07.zr
Normal file
13
examples/puzzles/euler_07.zr
Normal file
@@ -0,0 +1,13 @@
|
||||
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.println_i64(sum)
|
||||
@@ -1,6 +1,6 @@
|
||||
func main[] : I64
|
||||
let n: I64 = 600851475143
|
||||
let f: I64 = 2
|
||||
func main[] : i64
|
||||
let n: i64 = 600851475143
|
||||
let f: i64 = 2
|
||||
|
||||
while f * f <= n
|
||||
if n % f == 0
|
||||
@@ -8,4 +8,4 @@ func main[] : I64
|
||||
else
|
||||
f = f + 1
|
||||
|
||||
io.print_i64(n)
|
||||
io.println_i64(n)
|
||||
13
examples/puzzles/euler_09.zr
Normal file
13
examples/puzzles/euler_09.zr
Normal file
@@ -0,0 +1,13 @@
|
||||
func main[] : i64
|
||||
let out: i64 = 0
|
||||
|
||||
for a in 500..1000
|
||||
for b in 500..1000
|
||||
if a * b > out
|
||||
let s: str = str.from_i64(a * b)
|
||||
let s_rev: str = str.reverse(s)
|
||||
if str.equal(s, s_rev)
|
||||
out = a * b
|
||||
mem.free(s)
|
||||
mem.free(s_rev)
|
||||
io.println_i64(out)
|
||||
6
examples/puzzles/euler_10.zr
Normal file
6
examples/puzzles/euler_10.zr
Normal file
@@ -0,0 +1,6 @@
|
||||
func main[] : i64
|
||||
let out: i64 = 1
|
||||
|
||||
for i in 1..21
|
||||
out = math.lcm(out, i)
|
||||
io.println_i64(out)
|
||||
@@ -1,11 +1,11 @@
|
||||
func main[] : I64
|
||||
let sum_of_squares: I64 = 0
|
||||
func main[] : i64
|
||||
let sum_of_squares: i64 = 0
|
||||
for i in 1..101
|
||||
sum_of_squares = sum_of_squares + i * i
|
||||
|
||||
let square_of_sum: I64 = 0
|
||||
let square_of_sum: i64 = 0
|
||||
for i in 1..101
|
||||
square_of_sum = square_of_sum + i
|
||||
square_of_sum = square_of_sum * square_of_sum
|
||||
|
||||
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
|
||||
let found: I64 = 0
|
||||
func main[] : i64
|
||||
let found: i64 = 0
|
||||
|
||||
let i: I64 = 1
|
||||
let i: i64 = 1
|
||||
while true
|
||||
if math.is_prime(i)
|
||||
found = found + 1
|
||||
if found == 10001
|
||||
io.print_i64(i)
|
||||
io.println_i64(i)
|
||||
break
|
||||
i = i + 1
|
||||
14
examples/puzzles/euler_13.zr
Normal file
14
examples/puzzles/euler_13.zr
Normal file
@@ -0,0 +1,14 @@
|
||||
func main[] : i64
|
||||
let n: str = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
|
||||
|
||||
let out: i64 = 0
|
||||
let max: i64 = str.len(n) - 13
|
||||
for i in 0..max
|
||||
let s: i64 = 1
|
||||
let j: i64 = 0
|
||||
while j < 13
|
||||
s = s * (n[i + j] - '0')
|
||||
j = j + 1
|
||||
if s > out
|
||||
out = s
|
||||
io.println_i64(out)
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : I64
|
||||
func main[] : i64
|
||||
for a in 1..1000
|
||||
for b in 1..1000
|
||||
let c: I64 = 1000 - b - a
|
||||
let c: i64 = 1000 - b - a
|
||||
if a * a + b * b == c * c
|
||||
io.print_i64(a * b * c)
|
||||
io.println_i64(a * b * c)
|
||||
return 0
|
||||
@@ -1,36 +1,15 @@
|
||||
func quicksort[arr: Array] : Void
|
||||
do_quicksort(arr, 0, array.size(arr)-1)
|
||||
|
||||
func do_quicksort[arr: Array, low: I64, high: I64] : Void
|
||||
if low < high
|
||||
let i: I64 = partition(arr, low, high)
|
||||
do_quicksort(arr, low, i - 1)
|
||||
do_quicksort(arr, i + 1, high)
|
||||
|
||||
func 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 main[] : I64
|
||||
let arr: Array = []
|
||||
func main[] : i64
|
||||
let arr: array = []
|
||||
for i in 0..10
|
||||
array.push(arr, math.abs(math.urandom() % 1000))
|
||||
array.push(arr, math.abs(os.urandom() % 1000))
|
||||
|
||||
for i in 0..array.size(arr)
|
||||
io.print_i64(arr[i])
|
||||
io.print("------------")
|
||||
io.println_i64(array.nth(arr, i))
|
||||
io.println("------------")
|
||||
|
||||
quicksort(arr)
|
||||
alg.quicksort(arr)
|
||||
|
||||
for i in 0..array.size(arr)
|
||||
io.print_i64(arr[i])
|
||||
io.println_i64(array.nth(arr, i))
|
||||
|
||||
array.free(arr)
|
||||
35
examples/raylib.zr
Normal file
35
examples/raylib.zr
Normal file
@@ -0,0 +1,35 @@
|
||||
// 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
|
||||
let x: i64 = 200
|
||||
let y: i64 = 200
|
||||
|
||||
InitWindow(800, 600, "Hello, World!")
|
||||
SetTargetFPS(60)
|
||||
|
||||
while !WindowShouldClose()
|
||||
if IsKeyDown(87) & 255 // W
|
||||
y = y - 10
|
||||
if IsKeyDown(83) & 255 // S
|
||||
y = y + 10
|
||||
if IsKeyDown(65) & 255 // A
|
||||
x = x - 10
|
||||
if IsKeyDown(68) & 255 // D
|
||||
x = x + 10
|
||||
|
||||
BeginDrawing()
|
||||
ClearBackground(0xffffffff)
|
||||
DrawRectangle(x, y, 100, 100, 0xff0000ff)
|
||||
EndDrawing()
|
||||
|
||||
CloseWindow()
|
||||
@@ -1,38 +1,39 @@
|
||||
func rule110_step[state: Array] : Array
|
||||
let new_state: Array = []
|
||||
let state_len: I64 = array.size(state)
|
||||
func rule110_step[state: array] : array
|
||||
let new_state: array = []
|
||||
let state_len: i64 = array.size(state)
|
||||
|
||||
for i in 0..state_len
|
||||
let left: Bool = false
|
||||
let left: bool = false
|
||||
if i - 1 >= 0
|
||||
left = state[i-1]
|
||||
let center: Bool = state[i]
|
||||
let right: Bool = false
|
||||
left = array.nth(state, i - 1)
|
||||
let center: bool = array.nth(state, i)
|
||||
let right: bool = false
|
||||
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)))
|
||||
|
||||
return new_state
|
||||
|
||||
func to_str[state: Array]: String
|
||||
let out: String = malloc(array.size(state))
|
||||
func print_state[state: array]: void
|
||||
for i in 0..array.size(state)
|
||||
if state[i]
|
||||
str.set(out, i, '#')
|
||||
if array.nth(state, i)
|
||||
io.print_char('#')
|
||||
else
|
||||
str.set(out, i, ' ')
|
||||
return out
|
||||
io.print_char(' ')
|
||||
io.println("")
|
||||
|
||||
func main[] : I64
|
||||
let SIZE: I64 = 60
|
||||
func main[] : i64
|
||||
let SIZE: i64 = 60
|
||||
|
||||
let state: Array = []
|
||||
let state: array = []
|
||||
for i in 0..SIZE
|
||||
array.push(state, false)
|
||||
array.push(state, true)
|
||||
|
||||
io.print(to_str(state))
|
||||
print_state(state)
|
||||
for i in 0..SIZE
|
||||
state = rule110_step(state)
|
||||
io.print(to_str(state))
|
||||
print_state(state)
|
||||
|
||||
array.free(state)
|
||||
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: i64 = 0
|
||||
let db: i64 = 0
|
||||
let stmt: i64 = 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,13 +0,0 @@
|
||||
func main[] : I64
|
||||
let s: I64 = net.connect("devernay.free.fr", 80)
|
||||
|
||||
let req: String = "GET /hacks/chip8/C8TECH10.HTM HTTP/1.0\r\nHost: devernay.free.fr\r\nConnection: close\r\n\r\n"
|
||||
c.send(s, req, c.strlen(req), 0)
|
||||
|
||||
let resp: String = c.malloc(60000)
|
||||
let n: I64 = c.read(s, resp, 60000)
|
||||
str.set(resp, n, 0)
|
||||
io.print(resp)
|
||||
c.free(resp)
|
||||
|
||||
c.close(s)
|
||||
@@ -1,12 +1,13 @@
|
||||
func main[] : I64
|
||||
let s: I64 = net.listen(8000)
|
||||
func main[] : i64
|
||||
let host: i64 = net.pack_addr(127, 0, 0, 1)
|
||||
let s: i64 = net.listen(host, 8000)
|
||||
|
||||
let resp: String = c.malloc(60000)
|
||||
let resp: str = mem.alloc(60000)
|
||||
while true
|
||||
let c: I64 = c.accept(s, 0, 0)
|
||||
if c < 0
|
||||
let conn: i64 = net.accept(s)
|
||||
if conn < 0
|
||||
continue
|
||||
|
||||
let n: I64 = c.read(c, resp, 60000)
|
||||
c.send(c, resp, n, 0)
|
||||
c.close(c)
|
||||
let n: i64 = net.read(conn, resp, 60000)
|
||||
net.send(conn, resp, n)
|
||||
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,7 +6,7 @@ use crate::{
|
||||
};
|
||||
|
||||
pub struct Analyzer {
|
||||
pub functions: HashMap<String, usize>,
|
||||
pub functions: HashMap<String, i32>,
|
||||
}
|
||||
|
||||
impl Analyzer {
|
||||
@@ -22,12 +22,14 @@ impl Analyzer {
|
||||
params,
|
||||
return_type: _,
|
||||
body: _,
|
||||
exported: _,
|
||||
} = stmt
|
||||
{
|
||||
if self.functions.contains_key(&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(())
|
||||
}
|
||||
@@ -65,9 +67,10 @@ impl Analyzer {
|
||||
params: _,
|
||||
return_type,
|
||||
body,
|
||||
exported: _,
|
||||
} => {
|
||||
if name.lexeme == "main" && return_type.lexeme != "I64" {
|
||||
return error!(&name.loc, "main must return I64");
|
||||
if name.lexeme == "main" && return_type.lexeme != "i64" {
|
||||
return error!(&name.loc, "main must return i64");
|
||||
}
|
||||
|
||||
self.analyze_stmt(body)?;
|
||||
@@ -87,6 +90,12 @@ impl Analyzer {
|
||||
}
|
||||
Stmt::Break => {}
|
||||
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(())
|
||||
}
|
||||
@@ -111,20 +120,31 @@ impl Analyzer {
|
||||
paren,
|
||||
args,
|
||||
} => {
|
||||
let callee = match callee.as_ref() {
|
||||
Expr::Variable(name) => name.lexeme.clone(),
|
||||
_ => return error!(&paren.loc, "tried to call a non-constant expression"),
|
||||
};
|
||||
|
||||
if let Some(arity) = self.functions.get(&callee) {
|
||||
if *arity != args.len() {
|
||||
if let Expr::Variable(callee_name) = *callee.clone() {
|
||||
if callee_name.lexeme.starts_with("_builtin_")
|
||||
|| self.functions.contains_key(&callee_name.lexeme)
|
||||
{
|
||||
// its a function (defined/builtin/extern)
|
||||
if let Some(arity) = self.functions.get(&callee_name.lexeme) {
|
||||
if *arity >= 0 && *arity != args.len() as i32 {
|
||||
return error!(
|
||||
&paren.loc,
|
||||
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 {
|
||||
// TODO: cant error here since we dont analyze externs/builtins
|
||||
// its a variable containing function address
|
||||
self.analyze_expr(callee)?;
|
||||
}
|
||||
} else {
|
||||
// its an expression that evalutes to function address
|
||||
self.analyze_expr(callee)?;
|
||||
}
|
||||
|
||||
for arg in args {
|
||||
@@ -140,6 +160,9 @@ impl Analyzer {
|
||||
self.analyze_expr(expr)?;
|
||||
self.analyze_expr(index)?;
|
||||
}
|
||||
Expr::AddrOf { op: _, expr } => {
|
||||
self.analyze_expr(expr)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,501 +0,0 @@
|
||||
use std::{collections::HashMap, fmt::Write};
|
||||
|
||||
use crate::{
|
||||
parser::{Expr, Stmt},
|
||||
tokenizer::{TokenType, ZernError, error},
|
||||
};
|
||||
|
||||
pub struct Var {
|
||||
// pub var_type: String,
|
||||
pub stack_offset: usize,
|
||||
}
|
||||
|
||||
pub struct Env {
|
||||
scopes: Vec<HashMap<String, Var>>,
|
||||
next_offset: usize,
|
||||
loop_begin_label: String,
|
||||
loop_end_label: String,
|
||||
}
|
||||
|
||||
impl Env {
|
||||
pub fn new() -> Env {
|
||||
Env {
|
||||
scopes: vec![HashMap::new()],
|
||||
next_offset: 24,
|
||||
loop_begin_label: String::new(),
|
||||
loop_end_label: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_scope(&mut self) {
|
||||
self.scopes.push(HashMap::new());
|
||||
}
|
||||
|
||||
pub fn pop_scope(&mut self) {
|
||||
self.scopes.pop();
|
||||
}
|
||||
|
||||
pub fn define_var(&mut self, name: String, _var_type: String) -> usize {
|
||||
let offset = self.next_offset;
|
||||
self.next_offset += 16;
|
||||
self.scopes.last_mut().unwrap().insert(name, Var {
|
||||
stack_offset: offset,
|
||||
});
|
||||
offset
|
||||
}
|
||||
|
||||
pub fn get_var(&self, name: &str) -> Option<&Var> {
|
||||
for scope in self.scopes.iter().rev() {
|
||||
if let Some(var) = scope.get(name) {
|
||||
return Some(var);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! emit {
|
||||
($($arg:tt)*) => {
|
||||
let _ = writeln!($($arg)*);
|
||||
};
|
||||
}
|
||||
|
||||
static REGISTERS: [&str; 8] = ["a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7"];
|
||||
|
||||
pub struct CodegenRISCV {
|
||||
output: String,
|
||||
data_section: String,
|
||||
label_counter: usize,
|
||||
data_counter: usize,
|
||||
}
|
||||
|
||||
impl CodegenRISCV {
|
||||
pub fn new() -> CodegenRISCV {
|
||||
CodegenRISCV {
|
||||
output: String::new(),
|
||||
data_section: String::new(),
|
||||
label_counter: 0,
|
||||
data_counter: 1,
|
||||
}
|
||||
}
|
||||
|
||||
fn label(&mut self) -> String {
|
||||
self.label_counter += 1;
|
||||
format!(".L{}", self.label_counter)
|
||||
}
|
||||
|
||||
pub fn get_output(&self) -> String {
|
||||
format!(
|
||||
".section .data
|
||||
{}{}",
|
||||
self.data_section, self.output
|
||||
)
|
||||
}
|
||||
|
||||
pub fn emit_prologue(&mut self) -> Result<(), ZernError> {
|
||||
emit!(
|
||||
&mut self.output,
|
||||
".section .note.GNU-stack,\"\",@progbits
|
||||
|
||||
.section .text"
|
||||
);
|
||||
|
||||
// take that rustfmt
|
||||
for name in "malloc,calloc,realloc,free,puts,printf,sprintf,snprintf,strtol,strlen,strcmp,strcat,strcpy,strdup,strncpy,syscall,fopen,fseek,ftell,fread,fwrite,fclose,rewind,system,opendir,readdir,closedir,exit,gettimeofday,connect,socket,send,write,read,close,bind,listen,accept,getchar,gethostbyname".split(",")
|
||||
{
|
||||
emit!(&mut self.output, ".extern {}", name);
|
||||
emit!(&mut self.output, ".equ c.{}, {}", name, name);
|
||||
}
|
||||
|
||||
emit!(
|
||||
&mut self.output,
|
||||
"
|
||||
.section .text._builtin_deref8
|
||||
_builtin_deref8:
|
||||
lbu a0, 0(a0)
|
||||
ret
|
||||
|
||||
.section .text._builtin_deref64
|
||||
_builtin_deref64:
|
||||
ld a0, 0(a0)
|
||||
ret
|
||||
|
||||
.section .text._builtin_set8
|
||||
_builtin_set8:
|
||||
sb a1, 0(a0)
|
||||
ret
|
||||
|
||||
.section .text._builtin_set64
|
||||
_builtin_set64:
|
||||
sd a1, 0(a0)
|
||||
ret
|
||||
|
||||
.section .text._builtin_lshift
|
||||
_builtin_lshift:
|
||||
sll a0, a0, a1
|
||||
ret
|
||||
|
||||
.section .text._builtin_rshift
|
||||
_builtin_rshift:
|
||||
sra a0, a0, a1
|
||||
ret
|
||||
"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn compile_stmt(&mut self, env: &mut Env, stmt: Stmt) -> Result<(), ZernError> {
|
||||
match stmt {
|
||||
Stmt::Expression(expr) => self.compile_expr(env, expr)?,
|
||||
Stmt::Let {
|
||||
name,
|
||||
var_type,
|
||||
initializer,
|
||||
} => {
|
||||
// TODO: move to analyzer
|
||||
if env.get_var(&name.lexeme).is_some() {
|
||||
return error!(
|
||||
name.loc,
|
||||
format!("variable already defined: {}", &name.lexeme)
|
||||
);
|
||||
}
|
||||
|
||||
self.compile_expr(env, initializer)?;
|
||||
let offset = env.define_var(name.lexeme.clone(), var_type.lexeme);
|
||||
emit!(&mut self.output, " sd a0, -{}(fp)", offset);
|
||||
}
|
||||
Stmt::Block(statements) => {
|
||||
env.push_scope();
|
||||
for stmt in statements {
|
||||
self.compile_stmt(env, stmt)?;
|
||||
}
|
||||
env.pop_scope();
|
||||
}
|
||||
Stmt::If {
|
||||
condition,
|
||||
then_branch,
|
||||
else_branch,
|
||||
} => {
|
||||
let else_label = self.label();
|
||||
let end_label = self.label();
|
||||
|
||||
self.compile_expr(env, condition)?;
|
||||
emit!(&mut self.output, " beqz a0, {}", else_label);
|
||||
self.compile_stmt(env, *then_branch.clone())?;
|
||||
emit!(&mut self.output, " j {}", end_label);
|
||||
emit!(&mut self.output, "{}:", else_label);
|
||||
self.compile_stmt(env, *else_branch.clone())?;
|
||||
emit!(&mut self.output, "{}:", end_label);
|
||||
}
|
||||
Stmt::While { condition, body } => {
|
||||
let old_loop_begin_label = env.loop_begin_label.clone();
|
||||
let old_loop_end_label = env.loop_end_label.clone();
|
||||
env.loop_begin_label = self.label();
|
||||
env.loop_end_label = self.label();
|
||||
|
||||
emit!(&mut self.output, "{}:", env.loop_begin_label);
|
||||
self.compile_expr(env, condition)?;
|
||||
emit!(&mut self.output, " beqz a0, {}", env.loop_end_label);
|
||||
self.compile_stmt(env, *body.clone())?;
|
||||
emit!(&mut self.output, " j {}", env.loop_begin_label);
|
||||
emit!(&mut self.output, "{}:", env.loop_end_label);
|
||||
|
||||
env.loop_begin_label = old_loop_begin_label;
|
||||
env.loop_end_label = old_loop_end_label;
|
||||
}
|
||||
Stmt::Function {
|
||||
name,
|
||||
params,
|
||||
return_type: _,
|
||||
body,
|
||||
} => {
|
||||
if name.lexeme == "main" {
|
||||
emit!(&mut self.output, ".globl {}", name.lexeme);
|
||||
}
|
||||
emit!(&mut self.output, ".section .text.{}", name.lexeme);
|
||||
emit!(&mut self.output, "{}:", name.lexeme);
|
||||
emit!(&mut self.output, " addi sp, sp, -16");
|
||||
emit!(&mut self.output, " sd ra, 8(sp)");
|
||||
emit!(&mut self.output, " sd fp, 0(sp)");
|
||||
emit!(&mut self.output, " addi fp, sp, 16");
|
||||
emit!(&mut self.output, " addi sp, sp, -256"); // TODO
|
||||
|
||||
for (i, param) in params.iter().enumerate() {
|
||||
let offset = env
|
||||
.define_var(param.var_name.lexeme.clone(), param.var_type.lexeme.clone());
|
||||
let reg = match REGISTERS.get(i) {
|
||||
Some(x) => x,
|
||||
None => return error!(&name.loc, "only up to 8 params allowed"),
|
||||
};
|
||||
emit!(&mut self.output, " sd {}, -{}(fp)", reg, offset);
|
||||
}
|
||||
|
||||
self.compile_stmt(env, *body)?;
|
||||
|
||||
if name.lexeme == "main" {
|
||||
emit!(&mut self.output, " li a0, 0");
|
||||
}
|
||||
|
||||
emit!(&mut self.output, " addi sp, fp, -16");
|
||||
emit!(&mut self.output, " ld ra, 8(sp)");
|
||||
emit!(&mut self.output, " ld fp, 0(sp)");
|
||||
emit!(&mut self.output, " addi sp, sp, 16");
|
||||
emit!(&mut self.output, " ret");
|
||||
}
|
||||
Stmt::Return(expr) => {
|
||||
self.compile_expr(env, expr)?;
|
||||
emit!(&mut self.output, " addi sp, fp, -16");
|
||||
emit!(&mut self.output, " ld ra, 8(sp)");
|
||||
emit!(&mut self.output, " ld fp, 0(sp)");
|
||||
emit!(&mut self.output, " addi sp, sp, 16");
|
||||
emit!(&mut self.output, " ret");
|
||||
}
|
||||
Stmt::For {
|
||||
var,
|
||||
start,
|
||||
end,
|
||||
body,
|
||||
} => {
|
||||
let old_loop_begin_label = env.loop_begin_label.clone();
|
||||
let old_loop_end_label = env.loop_end_label.clone();
|
||||
env.loop_begin_label = self.label();
|
||||
env.loop_end_label = self.label();
|
||||
|
||||
env.push_scope();
|
||||
let offset = env.define_var(var.lexeme, "I64".into());
|
||||
|
||||
self.compile_expr(env, start)?;
|
||||
emit!(&mut self.output, " sd a0, -{}(fp)", offset);
|
||||
emit!(&mut self.output, "{}:", env.loop_begin_label);
|
||||
emit!(&mut self.output, " ld a0, -{}(fp)", offset);
|
||||
emit!(&mut self.output, " addi sp, sp, -16");
|
||||
emit!(&mut self.output, " sd a0, 0(sp)");
|
||||
self.compile_expr(env, end)?;
|
||||
emit!(&mut self.output, " ld a1, 0(sp)");
|
||||
emit!(&mut self.output, " addi sp, sp, 16");
|
||||
emit!(&mut self.output, " bge a1, a0, {}", env.loop_end_label);
|
||||
self.compile_stmt(env, *body)?;
|
||||
emit!(&mut self.output, " ld a0, -{}(fp)", offset);
|
||||
emit!(&mut self.output, " addi a0, a0, 1");
|
||||
emit!(&mut self.output, " sd a0, -{}(fp)", offset);
|
||||
emit!(&mut self.output, " j {}", env.loop_begin_label);
|
||||
emit!(&mut self.output, "{}:", env.loop_end_label);
|
||||
env.pop_scope();
|
||||
|
||||
env.loop_begin_label = old_loop_begin_label;
|
||||
env.loop_end_label = old_loop_end_label;
|
||||
}
|
||||
Stmt::Break => {
|
||||
emit!(&mut self.output, " j {}", env.loop_end_label);
|
||||
}
|
||||
Stmt::Continue => {
|
||||
// TODO: skips incrementing when used in a for loop
|
||||
emit!(&mut self.output, " j {}", env.loop_begin_label);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn compile_expr(&mut self, env: &mut Env, expr: Expr) -> Result<(), ZernError> {
|
||||
match expr {
|
||||
Expr::Binary { left, op, right } => {
|
||||
self.compile_expr(env, *left)?;
|
||||
emit!(&mut self.output, " addi sp, sp, -16");
|
||||
emit!(&mut self.output, " sd a0, 0(sp)");
|
||||
self.compile_expr(env, *right)?;
|
||||
emit!(&mut self.output, " mv a1, a0");
|
||||
emit!(&mut self.output, " ld a0, 0(sp)");
|
||||
emit!(&mut self.output, " addi sp, sp, 16");
|
||||
|
||||
match op.token_type {
|
||||
TokenType::Plus => {
|
||||
emit!(&mut self.output, " add a0, a0, a1");
|
||||
}
|
||||
TokenType::Minus => {
|
||||
emit!(&mut self.output, " sub a0, a0, a1");
|
||||
}
|
||||
TokenType::Star => {
|
||||
emit!(&mut self.output, " mul a0, a0, a1");
|
||||
}
|
||||
TokenType::Slash => {
|
||||
emit!(&mut self.output, " div a0, a0, a1");
|
||||
}
|
||||
TokenType::Mod => {
|
||||
emit!(&mut self.output, " rem a0, a0, a1");
|
||||
}
|
||||
TokenType::Xor => {
|
||||
emit!(&mut self.output, " xor a0, a0, a1");
|
||||
}
|
||||
TokenType::And => {
|
||||
emit!(&mut self.output, " and a0, a0, a1");
|
||||
}
|
||||
TokenType::Or => {
|
||||
emit!(&mut self.output, " or a0, a0, a1");
|
||||
}
|
||||
TokenType::DoubleEqual => {
|
||||
emit!(&mut self.output, " xor a0, a0, a1");
|
||||
emit!(&mut self.output, " seqz a0, a0");
|
||||
}
|
||||
TokenType::NotEqual => {
|
||||
emit!(&mut self.output, " xor a0, a0, a1");
|
||||
emit!(&mut self.output, " snez a0, a0");
|
||||
}
|
||||
TokenType::Greater => {
|
||||
emit!(&mut self.output, " slt a0, a1, a0");
|
||||
}
|
||||
TokenType::GreaterEqual => {
|
||||
emit!(&mut self.output, " slt a0, a0, a1");
|
||||
emit!(&mut self.output, " xori a0, a0, 1");
|
||||
}
|
||||
TokenType::Less => {
|
||||
emit!(&mut self.output, " slt a0, a0, a1");
|
||||
}
|
||||
TokenType::LessEqual => {
|
||||
emit!(&mut self.output, " slt a0, a1, a0");
|
||||
emit!(&mut self.output, " xori a0, a0, 1");
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
Expr::Grouping(expr) => self.compile_expr(env, *expr)?,
|
||||
Expr::Literal(token) => match token.token_type {
|
||||
TokenType::Number => {
|
||||
emit!(&mut self.output, " li a0, {}", token.lexeme);
|
||||
}
|
||||
TokenType::Char => {
|
||||
emit!(
|
||||
&mut self.output,
|
||||
" li a0, {}",
|
||||
token.lexeme.chars().nth(1).unwrap() as u8
|
||||
);
|
||||
}
|
||||
TokenType::String => {
|
||||
// TODO: actual string parsing in the tokenizer
|
||||
let value = &token.lexeme[1..token.lexeme.len() - 1]
|
||||
.replace("\\n", "\n")
|
||||
.replace("\\r", "\r")
|
||||
.replace("\\t", "\t")
|
||||
.replace("\\033", "\x1b")
|
||||
.replace("\\0", "\0");
|
||||
|
||||
let label = format!("str_{:03}", self.data_counter);
|
||||
|
||||
if value.is_empty() {
|
||||
emit!(&mut self.data_section, " {}: .byte 0", label);
|
||||
} else {
|
||||
let charcodes = value
|
||||
.chars()
|
||||
.map(|x| (x as u8).to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(",");
|
||||
emit!(
|
||||
&mut self.data_section,
|
||||
" {}: .byte {},0",
|
||||
label,
|
||||
charcodes,
|
||||
);
|
||||
}
|
||||
emit!(&mut self.output, " la a0, {}", label);
|
||||
self.data_counter += 1;
|
||||
}
|
||||
TokenType::True => {
|
||||
emit!(&mut self.output, " li a0, 1");
|
||||
}
|
||||
TokenType::False => {
|
||||
emit!(&mut self.output, " li a0, 0");
|
||||
}
|
||||
_ => unreachable!(),
|
||||
},
|
||||
Expr::Unary { op, right } => {
|
||||
self.compile_expr(env, *right)?;
|
||||
match op.token_type {
|
||||
TokenType::Minus => {
|
||||
emit!(&mut self.output, " sub a0, x0, a0");
|
||||
}
|
||||
TokenType::Bang => {
|
||||
emit!(&mut self.output, " seqz a0, a0");
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
Expr::Variable(name) => {
|
||||
// TODO: move to analyzer
|
||||
let var = match env.get_var(&name.lexeme) {
|
||||
Some(x) => x,
|
||||
None => {
|
||||
return error!(name.loc, format!("undefined variable: {}", &name.lexeme));
|
||||
}
|
||||
};
|
||||
emit!(&mut self.output, " ld a0, -{}(fp)", var.stack_offset);
|
||||
}
|
||||
Expr::Assign { name, value } => {
|
||||
self.compile_expr(env, *value)?;
|
||||
|
||||
// TODO: move to analyzer
|
||||
let var = match env.get_var(&name.lexeme) {
|
||||
Some(x) => x,
|
||||
None => {
|
||||
return error!(name.loc, format!("undefined variable: {}", &name.lexeme));
|
||||
}
|
||||
};
|
||||
emit!(&mut self.output, " sd a0, -{}(fp)", var.stack_offset);
|
||||
}
|
||||
Expr::Call {
|
||||
callee,
|
||||
paren,
|
||||
args,
|
||||
} => {
|
||||
let callee = match *callee {
|
||||
Expr::Variable(name) => name.lexeme,
|
||||
_ => 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, " addi sp, sp, -16");
|
||||
emit!(&mut self.output, " sd a0, 0(sp)");
|
||||
}
|
||||
|
||||
for i in (0..args.len()).rev() {
|
||||
let reg = match REGISTERS.get(i) {
|
||||
Some(x) => x,
|
||||
None => return error!(&paren.loc, "only up to 8 args allowed"),
|
||||
};
|
||||
emit!(&mut self.output, " ld {}, 0(sp)", reg);
|
||||
emit!(&mut self.output, " addi sp, sp, 16");
|
||||
}
|
||||
|
||||
emit!(&mut self.output, " call {}", callee);
|
||||
}
|
||||
Expr::ArrayLiteral(exprs) => {
|
||||
emit!(&mut self.output, " call array.new");
|
||||
emit!(&mut self.output, " addi sp, sp, -16");
|
||||
emit!(&mut self.output, " sd a0, 0(sp)");
|
||||
|
||||
for expr in exprs {
|
||||
self.compile_expr(env, expr)?;
|
||||
emit!(&mut self.output, " mv a1, a0");
|
||||
emit!(&mut self.output, " ld a0, 0(sp)");
|
||||
emit!(&mut self.output, " sd a0, 0(sp)");
|
||||
emit!(&mut self.output, " call array.push");
|
||||
}
|
||||
emit!(&mut self.output, " ld a0, 0(sp)");
|
||||
emit!(&mut self.output, " addi sp, sp, 16");
|
||||
}
|
||||
Expr::Index { expr, index } => {
|
||||
self.compile_expr(env, *expr)?;
|
||||
emit!(&mut self.output, " addi sp, sp, -16");
|
||||
emit!(&mut self.output, " sd a0, 0(sp)");
|
||||
self.compile_expr(env, *index)?;
|
||||
emit!(&mut self.output, " ld t0, 0(sp)");
|
||||
emit!(&mut self.output, " addi sp, sp, 16");
|
||||
emit!(&mut self.output, " ld t0, 0(t0)");
|
||||
emit!(&mut self.output, " li t1, 8");
|
||||
emit!(&mut self.output, " mul t2, a0, t1");
|
||||
emit!(&mut self.output, " add t0, t0, t2");
|
||||
emit!(&mut self.output, " ld a0, 0(t0)");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
use std::{collections::HashMap, fmt::Write};
|
||||
|
||||
use crate::{
|
||||
analyzer::Analyzer,
|
||||
parser::{Expr, Stmt},
|
||||
tokenizer::{TokenType, ZernError, error},
|
||||
};
|
||||
|
||||
pub struct Var {
|
||||
// pub var_type: String,
|
||||
pub stack_offset: usize,
|
||||
}
|
||||
|
||||
@@ -38,9 +38,12 @@ impl Env {
|
||||
pub fn define_var(&mut self, name: String, _var_type: String) -> usize {
|
||||
let offset = self.next_offset;
|
||||
self.next_offset += 8;
|
||||
self.scopes.last_mut().unwrap().insert(name, Var {
|
||||
self.scopes.last_mut().unwrap().insert(
|
||||
name,
|
||||
Var {
|
||||
stack_offset: offset,
|
||||
});
|
||||
},
|
||||
);
|
||||
offset
|
||||
}
|
||||
|
||||
@@ -62,20 +65,22 @@ macro_rules! emit {
|
||||
|
||||
static REGISTERS: [&str; 6] = ["rdi", "rsi", "rdx", "rcx", "r8", "r9"];
|
||||
|
||||
pub struct CodegenX86_64 {
|
||||
pub struct CodegenX86_64<'a> {
|
||||
output: String,
|
||||
data_section: String,
|
||||
label_counter: usize,
|
||||
data_counter: usize,
|
||||
pub analyzer: &'a mut Analyzer,
|
||||
}
|
||||
|
||||
impl CodegenX86_64 {
|
||||
pub fn new() -> CodegenX86_64 {
|
||||
impl<'a> CodegenX86_64<'a> {
|
||||
pub fn new(analyzer: &'a mut Analyzer) -> CodegenX86_64<'a> {
|
||||
CodegenX86_64 {
|
||||
output: String::new(),
|
||||
data_section: String::new(),
|
||||
label_counter: 0,
|
||||
data_counter: 1,
|
||||
analyzer,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,11 +90,7 @@ impl CodegenX86_64 {
|
||||
}
|
||||
|
||||
pub fn get_output(&self) -> String {
|
||||
format!(
|
||||
"section .data
|
||||
{}{}",
|
||||
self.data_section, self.output
|
||||
)
|
||||
format!("section .data\n{}{}", self.data_section, self.output)
|
||||
}
|
||||
|
||||
pub fn emit_prologue(&mut self) -> Result<(), ZernError> {
|
||||
@@ -98,28 +99,14 @@ impl CodegenX86_64 {
|
||||
"section .note.GNU-stack
|
||||
db 0
|
||||
|
||||
section .text
|
||||
"
|
||||
);
|
||||
|
||||
// take that rustfmt
|
||||
for name in "malloc,calloc,realloc,free,puts,printf,sprintf,snprintf,strtol,strlen,strcmp,strcat,strcpy,strdup,strncpy,syscall,fopen,fseek,ftell,fread,fwrite,fclose,rewind,system,opendir,readdir,closedir,exit,gettimeofday,connect,socket,send,write,read,close,bind,listen,accept,getchar,gethostbyname".split(",")
|
||||
{
|
||||
emit!(&mut self.output, "extern {}", name);
|
||||
emit!(&mut self.output, "c.{} equ {}", name, name);
|
||||
}
|
||||
|
||||
emit!(
|
||||
&mut self.output,
|
||||
"
|
||||
section .text._builtin_deref8
|
||||
_builtin_deref8:
|
||||
section .text._builtin_read8
|
||||
_builtin_read8:
|
||||
xor rax, rax
|
||||
mov al, byte [rdi]
|
||||
ret
|
||||
|
||||
section .text._builtin_deref64
|
||||
_builtin_deref64:
|
||||
section .text._builtin_read64
|
||||
_builtin_read64:
|
||||
mov rax, qword [rdi]
|
||||
ret
|
||||
|
||||
@@ -133,18 +120,22 @@ _builtin_set64:
|
||||
mov [rdi], rsi
|
||||
ret
|
||||
|
||||
section .text._builtin_lshift
|
||||
_builtin_lshift:
|
||||
mov rcx, rsi
|
||||
section .text._builtin_syscall
|
||||
_builtin_syscall:
|
||||
mov rax, rdi
|
||||
shl rax, cl
|
||||
mov rdi, rsi
|
||||
mov rsi, rdx
|
||||
mov rdx, rcx
|
||||
mov r10, r8
|
||||
mov r8, r9
|
||||
mov r9, [rsp+8]
|
||||
syscall
|
||||
ret
|
||||
|
||||
section .text._builtin_rshift
|
||||
_builtin_rshift:
|
||||
mov rcx, rsi
|
||||
mov rax, rdi
|
||||
sar rax, cl
|
||||
section .text._builtin_environ
|
||||
_builtin_environ:
|
||||
extern environ
|
||||
mov rax, [rel environ]
|
||||
ret
|
||||
"
|
||||
);
|
||||
@@ -217,8 +208,9 @@ _builtin_rshift:
|
||||
params,
|
||||
return_type: _,
|
||||
body,
|
||||
exported,
|
||||
} => {
|
||||
if name.lexeme == "main" {
|
||||
if exported || name.lexeme == "main" {
|
||||
emit!(&mut self.output, "global {}", name.lexeme);
|
||||
}
|
||||
emit!(&mut self.output, "section .text.{}", name.lexeme);
|
||||
@@ -239,14 +231,15 @@ _builtin_rshift:
|
||||
|
||||
self.compile_stmt(env, *body)?;
|
||||
|
||||
if name.lexeme == "main" {
|
||||
// fallback to null
|
||||
// very hacky but works
|
||||
if !self.output.trim_end().ends_with(" ret") {
|
||||
emit!(&mut self.output, " mov rax, 0");
|
||||
}
|
||||
|
||||
emit!(&mut self.output, " mov rsp, rbp");
|
||||
emit!(&mut self.output, " pop rbp");
|
||||
emit!(&mut self.output, " ret");
|
||||
}
|
||||
}
|
||||
Stmt::Return(expr) => {
|
||||
self.compile_expr(env, expr)?;
|
||||
emit!(&mut self.output, " mov rsp, rbp");
|
||||
@@ -265,7 +258,7 @@ _builtin_rshift:
|
||||
env.loop_end_label = self.label();
|
||||
|
||||
env.push_scope();
|
||||
let offset = env.define_var(var.lexeme, "I64".into());
|
||||
let offset = env.define_var(var.lexeme, "i64".into());
|
||||
|
||||
self.compile_expr(env, start)?;
|
||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
||||
@@ -294,6 +287,9 @@ _builtin_rshift:
|
||||
// TODO: skips incrementing when used in a for loop
|
||||
emit!(&mut self.output, " jmp {}", env.loop_begin_label);
|
||||
}
|
||||
Stmt::Extern(name) => {
|
||||
emit!(&mut self.output, "extern {}", name.lexeme);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -329,10 +325,10 @@ _builtin_rshift:
|
||||
TokenType::Xor => {
|
||||
emit!(&mut self.output, " xor rax, rbx");
|
||||
}
|
||||
TokenType::And => {
|
||||
TokenType::BitAnd => {
|
||||
emit!(&mut self.output, " and rax, rbx");
|
||||
}
|
||||
TokenType::Or => {
|
||||
TokenType::BitOr => {
|
||||
emit!(&mut self.output, " or rax, rbx");
|
||||
}
|
||||
TokenType::DoubleEqual => {
|
||||
@@ -365,6 +361,14 @@ _builtin_rshift:
|
||||
emit!(&mut self.output, " setle al");
|
||||
emit!(&mut self.output, " movzx rax, al");
|
||||
}
|
||||
TokenType::ShiftLeft => {
|
||||
emit!(&mut self.output, " mov rcx, rbx");
|
||||
emit!(&mut self.output, " sal rax, cl");
|
||||
}
|
||||
TokenType::ShiftRight => {
|
||||
emit!(&mut self.output, " mov rcx, rbx");
|
||||
emit!(&mut self.output, " sar rax, cl");
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
@@ -458,28 +462,60 @@ _builtin_rshift:
|
||||
}
|
||||
Expr::Call {
|
||||
callee,
|
||||
paren,
|
||||
paren: _,
|
||||
args,
|
||||
} => {
|
||||
let callee = match *callee {
|
||||
Expr::Variable(name) => name.lexeme,
|
||||
_ => 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");
|
||||
}
|
||||
|
||||
for i in (0..args.len()).rev() {
|
||||
let reg = match REGISTERS.get(i) {
|
||||
Some(x) => x,
|
||||
None => return error!(&paren.loc, "only up to 6 args allowed"),
|
||||
};
|
||||
emit!(&mut self.output, " pop {}", reg);
|
||||
let arg_count = args.len();
|
||||
if arg_count <= 6 {
|
||||
for i in (0..arg_count).rev() {
|
||||
emit!(&mut self.output, " pop {}", REGISTERS[i]);
|
||||
}
|
||||
} else {
|
||||
for (i, reg) in REGISTERS.iter().enumerate() {
|
||||
let offset = 8 * (arg_count - 1 - i);
|
||||
emit!(
|
||||
&mut self.output,
|
||||
" mov {}, QWORD [rsp + {}]",
|
||||
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);
|
||||
emit!(&mut self.output, " push rax");
|
||||
}
|
||||
}
|
||||
|
||||
emit!(&mut self.output, " call {}", callee);
|
||||
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");
|
||||
}
|
||||
} else {
|
||||
// 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) => {
|
||||
emit!(&mut self.output, " call array.new");
|
||||
@@ -496,12 +532,36 @@ _builtin_rshift:
|
||||
}
|
||||
Expr::Index { expr, index } => {
|
||||
self.compile_expr(env, *expr)?;
|
||||
emit!(&mut self.output, " push rax");
|
||||
emit!(&mut self.output, " mov rdi, rax");
|
||||
self.compile_expr(env, *index)?;
|
||||
emit!(&mut self.output, " pop rbx");
|
||||
emit!(&mut self.output, " mov rbx, [rbx]");
|
||||
emit!(&mut self.output, " mov rax, [rbx + rax*8]");
|
||||
emit!(&mut self.output, " add rdi, rax");
|
||||
emit!(&mut self.output, " call _builtin_read8");
|
||||
}
|
||||
Expr::AddrOf { op, expr } => match *expr {
|
||||
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(())
|
||||
}
|
||||
|
||||
41
src/main.rs
41
src/main.rs
@@ -14,7 +14,6 @@ use tokenizer::ZernError;
|
||||
use clap::Parser;
|
||||
|
||||
fn compile_file_to(
|
||||
analyzer: &mut analyzer::Analyzer,
|
||||
codegen: &mut codegen_x86_64::CodegenX86_64,
|
||||
filename: &str,
|
||||
source: String,
|
||||
@@ -26,11 +25,11 @@ fn compile_file_to(
|
||||
let statements = parser.parse()?;
|
||||
|
||||
for stmt in &statements {
|
||||
analyzer.register_function(stmt)?;
|
||||
codegen.analyzer.register_function(stmt)?;
|
||||
}
|
||||
|
||||
for stmt in &statements {
|
||||
analyzer.analyze_stmt(stmt)?;
|
||||
codegen.analyzer.analyze_stmt(stmt)?;
|
||||
}
|
||||
|
||||
for stmt in statements {
|
||||
@@ -62,26 +61,36 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
|
||||
let filename = Path::new(&args.path).file_name().unwrap().to_str().unwrap();
|
||||
|
||||
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()?;
|
||||
compile_file_to(
|
||||
&mut analyzer,
|
||||
&mut codegen,
|
||||
"std.zr",
|
||||
include_str!("std.zr").into(),
|
||||
)?;
|
||||
compile_file_to(&mut analyzer, &mut codegen, filename, source)?;
|
||||
compile_file_to(&mut codegen, "std.zr", include_str!("std.zr").into())?;
|
||||
compile_file_to(&mut codegen, filename, source)?;
|
||||
|
||||
if !args.output_asm {
|
||||
fs::write(format!("{}.s", args.out), codegen.get_output()).unwrap();
|
||||
|
||||
run_command(format!("nasm -f elf64 -o {}.o {}.s", args.out, args.out));
|
||||
|
||||
// TODO: drop libc entirely
|
||||
if args.no_musl {
|
||||
run_command(format!(
|
||||
"./musl-1.2.4/root/bin/musl-gcc -static -o {} {}.o -flto -Wl,--gc-sections {}",
|
||||
"gcc -no-pie -o {} {}.o -flto -Wl,--gc-sections {}",
|
||||
args.out, args.out, args.cflags
|
||||
));
|
||||
} else {
|
||||
run_command(format!(
|
||||
"musl-gcc -static -o {} {}.o -flto -Wl,--gc-sections {}",
|
||||
args.out, args.out, args.cflags
|
||||
));
|
||||
}
|
||||
|
||||
if args.run_exe {
|
||||
run_command(
|
||||
std::fs::canonicalize(args.out)
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.into_owned(),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
fs::write(&args.out, codegen.get_output()).unwrap();
|
||||
}
|
||||
@@ -100,6 +109,12 @@ struct Args {
|
||||
#[arg(short = 'S', help = "Only generate assembly")]
|
||||
output_asm: bool,
|
||||
|
||||
#[arg(short = 'r', help = "Run the compiled executable")]
|
||||
run_exe: bool,
|
||||
|
||||
#[arg(short = 'm', help = "Don't use musl")]
|
||||
no_musl: bool,
|
||||
|
||||
#[arg(short = 'C', default_value = "", help = "Extra flags to pass to gcc")]
|
||||
cflags: String,
|
||||
}
|
||||
|
||||
@@ -35,10 +35,12 @@ pub enum Stmt {
|
||||
params: Vec<Param>,
|
||||
return_type: Token,
|
||||
body: Box<Stmt>,
|
||||
exported: bool,
|
||||
},
|
||||
Return(Expr),
|
||||
Break,
|
||||
Continue,
|
||||
Extern(Token),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -69,10 +71,14 @@ pub enum Expr {
|
||||
expr: Box<Expr>,
|
||||
index: Box<Expr>,
|
||||
},
|
||||
AddrOf {
|
||||
op: Token,
|
||||
expr: Box<Expr>,
|
||||
},
|
||||
}
|
||||
|
||||
// TODO: currently they are all just 8 byte values
|
||||
static TYPES: [&str; 7] = ["Void", "U8", "I64", "String", "Bool", "Ptr", "Array"];
|
||||
static TYPES: [&str; 7] = ["void", "u8", "i64", "str", "bool", "ptr", "array"];
|
||||
|
||||
pub struct Parser {
|
||||
tokens: Vec<Token>,
|
||||
@@ -100,7 +106,14 @@ impl Parser {
|
||||
fn declaration(&mut self) -> Result<Stmt, ZernError> {
|
||||
if !self.is_inside_function {
|
||||
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();
|
||||
}
|
||||
return error!(
|
||||
self.peek().loc,
|
||||
@@ -115,8 +128,7 @@ impl Parser {
|
||||
}
|
||||
}
|
||||
|
||||
// TOOD: parse return type
|
||||
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")?;
|
||||
self.consume(TokenType::LeftBracket, "expected '[' after function name")?;
|
||||
|
||||
@@ -154,6 +166,7 @@ impl Parser {
|
||||
params,
|
||||
return_type,
|
||||
body,
|
||||
exported,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -175,6 +188,12 @@ impl Parser {
|
||||
})
|
||||
}
|
||||
|
||||
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> {
|
||||
self.consume(TokenType::Indent, "expected an indent")?;
|
||||
|
||||
@@ -272,7 +291,7 @@ impl Parser {
|
||||
}
|
||||
|
||||
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]) {
|
||||
let pipe = self.previous().clone();
|
||||
@@ -301,26 +320,10 @@ impl Parser {
|
||||
Ok(expr)
|
||||
}
|
||||
|
||||
fn logical_or(&mut self) -> Result<Expr, ZernError> {
|
||||
let mut expr = self.logical_and()?;
|
||||
|
||||
while self.match_token(&[TokenType::Or]) {
|
||||
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> {
|
||||
fn or_and(&mut self) -> Result<Expr, ZernError> {
|
||||
let mut expr = self.equality()?;
|
||||
|
||||
while self.match_token(&[TokenType::And]) {
|
||||
while self.match_token(&[TokenType::BitOr, TokenType::BitAnd]) {
|
||||
let op = self.previous().clone();
|
||||
let right = self.equality()?;
|
||||
expr = Expr::Binary {
|
||||
@@ -389,7 +392,13 @@ impl Parser {
|
||||
fn factor(&mut self) -> Result<Expr, ZernError> {
|
||||
let mut expr = self.unary()?;
|
||||
|
||||
while self.match_token(&[TokenType::Star, TokenType::Slash, TokenType::Mod]) {
|
||||
while self.match_token(&[
|
||||
TokenType::Star,
|
||||
TokenType::Slash,
|
||||
TokenType::Mod,
|
||||
TokenType::ShiftLeft,
|
||||
TokenType::ShiftRight,
|
||||
]) {
|
||||
let op = self.previous().clone();
|
||||
let right = self.unary()?;
|
||||
expr = Expr::Binary {
|
||||
@@ -403,6 +412,14 @@ impl Parser {
|
||||
}
|
||||
|
||||
fn unary(&mut self) -> Result<Expr, ZernError> {
|
||||
if self.match_token(&[TokenType::At]) {
|
||||
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]) {
|
||||
let op = self.previous().clone();
|
||||
let right = self.unary()?;
|
||||
|
||||
777
src/std.zr
777
src/std.zr
@@ -1,142 +1,345 @@
|
||||
func dbg.panic[msg: String] : Void
|
||||
c.printf("PANIC: %s\n", msg)
|
||||
c.exit(1)
|
||||
extern malloc
|
||||
extern realloc
|
||||
extern free
|
||||
extern gethostbyname
|
||||
|
||||
func io.print[x: String] : Void
|
||||
c.puts(x)
|
||||
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 io.print_i64[x: I64] : Void
|
||||
c.printf("%ld\n", x)
|
||||
func mem.alloc[x: i64] : ptr
|
||||
return malloc(x)
|
||||
|
||||
func io.read_stdin[]: String
|
||||
let buffer: String = c.malloc(1025)
|
||||
let n: I64 = c.syscall(0, 0, buffer, 1024)
|
||||
str.set(buffer, n, 0)
|
||||
return buffer
|
||||
func mem.free[x: ptr] : void
|
||||
free(x)
|
||||
|
||||
func io.read_file[path: String]: String
|
||||
let file: Ptr = c.fopen(path, "rb")
|
||||
if !file
|
||||
dbg.panic("failed to open file")
|
||||
func mem.zero[x: i64, size: i64] : void
|
||||
for i in 0..size
|
||||
mem.write8(x + i, 0)
|
||||
|
||||
c.fseek(file, 0, 2)
|
||||
let size: I64 = c.ftell(file)
|
||||
c.rewind(file)
|
||||
func mem.read8[x: ptr] : u8
|
||||
return _builtin_read8(x)
|
||||
|
||||
let buffer: String = c.malloc(size + 1)
|
||||
func mem.read16[x: ptr] : i64
|
||||
let low: i64 = mem.read8(x)
|
||||
let high: i64 = mem.read8(x + 1)
|
||||
return low | (high << 8)
|
||||
|
||||
let n: I64 = c.fread(buffer, 1, size, file)
|
||||
str.set(buffer, n, 0)
|
||||
c.fclose(file)
|
||||
return buffer
|
||||
func mem.read64[x: ptr] : i64
|
||||
return _builtin_read64(x)
|
||||
|
||||
func io.write_file[path: String, content: String] : Void
|
||||
let file: Ptr = c.fopen(path, "wb")
|
||||
if !file
|
||||
dbg.panic("failed to open file")
|
||||
func mem.write8[x: ptr, d: u8] : void
|
||||
_builtin_set8(x, d)
|
||||
|
||||
c.fwrite(content, 1, c.strlen(content), file)
|
||||
c.fclose(file)
|
||||
func mem.write64[x: ptr, d: i64] : void
|
||||
_builtin_set64(x, d)
|
||||
|
||||
func str.nth[s: String, n: I64] : U8
|
||||
return _builtin_deref8(s + n)
|
||||
func io.print_sized[x: str, size: i64] : void
|
||||
_builtin_syscall(1, 1, x, size) // write
|
||||
|
||||
func str.set[s: String, n: I64, c: U8] : Void
|
||||
_builtin_set8(s+n, c)
|
||||
func io.print[x: str] : void
|
||||
io.print_sized(x, str.len(x))
|
||||
|
||||
func str.is_whitespace[c: U8] : Bool
|
||||
return c == ' ' | c == 10 | c == 13 | c == 9
|
||||
func io.println[x: str] : void
|
||||
io.print(x)
|
||||
io.print("\n")
|
||||
|
||||
func str.concat[a: String, b: String] : String
|
||||
let c: String = c.malloc(c.strlen(a) + c.strlen(b) + 1)
|
||||
c.strcpy(c, a)
|
||||
c.strcat(c, b)
|
||||
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(0, 0, @c, 1) // read
|
||||
return c
|
||||
|
||||
func str.find[s: String, c: U8] : I64
|
||||
let s_len: I64 = c.strlen(s)
|
||||
for i in 0..s_len
|
||||
if str.nth(s, i) == c
|
||||
func io.read_line[]: str
|
||||
let MAX_SIZE: i64 = 60000
|
||||
let buffer: str = mem.alloc(MAX_SIZE + 1)
|
||||
let n: i64 = _builtin_syscall(0, 0, buffer, MAX_SIZE) // read
|
||||
if n < 0
|
||||
return ""
|
||||
str.set(buffer, n, 0)
|
||||
return buffer
|
||||
|
||||
func io.read_file[path: str]: str
|
||||
let fd: i64 = _builtin_syscall(257, -100, path, 0, 0) // openat
|
||||
if fd <= 0
|
||||
dbg.panic("failed to open file")
|
||||
|
||||
let size: i64 = _builtin_syscall(8, fd, 0, 2) // lseek to the end
|
||||
_builtin_syscall(8, fd, 0, 0) // lseek back to start
|
||||
|
||||
let buffer: str = mem.alloc(size + 1)
|
||||
let n: i64 = _builtin_syscall(0, fd, buffer, size) // read
|
||||
str.set(buffer, n, 0)
|
||||
_builtin_syscall(3, fd) // close
|
||||
return buffer
|
||||
|
||||
func io.write_file[path: str, content: str] : void
|
||||
let fd: ptr = _builtin_syscall(257, -100, path, 0x241, 0o644) // openat
|
||||
if fd < 0
|
||||
dbg.panic("failed to open file")
|
||||
|
||||
_builtin_syscall(1, fd, content, str.len(content)) // write
|
||||
_builtin_syscall(3, fd) // close
|
||||
|
||||
func str.len[s: str] : i64
|
||||
let i: i64 = 0
|
||||
while mem.read8(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: i64 = 0
|
||||
while a[i] != 0 & b[i] != 0
|
||||
if a[i] != b[i]
|
||||
return false
|
||||
i = i + 1
|
||||
return a[i] == b[i]
|
||||
|
||||
func str.is_whitespace[x: u8] : bool
|
||||
return x == ' ' | x == 10 | x == 13 | x == 9
|
||||
|
||||
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: String, start: I64, length: I64] : String
|
||||
if start < 0 | length < 0 | start + length > c.strlen(s)
|
||||
dbg.panic("String.substr out of bounds")
|
||||
func str.substr[s: str, start: i64, length: i64] : str
|
||||
if start < 0 | length < 0 | start + length > str.len(s)
|
||||
dbg.panic("str.substr out of bounds")
|
||||
|
||||
let out: String = c.malloc(length + 1)
|
||||
c.strncpy(out, s + start, length)
|
||||
let out: str = mem.alloc(length + 1)
|
||||
for i in 0..length
|
||||
str.set(out, i, s[start + i])
|
||||
str.set(out, length, 0)
|
||||
return out
|
||||
|
||||
func str.trim[s: String] : String
|
||||
let start: I64 = 0
|
||||
let end: I64 = c.strlen(s) - 1
|
||||
func str.trim[s: str] : str
|
||||
let len: i64 = str.len(s)
|
||||
if len == 0
|
||||
return ""
|
||||
|
||||
while start <= end & str.is_whitespace(str.nth(s, start))
|
||||
let start: i64 = 0
|
||||
let end: i64 = len - 1
|
||||
|
||||
while start <= end & str.is_whitespace(s[start])
|
||||
start = start + 1
|
||||
|
||||
while end >= start & str.is_whitespace(str.nth(s, end))
|
||||
while end >= start & str.is_whitespace(s[end])
|
||||
end = end - 1
|
||||
|
||||
return str.substr(s, start, end - start + 1)
|
||||
|
||||
func str.reverse[s: String] : String
|
||||
let len: I64 = c.strlen(s)
|
||||
let out: String = c.malloc(len + 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: 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 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, str.nth(s, len - i - 1))
|
||||
str.set(out, i, s[len - i - 1])
|
||||
str.set(out, len, 0)
|
||||
return out
|
||||
|
||||
func str.from_i64[n: I64] : String
|
||||
let x: String = c.malloc(21)
|
||||
c.sprintf(x, "%ld", n)
|
||||
return x
|
||||
// not sure this covers all wacky edge cases
|
||||
func str.from_i64[n: i64] : str
|
||||
if n == 0
|
||||
return str.copy("0")
|
||||
|
||||
func str.parse_i64[s: String] : I64
|
||||
return c.strtol(s, 0, 0)
|
||||
let neg: bool = n < 0
|
||||
if neg
|
||||
n = -n
|
||||
let buf: str = mem.alloc(21) // enough to fit -MAX_I64
|
||||
let i: i64 = 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 math.gcd[a: I64, b: I64] : I64
|
||||
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: i64 = 0
|
||||
|
||||
let sign: i64 = 1
|
||||
if i < len & s[i] == '-'
|
||||
sign = -1
|
||||
i = i + 1
|
||||
|
||||
let num: i64 = 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] : str
|
||||
let hex_chars: str = "0123456789abcdef"
|
||||
let s_len: i64 = str.len(s)
|
||||
let j: i64 = 0
|
||||
let out: str = mem.alloc(s_len * 2 + 1)
|
||||
|
||||
for i in 0..s_len
|
||||
let high: u8 = (s[i] >> 4) & 15
|
||||
let low: u8 = s[i] & 15
|
||||
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: i64 = 0
|
||||
let j: i64 = 0
|
||||
let out: str = mem.alloc(s_len / 2 + 1)
|
||||
|
||||
while i < s_len
|
||||
str.set(out, j, str._hex_digit_to_int(s[i]) * 16 + str._hex_digit_to_int(s[i + 1]))
|
||||
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
|
||||
let tmp: i64 = b
|
||||
b = a % b
|
||||
a = tmp
|
||||
return a
|
||||
|
||||
func math.min[a: I64, b: I64] : I64
|
||||
func math.min[a: i64, b: i64] : i64
|
||||
if a < b
|
||||
return a
|
||||
return b
|
||||
|
||||
func math.max[a: I64, b: I64] : I64
|
||||
func math.max[a: i64, b: i64] : i64
|
||||
if a > b
|
||||
return a
|
||||
return b
|
||||
|
||||
func math.abs[n: I64] : I64
|
||||
func math.abs[n: i64] : i64
|
||||
if n < 0
|
||||
return -n
|
||||
return n
|
||||
|
||||
func math.pow[b: I64, e: I64] : I64
|
||||
let out: I64 = 1
|
||||
func math.pow[b: i64, e: i64] : i64
|
||||
let out: i64 = 1
|
||||
for i in 0..e
|
||||
out = out * b
|
||||
return out
|
||||
|
||||
func math.lcm[a: I64, b: I64] : I64
|
||||
func math.lcm[a: i64, b: i64] : i64
|
||||
return (a * b) / math.gcd(a, b)
|
||||
|
||||
func math.isqrt[n: I64] : I64
|
||||
func math.isqrt[n: i64] : i64
|
||||
if n < 0
|
||||
return -1
|
||||
dbg.panic("negative number passed to math.isqrt")
|
||||
if n == 0 | n == 1
|
||||
return n
|
||||
|
||||
let guess: I64 = n
|
||||
let next_guess: I64 = (guess + n / guess) / 2
|
||||
let guess: i64 = n
|
||||
let next_guess: i64 = (guess + n / guess) / 2
|
||||
|
||||
while next_guess < guess
|
||||
guess = next_guess
|
||||
@@ -144,7 +347,7 @@ func math.isqrt[n: I64] : I64
|
||||
|
||||
return guess
|
||||
|
||||
func math.is_prime[n: I64]: Bool
|
||||
func math.is_prime[n: i64]: bool
|
||||
if n <= 1
|
||||
return false
|
||||
if n == 2 | n == 3
|
||||
@@ -152,275 +355,249 @@ func math.is_prime[n: I64]: Bool
|
||||
if n % 2 == 0 | n % 3 == 0
|
||||
return false
|
||||
|
||||
let i: I64 = 5
|
||||
let i: i64 = 5
|
||||
while i * i <= n
|
||||
if n % i == 0 | n % (i + 2) == 0
|
||||
return false
|
||||
i = i + 6
|
||||
return true
|
||||
|
||||
func math.urandom[]: I64
|
||||
let buffer: Ptr = c.malloc(8)
|
||||
let file: Ptr = c.fopen("/dev/urandom", "rb")
|
||||
c.fread(buffer, 8, 1, file)
|
||||
c.fclose(file)
|
||||
let n: I64 = _builtin_deref64(buffer)
|
||||
c.free(buffer)
|
||||
return n
|
||||
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.new[] : Array
|
||||
return c.calloc(1, 24)
|
||||
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
|
||||
let data: Ptr = _builtin_deref64(xs)
|
||||
_builtin_set64(data+n*8, x)
|
||||
func array.set[xs: array, n: i64, x: i64] : void
|
||||
if n < 0 | n >= array.size(xs)
|
||||
dbg.panic("array.nth 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 = _builtin_deref64(xs)
|
||||
let capacity: I64 = _builtin_deref64(xs+8)
|
||||
let size: I64 = _builtin_deref64(xs+16)
|
||||
func array.push[xs: array, x: i64] : void
|
||||
let data: ptr = mem.read64(xs)
|
||||
let capacity: i64 = mem.read64(xs + 8)
|
||||
let size: i64 = mem.read64(xs + 16)
|
||||
|
||||
if size == capacity
|
||||
let new_capacity: I64 = 4
|
||||
let new_capacity: i64 = 4
|
||||
if capacity != 0
|
||||
new_capacity = capacity * 2
|
||||
let new_data: Ptr = c.realloc(data, new_capacity * 8)
|
||||
_builtin_set64(xs, new_data)
|
||||
_builtin_set64(xs+8, new_capacity)
|
||||
let new_data: ptr = realloc(data, new_capacity * 8)
|
||||
mem.write64(xs, new_data)
|
||||
mem.write64(xs + 8, new_capacity)
|
||||
data = new_data
|
||||
|
||||
_builtin_set64(data+size*8, x)
|
||||
_builtin_set64(xs+16, size + 1)
|
||||
mem.write64(data + size * 8, x)
|
||||
mem.write64(xs + 16, size + 1)
|
||||
|
||||
func array.size[xs: Array] : I64
|
||||
return _builtin_deref64(xs+16)
|
||||
func array.size[xs: array] : i64
|
||||
return mem.read64(xs + 16)
|
||||
|
||||
func array.free[xs: Array] : Void
|
||||
c.free(_builtin_deref64(xs))
|
||||
c.free(xs)
|
||||
func array.free[xs: array] : void
|
||||
let data: ptr = mem.read64(xs)
|
||||
if data != 0
|
||||
mem.free(data)
|
||||
mem.free(xs)
|
||||
|
||||
func os.time[] : I64
|
||||
let tv: Ptr = c.malloc(16)
|
||||
c.gettimeofday(tv, 0)
|
||||
let seconds: I64 = _builtin_deref64(tv)
|
||||
let microseconds: I64 = _builtin_deref64(tv+8)
|
||||
c.free(tv)
|
||||
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: i64 = 0
|
||||
let size: i64 = array.size(arr)
|
||||
for i in 0..size
|
||||
if array.nth(arr, i) == item
|
||||
count = count + 1
|
||||
return count
|
||||
|
||||
func os.exit[code: i64] : void
|
||||
_builtin_syscall(60, code)
|
||||
|
||||
func os.urandom[]: i64
|
||||
let n: i64 = 0
|
||||
let fd: i64 = _builtin_syscall(257, -100, "/dev/urandom", 0, 0) // openat
|
||||
_builtin_syscall(0, fd, @n, 8) // read
|
||||
_builtin_syscall(3, fd) // close
|
||||
return n
|
||||
|
||||
func os.time[] : i64
|
||||
let tv: ptr = mem.alloc(16)
|
||||
_builtin_syscall(96, tv, 0) // gettimeofday
|
||||
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 = []
|
||||
// voodoo magic
|
||||
func os.shell[command: str] : i64
|
||||
let pid: i64 = _builtin_syscall(57) // fork
|
||||
if pid == 0
|
||||
// leaky but not sure where can i free it
|
||||
let argv: array = ["sh", "-c", command, 0]
|
||||
_builtin_syscall(59, "/bin/sh", mem.read64(argv), _builtin_environ()) // execve
|
||||
_builtin_syscall(60, 1) // exit
|
||||
else
|
||||
let status: i64 = 0
|
||||
let wp: i64 = _builtin_syscall(61, pid, @status, 0, 0) // waitpid
|
||||
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(257, -100, path, 0, 0) // openat
|
||||
if fd < 0
|
||||
return []
|
||||
|
||||
let files: array = []
|
||||
let buf: ptr = mem.alloc(1024)
|
||||
while true
|
||||
let entry: Ptr = c.readdir(dir)
|
||||
if entry == 0
|
||||
let n: i64 = _builtin_syscall(217, fd, buf, 1024) // getdents64
|
||||
if n <= 0
|
||||
break
|
||||
|
||||
let skip: Bool = false
|
||||
if str.nth(entry, 19) == '.'
|
||||
if str.nth(entry, 20) == 0
|
||||
let pos: i64 = 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 str.nth(entry, 20) == '.'
|
||||
if str.nth(entry, 21) == 0
|
||||
else if name[1] == '.'
|
||||
if name[2] == 0
|
||||
skip = true
|
||||
|
||||
if !skip
|
||||
array.push(files, c.strdup(entry + 19))
|
||||
c.closedir(dir)
|
||||
array.push(files, str.copy(name))
|
||||
pos = pos + len
|
||||
|
||||
mem.free(buf)
|
||||
_builtin_syscall(3, fd) // close
|
||||
return files
|
||||
|
||||
func bit.lshift[a: I64, b: I64] : I64
|
||||
return _builtin_lshift(a, b)
|
||||
|
||||
func bit.rshift[a: I64, b: I64] : I64
|
||||
return _builtin_rshift(a, b)
|
||||
|
||||
func crypto.hex_encode[s: String] : String
|
||||
let hex_chars: String = "0123456789abcdef"
|
||||
let s_len: I64 = c.strlen(s)
|
||||
let j: I64 = 0
|
||||
let out: String = c.malloc(s_len*2+1)
|
||||
|
||||
for i in 0..s_len
|
||||
let high: U8 = bit.rshift(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 crypto.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 crypto.hex_decode[s: String] : String
|
||||
let s_len: I64 = c.strlen(s)
|
||||
let i: I64 = 0
|
||||
let j: I64 = 0
|
||||
let out: String = c.malloc(s_len/2+1)
|
||||
|
||||
while i < s_len
|
||||
str.set(out, j, crypto.from_hex_digit(str.nth(s, i)) * 16 + crypto.from_hex_digit(str.nth(s, i+1)))
|
||||
i = i + 2
|
||||
j = j + 1
|
||||
|
||||
str.set(out, j, 0)
|
||||
return out
|
||||
|
||||
func crypto.rc4[key: String, plaintext: String]: String
|
||||
let S: String = c.malloc(256)
|
||||
for i in 0..256
|
||||
str.set(S, i, i)
|
||||
|
||||
let j: I64 = 0
|
||||
let key_len: I64 = c.strlen(key)
|
||||
for i in 0..256
|
||||
j = (j + str.nth(S, i) + str.nth(key, i % key_len)) % 256
|
||||
let tmp: U8 = str.nth(S, i)
|
||||
str.set(S, i, str.nth(S, j))
|
||||
str.set(S, j, tmp)
|
||||
|
||||
let i: I64 = 0
|
||||
j = 0
|
||||
let plaintext_len: I64 = c.strlen(plaintext)
|
||||
let ciphertext: String = c.malloc(plaintext_len+1)
|
||||
for n in 0..plaintext_len
|
||||
i = (i + 1) % 256
|
||||
j = (j + str.nth(S, i)) % 256
|
||||
|
||||
let tmp: U8 = str.nth(S, i)
|
||||
str.set(S, i, str.nth(S, j))
|
||||
str.set(S, j, tmp)
|
||||
|
||||
let r: I64 = str.nth(S, (str.nth(S, i) + str.nth(S, j)) % 256)
|
||||
str.set(ciphertext, n, r ^ str.nth(plaintext, n))
|
||||
|
||||
str.set(ciphertext, plaintext_len, 0)
|
||||
c.free(S)
|
||||
return ciphertext
|
||||
|
||||
func crypto.base64_encode[s: String] : String
|
||||
let chars: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
let s_len: I64 = c.strlen(s)
|
||||
let out: String = c.malloc(s_len*2)
|
||||
let i: I64 = 0
|
||||
let j: I64 = 0
|
||||
|
||||
while i < s_len
|
||||
let b1: U8 = str.nth(s, i)
|
||||
let b2: U8 = 0
|
||||
if i + 1 < s_len
|
||||
b2 = str.nth(s, i+1)
|
||||
let b3: U8 = 0
|
||||
if i + 2 < s_len
|
||||
b3 = str.nth(s, i+2)
|
||||
i = i + 3
|
||||
|
||||
let triple: I64 = bit.lshift(b1, 16) | bit.lshift(b2, 8) | b3
|
||||
str.set(out, j, str.nth(chars, bit.rshift(triple, 18) & 63))
|
||||
str.set(out, j+1, str.nth(chars, bit.rshift(triple, 12) & 63))
|
||||
str.set(out, j+2, str.nth(chars, bit.rshift(triple, 6) & 63))
|
||||
str.set(out, j+3, str.nth(chars, triple & 63))
|
||||
j = j + 4
|
||||
|
||||
let padding: I64 = s_len % 3
|
||||
if padding == 1
|
||||
str.set(out, j-2, '=')
|
||||
str.set(out, j-1, '=')
|
||||
else if padding == 2
|
||||
str.set(out, j-1, '=')
|
||||
|
||||
str.set(out, j, 0)
|
||||
return out
|
||||
|
||||
func crypto.base64_decode[s: String] : String
|
||||
let chars: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
let s_len: I64 = c.strlen(s)
|
||||
let out: String = c.malloc(s_len)
|
||||
let i: I64 = 0
|
||||
let j: I64 = 0
|
||||
|
||||
while str.nth(s, s_len-1) == '='
|
||||
s_len = s_len - 1
|
||||
|
||||
while i < s_len
|
||||
let s1: U8 = str.find(chars, str.nth(s, i))
|
||||
let s2: U8 = 0
|
||||
if i + 1 < s_len
|
||||
s2 = str.find(chars, str.nth(s, i+1))
|
||||
let s3: U8 = 0
|
||||
if i + 2 < s_len
|
||||
s3 = str.find(chars, str.nth(s, i+2))
|
||||
let s4: U8 = 0
|
||||
if i + 3 < s_len
|
||||
s4 = str.find(chars, str.nth(s, i+3))
|
||||
i = i + 4
|
||||
|
||||
let triple: I64 = bit.lshift(s1, 18) | bit.lshift(s2, 12) | bit.lshift(s3, 6) | s4
|
||||
|
||||
str.set(out, j, bit.rshift(triple, 16) & 255)
|
||||
j = j + 1
|
||||
if s3 != 64
|
||||
str.set(out, j, bit.rshift(triple, 8) & 255)
|
||||
j = j + 1
|
||||
if s4 != 64
|
||||
str.set(out, j, triple & 255)
|
||||
j = j + 1
|
||||
|
||||
str.set(out, j, 0)
|
||||
return out
|
||||
|
||||
func net.listen[port: I64] : I64
|
||||
let s: I64 = c.socket(2, 1, 0)
|
||||
func net.listen[packed_host: i64, port: i64] : i64
|
||||
let s: i64 = _builtin_syscall(41, 2, 1, 0) // socket
|
||||
if s < 0
|
||||
return -1
|
||||
|
||||
let sa: Ptr = c.calloc(1, 16)
|
||||
str.set(sa, 0, 2)
|
||||
str.set(sa, 1, 0)
|
||||
str.set(sa, 2, bit.rshift(port, 8) & 255)
|
||||
str.set(sa, 3, port & 255)
|
||||
|
||||
if c.bind(s, sa, 16) < 0
|
||||
c.close(s)
|
||||
let optval: i64 = 1
|
||||
if _builtin_syscall(54, s, 1, 2, @optval, 8) < 0 // setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
||||
_builtin_syscall(3, s) // close
|
||||
return -1
|
||||
c.free(sa)
|
||||
|
||||
if c.listen(s, 1) < 0
|
||||
c.close(s)
|
||||
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(49, s, sa, 16) < 0 // bind
|
||||
_builtin_syscall(3, s) // close
|
||||
mem.free(sa)
|
||||
return -1
|
||||
mem.free(sa)
|
||||
|
||||
if _builtin_syscall(50, s, 1) < 0 // listen
|
||||
_builtin_syscall(3, s) // close
|
||||
return -1
|
||||
|
||||
return s
|
||||
|
||||
func net.connect[host: String, port: I64] : I64
|
||||
let he: Ptr = c.gethostbyname(host)
|
||||
func net.connect[host: str, port: i64] : i64
|
||||
let he: ptr = gethostbyname(host)
|
||||
if he == 0
|
||||
return -1
|
||||
|
||||
let ip_ptr: Ptr = _builtin_deref64(_builtin_deref64(he + 24))
|
||||
let ip_ptr: ptr = mem.read64(mem.read64(he + 24))
|
||||
|
||||
let s: I64 = c.socket(2, 1, 0)
|
||||
let s: i64 = _builtin_syscall(41, 2, 1, 0) // socket
|
||||
if s < 0
|
||||
return -1
|
||||
|
||||
let sa: Ptr = c.calloc(1, 16)
|
||||
str.set(sa, 0, 2)
|
||||
str.set(sa, 2, bit.rshift(port, 8) & 255)
|
||||
str.set(sa, 3, port & 255)
|
||||
str.set(sa, 4, _builtin_deref8(ip_ptr + 0))
|
||||
str.set(sa, 5, _builtin_deref8(ip_ptr + 1))
|
||||
str.set(sa, 6, _builtin_deref8(ip_ptr + 2))
|
||||
str.set(sa, 7, _builtin_deref8(ip_ptr + 3))
|
||||
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 c.connect(s, sa, 16) < 0
|
||||
c.free(sa)
|
||||
c.close(s)
|
||||
if _builtin_syscall(42, s, sa, 16) < 0 // connect
|
||||
mem.free(sa)
|
||||
_builtin_syscall(3, s) // close
|
||||
return -1
|
||||
|
||||
c.free(sa)
|
||||
mem.free(sa)
|
||||
return s
|
||||
|
||||
func net.accept[s: i64] : i64
|
||||
return _builtin_syscall(43, s, 0, 0)
|
||||
|
||||
func net.send[s: i64, data: str, size: i64] : void
|
||||
_builtin_syscall(44, s, data, size, 0, 0, 0)
|
||||
|
||||
func net.read[s: i64, buffer: ptr, size: i64] : i64
|
||||
return _builtin_syscall(0, s, buffer, size)
|
||||
|
||||
func net.close[s: i64] : void
|
||||
_builtin_syscall(3, s)
|
||||
|
||||
func net.pack_addr[a: i64, b: i64, c: i64, d: i64] : i64
|
||||
return (a << 24) | (b << 16) | (c << 8) | d
|
||||
|
||||
@@ -15,10 +15,13 @@ pub enum TokenType {
|
||||
Xor,
|
||||
Bang,
|
||||
Colon,
|
||||
And,
|
||||
Or,
|
||||
BitAnd,
|
||||
BitOr,
|
||||
Pipe,
|
||||
At,
|
||||
DoubleDot,
|
||||
ShiftLeft,
|
||||
ShiftRight,
|
||||
|
||||
Equal,
|
||||
DoubleEqual,
|
||||
@@ -45,6 +48,8 @@ pub enum TokenType {
|
||||
KeywordReturn,
|
||||
KeywordBreak,
|
||||
KeywordContinue,
|
||||
KeywordExtern,
|
||||
KeywordExport,
|
||||
|
||||
Indent,
|
||||
Dedent,
|
||||
@@ -150,6 +155,7 @@ impl Tokenizer {
|
||||
'%' => self.add_token(TokenType::Mod),
|
||||
'^' => self.add_token(TokenType::Xor),
|
||||
':' => self.add_token(TokenType::Colon),
|
||||
'@' => self.add_token(TokenType::At),
|
||||
'.' => {
|
||||
if self.match_char('.') {
|
||||
self.add_token(TokenType::DoubleDot)
|
||||
@@ -166,12 +172,12 @@ impl Tokenizer {
|
||||
self.add_token(TokenType::Slash)
|
||||
}
|
||||
}
|
||||
'&' => self.add_token(TokenType::And),
|
||||
'&' => self.add_token(TokenType::BitAnd),
|
||||
'|' => {
|
||||
if self.match_char('>') {
|
||||
self.add_token(TokenType::Pipe);
|
||||
} else {
|
||||
self.add_token(TokenType::Or);
|
||||
self.add_token(TokenType::BitOr);
|
||||
}
|
||||
}
|
||||
'!' => {
|
||||
@@ -189,14 +195,18 @@ impl Tokenizer {
|
||||
}
|
||||
}
|
||||
'>' => {
|
||||
if self.match_char('=') {
|
||||
if self.match_char('>') {
|
||||
self.add_token(TokenType::ShiftRight);
|
||||
} else if self.match_char('=') {
|
||||
self.add_token(TokenType::GreaterEqual)
|
||||
} else {
|
||||
self.add_token(TokenType::Greater)
|
||||
}
|
||||
}
|
||||
'<' => {
|
||||
if self.match_char('=') {
|
||||
if self.match_char('<') {
|
||||
self.add_token(TokenType::ShiftLeft);
|
||||
} else if self.match_char('=') {
|
||||
self.add_token(TokenType::LessEqual)
|
||||
} else {
|
||||
self.add_token(TokenType::Less)
|
||||
@@ -204,6 +214,9 @@ impl Tokenizer {
|
||||
}
|
||||
// TODO: escape sequences
|
||||
'\'' => {
|
||||
if self.eof() {
|
||||
return error!(self.loc, "unterminated char literal");
|
||||
}
|
||||
self.advance();
|
||||
if !self.match_char('\'') {
|
||||
return error!(self.loc, "expected ' after char literal");
|
||||
@@ -226,7 +239,7 @@ impl Tokenizer {
|
||||
self.advance();
|
||||
self.add_token(TokenType::String);
|
||||
}
|
||||
' ' | '\t' | '\r' => {}
|
||||
' ' | '\r' => {}
|
||||
'\n' => {
|
||||
self.loc.line += 1;
|
||||
self.loc.column = 1;
|
||||
@@ -280,25 +293,29 @@ impl Tokenizer {
|
||||
fn count_indentation(&mut self) -> usize {
|
||||
let mut count = 0;
|
||||
|
||||
while self.peek() == ' ' || self.peek() == '\t' {
|
||||
if self.peek() == ' ' {
|
||||
while self.peek() == ' ' {
|
||||
count += 1;
|
||||
}
|
||||
if self.peek() == '\t' {
|
||||
count += 4;
|
||||
}
|
||||
self.advance();
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
fn scan_number(&mut self) {
|
||||
if self.match_char('x') {
|
||||
while self.peek().is_ascii_hexdigit() {
|
||||
self.advance();
|
||||
}
|
||||
} else if self.match_char('o') {
|
||||
while matches!(self.peek(), '0'..='7') {
|
||||
self.advance();
|
||||
}
|
||||
} else {
|
||||
while self.peek().is_ascii_digit() {
|
||||
self.advance();
|
||||
}
|
||||
|
||||
if self.peek() == '.'
|
||||
&& (self.current + 1 >= self.source.len())
|
||||
&& self.current + 1 < self.source.len()
|
||||
&& self.source[self.current + 1].is_ascii_digit()
|
||||
{
|
||||
self.advance();
|
||||
@@ -306,16 +323,13 @@ impl Tokenizer {
|
||||
self.advance();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add_token(TokenType::Number);
|
||||
}
|
||||
|
||||
fn scan_identifier(&mut self) {
|
||||
while self.peek().is_alphanumeric()
|
||||
|| self.peek() == '_'
|
||||
|| self.peek() == '.'
|
||||
|| self.peek() == '!'
|
||||
{
|
||||
while self.peek().is_alphanumeric() || self.peek() == '_' || self.peek() == '.' {
|
||||
self.advance();
|
||||
}
|
||||
|
||||
@@ -331,6 +345,8 @@ impl Tokenizer {
|
||||
"return" => TokenType::KeywordReturn,
|
||||
"break" => TokenType::KeywordBreak,
|
||||
"continue" => TokenType::KeywordContinue,
|
||||
"extern" => TokenType::KeywordExtern,
|
||||
"export" => TokenType::KeywordExport,
|
||||
"true" => TokenType::True,
|
||||
"false" => TokenType::False,
|
||||
_ => TokenType::Identifier,
|
||||
@@ -342,6 +358,7 @@ impl Tokenizer {
|
||||
false
|
||||
} else {
|
||||
self.current += 1;
|
||||
self.loc.column += 1;
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
72
test.zr
72
test.zr
@@ -1,28 +1,60 @@
|
||||
func run_test[x: String] : Void
|
||||
c.printf("\033[93mBuilding %s...\033[0m", x)
|
||||
let cmd: String = str.concat("./target/release/zern examples/", x)
|
||||
func run_test[x: str] : void
|
||||
if str.equal(x, "puzzles") | str.equal(x, "raylib.zr") | str.equal(x, "x11.zr") | str.equal(x, "sqlite_todo.zr")
|
||||
io.print("\033[93mSkipping ")
|
||||
io.print(x)
|
||||
io.println("...\033[0m")
|
||||
return 0
|
||||
|
||||
let build_start_time: I64 = os.time()
|
||||
if c.system(cmd) != 0
|
||||
c.exit(1)
|
||||
let build_end_time: I64 = os.time()
|
||||
io.print("\033[93mBuilding ")
|
||||
io.print(x)
|
||||
io.print("...\033[0m ")
|
||||
let cmd: str = str.concat("./target/release/zern examples/", x)
|
||||
|
||||
c.free(cmd)
|
||||
c.printf(" %ldms\n", build_end_time - build_start_time)
|
||||
let build_start_time: i64 = os.time()
|
||||
if os.shell(cmd) != 0
|
||||
os.exit(1)
|
||||
let build_end_time: i64 = os.time()
|
||||
|
||||
if c.strcmp(x, "guess_number.zr") == 0 | c.strcmp(x, "tcp_server.zr") == 0
|
||||
c.printf("\033[93mSkipping %s...\033[0m\n", x)
|
||||
mem.free(cmd)
|
||||
io.print_i64(build_end_time - build_start_time)
|
||||
io.println("ms")
|
||||
|
||||
if str.find(x, "/aoc") != -1 | str.equal(x, "guess_number.zr") | str.equal(x, "tcp_server.zr")
|
||||
io.print("\033[93mSkipping ")
|
||||
io.print(x)
|
||||
io.println("...\033[0m")
|
||||
return 0
|
||||
|
||||
io.print("\033[93mRunning ")
|
||||
io.print(x)
|
||||
io.println("...\033[0m")
|
||||
|
||||
let run_start_time: i64 = os.time()
|
||||
if str.equal(x, "curl.zr")
|
||||
if os.shell("./out http://example.com") != 0
|
||||
os.exit(1)
|
||||
else
|
||||
let run_start_time: I64 = os.time()
|
||||
if c.system("./out") != 0
|
||||
c.exit(1)
|
||||
let run_end_time: I64 = os.time()
|
||||
if os.shell("./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[93mRunning ")
|
||||
io.print(x)
|
||||
io.print(" took\033[0m ")
|
||||
io.print_i64(run_end_time - run_start_time)
|
||||
io.println("ms")
|
||||
|
||||
func main[] : I64
|
||||
c.system("cargo build --release")
|
||||
func main[] : i64
|
||||
os.shell("cargo build --release")
|
||||
|
||||
let files: Array = os.listdir("examples/")
|
||||
let files: array = os.listdir("examples/")
|
||||
for i in 0..array.size(files)
|
||||
run_test(files[i])
|
||||
run_test(array.nth(files, i))
|
||||
|
||||
array.free(files)
|
||||
|
||||
let puzzle_files: array = os.listdir("examples/puzzles/")
|
||||
for i in 0..array.size(puzzle_files)
|
||||
run_test(str.concat("puzzles/", array.nth(puzzle_files, i)))
|
||||
|
||||
array.free(puzzle_files)
|
||||
Reference in New Issue
Block a user