Compare commits
14 Commits
7259f13191
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
af08d1888a
|
|||
|
9bbd382e10
|
|||
|
aea9ce911a
|
|||
|
873aa3e1cc
|
|||
|
bc4e09805b
|
|||
|
3a4a499cf6
|
|||
|
131db6a453
|
|||
|
43a20c99f6
|
|||
|
6fe971c615
|
|||
|
8ad69be5aa
|
|||
|
5a09699845
|
|||
|
3f3f161151
|
|||
|
e4582ec85a
|
|||
|
213e3d4b37
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,3 +2,5 @@
|
|||||||
/out*
|
/out*
|
||||||
/TODO
|
/TODO
|
||||||
/vscode
|
/vscode
|
||||||
|
/fuzz
|
||||||
|
/perf.data*
|
||||||
|
|||||||
@@ -4,14 +4,15 @@ A very cool language
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
* Clean indentation-based syntax
|
* Clean indentation-based syntax
|
||||||
* Compiles to x86_64 Assembly
|
* Compiles to x86-64 Assembly
|
||||||
* No libc required!
|
* No libc required!
|
||||||
* Produces tiny static executables (11KB for `hello.zr`)
|
* Produces tiny static executables (11KB for `hello.zr`)
|
||||||
* Has type inference, [UFCS](https://en.wikipedia.org/wiki/Uniform_function_call_syntax), variadics, dynamic arrays, hashmaps, DNS resolver, etc.
|
* Has static typing, [UFCS](https://en.wikipedia.org/wiki/Uniform_function_call_syntax), variadics, dynamic arrays, hashmaps, DNS resolver, etc.
|
||||||
* Growing [standard library](https://git.ton1.dev/toni/zern/src/branch/main/src/std)
|
|
||||||
|
|
||||||
## Syntax
|
## Syntax
|
||||||
```rust
|
```rust
|
||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
answer := os.urandom_i64()->abs() % 100
|
answer := os.urandom_i64()->abs() % 100
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
// https://brainfuck.org/sierpinski.b
|
// https://brainfuck.org/sierpinski.b
|
||||||
src := "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
src := "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
// needs to be compiled with -m -C "-lraylib"
|
// needs to be compiled with -m -C "-lraylib"
|
||||||
|
include "$/io.zr"
|
||||||
|
include "$/os.zr"
|
||||||
|
|
||||||
extern InitWindow
|
extern InitWindow
|
||||||
extern SetTargetFPS
|
extern SetTargetFPS
|
||||||
extern WindowShouldClose
|
extern WindowShouldClose
|
||||||
@@ -49,7 +52,7 @@ func CHIP8.free[c: CHIP8] : void
|
|||||||
c->keyboard->free()
|
c->keyboard->free()
|
||||||
c->display->free()
|
c->display->free()
|
||||||
c->keyboard_map->free()
|
c->keyboard_map->free()
|
||||||
mem.free(c)
|
(c as ptr)->free()
|
||||||
|
|
||||||
func CHIP8.disassemble[c: CHIP8, ins_count: i64] : void
|
func CHIP8.disassemble[c: CHIP8, ins_count: i64] : void
|
||||||
for i in 0..ins_count
|
for i in 0..ins_count
|
||||||
@@ -275,7 +278,7 @@ func main[argc: i64, argv: ptr] : i64
|
|||||||
disassemble := false
|
disassemble := false
|
||||||
|
|
||||||
for i in 1..argc
|
for i in 1..argc
|
||||||
arg := mem.read64(argv + i * 8) as str
|
arg := os.arg(argv, i)
|
||||||
if arg->equal("-d")
|
if arg->equal("-d")
|
||||||
disassemble = true
|
disassemble = true
|
||||||
else
|
else
|
||||||
@@ -288,7 +291,7 @@ func main[argc: i64, argv: ptr] : i64
|
|||||||
c := new CHIP8
|
c := new CHIP8
|
||||||
c->init()
|
c->init()
|
||||||
|
|
||||||
~buffer, ok := io.read_binary_file(path)
|
buffer, ok := io.read_binary_file(path)
|
||||||
if !ok
|
if !ok
|
||||||
panic("failed to read file")
|
panic("failed to read file")
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
include "$/net.zr"
|
||||||
|
|
||||||
func main[argc: i64, argv: ptr] : i64
|
func main[argc: i64, argv: ptr] : i64
|
||||||
if argc < 2
|
if argc < 2
|
||||||
io.println("ERROR: url is missing")
|
io.println("ERROR: url is missing")
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
url := mem.read64(argv + 8) as str
|
url := os.arg(argv, 1)
|
||||||
|
|
||||||
if url->len() <= 7
|
if url->len() <= 7
|
||||||
io.println("ERROR: invalid url (Did you forget \"http://\"?)")
|
io.println("ERROR: invalid url (Did you forget \"http://\"?)")
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
if !url->substr(0, 7)->equal("http://")
|
if !url->substr_n(0, 7)->equal("http://")
|
||||||
io.println("ERROR: only http scheme is supported")
|
io.println("ERROR: only http scheme is supported")
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
@@ -21,23 +24,19 @@ func main[argc: i64, argv: ptr] : i64
|
|||||||
break
|
break
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
host := url->substr(host_start, i - host_start)
|
host := url->substr_n(host_start, i - host_start)
|
||||||
path := "/"
|
path := "/"
|
||||||
if i < url_len
|
if i < url_len
|
||||||
path = url->substr(i, url_len - i)
|
path = url->substr_n(i, url_len - i)
|
||||||
|
|
||||||
~s, ok := net.connect(host, 80)
|
s, ok := net.connect(host, 80)
|
||||||
if !ok
|
if !ok
|
||||||
panic("failed to connect")
|
panic("failed to connect")
|
||||||
|
|
||||||
req := new str.Builder
|
// seems reasonable
|
||||||
req->append("GET ")
|
req := _stackalloc(4096) as str
|
||||||
req->append(path)
|
req_size := req->format_into(4096, "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", path, host)
|
||||||
req->append(" HTTP/1.0\r\nHost: ")
|
net.send(s, req as ptr, req_size)
|
||||||
req->append(host)
|
|
||||||
req->append("\r\nConnection: close\r\n\r\n")
|
|
||||||
net.send(s, req->data, req->size)
|
|
||||||
req->destroy()
|
|
||||||
|
|
||||||
header_buf := mem.alloc(8192) as str
|
header_buf := mem.alloc(8192) as str
|
||||||
header_size := 0
|
header_size := 0
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
sum := 0
|
sum := 0
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
sum := 0
|
sum := 0
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
sum := 0
|
sum := 0
|
||||||
a := 0
|
a := 0
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
n := 600851475143
|
n := 600851475143
|
||||||
f := 2
|
f := 2
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
out := 0
|
out := 0
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
out := 1
|
out := 1
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
sum_of_squares := 0
|
sum_of_squares := 0
|
||||||
for i in 1..101
|
for i in 1..101
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
found := 0
|
found := 0
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
n := "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
|
n := "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
for a in 1..1000
|
for a in 1..1000
|
||||||
for b in 1..1000
|
for b in 1..1000
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
sum := 0
|
sum := 0
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
include "$/containers.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
N := 20
|
N := 20
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func num_divisors[n: i64] : i64
|
func num_divisors[n: i64] : i64
|
||||||
end := n->isqrt()
|
end := n->isqrt()
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
n := 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
|
n := 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
|
||||||
io.println(n->to_str()->substr(0, 10))
|
io.println(n->to_str()->substr_n(0, 10))
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func collatz_num[n: i64] : i64
|
func collatz_num[n: i64] : i64
|
||||||
if n % 2 == 0
|
if n % 2 == 0
|
||||||
return n / 2
|
return n / 2
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
n := 40
|
n := 40
|
||||||
r := 20
|
r := 20
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
include "$/containers.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
n := [1]
|
n := [1]
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
include "$/containers.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
s1 := [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
|
s1 := [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
|
||||||
s2 := [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
|
s2 := [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
include "$/containers.zr"
|
||||||
|
|
||||||
func findmax[triangle: Array, row: i64, col: i64] : i64
|
func findmax[triangle: Array, row: i64, col: i64] : i64
|
||||||
if row == 14
|
if row == 14
|
||||||
return (triangle->nth(row) as Array)->nth(col)
|
return (triangle->nth(row) as Array)->nth(col)
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func days[y: i64, m: i64] : i64
|
func days[y: i64, m: i64] : i64
|
||||||
if m == 2
|
if m == 2
|
||||||
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
|
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
include "$/containers.zr"
|
||||||
|
|
||||||
func multiply[n: Array, x: i64] : void
|
func multiply[n: Array, x: i64] : void
|
||||||
carry := 0
|
carry := 0
|
||||||
for i in 0..n->size
|
for i in 0..n->size
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func divisors_sum[n: i64] : i64
|
func divisors_sum[n: i64] : i64
|
||||||
k := n
|
k := n
|
||||||
sum := 1
|
sum := 1
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
a := 0
|
a := 0
|
||||||
b := 1
|
b := 1
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
for i in 1..40
|
for i in 1..40
|
||||||
if i % 15 == 0
|
if i % 15 == 0
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
include "$/os.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
answer := os.urandom_i64()->abs() % 100
|
answer := os.urandom_i64()->abs() % 100
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
func main[] : i64
|
|
||||||
io.println("Hello, World!")
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
func main[] : i64
|
|
||||||
arr := []
|
|
||||||
for i in 0..10
|
|
||||||
arr->push(os.urandom_i64()->abs() % 1000)
|
|
||||||
|
|
||||||
for i in 0..arr->size
|
|
||||||
io.println_i64(arr->nth(i))
|
|
||||||
io.println("------------")
|
|
||||||
|
|
||||||
arr->quicksort()
|
|
||||||
|
|
||||||
for i in 0..arr->size
|
|
||||||
io.println_i64(arr->nth(i))
|
|
||||||
|
|
||||||
arr->free()
|
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
// needs to be compiled with -m -C "-lraylib"
|
// needs to be compiled with -m -C "-lraylib"
|
||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
extern InitWindow
|
extern InitWindow
|
||||||
extern SetTargetFPS
|
extern SetTargetFPS
|
||||||
extern WindowShouldClose
|
extern WindowShouldClose
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
include "$/containers.zr"
|
||||||
|
|
||||||
func rule110_step[state: Array] : void
|
func rule110_step[state: Array] : void
|
||||||
new_state := []
|
new_state := []
|
||||||
|
|
||||||
|
|||||||
96
examples/serve.zr
Normal file
96
examples/serve.zr
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
// needs to be compiled with -m -C "-lpthread"
|
||||||
|
include "$/net.zr"
|
||||||
|
|
||||||
|
extern pthread_create
|
||||||
|
extern pthread_detach
|
||||||
|
|
||||||
|
struct ConnData
|
||||||
|
sock: i64
|
||||||
|
addr: str
|
||||||
|
|
||||||
|
func handle_connection[conn: ConnData] : void
|
||||||
|
req := _stackalloc(10001) as str
|
||||||
|
n := net.read(conn->sock, req as ptr, 10000)
|
||||||
|
if n <= 0
|
||||||
|
net.close(conn->sock)
|
||||||
|
conn->addr->free()
|
||||||
|
(conn as ptr)->free()
|
||||||
|
return
|
||||||
|
|
||||||
|
req[n] = 0
|
||||||
|
|
||||||
|
pos := req->find("\r\n")
|
||||||
|
if pos == -1
|
||||||
|
panic("TODO: bad request")
|
||||||
|
|
||||||
|
first_line := req->substr_n(0, pos)
|
||||||
|
parts := first_line->split(" ")
|
||||||
|
method := parts->nth(0) as str
|
||||||
|
path := parts->nth(1) as str
|
||||||
|
|
||||||
|
if !method->equal("GET")
|
||||||
|
// TODO: method not supported
|
||||||
|
return
|
||||||
|
|
||||||
|
if path->equal("/")
|
||||||
|
// TODO: index
|
||||||
|
return
|
||||||
|
|
||||||
|
// TODO: ANY validation
|
||||||
|
file_path := "."->concat(path)
|
||||||
|
|
||||||
|
file_size, ok := os.get_file_size(file_path)
|
||||||
|
if !ok
|
||||||
|
// TODO: not found
|
||||||
|
return
|
||||||
|
|
||||||
|
fd := _builtin_syscall(SYS_openat, AT_FDCWD, file_path, O_RDONLY, 0)
|
||||||
|
if fd < 0
|
||||||
|
// TODO: not found
|
||||||
|
return
|
||||||
|
|
||||||
|
resp := "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n"
|
||||||
|
net.send(conn->sock, resp as ptr, resp->len())
|
||||||
|
|
||||||
|
offset := 0
|
||||||
|
while offset < file_size
|
||||||
|
sent := _builtin_syscall(SYS_sendfile, conn->sock, fd, ^offset, IP_MAXPACKET)
|
||||||
|
if sent <= 0
|
||||||
|
break
|
||||||
|
|
||||||
|
net.close(conn->sock)
|
||||||
|
|
||||||
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
file_path->free()
|
||||||
|
first_line->free()
|
||||||
|
parts->free_with_items()
|
||||||
|
conn->addr->free()
|
||||||
|
(conn as ptr)->free()
|
||||||
|
|
||||||
|
func main[argc: i64, argv: ptr] : i64
|
||||||
|
port := 8000
|
||||||
|
|
||||||
|
if argc > 1
|
||||||
|
port = os.arg(argv, 1)->parse_i64()
|
||||||
|
|
||||||
|
server_sock, ok := net.listen("0.0.0.0", port)
|
||||||
|
if !ok
|
||||||
|
panic("failed to listen")
|
||||||
|
|
||||||
|
io.printf("Listening on :%d...\n", port)
|
||||||
|
|
||||||
|
addr := _stackalloc(16)
|
||||||
|
while true
|
||||||
|
conn := net.accept(server_sock, addr)
|
||||||
|
if conn < 0
|
||||||
|
io.println("ERROR: failed to accept connection")
|
||||||
|
continue
|
||||||
|
|
||||||
|
conn_data := new* ConnData
|
||||||
|
conn_data->sock = conn
|
||||||
|
conn_data->addr = mem.alloc(30) as str
|
||||||
|
conn_data->addr->format_into(30, "%d.%d.%d.%d:%d", addr[4], addr[5], addr[6], addr[7], mem.read16be(addr + 2))
|
||||||
|
|
||||||
|
thread := 0
|
||||||
|
pthread_create(^thread, 0, ^handle_connection, conn_data)
|
||||||
|
pthread_detach(thread)
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
// needs to be compiled with -m -C "-lsqlite3"
|
// needs to be compiled with -m -C "-lsqlite3"
|
||||||
|
include "$/io.zr"
|
||||||
|
|
||||||
extern sqlite3_open
|
extern sqlite3_open
|
||||||
extern sqlite3_exec
|
extern sqlite3_exec
|
||||||
extern sqlite3_prepare_v2
|
extern sqlite3_prepare_v2
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
func main[] : i64
|
|
||||||
~s, ok := net.listen("127.0.0.1", 8000)
|
|
||||||
if !ok
|
|
||||||
panic("failed to listen")
|
|
||||||
|
|
||||||
io.println("Listening on port 8000...")
|
|
||||||
|
|
||||||
resp := mem.alloc(IP_MAXPACKET)
|
|
||||||
addr := _stackalloc(16)
|
|
||||||
while true
|
|
||||||
conn := net.accept(s, addr)
|
|
||||||
if conn < 0
|
|
||||||
continue
|
|
||||||
|
|
||||||
io.printf("%d.%d.%d.%d:%d\n", addr[4], addr[5], addr[6], addr[7], mem.read16be(addr + 2))
|
|
||||||
|
|
||||||
n := net.read(conn, resp, IP_MAXPACKET)
|
|
||||||
|
|
||||||
io.print_sized(resp, n)
|
|
||||||
io.println("")
|
|
||||||
|
|
||||||
net.send(conn, resp, n)
|
|
||||||
net.close(conn)
|
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
|
include "$/io.zr"
|
||||||
|
include "$/net.zr"
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
~s, ok := net.create_udp_server("127.0.0.1", 8000)
|
s, ok := net.create_udp_server("127.0.0.1", 8000)
|
||||||
if !ok
|
if !ok
|
||||||
panic("net.create_udp_server failed")
|
panic("net.create_udp_server failed")
|
||||||
|
|
||||||
|
|||||||
@@ -77,14 +77,12 @@ pub struct CodegenX86_64<'a> {
|
|||||||
data_counter: usize,
|
data_counter: usize,
|
||||||
pub symbol_table: &'a SymbolTable,
|
pub symbol_table: &'a SymbolTable,
|
||||||
pub expr_types: &'a HashMap<usize, String>,
|
pub expr_types: &'a HashMap<usize, String>,
|
||||||
emit_debug: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CodegenX86_64<'a> {
|
impl<'a> CodegenX86_64<'a> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
symbol_table: &'a SymbolTable,
|
symbol_table: &'a SymbolTable,
|
||||||
expr_types: &'a HashMap<usize, String>,
|
expr_types: &'a HashMap<usize, String>,
|
||||||
emit_debug: bool,
|
|
||||||
) -> CodegenX86_64<'a> {
|
) -> CodegenX86_64<'a> {
|
||||||
CodegenX86_64 {
|
CodegenX86_64 {
|
||||||
output: String::new(),
|
output: String::new(),
|
||||||
@@ -93,7 +91,6 @@ impl<'a> CodegenX86_64<'a> {
|
|||||||
data_counter: 1,
|
data_counter: 1,
|
||||||
symbol_table,
|
symbol_table,
|
||||||
expr_types,
|
expr_types,
|
||||||
emit_debug,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,41 +100,60 @@ impl<'a> CodegenX86_64<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_output(&self) -> String {
|
pub fn get_output(&self) -> String {
|
||||||
format!("section .data\n{}{}", self.data_section, self.output)
|
format!(".section .data\n{}{}", self.data_section, self.output)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn emit_prologue(&mut self, use_crt: bool) -> Result<(), ZernError> {
|
pub fn emit_prologue(&mut self, use_crt: bool) -> Result<(), ZernError> {
|
||||||
emit!(
|
emit!(
|
||||||
&mut self.output,
|
&mut self.output,
|
||||||
"section .note.GNU-stack
|
".intel_syntax noprefix
|
||||||
db 0
|
|
||||||
|
|
||||||
section .bss
|
.section .note.GNU-stack
|
||||||
_heap_head: resq 1
|
.byte 0
|
||||||
_heap_tail: resq 1
|
|
||||||
_environ: resq 1
|
|
||||||
|
|
||||||
section .text._builtin_heap_head
|
.section .bss
|
||||||
|
_heap_head: .zero 8
|
||||||
|
_heap_tail: .zero 8
|
||||||
|
_environ: .zero 8
|
||||||
|
|
||||||
|
.section .text._builtin_heap_head
|
||||||
_builtin_heap_head:
|
_builtin_heap_head:
|
||||||
lea rax, [rel _heap_head]
|
lea rax, [rip + _heap_head]
|
||||||
ret
|
ret
|
||||||
|
|
||||||
section .text._builtin_heap_tail
|
.section .text._builtin_heap_tail
|
||||||
_builtin_heap_tail:
|
_builtin_heap_tail:
|
||||||
lea rax, [rel _heap_tail]
|
lea rax, [rip + _heap_tail]
|
||||||
ret
|
ret
|
||||||
|
|
||||||
section .text._builtin_read64
|
.section .text._builtin_read64
|
||||||
_builtin_read64:
|
_builtin_read64:
|
||||||
mov rax, QWORD [rdi]
|
mov rax, QWORD PTR [rdi]
|
||||||
ret
|
ret
|
||||||
|
|
||||||
section .text._builtin_set64
|
.section .text._builtin_set64
|
||||||
_builtin_set64:
|
_builtin_set64:
|
||||||
mov [rdi], rsi
|
mov [rdi], rsi
|
||||||
ret
|
ret
|
||||||
|
|
||||||
section .text._builtin_syscall
|
.section .text._builtin_cvtsi2sd
|
||||||
|
_builtin_cvtsi2sd:
|
||||||
|
cvtsi2sd xmm0, rdi
|
||||||
|
movq rax, xmm0
|
||||||
|
ret
|
||||||
|
|
||||||
|
.section .text._builtin_cvttsd2si
|
||||||
|
_builtin_cvttsd2si:
|
||||||
|
cvttsd2si rax, xmm0
|
||||||
|
ret
|
||||||
|
|
||||||
|
.section .text._builtin_f64_to_f32
|
||||||
|
_builtin_f64_to_f32:
|
||||||
|
cvtsd2ss xmm0, xmm0
|
||||||
|
movd eax, xmm0
|
||||||
|
ret
|
||||||
|
|
||||||
|
.section .text._builtin_syscall
|
||||||
_builtin_syscall:
|
_builtin_syscall:
|
||||||
mov rax, rdi
|
mov rax, rdi
|
||||||
mov rdi, rsi
|
mov rdi, rsi
|
||||||
@@ -155,26 +171,26 @@ _builtin_syscall:
|
|||||||
emit!(
|
emit!(
|
||||||
&mut self.output,
|
&mut self.output,
|
||||||
"
|
"
|
||||||
section .text._builtin_environ
|
.section .text._builtin_environ
|
||||||
_builtin_environ:
|
_builtin_environ:
|
||||||
lea rax, [rel _environ]
|
lea rax, [rip + _environ]
|
||||||
mov rax, [rax]
|
mov rax, [rax]
|
||||||
ret
|
ret
|
||||||
|
|
||||||
global _start
|
.globl _start
|
||||||
section .text
|
.section .text
|
||||||
_start:
|
_start:
|
||||||
xor rbp, rbp
|
xor rbp, rbp
|
||||||
; setup args
|
// setup args
|
||||||
pop rdi
|
pop rdi
|
||||||
mov rsi, rsp
|
mov rsi, rsp
|
||||||
; save environ
|
// save environ
|
||||||
lea rdx, [rsi + rdi*8 + 8]
|
lea rdx, [rsi + rdi*8 + 8]
|
||||||
lea rax, [rel _environ]
|
lea rax, [rip + _environ]
|
||||||
mov [rax], rdx
|
mov [rax], rdx
|
||||||
; align stack
|
// align stack
|
||||||
and rsp, -16
|
and rsp, -16
|
||||||
; exit(main())
|
// exit(main())
|
||||||
call main
|
call main
|
||||||
mov rdi, rax
|
mov rdi, rax
|
||||||
mov rax, 60
|
mov rax, 60
|
||||||
@@ -185,10 +201,10 @@ _start:
|
|||||||
emit!(
|
emit!(
|
||||||
&mut self.output,
|
&mut self.output,
|
||||||
"
|
"
|
||||||
section .text._builtin_environ
|
.section .text._builtin_environ
|
||||||
_builtin_environ:
|
_builtin_environ:
|
||||||
extern environ
|
.extern environ
|
||||||
mov rax, [rel environ]
|
mov rax, [rip + environ]
|
||||||
ret
|
ret
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
@@ -222,7 +238,7 @@ _builtin_environ:
|
|||||||
|
|
||||||
self.compile_expr(env, initializer)?;
|
self.compile_expr(env, initializer)?;
|
||||||
let offset = env.define_var(name.lexeme.clone(), var_type);
|
let offset = env.define_var(name.lexeme.clone(), var_type);
|
||||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
emit!(&mut self.output, " mov QWORD PTR [rbp-{}], rax", offset);
|
||||||
}
|
}
|
||||||
Stmt::Assign { left, op, value } => {
|
Stmt::Assign { left, op, value } => {
|
||||||
self.compile_expr(env, value)?;
|
self.compile_expr(env, value)?;
|
||||||
@@ -233,7 +249,7 @@ _builtin_environ:
|
|||||||
let var = env.get_var(&name.lexeme).unwrap();
|
let var = env.get_var(&name.lexeme).unwrap();
|
||||||
emit!(
|
emit!(
|
||||||
&mut self.output,
|
&mut self.output,
|
||||||
" mov QWORD [rbp-{}], rax",
|
" mov QWORD PTR [rbp-{}], rax",
|
||||||
var.stack_offset,
|
var.stack_offset,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -249,7 +265,7 @@ _builtin_environ:
|
|||||||
emit!(&mut self.output, " pop rbx");
|
emit!(&mut self.output, " pop rbx");
|
||||||
emit!(&mut self.output, " add rbx, rax");
|
emit!(&mut self.output, " add rbx, rax");
|
||||||
emit!(&mut self.output, " pop rax");
|
emit!(&mut self.output, " pop rax");
|
||||||
emit!(&mut self.output, " mov BYTE [rbx], al");
|
emit!(&mut self.output, " mov BYTE PTR [rbx], al");
|
||||||
}
|
}
|
||||||
ExprKind::MemberAccess { left, field } => {
|
ExprKind::MemberAccess { left, field } => {
|
||||||
emit!(&mut self.output, " push rax");
|
emit!(&mut self.output, " push rax");
|
||||||
@@ -258,7 +274,7 @@ _builtin_environ:
|
|||||||
|
|
||||||
self.compile_expr(env, left)?;
|
self.compile_expr(env, left)?;
|
||||||
emit!(&mut self.output, " pop rbx");
|
emit!(&mut self.output, " pop rbx");
|
||||||
emit!(&mut self.output, " mov QWORD [rax+{}], rbx", offset);
|
emit!(&mut self.output, " mov QWORD PTR [rax+{}], rbx", offset);
|
||||||
}
|
}
|
||||||
_ => return error!(&op.loc, "invalid assignment target"),
|
_ => return error!(&op.loc, "invalid assignment target"),
|
||||||
};
|
};
|
||||||
@@ -285,7 +301,12 @@ _builtin_environ:
|
|||||||
env.define_var(target.lexeme.clone(), types[i].to_string())
|
env.define_var(target.lexeme.clone(), types[i].to_string())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
emit!(&mut self.output, " mov QWORD [rbp-{}], {}", offset, reg);
|
emit!(
|
||||||
|
&mut self.output,
|
||||||
|
" mov QWORD PTR [rbp-{}], {}",
|
||||||
|
offset,
|
||||||
|
reg
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::Const { .. } => {
|
Stmt::Const { .. } => {
|
||||||
@@ -348,14 +369,11 @@ _builtin_environ:
|
|||||||
exported,
|
exported,
|
||||||
} => {
|
} => {
|
||||||
let name = &name.lexeme;
|
let name = &name.lexeme;
|
||||||
if self.emit_debug || *exported || name == "main" {
|
if *exported || name == "main" {
|
||||||
emit!(
|
emit!(&mut self.output, ".globl {0}", name);
|
||||||
&mut self.output,
|
|
||||||
"global {0}:function ({0}.end - {0})",
|
|
||||||
name
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
emit!(&mut self.output, "section .text.{}", name);
|
emit!(&mut self.output, ".type {0}, @function", name);
|
||||||
|
emit!(&mut self.output, ".section .text.{}", name);
|
||||||
emit!(&mut self.output, "{}:", name);
|
emit!(&mut self.output, "{}:", name);
|
||||||
emit!(&mut self.output, " push rbp");
|
emit!(&mut self.output, " push rbp");
|
||||||
emit!(&mut self.output, " mov rbp, rsp");
|
emit!(&mut self.output, " mov rbp, rsp");
|
||||||
@@ -371,15 +389,20 @@ _builtin_environ:
|
|||||||
param.var_type.lexeme.clone(),
|
param.var_type.lexeme.clone(),
|
||||||
);
|
);
|
||||||
if let Some(reg) = REGISTERS.get(i) {
|
if let Some(reg) = REGISTERS.get(i) {
|
||||||
emit!(&mut self.output, " mov QWORD [rbp-{}], {}", offset, reg);
|
emit!(
|
||||||
|
&mut self.output,
|
||||||
|
" mov QWORD PTR [rbp-{}], {}",
|
||||||
|
offset,
|
||||||
|
reg
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
let stack_offset = 16 + 8 * (i - REGISTERS.len());
|
let stack_offset = 16 + 8 * (i - REGISTERS.len());
|
||||||
emit!(
|
emit!(
|
||||||
&mut self.output,
|
&mut self.output,
|
||||||
" mov rax, QWORD [rbp+{}]",
|
" mov rax, QWORD PTR [rbp+{}]",
|
||||||
stack_offset
|
stack_offset
|
||||||
);
|
);
|
||||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
emit!(&mut self.output, " mov QWORD PTR [rbp-{}], rax", offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -406,7 +429,7 @@ _builtin_environ:
|
|||||||
emit!(&mut self.output, " ret");
|
emit!(&mut self.output, " ret");
|
||||||
}
|
}
|
||||||
|
|
||||||
emit!(&mut self.output, ".end:");
|
emit!(&mut self.output, ".size {0}, . - {0}", name);
|
||||||
|
|
||||||
// patch the stack size after we know how much we actually need
|
// patch the stack size after we know how much we actually need
|
||||||
let patch = format!(" sub rsp, {:<10}", (env.next_offset + 15) & !15);
|
let patch = format!(" sub rsp, {:<10}", (env.next_offset + 15) & !15);
|
||||||
@@ -448,21 +471,29 @@ _builtin_environ:
|
|||||||
let offset = env.define_var(var.lexeme.clone(), "i64".into());
|
let offset = env.define_var(var.lexeme.clone(), "i64".into());
|
||||||
|
|
||||||
self.compile_expr(env, start)?;
|
self.compile_expr(env, start)?;
|
||||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
emit!(&mut self.output, " mov QWORD PTR [rbp-{}], rax", offset);
|
||||||
self.compile_expr(env, end)?;
|
self.compile_expr(env, end)?;
|
||||||
let end_offset = env.next_offset;
|
let end_offset = env.next_offset;
|
||||||
env.next_offset += 8;
|
env.next_offset += 8;
|
||||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", end_offset);
|
emit!(
|
||||||
|
&mut self.output,
|
||||||
|
" mov QWORD PTR [rbp-{}], rax",
|
||||||
|
end_offset
|
||||||
|
);
|
||||||
emit!(&mut self.output, "{}:", env.loop_begin_label);
|
emit!(&mut self.output, "{}:", env.loop_begin_label);
|
||||||
emit!(&mut self.output, " mov rax, QWORD [rbp-{}]", offset);
|
emit!(&mut self.output, " mov rax, QWORD PTR [rbp-{}]", offset);
|
||||||
emit!(&mut self.output, " mov rcx, QWORD [rbp-{}]", end_offset);
|
emit!(
|
||||||
|
&mut self.output,
|
||||||
|
" mov rcx, QWORD PTR [rbp-{}]",
|
||||||
|
end_offset
|
||||||
|
);
|
||||||
emit!(&mut self.output, " cmp rax, rcx");
|
emit!(&mut self.output, " cmp rax, rcx");
|
||||||
emit!(&mut self.output, " jge {}", env.loop_end_label);
|
emit!(&mut self.output, " jge {}", env.loop_end_label);
|
||||||
self.compile_stmt(env, body)?;
|
self.compile_stmt(env, body)?;
|
||||||
emit!(&mut self.output, "{}:", env.loop_continue_label);
|
emit!(&mut self.output, "{}:", env.loop_continue_label);
|
||||||
emit!(&mut self.output, " mov rax, QWORD [rbp-{}]", offset);
|
emit!(&mut self.output, " mov rax, QWORD PTR [rbp-{}]", offset);
|
||||||
emit!(&mut self.output, " add rax, 1");
|
emit!(&mut self.output, " add rax, 1");
|
||||||
emit!(&mut self.output, " mov QWORD [rbp-{}], rax", offset);
|
emit!(&mut self.output, " mov QWORD PTR [rbp-{}], rax", offset);
|
||||||
emit!(&mut self.output, " jmp {}", env.loop_begin_label);
|
emit!(&mut self.output, " jmp {}", env.loop_begin_label);
|
||||||
emit!(&mut self.output, "{}:", env.loop_end_label);
|
emit!(&mut self.output, "{}:", env.loop_end_label);
|
||||||
env.pop_scope();
|
env.pop_scope();
|
||||||
@@ -478,14 +509,11 @@ _builtin_environ:
|
|||||||
emit!(&mut self.output, " jmp {}", env.loop_continue_label);
|
emit!(&mut self.output, " jmp {}", env.loop_continue_label);
|
||||||
}
|
}
|
||||||
Stmt::Extern(name) => {
|
Stmt::Extern(name) => {
|
||||||
emit!(&mut self.output, "extern {}", name.lexeme);
|
emit!(&mut self.output, ".extern {}", name.lexeme);
|
||||||
}
|
}
|
||||||
Stmt::Struct { .. } => {
|
Stmt::Struct { .. } => {
|
||||||
// handled in SymbolTable
|
// handled in SymbolTable
|
||||||
}
|
}
|
||||||
Stmt::Enum { .. } => {
|
|
||||||
// handled in SymbolTable
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -592,6 +620,10 @@ _builtin_environ:
|
|||||||
TokenType::IntLiteral => {
|
TokenType::IntLiteral => {
|
||||||
emit!(&mut self.output, " mov rax, {}", token.lexeme);
|
emit!(&mut self.output, " mov rax, {}", token.lexeme);
|
||||||
}
|
}
|
||||||
|
TokenType::FloatLiteral => {
|
||||||
|
let value: f64 = token.lexeme.parse().unwrap();
|
||||||
|
emit!(&mut self.output, " mov rax, {}", value.to_bits());
|
||||||
|
}
|
||||||
TokenType::CharLiteral => {
|
TokenType::CharLiteral => {
|
||||||
emit!(
|
emit!(
|
||||||
&mut self.output,
|
&mut self.output,
|
||||||
@@ -611,15 +643,15 @@ _builtin_environ:
|
|||||||
.chain(std::iter::once("0".into()))
|
.chain(std::iter::once("0".into()))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(",");
|
.join(",");
|
||||||
emit!(&mut self.data_section, " {} db {}", label, charcodes);
|
emit!(&mut self.data_section, " {}: .byte {}", label, charcodes);
|
||||||
self.data_counter += 1;
|
self.data_counter += 1;
|
||||||
|
|
||||||
emit!(&mut self.output, " mov rax, {}", label);
|
emit!(&mut self.output, " lea rax, [rip + {}]", label);
|
||||||
}
|
}
|
||||||
TokenType::True => {
|
TokenType::KeywordTrue => {
|
||||||
emit!(&mut self.output, " mov rax, 1");
|
emit!(&mut self.output, " mov rax, 1");
|
||||||
}
|
}
|
||||||
TokenType::False => {
|
TokenType::KeywordFalse => {
|
||||||
emit!(&mut self.output, " mov rax, 0");
|
emit!(&mut self.output, " mov rax, 0");
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
@@ -628,7 +660,12 @@ _builtin_environ:
|
|||||||
self.compile_expr(env, right)?;
|
self.compile_expr(env, right)?;
|
||||||
match op.token_type {
|
match op.token_type {
|
||||||
TokenType::Minus => {
|
TokenType::Minus => {
|
||||||
emit!(&mut self.output, " neg rax");
|
if self.expr_types[&right.id] == "f64" {
|
||||||
|
emit!(&mut self.output, " mov rbx, 0x8000000000000000");
|
||||||
|
emit!(&mut self.output, " xor rax, rbx");
|
||||||
|
} else {
|
||||||
|
emit!(&mut self.output, " neg rax");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
TokenType::Bang => {
|
TokenType::Bang => {
|
||||||
emit!(&mut self.output, " test rax, rax");
|
emit!(&mut self.output, " test rax, rax");
|
||||||
@@ -650,7 +687,7 @@ _builtin_environ:
|
|||||||
let var = env.get_var(&name.lexeme).unwrap();
|
let var = env.get_var(&name.lexeme).unwrap();
|
||||||
emit!(
|
emit!(
|
||||||
&mut self.output,
|
&mut self.output,
|
||||||
" mov rax, QWORD [rbp-{}]",
|
" mov rax, QWORD PTR [rbp-{}]",
|
||||||
var.stack_offset,
|
var.stack_offset,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -682,8 +719,11 @@ _builtin_environ:
|
|||||||
emit!(&mut self.output, " push rax");
|
emit!(&mut self.output, " push rax");
|
||||||
}
|
}
|
||||||
|
|
||||||
let arg_count = args.len();
|
let arg_types: Vec<String> = args
|
||||||
self.emit_call_setup(arg_count);
|
.iter()
|
||||||
|
.map(|a| self.expr_types[&a.id].clone())
|
||||||
|
.collect();
|
||||||
|
self.emit_call_setup(&arg_types);
|
||||||
|
|
||||||
if let ExprKind::Variable(callee_name) = &callee.kind {
|
if let ExprKind::Variable(callee_name) = &callee.kind {
|
||||||
if self
|
if self
|
||||||
@@ -704,7 +744,7 @@ _builtin_environ:
|
|||||||
emit!(&mut self.output, " call rax");
|
emit!(&mut self.output, " call rax");
|
||||||
}
|
}
|
||||||
|
|
||||||
self.emit_call_cleanup(arg_count);
|
self.emit_call_cleanup(args.len());
|
||||||
}
|
}
|
||||||
ExprKind::ArrayLiteral(exprs) => {
|
ExprKind::ArrayLiteral(exprs) => {
|
||||||
emit!(&mut self.output, " mov rdi, 24");
|
emit!(&mut self.output, " mov rdi, 24");
|
||||||
@@ -735,12 +775,12 @@ _builtin_environ:
|
|||||||
self.compile_expr(env, index)?;
|
self.compile_expr(env, index)?;
|
||||||
emit!(&mut self.output, " pop rbx");
|
emit!(&mut self.output, " pop rbx");
|
||||||
emit!(&mut self.output, " add rax, rbx");
|
emit!(&mut self.output, " add rax, rbx");
|
||||||
emit!(&mut self.output, " movzx rax, BYTE [rax]");
|
emit!(&mut self.output, " movzx rax, BYTE PTR [rax]");
|
||||||
}
|
}
|
||||||
ExprKind::AddrOf { op, expr } => match &expr.kind {
|
ExprKind::AddrOf { op, expr } => match &expr.kind {
|
||||||
ExprKind::Variable(name) => {
|
ExprKind::Variable(name) => {
|
||||||
if self.symbol_table.functions.contains_key(&name.lexeme) {
|
if self.symbol_table.functions.contains_key(&name.lexeme) {
|
||||||
emit!(&mut self.output, " mov rax, {}", name.lexeme);
|
emit!(&mut self.output, " lea rax, [rip + {}]", name.lexeme);
|
||||||
} else {
|
} else {
|
||||||
let var = match env.get_var(&name.lexeme) {
|
let var = match env.get_var(&name.lexeme) {
|
||||||
Some(x) => x,
|
Some(x) => x,
|
||||||
@@ -753,7 +793,7 @@ _builtin_environ:
|
|||||||
};
|
};
|
||||||
emit!(
|
emit!(
|
||||||
&mut self.output,
|
&mut self.output,
|
||||||
" lea rax, QWORD [rbp-{}]",
|
" lea rax, QWORD PTR [rbp-{}]",
|
||||||
var.stack_offset,
|
var.stack_offset,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -772,7 +812,6 @@ _builtin_environ:
|
|||||||
if *use_heap {
|
if *use_heap {
|
||||||
emit!(&mut self.output, " mov rdi, {}", memory_size);
|
emit!(&mut self.output, " mov rdi, {}", memory_size);
|
||||||
emit!(&mut self.output, " call mem.alloc");
|
emit!(&mut self.output, " call mem.alloc");
|
||||||
emit!(&mut self.output, " push rax");
|
|
||||||
} else {
|
} else {
|
||||||
let aligned_size = (memory_size + 15) & !15;
|
let aligned_size = (memory_size + 15) & !15;
|
||||||
emit!(&mut self.output, " sub rsp, {}", aligned_size);
|
emit!(&mut self.output, " sub rsp, {}", aligned_size);
|
||||||
@@ -789,7 +828,7 @@ _builtin_environ:
|
|||||||
ExprKind::MemberAccess { left, field } => {
|
ExprKind::MemberAccess { left, field } => {
|
||||||
let offset = self.get_field_offset(left, field)?;
|
let offset = self.get_field_offset(left, field)?;
|
||||||
self.compile_expr(env, left)?;
|
self.compile_expr(env, left)?;
|
||||||
emit!(&mut self.output, " mov rax, QWORD [rax+{}]", offset);
|
emit!(&mut self.output, " mov rax, QWORD PTR [rax+{}]", offset);
|
||||||
}
|
}
|
||||||
ExprKind::Cast { expr, type_name: _ } => {
|
ExprKind::Cast { expr, type_name: _ } => {
|
||||||
self.compile_expr(env, expr)?;
|
self.compile_expr(env, expr)?;
|
||||||
@@ -805,50 +844,69 @@ _builtin_environ:
|
|||||||
emit!(&mut self.output, " push rax");
|
emit!(&mut self.output, " push rax");
|
||||||
}
|
}
|
||||||
|
|
||||||
let arg_count = 1 + args.len();
|
let mut arg_types = vec![];
|
||||||
self.emit_call_setup(arg_count);
|
arg_types.push(receiver_type.clone());
|
||||||
|
arg_types.extend(args.iter().map(|a| self.expr_types[&a.id].clone()));
|
||||||
|
|
||||||
|
self.emit_call_setup(&arg_types);
|
||||||
emit!(&mut self.output, " call {}", func_name);
|
emit!(&mut self.output, " call {}", func_name);
|
||||||
self.emit_call_cleanup(arg_count);
|
self.emit_call_cleanup(1 + args.len());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn emit_call_setup(&mut self, arg_count: usize) {
|
fn emit_call_setup(&mut self, arg_types: &[String]) {
|
||||||
if arg_count <= 6 {
|
let arg_count = arg_types.len();
|
||||||
for i in (0..arg_count).rev() {
|
|
||||||
emit!(&mut self.output, " pop {}", REGISTERS[i]);
|
let to_register = arg_count.min(6);
|
||||||
}
|
let mut fp_idx = 0;
|
||||||
} else {
|
let mut int_idx = 0;
|
||||||
for (i, reg) in REGISTERS.iter().enumerate() {
|
for (i, arg_type) in arg_types.iter().enumerate().take(to_register) {
|
||||||
let offset = 8 * (arg_count - 1 - i);
|
let offset = 8 * (arg_count - 1 - i);
|
||||||
emit!(
|
emit!(
|
||||||
&mut self.output,
|
&mut self.output,
|
||||||
" mov {}, QWORD [rsp + {}]",
|
" mov rax, QWORD PTR [rsp + {}]",
|
||||||
reg,
|
offset
|
||||||
offset
|
);
|
||||||
);
|
if arg_type == "f64" {
|
||||||
}
|
emit!(&mut self.output, " movq xmm{}, rax", fp_idx);
|
||||||
// TODO: since all zern values are 64bit large we currently cannot call
|
fp_idx += 1;
|
||||||
// external functions that expect a non-64bit value past the 6th argument
|
} else {
|
||||||
let num_stack = arg_count - 6;
|
emit!(&mut self.output, " mov {}, rax", REGISTERS[int_idx]);
|
||||||
for i in 0..num_stack {
|
int_idx += 1;
|
||||||
let arg_idx = arg_count - 1 - i;
|
|
||||||
let offset = 8 * (arg_count - 1 - arg_idx);
|
|
||||||
emit!(
|
|
||||||
&mut self.output,
|
|
||||||
" mov rax, QWORD [rsp + {}]",
|
|
||||||
offset + 8 * i
|
|
||||||
);
|
|
||||||
emit!(&mut self.output, " push rax");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: since all zern values are 64bit large we currently cannot call
|
||||||
|
// external functions that expect a non-64bit value past the 6th argument
|
||||||
|
let num_stack = arg_count.saturating_sub(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 PTR [rsp + {}]",
|
||||||
|
offset + 8 * i
|
||||||
|
);
|
||||||
|
emit!(&mut self.output, " push rax");
|
||||||
|
}
|
||||||
|
|
||||||
|
emit!(&mut self.output, " mov al, {}", fp_idx);
|
||||||
|
|
||||||
|
if num_stack == 0 {
|
||||||
|
emit!(&mut self.output, " add rsp, {}", 8 * to_register);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn emit_call_cleanup(&mut self, arg_count: usize) {
|
fn emit_call_cleanup(&mut self, arg_count: usize) {
|
||||||
if arg_count > 6 {
|
let num_stack = arg_count.saturating_sub(6);
|
||||||
emit!(&mut self.output, " add rsp, {}", 8 * (arg_count - 6));
|
if num_stack > 0 {
|
||||||
emit!(&mut self.output, " add rsp, {}", 8 * arg_count);
|
emit!(
|
||||||
|
&mut self.output,
|
||||||
|
" add rsp, {}",
|
||||||
|
8 * (arg_count + num_stack)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
45
src/main.rs
45
src/main.rs
@@ -5,44 +5,26 @@ mod tokenizer;
|
|||||||
mod typechecker;
|
mod typechecker;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
collections::HashSet,
|
||||||
fs,
|
fs,
|
||||||
path::Path,
|
|
||||||
process::{self, Command},
|
process::{self, Command},
|
||||||
};
|
};
|
||||||
|
|
||||||
use tokenizer::ZernError;
|
use tokenizer::ZernError;
|
||||||
|
|
||||||
macro_rules! parse_std_file {
|
|
||||||
($statements:expr, $filename:expr) => {
|
|
||||||
let source: String = include_str!($filename).into();
|
|
||||||
let tokenizer = tokenizer::Tokenizer::new($filename.to_owned(), source);
|
|
||||||
let parser = parser::Parser::new(tokenizer.tokenize()?);
|
|
||||||
$statements.extend(parser.parse()?);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
fn compile_file(args: Args) -> Result<(), ZernError> {
|
fn compile_file(args: Args) -> Result<(), ZernError> {
|
||||||
let source = match fs::read_to_string(&args.path) {
|
let source = match fs::read_to_string(&args.path) {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
Err(_) => {
|
Err(e) => {
|
||||||
eprintln!("\x1b[91mERROR\x1b[0m: failed to open {}", args.path);
|
eprintln!("\x1b[91mERROR\x1b[0m: failed to open {}: {e}", args.path);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let filename = Path::new(&args.path).file_name().unwrap().to_str().unwrap();
|
let mut included_paths = HashSet::new();
|
||||||
|
let tokenizer = tokenizer::Tokenizer::new(args.path, source, &mut included_paths);
|
||||||
let mut statements = Vec::new();
|
|
||||||
|
|
||||||
if args.include_stdlib {
|
|
||||||
parse_std_file!(statements, "std/std.zr");
|
|
||||||
parse_std_file!(statements, "std/net.zr");
|
|
||||||
parse_std_file!(statements, "std/linux_constants.zr");
|
|
||||||
}
|
|
||||||
|
|
||||||
let tokenizer = tokenizer::Tokenizer::new(filename.to_owned(), source);
|
|
||||||
let parser = parser::Parser::new(tokenizer.tokenize()?);
|
let parser = parser::Parser::new(tokenizer.tokenize()?);
|
||||||
statements.extend(parser.parse()?);
|
let statements = parser.parse()?;
|
||||||
|
|
||||||
let mut symbol_table = symbol_table::SymbolTable::new();
|
let mut symbol_table = symbol_table::SymbolTable::new();
|
||||||
for stmt in &statements {
|
for stmt in &statements {
|
||||||
@@ -54,8 +36,7 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
|
|||||||
typechecker.typecheck_stmt(&mut typechecker::Env::new(), stmt)?;
|
typechecker.typecheck_stmt(&mut typechecker::Env::new(), stmt)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut codegen =
|
let mut codegen = codegen_x86_64::CodegenX86_64::new(&symbol_table, &typechecker.expr_types);
|
||||||
codegen_x86_64::CodegenX86_64::new(&symbol_table, &typechecker.expr_types, args.emit_debug);
|
|
||||||
codegen.emit_prologue(args.use_crt)?;
|
codegen.emit_prologue(args.use_crt)?;
|
||||||
for stmt in statements {
|
for stmt in statements {
|
||||||
codegen.compile_stmt(&mut codegen_x86_64::Env::new(), &stmt)?;
|
codegen.compile_stmt(&mut codegen_x86_64::Env::new(), &stmt)?;
|
||||||
@@ -66,7 +47,9 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
|
|||||||
|
|
||||||
fs::write(format!("{out}.s"), codegen.get_output()).unwrap();
|
fs::write(format!("{out}.s"), codegen.get_output()).unwrap();
|
||||||
|
|
||||||
run_command(format!("nasm -f elf64 -o {out}.o {out}.s"));
|
let debug_flag = if args.emit_debug { "-g" } else { "" };
|
||||||
|
|
||||||
|
run_command(format!("as --64 {debug_flag} -o {out}.o {out}.s"));
|
||||||
|
|
||||||
if args.use_crt {
|
if args.use_crt {
|
||||||
run_command(format!(
|
run_command(format!(
|
||||||
@@ -114,7 +97,6 @@ struct Args {
|
|||||||
out: Option<String>,
|
out: Option<String>,
|
||||||
emit_only: bool,
|
emit_only: bool,
|
||||||
emit_debug: bool,
|
emit_debug: bool,
|
||||||
include_stdlib: bool,
|
|
||||||
run_exe: bool,
|
run_exe: bool,
|
||||||
use_crt: bool,
|
use_crt: bool,
|
||||||
cflags: String,
|
cflags: String,
|
||||||
@@ -129,7 +111,6 @@ impl Args {
|
|||||||
out: None,
|
out: None,
|
||||||
emit_only: false,
|
emit_only: false,
|
||||||
emit_debug: false,
|
emit_debug: false,
|
||||||
include_stdlib: true,
|
|
||||||
run_exe: false,
|
run_exe: false,
|
||||||
use_crt: false,
|
use_crt: false,
|
||||||
cflags: String::new(),
|
cflags: String::new(),
|
||||||
@@ -146,8 +127,6 @@ impl Args {
|
|||||||
}
|
}
|
||||||
} else if arg == "--emit-only" {
|
} else if arg == "--emit-only" {
|
||||||
out.emit_only = true;
|
out.emit_only = true;
|
||||||
} else if arg == "--no-stdlib" {
|
|
||||||
out.include_stdlib = false;
|
|
||||||
} else if arg == "-r" {
|
} else if arg == "-r" {
|
||||||
out.run_exe = true;
|
out.run_exe = true;
|
||||||
} else if arg == "-m" {
|
} else if arg == "-m" {
|
||||||
@@ -163,9 +142,7 @@ impl Args {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if arg == "-h" || arg == "--help" {
|
} else if arg == "-h" || arg == "--help" {
|
||||||
println!(
|
println!("Usage: zern [-o path] [-r] [-m] [-g] [-C cflags] [--emit-only] path");
|
||||||
"Usage: zern [-o path] [-r] [-m] [-g] [-C cflags] [--emit-only] [--no-stdlib] path"
|
|
||||||
);
|
|
||||||
process::exit(0);
|
process::exit(0);
|
||||||
} else if arg.starts_with('-') {
|
} else if arg.starts_with('-') {
|
||||||
eprintln!("\x1b[91mERROR\x1b[0m: unrecognized option: {arg}");
|
eprintln!("\x1b[91mERROR\x1b[0m: unrecognized option: {arg}");
|
||||||
|
|||||||
@@ -77,10 +77,6 @@ pub enum Stmt {
|
|||||||
name: Token,
|
name: Token,
|
||||||
fields: Vec<Param>,
|
fields: Vec<Param>,
|
||||||
},
|
},
|
||||||
Enum {
|
|
||||||
name: Token,
|
|
||||||
variants: Vec<Token>,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static NEXT_EXPR_ID: AtomicUsize = AtomicUsize::new(0);
|
pub static NEXT_EXPR_ID: AtomicUsize = AtomicUsize::new(0);
|
||||||
@@ -158,6 +154,7 @@ pub struct Parser {
|
|||||||
tokens: Vec<Token>,
|
tokens: Vec<Token>,
|
||||||
current: usize,
|
current: usize,
|
||||||
is_inside_function: bool,
|
is_inside_function: bool,
|
||||||
|
depth: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parser {
|
impl Parser {
|
||||||
@@ -166,6 +163,7 @@ impl Parser {
|
|||||||
tokens,
|
tokens,
|
||||||
current: 0,
|
current: 0,
|
||||||
is_inside_function: false,
|
is_inside_function: false,
|
||||||
|
depth: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,9 +193,6 @@ impl Parser {
|
|||||||
if self.match_token(&[TokenType::KeywordStruct]) {
|
if self.match_token(&[TokenType::KeywordStruct]) {
|
||||||
return self.struct_declaration();
|
return self.struct_declaration();
|
||||||
}
|
}
|
||||||
if self.match_token(&[TokenType::KeywordEnum]) {
|
|
||||||
return self.enum_declaration();
|
|
||||||
}
|
|
||||||
return error!(
|
return error!(
|
||||||
self.peek().loc,
|
self.peek().loc,
|
||||||
"statements not allowed outside function body"
|
"statements not allowed outside function body"
|
||||||
@@ -281,21 +276,6 @@ impl Parser {
|
|||||||
Ok(Stmt::Struct { name, fields })
|
Ok(Stmt::Struct { name, fields })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enum_declaration(&mut self) -> Result<Stmt, ZernError> {
|
|
||||||
let name = self.consume(TokenType::Identifier, "expected enum name")?;
|
|
||||||
|
|
||||||
self.consume(TokenType::Indent, "expected indent after enum name")?;
|
|
||||||
|
|
||||||
let mut variants = vec![];
|
|
||||||
while !self.eof() && !self.check(&TokenType::Dedent) {
|
|
||||||
variants.push(self.consume(TokenType::Identifier, "expected variant name")?);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.consume(TokenType::Dedent, "expected dedent after enum variants")?;
|
|
||||||
|
|
||||||
Ok(Stmt::Enum { name, variants })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn const_declaration(&mut self) -> Result<Stmt, ZernError> {
|
fn const_declaration(&mut self) -> Result<Stmt, ZernError> {
|
||||||
let name = self.consume(TokenType::Identifier, "expected const name")?;
|
let name = self.consume(TokenType::Identifier, "expected const name")?;
|
||||||
self.consume(TokenType::Equal, "expected '=' after const name")?;
|
self.consume(TokenType::Equal, "expected '=' after const name")?;
|
||||||
@@ -362,7 +342,7 @@ impl Parser {
|
|||||||
Ok(Stmt::Break)
|
Ok(Stmt::Break)
|
||||||
} else if self.match_token(&[TokenType::KeywordContinue]) {
|
} else if self.match_token(&[TokenType::KeywordContinue]) {
|
||||||
Ok(Stmt::Continue)
|
Ok(Stmt::Continue)
|
||||||
} else if self.match_token(&[TokenType::Tilde]) {
|
} else if self.check(&TokenType::Identifier) && self.check_ahead(&TokenType::Comma) {
|
||||||
let mut targets = vec![];
|
let mut targets = vec![];
|
||||||
loop {
|
loop {
|
||||||
targets.push(self.consume(Identifier, "expected an identifier")?);
|
targets.push(self.consume(Identifier, "expected an identifier")?);
|
||||||
@@ -468,7 +448,14 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn expression(&mut self) -> Result<Expr, ZernError> {
|
fn expression(&mut self) -> Result<Expr, ZernError> {
|
||||||
self.or_and()
|
self.depth += 1;
|
||||||
|
if self.depth > 200 {
|
||||||
|
return error!(self.previous().loc, "maximum expression depth reached");
|
||||||
|
}
|
||||||
|
|
||||||
|
let out = self.or_and();
|
||||||
|
self.depth -= 1;
|
||||||
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
fn or_and(&mut self) -> Result<Expr, ZernError> {
|
fn or_and(&mut self) -> Result<Expr, ZernError> {
|
||||||
@@ -675,10 +662,11 @@ impl Parser {
|
|||||||
fn primary(&mut self) -> Result<Expr, ZernError> {
|
fn primary(&mut self) -> Result<Expr, ZernError> {
|
||||||
if self.match_token(&[
|
if self.match_token(&[
|
||||||
TokenType::IntLiteral,
|
TokenType::IntLiteral,
|
||||||
|
TokenType::FloatLiteral,
|
||||||
TokenType::CharLiteral,
|
TokenType::CharLiteral,
|
||||||
TokenType::StringLiteral,
|
TokenType::StringLiteral,
|
||||||
TokenType::True,
|
TokenType::KeywordTrue,
|
||||||
TokenType::False,
|
TokenType::KeywordFalse,
|
||||||
]) {
|
]) {
|
||||||
Ok(Expr::new(ExprKind::Literal(self.previous().clone())))
|
Ok(Expr::new(ExprKind::Literal(self.previous().clone())))
|
||||||
} else if self.match_token(&[TokenType::LeftParen]) {
|
} else if self.match_token(&[TokenType::LeftParen]) {
|
||||||
|
|||||||
1145
src/std/std.zr
1145
src/std/std.zr
File diff suppressed because it is too large
Load Diff
@@ -5,31 +5,35 @@ use crate::{
|
|||||||
tokenizer::{ZernError, error},
|
tokenizer::{ZernError, error},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type Type = String;
|
|
||||||
|
|
||||||
pub struct StructField {
|
pub struct StructField {
|
||||||
pub offset: usize,
|
pub offset: usize,
|
||||||
pub field_type: Type,
|
pub field_type: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum FnParams {
|
||||||
|
Normal(Vec<String>),
|
||||||
|
Variadic,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct FnType {
|
pub struct FnType {
|
||||||
pub return_type: Type,
|
pub return_type: String,
|
||||||
pub params: Option<Vec<Type>>,
|
pub params: FnParams,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FnType {
|
impl FnType {
|
||||||
fn new(return_type: &str, params: Vec<&str>) -> FnType {
|
fn new(return_type: &str, params: Vec<&str>) -> FnType {
|
||||||
FnType {
|
FnType {
|
||||||
return_type: return_type.to_string(),
|
return_type: return_type.to_string(),
|
||||||
params: Some(params.iter().map(|x| x.to_string()).collect()),
|
params: FnParams::Normal(params.iter().map(|x| x.to_string()).collect()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_variadic(return_type: &str) -> FnType {
|
fn new_variadic(return_type: &str) -> FnType {
|
||||||
FnType {
|
FnType {
|
||||||
return_type: return_type.to_string(),
|
return_type: return_type.to_string(),
|
||||||
params: None,
|
params: FnParams::Variadic,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,6 +55,12 @@ impl SymbolTable {
|
|||||||
"_builtin_set64".into(),
|
"_builtin_set64".into(),
|
||||||
FnType::new("void", vec!["ptr", "i64"]),
|
FnType::new("void", vec!["ptr", "i64"]),
|
||||||
),
|
),
|
||||||
|
("_builtin_cvtsi2sd".into(), FnType::new("f64", vec!["i64"])),
|
||||||
|
("_builtin_cvttsd2si".into(), FnType::new("i64", vec!["f64"])),
|
||||||
|
(
|
||||||
|
"_builtin_f64_to_f32".into(),
|
||||||
|
FnType::new("any", vec!["f64"]),
|
||||||
|
),
|
||||||
("_builtin_syscall".into(), FnType::new_variadic("i64")),
|
("_builtin_syscall".into(), FnType::new_variadic("i64")),
|
||||||
("_builtin_environ".into(), FnType::new("ptr", vec![])),
|
("_builtin_environ".into(), FnType::new("ptr", vec![])),
|
||||||
("_var_arg".into(), FnType::new("any", vec!["i64"])),
|
("_var_arg".into(), FnType::new("any", vec!["i64"])),
|
||||||
@@ -68,11 +78,15 @@ impl SymbolTable {
|
|||||||
return error!(name.loc, format!("tried to redefine '{}'", name.lexeme));
|
return error!(name.loc, format!("tried to redefine '{}'", name.lexeme));
|
||||||
}
|
}
|
||||||
let mut value = if value.lexeme.starts_with("0x") {
|
let mut value = if value.lexeme.starts_with("0x") {
|
||||||
u64::from_str_radix(&value.lexeme[2..], 16).unwrap()
|
match u64::from_str_radix(&value.lexeme[2..], 16) {
|
||||||
} else if value.lexeme.starts_with("0o") {
|
Ok(v) => v,
|
||||||
u64::from_str_radix(&value.lexeme[2..], 8).unwrap()
|
Err(_) => return error!(value.loc, "failed to parse hex numeric constant"),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
value.lexeme.parse().unwrap()
|
match value.lexeme.parse() {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(_) => return error!(value.loc, "failed to parse numeric constant"),
|
||||||
|
}
|
||||||
} as i64;
|
} as i64;
|
||||||
if *neg {
|
if *neg {
|
||||||
value = -value;
|
value = -value;
|
||||||
@@ -106,7 +120,7 @@ impl SymbolTable {
|
|||||||
name.lexeme.clone(),
|
name.lexeme.clone(),
|
||||||
FnType {
|
FnType {
|
||||||
return_type,
|
return_type,
|
||||||
params: Some(
|
params: FnParams::Normal(
|
||||||
params.iter().map(|x| x.var_type.lexeme.clone()).collect(),
|
params.iter().map(|x| x.var_type.lexeme.clone()).collect(),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
@@ -115,7 +129,7 @@ impl SymbolTable {
|
|||||||
name.lexeme.clone(),
|
name.lexeme.clone(),
|
||||||
FnType {
|
FnType {
|
||||||
return_type,
|
return_type,
|
||||||
params: None,
|
params: FnParams::Variadic,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
@@ -140,16 +154,6 @@ impl SymbolTable {
|
|||||||
|
|
||||||
self.structs.insert(name.lexeme.clone(), fields_map);
|
self.structs.insert(name.lexeme.clone(), fields_map);
|
||||||
}
|
}
|
||||||
Stmt::Enum { name, variants } => {
|
|
||||||
// TODO: distinct enum types
|
|
||||||
for (i, v) in variants.iter().enumerate() {
|
|
||||||
let variant = format!("{}.{}", name.lexeme, v.lexeme);
|
|
||||||
if self.is_name_defined(&variant) {
|
|
||||||
return error!(name.loc, format!("tried to redefine '{}'", variant));
|
|
||||||
}
|
|
||||||
self.constants.insert(variant, i as i64);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
153
src/tokenizer.rs
153
src/tokenizer.rs
@@ -1,4 +1,9 @@
|
|||||||
use std::{cmp::Ordering, fmt};
|
use std::{
|
||||||
|
cmp::Ordering,
|
||||||
|
collections::HashSet,
|
||||||
|
fmt, fs,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum TokenType {
|
pub enum TokenType {
|
||||||
@@ -25,7 +30,6 @@ pub enum TokenType {
|
|||||||
ShiftLeft,
|
ShiftLeft,
|
||||||
ShiftRight,
|
ShiftRight,
|
||||||
Arrow,
|
Arrow,
|
||||||
Tilde,
|
|
||||||
|
|
||||||
Equal,
|
Equal,
|
||||||
DoubleEqual,
|
DoubleEqual,
|
||||||
@@ -39,8 +43,7 @@ pub enum TokenType {
|
|||||||
StringLiteral,
|
StringLiteral,
|
||||||
CharLiteral,
|
CharLiteral,
|
||||||
IntLiteral,
|
IntLiteral,
|
||||||
True,
|
FloatLiteral,
|
||||||
False,
|
|
||||||
|
|
||||||
KeywordConst,
|
KeywordConst,
|
||||||
KeywordIf,
|
KeywordIf,
|
||||||
@@ -57,7 +60,8 @@ pub enum TokenType {
|
|||||||
KeywordStruct,
|
KeywordStruct,
|
||||||
KeywordNew,
|
KeywordNew,
|
||||||
KeywordAs,
|
KeywordAs,
|
||||||
KeywordEnum,
|
KeywordTrue,
|
||||||
|
KeywordFalse,
|
||||||
|
|
||||||
Indent,
|
Indent,
|
||||||
Dedent,
|
Dedent,
|
||||||
@@ -109,7 +113,7 @@ pub struct Token {
|
|||||||
pub loc: Loc,
|
pub loc: Loc,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Tokenizer {
|
pub struct Tokenizer<'a> {
|
||||||
source: Vec<char>,
|
source: Vec<char>,
|
||||||
tokens: Vec<Token>,
|
tokens: Vec<Token>,
|
||||||
indent_stack: Vec<usize>,
|
indent_stack: Vec<usize>,
|
||||||
@@ -117,10 +121,15 @@ pub struct Tokenizer {
|
|||||||
start: usize,
|
start: usize,
|
||||||
current: usize,
|
current: usize,
|
||||||
loc: Loc,
|
loc: Loc,
|
||||||
|
included_paths: &'a mut HashSet<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tokenizer {
|
impl<'a> Tokenizer<'a> {
|
||||||
pub fn new(filename: String, source: String) -> Tokenizer {
|
pub fn new(
|
||||||
|
filename: String,
|
||||||
|
source: String,
|
||||||
|
included_paths: &'a mut HashSet<PathBuf>,
|
||||||
|
) -> Tokenizer<'a> {
|
||||||
Tokenizer {
|
Tokenizer {
|
||||||
source: source.chars().collect(),
|
source: source.chars().collect(),
|
||||||
tokens: vec![],
|
tokens: vec![],
|
||||||
@@ -133,6 +142,7 @@ impl Tokenizer {
|
|||||||
line: 1,
|
line: 1,
|
||||||
column: 1,
|
column: 1,
|
||||||
},
|
},
|
||||||
|
included_paths,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,7 +178,6 @@ impl Tokenizer {
|
|||||||
'%' => self.add_token(TokenType::Mod)?,
|
'%' => self.add_token(TokenType::Mod)?,
|
||||||
'^' => self.add_token(TokenType::Xor)?,
|
'^' => self.add_token(TokenType::Xor)?,
|
||||||
':' => self.add_token(TokenType::Colon)?,
|
':' => self.add_token(TokenType::Colon)?,
|
||||||
'~' => self.add_token(TokenType::Tilde)?,
|
|
||||||
'-' => {
|
'-' => {
|
||||||
if self.match_char('=') {
|
if self.match_char('=') {
|
||||||
self.add_token(TokenType::MinusEqual)?
|
self.add_token(TokenType::MinusEqual)?
|
||||||
@@ -245,6 +254,9 @@ impl Tokenizer {
|
|||||||
return error!(self.loc, "unterminated char literal");
|
return error!(self.loc, "unterminated char literal");
|
||||||
}
|
}
|
||||||
_ = self.match_char('\\'); // if its an escape sequence skip \ and read one more
|
_ = self.match_char('\\'); // if its an escape sequence skip \ and read one more
|
||||||
|
if self.eof() {
|
||||||
|
return error!(self.loc, "unterminated char literal");
|
||||||
|
}
|
||||||
self.advance();
|
self.advance();
|
||||||
if !self.match_char('\'') {
|
if !self.match_char('\'') {
|
||||||
return error!(self.loc, "expected ' after char literal");
|
return error!(self.loc, "expected ' after char literal");
|
||||||
@@ -257,6 +269,12 @@ impl Tokenizer {
|
|||||||
while !self.eof() {
|
while !self.eof() {
|
||||||
if self.peek() == '\\' {
|
if self.peek() == '\\' {
|
||||||
self.advance();
|
self.advance();
|
||||||
|
if self.eof() {
|
||||||
|
return error!(
|
||||||
|
self.loc,
|
||||||
|
format!("unterminated string, started at {}", start_loc)
|
||||||
|
);
|
||||||
|
}
|
||||||
} else if self.peek() == '"' {
|
} else if self.peek() == '"' {
|
||||||
break;
|
break;
|
||||||
} else if self.peek() == '\n' {
|
} else if self.peek() == '\n' {
|
||||||
@@ -338,21 +356,39 @@ impl Tokenizer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn scan_number(&mut self) -> Result<(), ZernError> {
|
fn scan_number(&mut self) -> Result<(), ZernError> {
|
||||||
if self.match_char('x') {
|
let mut is_float = false;
|
||||||
|
|
||||||
|
if self.source[self.current - 1] == '0' && self.match_char('x') {
|
||||||
|
if !self.peek().is_ascii_hexdigit() {
|
||||||
|
return error!(self.loc, "expected a digit after '0x'");
|
||||||
|
}
|
||||||
while self.peek().is_ascii_hexdigit() {
|
while self.peek().is_ascii_hexdigit() {
|
||||||
self.advance();
|
self.advance();
|
||||||
}
|
}
|
||||||
} else if self.match_char('o') {
|
|
||||||
while matches!(self.peek(), '0'..='7') {
|
|
||||||
self.advance();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
if self.source[self.current - 1] == '0' && self.peek().is_ascii_digit() {
|
||||||
|
return error!(self.loc, "octal literals are not allowed");
|
||||||
|
}
|
||||||
while self.peek().is_ascii_digit() {
|
while self.peek().is_ascii_digit() {
|
||||||
self.advance();
|
self.advance();
|
||||||
}
|
}
|
||||||
|
if self.current + 1 < self.source.len()
|
||||||
|
&& self.peek() == '.'
|
||||||
|
&& self.source[self.current + 1] != '.'
|
||||||
|
{
|
||||||
|
is_float = true;
|
||||||
|
self.advance();
|
||||||
|
while self.peek().is_ascii_digit() {
|
||||||
|
self.advance();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.add_token(TokenType::IntLiteral)
|
if is_float {
|
||||||
|
self.add_token(TokenType::FloatLiteral)
|
||||||
|
} else {
|
||||||
|
self.add_token(TokenType::IntLiteral)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scan_identifier(&mut self) -> Result<(), ZernError> {
|
fn scan_identifier(&mut self) -> Result<(), ZernError> {
|
||||||
@@ -361,6 +397,11 @@ impl Tokenizer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let lexeme: String = self.source[self.start..self.current].iter().collect();
|
let lexeme: String = self.source[self.start..self.current].iter().collect();
|
||||||
|
|
||||||
|
if lexeme == "include" {
|
||||||
|
return self.scan_include();
|
||||||
|
}
|
||||||
|
|
||||||
self.add_token(match lexeme.as_str() {
|
self.add_token(match lexeme.as_str() {
|
||||||
"const" => TokenType::KeywordConst,
|
"const" => TokenType::KeywordConst,
|
||||||
"if" => TokenType::KeywordIf,
|
"if" => TokenType::KeywordIf,
|
||||||
@@ -377,13 +418,77 @@ impl Tokenizer {
|
|||||||
"struct" => TokenType::KeywordStruct,
|
"struct" => TokenType::KeywordStruct,
|
||||||
"new" => TokenType::KeywordNew,
|
"new" => TokenType::KeywordNew,
|
||||||
"as" => TokenType::KeywordAs,
|
"as" => TokenType::KeywordAs,
|
||||||
"enum" => TokenType::KeywordEnum,
|
"true" => TokenType::KeywordTrue,
|
||||||
"true" => TokenType::True,
|
"false" => TokenType::KeywordFalse,
|
||||||
"false" => TokenType::False,
|
|
||||||
_ => TokenType::Identifier,
|
_ => TokenType::Identifier,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn scan_include(&mut self) -> Result<(), ZernError> {
|
||||||
|
if !self.match_char(' ') {
|
||||||
|
return error!(self.loc, "expected a space after 'include'");
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.peek() != '"' {
|
||||||
|
return error!(self.loc, "expected '\"' after 'include '");
|
||||||
|
}
|
||||||
|
self.advance();
|
||||||
|
|
||||||
|
let path_start = self.current;
|
||||||
|
while !self.eof() && self.peek() != '"' {
|
||||||
|
self.advance();
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.eof() {
|
||||||
|
return error!(self.loc, "unterminated string after 'include '");
|
||||||
|
}
|
||||||
|
|
||||||
|
let path: String = self.source[path_start..self.current].iter().collect();
|
||||||
|
self.advance(); // consume closing quote
|
||||||
|
|
||||||
|
self.include_file(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn include_file(&mut self, mut path: String) -> Result<(), ZernError> {
|
||||||
|
if path.starts_with("$/") {
|
||||||
|
path = find_std_path()
|
||||||
|
.join(&path[2..])
|
||||||
|
.to_string_lossy()
|
||||||
|
.into_owned();
|
||||||
|
}
|
||||||
|
|
||||||
|
let base_dir = Path::new(&self.loc.filename).parent().unwrap();
|
||||||
|
let resolved_path = base_dir.join(&path);
|
||||||
|
|
||||||
|
let canonical = match fs::canonicalize(&resolved_path) {
|
||||||
|
Ok(p) => p,
|
||||||
|
Err(e) => {
|
||||||
|
return error!(self.loc, format!("failed to resolve {}: {}", path, e));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if !self.included_paths.insert(canonical.clone()) {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let source = match fs::read_to_string(&canonical) {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(_) => {
|
||||||
|
return error!(self.loc, format!("failed to include {}", path));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let tokenizer = Tokenizer::new(
|
||||||
|
canonical.to_string_lossy().into_owned(),
|
||||||
|
source,
|
||||||
|
&mut *self.included_paths,
|
||||||
|
);
|
||||||
|
self.tokens.extend(tokenizer.tokenize()?);
|
||||||
|
self.tokens.pop(); // remove inner Eof
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn match_char(&mut self, expected: char) -> bool {
|
fn match_char(&mut self, expected: char) -> bool {
|
||||||
if self.eof() || self.peek() != expected {
|
if self.eof() || self.peek() != expected {
|
||||||
false
|
false
|
||||||
@@ -457,3 +562,15 @@ impl Tokenizer {
|
|||||||
self.current >= self.source.len()
|
self.current >= self.source.len()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_std_path() -> PathBuf {
|
||||||
|
let path = std::env::current_exe().unwrap();
|
||||||
|
|
||||||
|
for dir in path.ancestors() {
|
||||||
|
let candidate = dir.join("std");
|
||||||
|
if candidate.is_dir() {
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
panic!("could not find zern std directory");
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
parser::{Expr, ExprKind, Params, Stmt},
|
parser::{Expr, ExprKind, Params, Stmt},
|
||||||
symbol_table::{SymbolTable, Type},
|
symbol_table::{FnParams, SymbolTable},
|
||||||
tokenizer::{TokenType, ZernError, error},
|
tokenizer::{TokenType, ZernError, error},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -33,10 +33,10 @@ macro_rules! expect_types {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static BUILTIN_TYPES: [&str; 7] = ["void", "u8", "i64", "str", "bool", "ptr", "any"];
|
static BUILTIN_TYPES: [&str; 8] = ["void", "u8", "i64", "f64", "str", "bool", "ptr", "any"];
|
||||||
|
|
||||||
pub struct Env {
|
pub struct Env {
|
||||||
scopes: Vec<HashMap<String, Type>>,
|
scopes: Vec<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Env {
|
impl Env {
|
||||||
@@ -59,7 +59,7 @@ impl Env {
|
|||||||
self.scopes.last_mut().unwrap().insert(name, var_type);
|
self.scopes.last_mut().unwrap().insert(name, var_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_var_type(&self, name: &str) -> Option<&Type> {
|
fn get_var_type(&self, name: &str) -> Option<&String> {
|
||||||
for scope in self.scopes.iter().rev() {
|
for scope in self.scopes.iter().rev() {
|
||||||
if let Some(var) = scope.get(name) {
|
if let Some(var) = scope.get(name) {
|
||||||
return Some(var);
|
return Some(var);
|
||||||
@@ -314,6 +314,12 @@ impl<'a> TypeChecker<'a> {
|
|||||||
"unrecognized type: ".to_owned() + ¶m.var_type.lexeme
|
"unrecognized type: ".to_owned() + ¶m.var_type.lexeme
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if param.var_type.lexeme == "f64" {
|
||||||
|
return error!(
|
||||||
|
¶m.var_name.loc,
|
||||||
|
"f64 params not implemented yet"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
env.define_var(
|
env.define_var(
|
||||||
param.var_name.lexeme.clone(),
|
param.var_name.lexeme.clone(),
|
||||||
@@ -355,12 +361,11 @@ impl<'a> TypeChecker<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::Enum { .. } => {}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn typecheck_expr(&mut self, env: &mut Env, expr: &Expr) -> Result<Type, ZernError> {
|
pub fn typecheck_expr(&mut self, env: &mut Env, expr: &Expr) -> Result<String, ZernError> {
|
||||||
let expr_type = match &expr.kind {
|
let expr_type = match &expr.kind {
|
||||||
ExprKind::Binary { left, op, right } => {
|
ExprKind::Binary { left, op, right } => {
|
||||||
let left_type = self.typecheck_expr(env, left)?;
|
let left_type = self.typecheck_expr(env, left)?;
|
||||||
@@ -420,18 +425,19 @@ impl<'a> TypeChecker<'a> {
|
|||||||
ExprKind::Grouping(expr) => self.typecheck_expr(env, expr),
|
ExprKind::Grouping(expr) => self.typecheck_expr(env, expr),
|
||||||
ExprKind::Literal(token) => match token.token_type {
|
ExprKind::Literal(token) => match token.token_type {
|
||||||
TokenType::IntLiteral => Ok("i64".into()),
|
TokenType::IntLiteral => Ok("i64".into()),
|
||||||
|
TokenType::FloatLiteral => Ok("f64".into()),
|
||||||
TokenType::CharLiteral => Ok("u8".into()),
|
TokenType::CharLiteral => Ok("u8".into()),
|
||||||
TokenType::StringLiteral => Ok("str".into()),
|
TokenType::StringLiteral => Ok("str".into()),
|
||||||
TokenType::True => Ok("bool".into()),
|
TokenType::KeywordTrue => Ok("bool".into()),
|
||||||
TokenType::False => Ok("bool".into()),
|
TokenType::KeywordFalse => Ok("bool".into()),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
},
|
},
|
||||||
ExprKind::Unary { op, right } => {
|
ExprKind::Unary { op, right } => {
|
||||||
let right_type = self.typecheck_expr(env, right)?;
|
let right_type = self.typecheck_expr(env, right)?;
|
||||||
match op.token_type {
|
match op.token_type {
|
||||||
TokenType::Minus => {
|
TokenType::Minus => {
|
||||||
expect_type!(right_type, "i64", op.loc);
|
expect_types!(right_type, ["f64", "i64"], op.loc);
|
||||||
Ok("i64".into())
|
Ok(right_type)
|
||||||
}
|
}
|
||||||
TokenType::Bang => {
|
TokenType::Bang => {
|
||||||
expect_types!(right_type, ["bool", "i64", "ptr", "u8"], op.loc);
|
expect_types!(right_type, ["bool", "i64", "ptr", "u8"], op.loc);
|
||||||
@@ -458,24 +464,31 @@ impl<'a> TypeChecker<'a> {
|
|||||||
if let ExprKind::Variable(callee_name) = &callee.kind {
|
if let ExprKind::Variable(callee_name) = &callee.kind {
|
||||||
if let Some(fn_type) = self.symbol_table.functions.get(&callee_name.lexeme) {
|
if let Some(fn_type) = self.symbol_table.functions.get(&callee_name.lexeme) {
|
||||||
// its a function (defined/builtin/extern)
|
// its a function (defined/builtin/extern)
|
||||||
if let Some(params) = fn_type.params.clone() {
|
match &fn_type.params {
|
||||||
if params.len() != args.len() {
|
FnParams::Normal(params) => {
|
||||||
return error!(
|
if params.len() != args.len() {
|
||||||
&paren.loc,
|
return error!(
|
||||||
format!(
|
&paren.loc,
|
||||||
"expected {} arguments, got {}",
|
format!(
|
||||||
params.len(),
|
"expected {} arguments, got {}",
|
||||||
args.len()
|
params.len(),
|
||||||
)
|
args.len()
|
||||||
);
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for (i, arg) in args.iter().enumerate() {
|
||||||
|
expect_type!(
|
||||||
|
self.typecheck_expr(env, arg)?,
|
||||||
|
params[i],
|
||||||
|
paren.loc
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (i, arg) in args.iter().enumerate() {
|
FnParams::Variadic => {
|
||||||
expect_type!(self.typecheck_expr(env, arg)?, params[i], paren.loc);
|
// cant check arg types
|
||||||
}
|
for arg in args {
|
||||||
} else {
|
self.typecheck_expr(env, arg)?;
|
||||||
// its a variadic function, cant check arg types
|
}
|
||||||
for arg in args {
|
|
||||||
self.typecheck_expr(env, arg)?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(fn_type.return_type.clone())
|
Ok(fn_type.return_type.clone())
|
||||||
@@ -549,7 +562,13 @@ impl<'a> TypeChecker<'a> {
|
|||||||
Ok(field.field_type.clone())
|
Ok(field.field_type.clone())
|
||||||
}
|
}
|
||||||
ExprKind::Cast { expr, type_name } => {
|
ExprKind::Cast { expr, type_name } => {
|
||||||
self.typecheck_expr(env, expr)?;
|
let expr_type = self.typecheck_expr(env, expr)?;
|
||||||
|
if expr_type != "any" && type_name.lexeme == "f64" {
|
||||||
|
return error!(
|
||||||
|
&type_name.loc,
|
||||||
|
"use _builtin_cvtsi2sd and _builtin_cvttsd2si to cast between integers and f64"
|
||||||
|
);
|
||||||
|
}
|
||||||
if !self.is_valid_type_name(&type_name.lexeme) {
|
if !self.is_valid_type_name(&type_name.lexeme) {
|
||||||
return error!(
|
return error!(
|
||||||
&type_name.loc,
|
&type_name.loc,
|
||||||
@@ -575,35 +594,38 @@ impl<'a> TypeChecker<'a> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(params) = &func_type.params {
|
match &func_type.params {
|
||||||
if params.len() != args.len() + 1 {
|
FnParams::Normal(params) => {
|
||||||
return error!(
|
if params.is_empty() || params[0] != receiver_type {
|
||||||
method.loc,
|
return error!(
|
||||||
format!(
|
method.loc,
|
||||||
"expected {} arguments, got {}",
|
format!(
|
||||||
params.len() - 1,
|
"first parameter of the method must be of type {}",
|
||||||
args.len()
|
receiver_type
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
if params.len() != args.len() + 1 {
|
||||||
|
return error!(
|
||||||
|
method.loc,
|
||||||
|
format!(
|
||||||
|
"expected {} arguments, got {}",
|
||||||
|
params.len() - 1,
|
||||||
|
args.len()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for (i, arg) in args.iter().enumerate() {
|
||||||
|
expect_type!(self.typecheck_expr(env, arg)?, params[i + 1], method.loc);
|
||||||
|
}
|
||||||
|
Ok(func_type.return_type.clone())
|
||||||
}
|
}
|
||||||
if params[0] != receiver_type {
|
FnParams::Variadic => {
|
||||||
return error!(
|
for arg in args {
|
||||||
method.loc,
|
self.typecheck_expr(env, arg)?;
|
||||||
format!(
|
}
|
||||||
"first parameter of the method must be of type {}",
|
Ok(func_type.return_type.clone())
|
||||||
receiver_type
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
for (i, arg) in args.iter().enumerate() {
|
|
||||||
expect_type!(self.typecheck_expr(env, arg)?, params[i + 1], method.loc);
|
|
||||||
}
|
|
||||||
Ok(func_type.return_type.clone())
|
|
||||||
} else {
|
|
||||||
for arg in args {
|
|
||||||
self.typecheck_expr(env, arg)?;
|
|
||||||
}
|
|
||||||
Ok(func_type.return_type.clone())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}?;
|
}?;
|
||||||
|
|||||||
249
std/containers.zr
Normal file
249
std/containers.zr
Normal file
@@ -0,0 +1,249 @@
|
|||||||
|
include "mem.zr"
|
||||||
|
|
||||||
|
struct Array
|
||||||
|
data: ptr
|
||||||
|
size: i64
|
||||||
|
capacity: i64
|
||||||
|
|
||||||
|
func Array.new_preallocated_and_zeroed[size: i64] : Array
|
||||||
|
xs := new* Array
|
||||||
|
if size > 0
|
||||||
|
xs->data = mem.alloc(size * 8)
|
||||||
|
mem.zero(xs->data, size * 8)
|
||||||
|
xs->size = size
|
||||||
|
xs->capacity = size
|
||||||
|
return xs
|
||||||
|
|
||||||
|
func Array.nth[xs: Array, n: i64] : any
|
||||||
|
if n < 0 || n >= xs->size
|
||||||
|
panic("Array.nth out of bounds")
|
||||||
|
return mem.read64(xs->data + n * 8)
|
||||||
|
|
||||||
|
func Array.set[xs: Array, n: i64, x: any] : void
|
||||||
|
if n < 0 || n >= xs->size
|
||||||
|
panic("Array.set out of bounds")
|
||||||
|
mem.write64(xs->data + n * 8, x)
|
||||||
|
|
||||||
|
func Array.push[xs: Array, x: any] : void
|
||||||
|
if xs->size == xs->capacity
|
||||||
|
new_capacity := 4
|
||||||
|
if xs->capacity != 0
|
||||||
|
new_capacity = xs->capacity * 2
|
||||||
|
xs->data = xs->data->realloc(new_capacity * 8)
|
||||||
|
xs->capacity = new_capacity
|
||||||
|
|
||||||
|
mem.write64(xs->data + xs->size * 8, x)
|
||||||
|
xs->size += 1
|
||||||
|
|
||||||
|
func Array.free[xs: Array] : void
|
||||||
|
if xs->data != 0
|
||||||
|
xs->data->free()
|
||||||
|
(xs as ptr)->free()
|
||||||
|
|
||||||
|
func Array.free_with_items[xs: Array] : void
|
||||||
|
if xs->data != 0
|
||||||
|
for i in 0..xs->size
|
||||||
|
(xs->nth(i) as ptr)->free()
|
||||||
|
xs->data->free()
|
||||||
|
(xs as ptr)->free()
|
||||||
|
|
||||||
|
func Array.pop[xs: Array] : any
|
||||||
|
if xs->size == 0
|
||||||
|
panic("Array.pop on empty array")
|
||||||
|
x : any = Array.last(xs)
|
||||||
|
xs->size = xs->size - 1
|
||||||
|
return x
|
||||||
|
|
||||||
|
func Array.last[xs: Array] : any
|
||||||
|
if xs->size == 0
|
||||||
|
panic("Array.last on empty array")
|
||||||
|
return xs->nth(xs->size - 1)
|
||||||
|
|
||||||
|
func Array.slice[xs: Array, start: i64, length: i64] : Array
|
||||||
|
if start < 0 || length < 0 || start + length > xs->size
|
||||||
|
panic("Array.slice out of bounds")
|
||||||
|
|
||||||
|
new_array := Array.new_preallocated_and_zeroed(length)
|
||||||
|
mem.copy(xs->data + start * 8, new_array->data, length * 8)
|
||||||
|
return new_array
|
||||||
|
|
||||||
|
func Array.concat[a: Array, b: Array] : Array
|
||||||
|
new_array := Array.new_preallocated_and_zeroed(a->size + b->size)
|
||||||
|
mem.copy(a->data, new_array->data, a->size * 8)
|
||||||
|
mem.copy(b->data, new_array->data + a->size * 8, b->size * 8)
|
||||||
|
return new_array
|
||||||
|
|
||||||
|
func Array.quicksort[arr: Array] : void
|
||||||
|
arr->_do_quicksort(0, arr->size - 1)
|
||||||
|
|
||||||
|
func Array._do_quicksort[arr: Array, low: i64, high: i64] : void
|
||||||
|
if low < high
|
||||||
|
i := arr->_partition(low, high)
|
||||||
|
arr->_do_quicksort(low, i - 1)
|
||||||
|
arr->_do_quicksort(i + 1, high)
|
||||||
|
|
||||||
|
func Array._partition[arr: Array, low: i64, high: i64] : i64
|
||||||
|
pivot : i64 = arr->nth(high)
|
||||||
|
i := low - 1
|
||||||
|
for j in (low)..high
|
||||||
|
if arr->nth(j) as i64 <= pivot
|
||||||
|
i += 1
|
||||||
|
temp : i64 = arr->nth(i)
|
||||||
|
arr->set(i, arr->nth(j))
|
||||||
|
arr->set(j, temp)
|
||||||
|
temp : i64 = arr->nth(i + 1)
|
||||||
|
arr->set(i + 1, arr->nth(high))
|
||||||
|
arr->set(high, temp)
|
||||||
|
return i + 1
|
||||||
|
|
||||||
|
func Array.contains_str[arr: Array, s: str] : bool
|
||||||
|
for i in 0..arr->size
|
||||||
|
if (arr->nth(i) as str)->equal(s)
|
||||||
|
return true
|
||||||
|
return false
|
||||||
|
|
||||||
|
func Array.count[arr: Array, item: any] : i64
|
||||||
|
count := 0
|
||||||
|
for i in 0..arr->size
|
||||||
|
if arr->nth(i) == item
|
||||||
|
count += 1
|
||||||
|
return count
|
||||||
|
|
||||||
|
func Array.map[arr: Array, fn: ptr] : Array
|
||||||
|
out := Array.new_preallocated_and_zeroed(arr->size)
|
||||||
|
for i in 0..arr->size
|
||||||
|
out->set(i, fn(arr->nth(i)))
|
||||||
|
return out
|
||||||
|
|
||||||
|
func Array.filter[arr: Array, fn: ptr] : Array
|
||||||
|
out := []
|
||||||
|
for i in 0..arr->size
|
||||||
|
if fn(arr->nth(i))
|
||||||
|
out->push(arr->nth(i))
|
||||||
|
return out
|
||||||
|
|
||||||
|
func Array.reduce[arr: Array, fn: ptr, acc: any] : any
|
||||||
|
for i in 0..arr->size
|
||||||
|
acc = fn(acc, arr->nth(i))
|
||||||
|
return acc
|
||||||
|
|
||||||
|
func str.split[haystack: str, needle: str]: Array
|
||||||
|
haystack_len := haystack->len()
|
||||||
|
needle_len := needle->len()
|
||||||
|
result := []
|
||||||
|
|
||||||
|
if !needle_len
|
||||||
|
if !haystack_len
|
||||||
|
return result
|
||||||
|
else
|
||||||
|
for i in 0..haystack_len
|
||||||
|
result->push(haystack->substr_n(i, 1))
|
||||||
|
return result
|
||||||
|
|
||||||
|
start := 0
|
||||||
|
i := 0
|
||||||
|
while i < haystack_len
|
||||||
|
if i <= haystack_len - needle_len
|
||||||
|
match := true
|
||||||
|
for j in 0..needle_len
|
||||||
|
if haystack[i + j] != needle[j]
|
||||||
|
match = false
|
||||||
|
break
|
||||||
|
if match
|
||||||
|
result->push(haystack->substr_n(start, i - start))
|
||||||
|
start = i + needle_len
|
||||||
|
i += needle_len
|
||||||
|
continue
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
result->push(haystack->substr_n(start, haystack_len - start))
|
||||||
|
return result
|
||||||
|
|
||||||
|
const HashMap._TABLE_SIZE = 100
|
||||||
|
|
||||||
|
struct HashMap
|
||||||
|
table: Array
|
||||||
|
|
||||||
|
struct HashMap._Node
|
||||||
|
key: str
|
||||||
|
value: any
|
||||||
|
next: HashMap._Node
|
||||||
|
|
||||||
|
func HashMap.new[] : HashMap
|
||||||
|
map := new* HashMap
|
||||||
|
map->table = Array.new_preallocated_and_zeroed(HashMap._TABLE_SIZE)
|
||||||
|
return map
|
||||||
|
|
||||||
|
func HashMap.insert[map: HashMap, key: str, value: any] : void
|
||||||
|
index := HashMap._djb2(key)
|
||||||
|
current : HashMap._Node = map->table->nth(index)
|
||||||
|
|
||||||
|
while current as ptr
|
||||||
|
if current->key->equal(key)
|
||||||
|
current->value = value
|
||||||
|
return
|
||||||
|
current = current->next
|
||||||
|
|
||||||
|
new_node := new* HashMap._Node
|
||||||
|
new_node->key = key->make_copy()
|
||||||
|
new_node->value = value
|
||||||
|
new_node->next = map->table->nth(index)
|
||||||
|
map->table->set(index, new_node)
|
||||||
|
|
||||||
|
func HashMap.get[map: HashMap, key: str] : any, bool
|
||||||
|
index := HashMap._djb2(key)
|
||||||
|
current : HashMap._Node = map->table->nth(index)
|
||||||
|
|
||||||
|
while current as ptr
|
||||||
|
if current->key->equal(key)
|
||||||
|
return current->value, true
|
||||||
|
current = current->next
|
||||||
|
|
||||||
|
return 0 as any, false
|
||||||
|
|
||||||
|
func HashMap.delete[map: HashMap, key: str] : void
|
||||||
|
index := HashMap._djb2(key)
|
||||||
|
current : HashMap._Node = map->table->nth(index)
|
||||||
|
prev := 0 as HashMap._Node
|
||||||
|
|
||||||
|
while current as ptr
|
||||||
|
if current->key->equal(key)
|
||||||
|
if prev as ptr
|
||||||
|
prev->next = current->next
|
||||||
|
else
|
||||||
|
map->table->set(index, current->next)
|
||||||
|
|
||||||
|
current->key->free()
|
||||||
|
(current as ptr)->free()
|
||||||
|
return
|
||||||
|
|
||||||
|
prev = current
|
||||||
|
current = current->next
|
||||||
|
|
||||||
|
func HashMap.keys[map: HashMap] : Array
|
||||||
|
out := []
|
||||||
|
for i in 0..HashMap._TABLE_SIZE
|
||||||
|
current : HashMap._Node = map->table->nth(i)
|
||||||
|
while current as ptr
|
||||||
|
out->push(current->key)
|
||||||
|
current = current->next
|
||||||
|
return out
|
||||||
|
|
||||||
|
func HashMap.free_with_keys[map: HashMap] : void
|
||||||
|
for i in 0..HashMap._TABLE_SIZE
|
||||||
|
current : HashMap._Node = map->table->nth(i)
|
||||||
|
while current as ptr
|
||||||
|
tmp := current
|
||||||
|
current = current->next
|
||||||
|
tmp->key->free()
|
||||||
|
(tmp as ptr)->free()
|
||||||
|
|
||||||
|
map->table->free()
|
||||||
|
(map as ptr)->free()
|
||||||
|
|
||||||
|
func HashMap._djb2[key: str] : i64
|
||||||
|
hash := 5381
|
||||||
|
for i in 0..key->len()
|
||||||
|
hash = ((hash << 5) + hash) + key[i]
|
||||||
|
hash = (hash & 0x7fffffffffffffff) // prevent negative
|
||||||
|
return hash % HashMap._TABLE_SIZE
|
||||||
152
std/io.zr
Normal file
152
std/io.zr
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
include "str.zr"
|
||||||
|
include "os.zr"
|
||||||
|
|
||||||
|
func panic[msg: str] : void
|
||||||
|
io.printf("PANIC: %s\n", msg)
|
||||||
|
// for gdb backtrace
|
||||||
|
_builtin_syscall(SYS_kill, os.getpid(), SIGABRT)
|
||||||
|
os.exit(1)
|
||||||
|
|
||||||
|
func assert[cond: bool, msg: str] : void
|
||||||
|
if !cond
|
||||||
|
panic(msg)
|
||||||
|
|
||||||
|
func io.printf[..] : void
|
||||||
|
s := _var_arg(0) as ptr
|
||||||
|
i := 1
|
||||||
|
|
||||||
|
while s[0]
|
||||||
|
if s[0] == '%'
|
||||||
|
s += 1
|
||||||
|
|
||||||
|
if s[0] == 'd'
|
||||||
|
io.print_i64(_var_arg(i) as i64)
|
||||||
|
i += 1
|
||||||
|
else if s[0] == 'x'
|
||||||
|
io.print_i64_hex(_var_arg(i) as i64)
|
||||||
|
i += 1
|
||||||
|
else if s[0] == 's'
|
||||||
|
io.print(_var_arg(i) as str)
|
||||||
|
i += 1
|
||||||
|
else if s[0] == 'c'
|
||||||
|
io.print_char(_var_arg(i) as u8)
|
||||||
|
i += 1
|
||||||
|
else if s[0] == 'f'
|
||||||
|
io.print_f64(_var_arg(i) as i64)
|
||||||
|
i += 1
|
||||||
|
else if s[0] == '%'
|
||||||
|
io.print_char('%')
|
||||||
|
else if s[0] == 0
|
||||||
|
break
|
||||||
|
else
|
||||||
|
panic("io.printf: unrecognized format")
|
||||||
|
else
|
||||||
|
io.print_char(s[0])
|
||||||
|
s += 1
|
||||||
|
|
||||||
|
func io.print_sized[x: ptr, size: i64] : void
|
||||||
|
_builtin_syscall(SYS_write, STDOUT_FILENO, x, size)
|
||||||
|
|
||||||
|
func io.print[x: str] : void
|
||||||
|
io.print_sized(x as ptr, x->len())
|
||||||
|
|
||||||
|
func io.println[x: str] : void
|
||||||
|
io.print(x)
|
||||||
|
io.print("\n")
|
||||||
|
|
||||||
|
func io.print_char[x: u8] : void
|
||||||
|
io.print_sized(^x, 1)
|
||||||
|
|
||||||
|
func io.print_bool[x: bool] : void
|
||||||
|
if x
|
||||||
|
io.print("true")
|
||||||
|
else
|
||||||
|
io.print("false")
|
||||||
|
|
||||||
|
func io.print_i64[x: i64] : void
|
||||||
|
s := _stackalloc(21)
|
||||||
|
x->to_str_buf(s)
|
||||||
|
io.print(s as str)
|
||||||
|
|
||||||
|
func io.print_i64_hex[x: i64] : void
|
||||||
|
s := _stackalloc(17)
|
||||||
|
x->to_hex_str_buf(s)
|
||||||
|
io.print(s as str)
|
||||||
|
|
||||||
|
func io.println_i64[x: i64] : void
|
||||||
|
s := _stackalloc(21)
|
||||||
|
x->to_str_buf(s)
|
||||||
|
io.println(s as str)
|
||||||
|
|
||||||
|
// TODO: fix when we implement f64 params
|
||||||
|
func io.print_f64[x: i64] : void
|
||||||
|
s := _stackalloc(64)
|
||||||
|
f64.to_str_buf(x, s)
|
||||||
|
io.print(s as str)
|
||||||
|
|
||||||
|
func io.read_char[] : u8
|
||||||
|
c := 0 as u8
|
||||||
|
_builtin_syscall(SYS_read, STDIN_FILENO, ^c, 1)
|
||||||
|
return c
|
||||||
|
|
||||||
|
func io.read_line[] : str
|
||||||
|
b := new str.Builder
|
||||||
|
while true
|
||||||
|
c := io.read_char()
|
||||||
|
if c == '\n'
|
||||||
|
break
|
||||||
|
b->append_char(c)
|
||||||
|
return b->build_and_free_buffer()
|
||||||
|
|
||||||
|
func io.read_text_file[path: str] : str, bool
|
||||||
|
fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_RDONLY, 0)
|
||||||
|
if fd < 0
|
||||||
|
return 0 as str, false
|
||||||
|
|
||||||
|
size := _builtin_syscall(SYS_lseek, fd, 0, SEEK_END)
|
||||||
|
_builtin_syscall(SYS_lseek, fd, 0, SEEK_SET)
|
||||||
|
|
||||||
|
buffer := mem.alloc(size + 1)
|
||||||
|
n := _builtin_syscall(SYS_read, fd, buffer, size)
|
||||||
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
|
||||||
|
if n != size
|
||||||
|
buffer->free()
|
||||||
|
return 0 as str, false
|
||||||
|
|
||||||
|
buffer[n] = 0
|
||||||
|
return buffer as str, true
|
||||||
|
|
||||||
|
func io.read_binary_file[path: str] : Blob, bool
|
||||||
|
fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_RDONLY, 0)
|
||||||
|
if fd < 0
|
||||||
|
return 0 as Blob, false
|
||||||
|
|
||||||
|
buf := new* Blob
|
||||||
|
buf->size = _builtin_syscall(SYS_lseek, fd, 0, SEEK_END)
|
||||||
|
_builtin_syscall(SYS_lseek, fd, 0, SEEK_SET)
|
||||||
|
|
||||||
|
buf->data = mem.alloc(buf->size)
|
||||||
|
|
||||||
|
n := _builtin_syscall(SYS_read, fd, buf->data, buf->size)
|
||||||
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
|
||||||
|
if n != buf->size
|
||||||
|
buf->free()
|
||||||
|
return 0 as Blob, false
|
||||||
|
|
||||||
|
return buf, true
|
||||||
|
|
||||||
|
func io.write_text_file[path: str, content: str] : bool
|
||||||
|
return io.write_binary_file(path, content as ptr, content->len())
|
||||||
|
|
||||||
|
func io.write_binary_file[path: str, content: ptr, size: i64] : bool
|
||||||
|
fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_WRONLY|O_CREAT|O_TRUNC, math.octal_to_decimal(644))
|
||||||
|
if fd < 0
|
||||||
|
return false
|
||||||
|
|
||||||
|
n := _builtin_syscall(SYS_write, fd, content, size)
|
||||||
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
if n != size
|
||||||
|
return false
|
||||||
|
return true
|
||||||
203
std/json.zr
Normal file
203
std/json.zr
Normal file
@@ -0,0 +1,203 @@
|
|||||||
|
include "containers.zr"
|
||||||
|
|
||||||
|
const json.OBJECT = 0
|
||||||
|
const json.ARRAY = 1
|
||||||
|
const json.STRING = 2
|
||||||
|
const json.NUMBER = 3
|
||||||
|
const json.NULL = 4
|
||||||
|
const json.BOOL = 5
|
||||||
|
|
||||||
|
struct json.Value
|
||||||
|
type: i64
|
||||||
|
value: any
|
||||||
|
|
||||||
|
func json.Value.dump[v: json.Value] : str
|
||||||
|
s := new str.Builder
|
||||||
|
|
||||||
|
if v->type == json.OBJECT
|
||||||
|
map := v->value as HashMap
|
||||||
|
keys := map->keys()
|
||||||
|
s->append_char('{')
|
||||||
|
for i in 0..keys->size
|
||||||
|
if i > 0
|
||||||
|
s->append_char(',')
|
||||||
|
s->append_char('"')
|
||||||
|
s->append(keys->nth(i) as str)
|
||||||
|
s->append_char('"')
|
||||||
|
s->append_char(':')
|
||||||
|
value, _ := map->get(keys->nth(i) as str)
|
||||||
|
s->append((value as json.Value)->dump())
|
||||||
|
s->append_char('}')
|
||||||
|
keys->free()
|
||||||
|
else if v->type == json.ARRAY
|
||||||
|
arr := v->value as Array
|
||||||
|
s->append_char('[')
|
||||||
|
for i in 0..arr->size
|
||||||
|
if i > 0
|
||||||
|
s->append_char(',')
|
||||||
|
s->append((arr->nth(i) as json.Value)->dump())
|
||||||
|
s->append_char(']')
|
||||||
|
else if v->type == json.STRING
|
||||||
|
// TODO: escape sequences
|
||||||
|
s->append_char('"')
|
||||||
|
s->append(v->value as str)
|
||||||
|
s->append_char('"')
|
||||||
|
else if v->type == json.NUMBER
|
||||||
|
s->append((v->value as i64)->to_str())
|
||||||
|
else if v->type == json.NULL
|
||||||
|
s->append("null")
|
||||||
|
else if v->type == json.BOOL
|
||||||
|
if v->value as bool
|
||||||
|
s->append("true")
|
||||||
|
else
|
||||||
|
s->append("false")
|
||||||
|
else
|
||||||
|
panic("json.Value.dump: unknown value type")
|
||||||
|
|
||||||
|
return s->build_and_free_buffer()
|
||||||
|
|
||||||
|
func json.Value.free[v: json.Value] : void
|
||||||
|
if v->type == json.OBJECT
|
||||||
|
map := v->value as HashMap
|
||||||
|
keys := map->keys()
|
||||||
|
for i in 0..keys->size
|
||||||
|
value, _ := map->get(keys->nth(i) as str)
|
||||||
|
(value as json.Value)->free()
|
||||||
|
keys->free()
|
||||||
|
|
||||||
|
map->free_with_keys()
|
||||||
|
else if v->type == json.ARRAY
|
||||||
|
arr := v->value as Array
|
||||||
|
for i in 0..arr->size
|
||||||
|
(arr->nth(i) as json.Value)->free()
|
||||||
|
arr->free()
|
||||||
|
else if v->type == json.STRING
|
||||||
|
(v->value as str)->free()
|
||||||
|
|
||||||
|
(v as ptr)->free()
|
||||||
|
|
||||||
|
func json.val[x: any] : any
|
||||||
|
return (x as json.Value)->value
|
||||||
|
|
||||||
|
func json.parse[s: str] : json.Value
|
||||||
|
i := 0
|
||||||
|
return json._parse_value(s, ^i)
|
||||||
|
|
||||||
|
func json._parse_value[s: str, i: ptr] : json.Value
|
||||||
|
out := new* json.Value
|
||||||
|
|
||||||
|
json._skip_whitespace(s, i)
|
||||||
|
|
||||||
|
if s[mem.read64(i)] == 0
|
||||||
|
panic("json.parse: unexpected EOF")
|
||||||
|
|
||||||
|
if s[mem.read64(i)] == '{'
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
|
json._skip_whitespace(s, i)
|
||||||
|
|
||||||
|
out->type = json.OBJECT
|
||||||
|
out->value = HashMap.new()
|
||||||
|
|
||||||
|
if s[mem.read64(i)] != '}'
|
||||||
|
while true
|
||||||
|
json._skip_whitespace(s, i)
|
||||||
|
key := json._parse_string(s, i)
|
||||||
|
json._skip_whitespace(s, i)
|
||||||
|
|
||||||
|
if s[mem.read64(i)] != ':'
|
||||||
|
panic("json.parse: expected ':'")
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
|
json._skip_whitespace(s, i)
|
||||||
|
|
||||||
|
value := json._parse_value(s, i)
|
||||||
|
(out->value as HashMap)->insert(key, value)
|
||||||
|
key->free()
|
||||||
|
|
||||||
|
json._skip_whitespace(s, i)
|
||||||
|
if s[mem.read64(i)] == ','
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
|
else
|
||||||
|
break
|
||||||
|
|
||||||
|
json._skip_whitespace(s, i)
|
||||||
|
if s[mem.read64(i)] != '}'
|
||||||
|
panic("json.parse: expected '}'")
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
|
else if s[mem.read64(i)] == '['
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
|
json._skip_whitespace(s, i)
|
||||||
|
|
||||||
|
out->type = json.ARRAY
|
||||||
|
out->value = []
|
||||||
|
|
||||||
|
while s[mem.read64(i)] != ']'
|
||||||
|
json._skip_whitespace(s, i)
|
||||||
|
|
||||||
|
(out->value as Array)->push(json._parse_value(s, i))
|
||||||
|
json._skip_whitespace(s, i)
|
||||||
|
|
||||||
|
if s[mem.read64(i)] == ','
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
|
else
|
||||||
|
break
|
||||||
|
|
||||||
|
json._skip_whitespace(s, i)
|
||||||
|
if s[mem.read64(i)] != ']'
|
||||||
|
panic("json.parse: expected ']'")
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
|
else if s[mem.read64(i)] == '"'
|
||||||
|
out->type = json.STRING
|
||||||
|
out->value = json._parse_string(s, i)
|
||||||
|
else if s[mem.read64(i)]->is_digit() || s[mem.read64(i)] == '-'
|
||||||
|
start := mem.read64(i)
|
||||||
|
|
||||||
|
if s[mem.read64(i)] == '-'
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
|
|
||||||
|
while s[mem.read64(i)]->is_digit()
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
|
// TODO: float support once str.parse_f64 is a thing
|
||||||
|
if s[mem.read64(i)] == '.'
|
||||||
|
panic("json.parse: parsing floats not implemented yet")
|
||||||
|
|
||||||
|
len := mem.read64(i) - start
|
||||||
|
|
||||||
|
num_str := s->substr_n(start, len)
|
||||||
|
out->type = json.NUMBER
|
||||||
|
out->value = num_str->parse_i64()
|
||||||
|
num_str->free()
|
||||||
|
else if s[mem.read64(i)] == 'n' && s[mem.read64(i)+1] == 'u' && s[mem.read64(i)+2] == 'l' && s[mem.read64(i)+3] == 'l'
|
||||||
|
out->type = json.NULL
|
||||||
|
mem.write64(i, mem.read64(i) + 4)
|
||||||
|
else if s[mem.read64(i)] == 't' && s[mem.read64(i)+1] == 'r' && s[mem.read64(i)+2] == 'u' && s[mem.read64(i)+3] == 'e'
|
||||||
|
out->type = json.BOOL
|
||||||
|
out->value = true
|
||||||
|
mem.write64(i, mem.read64(i) + 4)
|
||||||
|
else if s[mem.read64(i)] == 'f' && s[mem.read64(i)+1] == 'a' && s[mem.read64(i)+2] == 'l' && s[mem.read64(i)+3] == 's' && s[mem.read64(i)+4] == 'e'
|
||||||
|
out->type = json.BOOL
|
||||||
|
out->value = false
|
||||||
|
mem.write64(i, mem.read64(i) + 5)
|
||||||
|
else
|
||||||
|
panic("json.parse: unexpected character")
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
|
func json._parse_string[s: str, i: ptr] : str
|
||||||
|
if s[mem.read64(i)] != '"'
|
||||||
|
panic("json.parse: expected '\"'")
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
|
|
||||||
|
start := mem.read64(i)
|
||||||
|
// TODO: escape sequences
|
||||||
|
while s[mem.read64(i)] != '"'
|
||||||
|
if s[mem.read64(i)] == 0
|
||||||
|
panic("json.parse: unexpected EOF")
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
|
|
||||||
|
len := mem.read64(i) - start
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
|
return s->substr_n(start, len)
|
||||||
|
|
||||||
|
func json._skip_whitespace[s: str, i: ptr] : void
|
||||||
|
while s[mem.read64(i)]->is_whitespace()
|
||||||
|
mem.write64(i, mem.read64(i) + 1)
|
||||||
@@ -1,29 +1,3 @@
|
|||||||
const STDIN = 0
|
|
||||||
const STDOUT = 1
|
|
||||||
|
|
||||||
const SIGABRT = 6
|
|
||||||
|
|
||||||
const AT_FDCWD = -100
|
|
||||||
|
|
||||||
const S_IFDIR = 0o040000
|
|
||||||
const S_IFMT = 0o170000
|
|
||||||
|
|
||||||
const PROT_READ = 1
|
|
||||||
const PROT_WRITE = 2
|
|
||||||
|
|
||||||
const MAP_PRIVATE = 2
|
|
||||||
const MAP_ANONYMOUS = 32
|
|
||||||
|
|
||||||
const O_RDONLY = 0
|
|
||||||
const O_WRONLY = 1
|
|
||||||
const O_CREAT = 64
|
|
||||||
const O_TRUNC = 512
|
|
||||||
|
|
||||||
const SEEK_SET = 0
|
|
||||||
const SEEK_END = 2
|
|
||||||
|
|
||||||
const F_OK = 0
|
|
||||||
|
|
||||||
const SYS_read = 0
|
const SYS_read = 0
|
||||||
const SYS_write = 1
|
const SYS_write = 1
|
||||||
const SYS_open = 2
|
const SYS_open = 2
|
||||||
237
std/mem.zr
Normal file
237
std/mem.zr
Normal file
@@ -0,0 +1,237 @@
|
|||||||
|
include "linux_syscalls.zr"
|
||||||
|
include "num.zr"
|
||||||
|
|
||||||
|
const MEM_BLOCK_SIZE = 32
|
||||||
|
|
||||||
|
struct mem.Block
|
||||||
|
size: i64
|
||||||
|
free: bool
|
||||||
|
next: mem.Block
|
||||||
|
prev: mem.Block
|
||||||
|
|
||||||
|
func mem.align[x: i64] : i64
|
||||||
|
return (x + 7) & -8
|
||||||
|
|
||||||
|
func mem.Block._split[blk: mem.Block, needed: i64] : void
|
||||||
|
if blk->size >= needed + MEM_BLOCK_SIZE + 8
|
||||||
|
new_blk := (blk as ptr + MEM_BLOCK_SIZE + needed) as mem.Block
|
||||||
|
new_blk->size = blk->size - needed - MEM_BLOCK_SIZE
|
||||||
|
new_blk->free = true
|
||||||
|
new_blk->next = blk->next
|
||||||
|
new_blk->prev = blk
|
||||||
|
|
||||||
|
if blk->next as ptr
|
||||||
|
blk->next->prev = new_blk
|
||||||
|
else
|
||||||
|
mem.write64(_builtin_heap_tail(), new_blk)
|
||||||
|
|
||||||
|
blk->size = needed
|
||||||
|
blk->next = new_blk
|
||||||
|
|
||||||
|
func mem._request_space[size: i64] : mem.Block
|
||||||
|
needed := size + MEM_BLOCK_SIZE
|
||||||
|
alloc_size := (needed + 4095) & -4096
|
||||||
|
|
||||||
|
blk := _builtin_syscall(SYS_mmap, 0, alloc_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) as mem.Block
|
||||||
|
if (blk as ptr as i64) == MAP_FAILED
|
||||||
|
panic("mem._request_space: failed to mmap")
|
||||||
|
|
||||||
|
blk->size = alloc_size - MEM_BLOCK_SIZE
|
||||||
|
blk->free = false
|
||||||
|
blk->next = 0 as mem.Block
|
||||||
|
blk->prev = 0 as mem.Block
|
||||||
|
|
||||||
|
tail := mem.read64(_builtin_heap_tail()) as mem.Block
|
||||||
|
if tail as ptr
|
||||||
|
tail->next = blk
|
||||||
|
blk->prev = tail
|
||||||
|
|
||||||
|
mem.write64(_builtin_heap_tail(), blk)
|
||||||
|
return blk
|
||||||
|
|
||||||
|
func mem.alloc[size: i64] : ptr
|
||||||
|
if size <= 0
|
||||||
|
panic("mem.alloc: called with nonpositive size")
|
||||||
|
|
||||||
|
size = mem.align(size)
|
||||||
|
|
||||||
|
cur := mem.read64(_builtin_heap_head()) as mem.Block
|
||||||
|
while cur as ptr
|
||||||
|
if cur->free && cur->size >= size
|
||||||
|
cur->_split(size)
|
||||||
|
cur->free = false
|
||||||
|
return cur as ptr + MEM_BLOCK_SIZE
|
||||||
|
cur = cur->next
|
||||||
|
|
||||||
|
blk := mem._request_space(size)
|
||||||
|
|
||||||
|
if !mem.read64(_builtin_heap_head())
|
||||||
|
mem.write64(_builtin_heap_head(), blk)
|
||||||
|
|
||||||
|
blk->_split(size)
|
||||||
|
|
||||||
|
return blk as ptr + MEM_BLOCK_SIZE
|
||||||
|
|
||||||
|
func ptr.free[x: ptr] : void
|
||||||
|
if x == 0
|
||||||
|
return
|
||||||
|
|
||||||
|
blk := (x - MEM_BLOCK_SIZE) as mem.Block
|
||||||
|
if blk->free
|
||||||
|
return
|
||||||
|
|
||||||
|
blk->free = true
|
||||||
|
|
||||||
|
next := blk->next
|
||||||
|
if next as ptr && next->free
|
||||||
|
expected_next := (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block
|
||||||
|
if expected_next as ptr == next as ptr
|
||||||
|
blk->size += MEM_BLOCK_SIZE + next->size
|
||||||
|
blk->next = next->next
|
||||||
|
if next->next as ptr
|
||||||
|
next->next->prev = blk
|
||||||
|
if mem.read64(_builtin_heap_tail()) == next as ptr
|
||||||
|
mem.write64(_builtin_heap_tail(), blk)
|
||||||
|
|
||||||
|
prev := blk->prev
|
||||||
|
if prev as ptr && prev->free
|
||||||
|
expected_blk := (prev as ptr + MEM_BLOCK_SIZE + prev->size) as mem.Block
|
||||||
|
if expected_blk as ptr == blk as ptr
|
||||||
|
prev->size += MEM_BLOCK_SIZE + blk->size
|
||||||
|
prev->next = blk->next
|
||||||
|
if blk->next as ptr
|
||||||
|
blk->next->prev = prev
|
||||||
|
if mem.read64(_builtin_heap_tail()) == blk as ptr
|
||||||
|
mem.write64(_builtin_heap_tail(), prev)
|
||||||
|
blk = prev
|
||||||
|
|
||||||
|
block_total := blk->size + MEM_BLOCK_SIZE
|
||||||
|
if (blk as i64 & 4095) == 0 && (block_total & 4095) == 0
|
||||||
|
if blk->prev as ptr
|
||||||
|
blk->prev->next = blk->next
|
||||||
|
else
|
||||||
|
mem.write64(_builtin_heap_head(), blk->next)
|
||||||
|
if blk->next as ptr
|
||||||
|
blk->next->prev = blk->prev
|
||||||
|
else
|
||||||
|
mem.write64(_builtin_heap_tail(), blk->prev)
|
||||||
|
_builtin_syscall(SYS_munmap, blk, block_total)
|
||||||
|
|
||||||
|
func ptr.realloc[x: ptr, new_size: i64] : ptr
|
||||||
|
if !x
|
||||||
|
return mem.alloc(new_size)
|
||||||
|
|
||||||
|
if new_size < 0
|
||||||
|
panic("mem.realloc: called with negative new_size")
|
||||||
|
|
||||||
|
if new_size == 0
|
||||||
|
x->free()
|
||||||
|
return 0 as ptr
|
||||||
|
|
||||||
|
new_size = mem.align(new_size)
|
||||||
|
|
||||||
|
blk := (x - MEM_BLOCK_SIZE) as mem.Block
|
||||||
|
if blk->size >= new_size
|
||||||
|
blk->_split(new_size)
|
||||||
|
return x
|
||||||
|
|
||||||
|
next := blk->next
|
||||||
|
expected_next := (blk as ptr + MEM_BLOCK_SIZE + blk->size) as mem.Block
|
||||||
|
|
||||||
|
if next as ptr && next->free && expected_next as ptr == next as ptr
|
||||||
|
combined := blk->size + MEM_BLOCK_SIZE + next->size
|
||||||
|
if combined >= new_size
|
||||||
|
blk->size = combined
|
||||||
|
blk->next = next->next
|
||||||
|
if next->next as ptr
|
||||||
|
next->next->prev = blk
|
||||||
|
if mem.read64(_builtin_heap_tail()) == next as ptr
|
||||||
|
mem.write64(_builtin_heap_tail(), blk)
|
||||||
|
blk->_split(new_size)
|
||||||
|
return x
|
||||||
|
|
||||||
|
new_ptr := mem.alloc(new_size)
|
||||||
|
|
||||||
|
mem.copy(x, new_ptr, math.min(blk->size, new_size))
|
||||||
|
x->free()
|
||||||
|
return new_ptr
|
||||||
|
|
||||||
|
func ptr.zero_and_free[x: ptr, size: i64] : void
|
||||||
|
mem.zero(x, size)
|
||||||
|
x->free()
|
||||||
|
|
||||||
|
func mem.zero[x: ptr, size: i64] : void
|
||||||
|
i := 0
|
||||||
|
while i + 8 <= size
|
||||||
|
mem.write64(x + i, 0)
|
||||||
|
i += 8
|
||||||
|
while i < size
|
||||||
|
x[i] = 0 as u8
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
func mem.copy[src: ptr, dst: ptr, n: i64] : void
|
||||||
|
if dst > src && dst < src + n
|
||||||
|
i := n
|
||||||
|
while i - 8 >= 0
|
||||||
|
i -= 8
|
||||||
|
mem.write64(dst + i, mem.read64(src + i))
|
||||||
|
while i > 0
|
||||||
|
i -= 1
|
||||||
|
dst[i] = src[i]
|
||||||
|
else
|
||||||
|
i := 0
|
||||||
|
while i + 8 <= n
|
||||||
|
mem.write64(dst + i, mem.read64(src + i))
|
||||||
|
i += 8
|
||||||
|
while i < n
|
||||||
|
dst[i] = src[i]
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
func mem.read8[x: ptr] : u8
|
||||||
|
return x[0]
|
||||||
|
|
||||||
|
func mem.read16[x: ptr] : i64
|
||||||
|
return x[0] as i64 | (x[1] << 8)
|
||||||
|
|
||||||
|
func mem.read16be[x: ptr] : i64
|
||||||
|
return (x[0] << 8) as i64 | x[1]
|
||||||
|
|
||||||
|
func mem.read32[x: ptr] : i64
|
||||||
|
return x[0] as i64 | (x[1] << 8) | (x[2] << 16) | (x[3] << 24)
|
||||||
|
|
||||||
|
func mem.read32be[x: ptr] : i64
|
||||||
|
return (x[0] as i64 << 24) | (x[1] << 16) | (x[2] << 8) | x[3]
|
||||||
|
|
||||||
|
func mem.read64[x: ptr] : i64
|
||||||
|
return _builtin_read64(x)
|
||||||
|
|
||||||
|
func mem.write32[x: ptr, d: i64] : void
|
||||||
|
x[0] = d & 0xff
|
||||||
|
x[1] = (d >> 8) & 0xff
|
||||||
|
x[2] = (d >> 16) & 0xff
|
||||||
|
x[3] = (d >> 24) & 0xff
|
||||||
|
|
||||||
|
func mem.write16[x: ptr, d: i64] : void
|
||||||
|
x[0] = d & 0xff
|
||||||
|
x[1] = (d >> 8) & 0xff
|
||||||
|
|
||||||
|
func mem.write16be[x: ptr, d: i64] : void
|
||||||
|
x[0] = (d >> 8) & 0xff
|
||||||
|
x[1] = d & 0xff
|
||||||
|
|
||||||
|
func mem.write64[x: ptr, d: any] : void
|
||||||
|
_builtin_set64(x, d)
|
||||||
|
|
||||||
|
struct Blob
|
||||||
|
data: ptr
|
||||||
|
size: i64
|
||||||
|
|
||||||
|
func Blob.alloc[size: i64] : Blob
|
||||||
|
buffer := new* Blob
|
||||||
|
buffer->size = size
|
||||||
|
buffer->data = mem.alloc(size)
|
||||||
|
return buffer
|
||||||
|
|
||||||
|
func Blob.free[buf: Blob] : void
|
||||||
|
buf->data->free()
|
||||||
|
(buf as ptr)->free()
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include "os.zr"
|
||||||
|
|
||||||
const AF_INET = 2
|
const AF_INET = 2
|
||||||
const SOCK_STREAM = 1
|
const SOCK_STREAM = 1
|
||||||
const SOCK_DGRAM = 2
|
const SOCK_DGRAM = 2
|
||||||
@@ -20,7 +22,7 @@ func net.listen[host: str, port: i64] : i64, bool
|
|||||||
_builtin_syscall(SYS_close, s)
|
_builtin_syscall(SYS_close, s)
|
||||||
return -1, false
|
return -1, false
|
||||||
|
|
||||||
~packed_host, ok := net.resolve(host)
|
packed_host, ok := net.resolve(host)
|
||||||
if !ok
|
if !ok
|
||||||
return -1, false
|
return -1, false
|
||||||
|
|
||||||
@@ -42,7 +44,7 @@ func net.connect[host: str, port: i64] : i64, bool
|
|||||||
if s < 0
|
if s < 0
|
||||||
return -1, false
|
return -1, false
|
||||||
|
|
||||||
~packed_host, ok := net.resolve(host)
|
packed_host, ok := net.resolve(host)
|
||||||
if !ok
|
if !ok
|
||||||
return -1, false
|
return -1, false
|
||||||
|
|
||||||
@@ -99,18 +101,18 @@ func net.UDPSocket.receive[s: net.UDPSocket, size: i64] : net.UDPPacket
|
|||||||
func net.UDPSocket.close_and_free[s: net.UDPSocket] : void
|
func net.UDPSocket.close_and_free[s: net.UDPSocket] : void
|
||||||
_builtin_syscall(SYS_close, s->fd)
|
_builtin_syscall(SYS_close, s->fd)
|
||||||
s->addr->free()
|
s->addr->free()
|
||||||
mem.free(s)
|
(s as ptr)->free()
|
||||||
|
|
||||||
func net.create_udp_client[host: str, port: i64] : net.UDPSocket, bool
|
func net.create_udp_client[host: str, port: i64] : net.UDPSocket, bool
|
||||||
s := new* net.UDPSocket
|
s := new* net.UDPSocket
|
||||||
|
|
||||||
~packed_host, ok := net.resolve(host)
|
packed_host, ok := net.resolve(host)
|
||||||
if !ok
|
if !ok
|
||||||
return 0 as net.UDPSocket, false
|
return 0 as net.UDPSocket, false
|
||||||
|
|
||||||
s->fd = _builtin_syscall(SYS_socket, AF_INET, SOCK_DGRAM, 0)
|
s->fd = _builtin_syscall(SYS_socket, AF_INET, SOCK_DGRAM, 0)
|
||||||
if s->fd < 0
|
if s->fd < 0
|
||||||
mem.free(s)
|
(s as ptr)->free()
|
||||||
return 0 as net.UDPSocket, false
|
return 0 as net.UDPSocket, false
|
||||||
|
|
||||||
s->addr = mem.alloc(16)
|
s->addr = mem.alloc(16)
|
||||||
@@ -118,7 +120,7 @@ func net.create_udp_client[host: str, port: i64] : net.UDPSocket, bool
|
|||||||
return s, true
|
return s, true
|
||||||
|
|
||||||
func net.create_udp_server[host: str, port: i64] : net.UDPSocket, bool
|
func net.create_udp_server[host: str, port: i64] : net.UDPSocket, bool
|
||||||
~s, ok := net.create_udp_client(host, port)
|
s, ok := net.create_udp_client(host, port)
|
||||||
if !ok
|
if !ok
|
||||||
return 0 as net.UDPSocket, false
|
return 0 as net.UDPSocket, false
|
||||||
|
|
||||||
@@ -139,7 +141,7 @@ struct net.UDPPacket
|
|||||||
func net.UDPPacket.free[pkt: net.UDPPacket] : void
|
func net.UDPPacket.free[pkt: net.UDPPacket] : void
|
||||||
pkt->data->free()
|
pkt->data->free()
|
||||||
pkt->source_addr->free()
|
pkt->source_addr->free()
|
||||||
mem.free(pkt)
|
(pkt as ptr)->free()
|
||||||
|
|
||||||
func net.resolve[domain: str] : i64, bool
|
func net.resolve[domain: str] : i64, bool
|
||||||
// if domain is already an ip, skip dns resolution
|
// if domain is already an ip, skip dns resolution
|
||||||
@@ -170,7 +172,7 @@ func net.resolve[domain: str] : i64, bool
|
|||||||
query := net.build_dns_query(domain, DNS_TYPE_A)
|
query := net.build_dns_query(domain, DNS_TYPE_A)
|
||||||
|
|
||||||
// TODO: this probably shouldnt be hardcoded
|
// TODO: this probably shouldnt be hardcoded
|
||||||
~s, ok := net.create_udp_client("1.1.1.1", 53)
|
s, ok := net.create_udp_client("1.1.1.1", 53)
|
||||||
if !ok
|
if !ok
|
||||||
query->free()
|
query->free()
|
||||||
return -1, false
|
return -1, false
|
||||||
220
std/num.zr
Normal file
220
std/num.zr
Normal file
@@ -0,0 +1,220 @@
|
|||||||
|
include "io.zr"
|
||||||
|
|
||||||
|
func i64.abs[n: i64] : i64
|
||||||
|
if n == -9223372036854775808
|
||||||
|
panic("MIN_I64 passed to math.abs")
|
||||||
|
if n < 0
|
||||||
|
return -n
|
||||||
|
return n
|
||||||
|
|
||||||
|
func i64.sign[n: i64] : i64
|
||||||
|
if n < 0
|
||||||
|
return -1
|
||||||
|
else if n > 0
|
||||||
|
return 1
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
|
||||||
|
func i64.isqrt[n: i64] : i64
|
||||||
|
if n < 0
|
||||||
|
panic("negative number passed to math.isqrt")
|
||||||
|
if n == 0 || n == 1
|
||||||
|
return n
|
||||||
|
|
||||||
|
guess := n
|
||||||
|
next_guess := (guess + n / guess) / 2
|
||||||
|
|
||||||
|
while next_guess < guess
|
||||||
|
guess = next_guess
|
||||||
|
next_guess = (guess + n / guess) / 2
|
||||||
|
|
||||||
|
return guess
|
||||||
|
|
||||||
|
func i64.is_prime[n: i64]: bool
|
||||||
|
if n <= 1
|
||||||
|
return false
|
||||||
|
if n == 2 || n == 3
|
||||||
|
return true
|
||||||
|
if n % 2 == 0 || n % 3 == 0
|
||||||
|
return false
|
||||||
|
|
||||||
|
i := 5
|
||||||
|
while i * i <= n
|
||||||
|
if n % i == 0 || n % (i + 2) == 0
|
||||||
|
return false
|
||||||
|
i += 6
|
||||||
|
return true
|
||||||
|
|
||||||
|
func i64.to_str_buf[n: i64, buf: ptr] : void
|
||||||
|
if n == 0
|
||||||
|
buf[0] = '0'
|
||||||
|
buf[1] = 0
|
||||||
|
return
|
||||||
|
|
||||||
|
neg : bool = n < 0
|
||||||
|
if neg
|
||||||
|
// MIN_I64 will fail but i so dont care
|
||||||
|
n = -n
|
||||||
|
tmp := _stackalloc(21)
|
||||||
|
end := 20
|
||||||
|
tmp[end] = 0
|
||||||
|
end -= 1
|
||||||
|
for i in 0..19
|
||||||
|
if n == 0
|
||||||
|
break
|
||||||
|
tmp[end] = '0' + (n % 10)
|
||||||
|
n = n / 10
|
||||||
|
end -= 1
|
||||||
|
if neg
|
||||||
|
tmp[end] = '-'
|
||||||
|
end -= 1
|
||||||
|
len := 19 - end
|
||||||
|
for i in 0..len
|
||||||
|
buf[i] = tmp[end + 1 + i]
|
||||||
|
buf[len] = 0
|
||||||
|
|
||||||
|
func i64.to_hex_str_buf[n: i64, buf: ptr] : void
|
||||||
|
hex_chars := "0123456789abcdef"
|
||||||
|
|
||||||
|
if n == 0
|
||||||
|
buf[0] = '0'
|
||||||
|
buf[1] = 0
|
||||||
|
return
|
||||||
|
|
||||||
|
mask := (1 << 60) - 1
|
||||||
|
tmp := _stackalloc(17)
|
||||||
|
len := 0
|
||||||
|
|
||||||
|
for i in 0..16
|
||||||
|
if n == 0
|
||||||
|
break
|
||||||
|
tmp[len] = hex_chars[n & 15]
|
||||||
|
n = (n >> 4) & mask
|
||||||
|
len += 1
|
||||||
|
|
||||||
|
for j in 0..len
|
||||||
|
buf[j] = tmp[len - 1 - j]
|
||||||
|
|
||||||
|
buf[len] = 0
|
||||||
|
|
||||||
|
func i64.to_str[n: i64] : str
|
||||||
|
out := mem.alloc(21)
|
||||||
|
n->to_str_buf(out)
|
||||||
|
return out as str
|
||||||
|
|
||||||
|
func i64.to_hex_str[n: i64] : str
|
||||||
|
out := mem.alloc(17)
|
||||||
|
n->to_hex_str_buf(out)
|
||||||
|
return out as str
|
||||||
|
|
||||||
|
func f64.to_str_buf[x: i64, buf: ptr] : void
|
||||||
|
bits := x
|
||||||
|
sign := (bits >> 63) & 1
|
||||||
|
exp := (bits >> 52) & 0x7ff
|
||||||
|
mant := bits & 0x000fffffffffffff
|
||||||
|
|
||||||
|
if exp == 0x7ff
|
||||||
|
panic("invalid float")
|
||||||
|
|
||||||
|
p := 0
|
||||||
|
if sign
|
||||||
|
buf[p] = '-'
|
||||||
|
p += 1
|
||||||
|
bits = bits & 0x7fffffffffffffff
|
||||||
|
|
||||||
|
if exp == 0 && mant == 0
|
||||||
|
buf[p] = '0'
|
||||||
|
buf[p+1] = '.'
|
||||||
|
buf[p+2] = '0'
|
||||||
|
buf[p+3] = 0
|
||||||
|
return
|
||||||
|
|
||||||
|
sig := mant
|
||||||
|
if exp != 0
|
||||||
|
sig = sig | 0x0010000000000000
|
||||||
|
|
||||||
|
shift := exp - 1075
|
||||||
|
|
||||||
|
if shift >= 0
|
||||||
|
(sig << shift)->to_str_buf(buf + p)
|
||||||
|
while buf[p]
|
||||||
|
p += 1
|
||||||
|
buf[p] = '.'
|
||||||
|
buf[p+1] = '0'
|
||||||
|
buf[p+2] = 0
|
||||||
|
return
|
||||||
|
|
||||||
|
ns := -shift
|
||||||
|
|
||||||
|
if ns <= 52
|
||||||
|
int_part := sig >> ns
|
||||||
|
int_part->to_str_buf(buf + p)
|
||||||
|
while buf[p]
|
||||||
|
p += 1
|
||||||
|
else
|
||||||
|
buf[p] = '0'
|
||||||
|
p += 1
|
||||||
|
|
||||||
|
buf[p] = '.'
|
||||||
|
p += 1
|
||||||
|
|
||||||
|
mask := (1 << ns) - 1
|
||||||
|
frac := sig & mask
|
||||||
|
|
||||||
|
for i in 0..6
|
||||||
|
if frac == 0
|
||||||
|
if i > 0
|
||||||
|
break
|
||||||
|
buf[p] = '0'
|
||||||
|
p += 1
|
||||||
|
break
|
||||||
|
frac = frac * 10
|
||||||
|
digit := frac >> ns
|
||||||
|
buf[p] = '0' + digit as u8
|
||||||
|
p += 1
|
||||||
|
frac = frac & mask
|
||||||
|
buf[p] = 0
|
||||||
|
|
||||||
|
func math.gcd[a: i64, b: i64] : i64
|
||||||
|
a = a->abs()
|
||||||
|
b = b->abs()
|
||||||
|
while b != 0
|
||||||
|
tmp := b
|
||||||
|
b = a % b
|
||||||
|
a = tmp
|
||||||
|
return a
|
||||||
|
|
||||||
|
func math.min[a: i64, b: i64] : i64
|
||||||
|
if a < b
|
||||||
|
return a
|
||||||
|
return b
|
||||||
|
|
||||||
|
func math.max[a: i64, b: i64] : i64
|
||||||
|
if a > b
|
||||||
|
return a
|
||||||
|
return b
|
||||||
|
|
||||||
|
func math.pow[b: i64, e: i64] : i64
|
||||||
|
if e < 0
|
||||||
|
panic("negative exponent passed to math.pow")
|
||||||
|
out := 1
|
||||||
|
while e > 0
|
||||||
|
if e & 1
|
||||||
|
out = out * b
|
||||||
|
b = b * b
|
||||||
|
e = e >> 1
|
||||||
|
return out
|
||||||
|
|
||||||
|
func math.lcm[a: i64, b: i64] : i64
|
||||||
|
return (a / math.gcd(a, b)) * b
|
||||||
|
|
||||||
|
func math.octal_to_decimal[octal: i64] : i64
|
||||||
|
decimal := 0
|
||||||
|
base := 1
|
||||||
|
|
||||||
|
while octal > 0
|
||||||
|
digit := octal % 10
|
||||||
|
decimal += digit * base
|
||||||
|
base = base * 8
|
||||||
|
octal = octal / 10
|
||||||
|
return decimal
|
||||||
154
std/os.zr
Normal file
154
std/os.zr
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
include "posix.zr"
|
||||||
|
include "linux_syscalls.zr"
|
||||||
|
include "str.zr"
|
||||||
|
include "containers.zr"
|
||||||
|
|
||||||
|
func os.exit[code: i64] : void
|
||||||
|
_builtin_syscall(SYS_exit, code)
|
||||||
|
|
||||||
|
func os.getpid[] : i64
|
||||||
|
return _builtin_syscall(SYS_getpid)
|
||||||
|
|
||||||
|
func os.arg[argv: ptr, n: i64] : str
|
||||||
|
return mem.read64(argv + n * 8) as str
|
||||||
|
|
||||||
|
func os.getenv[name: str] : str, bool
|
||||||
|
name_len := name->len()
|
||||||
|
i := 0
|
||||||
|
while true
|
||||||
|
entry := mem.read64(_builtin_environ() + i * 8) as str
|
||||||
|
if entry as ptr == 0
|
||||||
|
break
|
||||||
|
if entry->equal_n(name, name_len) && entry[name_len] == '='
|
||||||
|
return (entry as ptr + name_len + 1) as str, true
|
||||||
|
i += 1
|
||||||
|
return 0 as str, false
|
||||||
|
|
||||||
|
func os.basename[path: str] : str
|
||||||
|
len := path->len()
|
||||||
|
end := len
|
||||||
|
while end > 0 && path[end - 1] == '/'
|
||||||
|
end -= 1
|
||||||
|
|
||||||
|
if end == 0
|
||||||
|
return "/"
|
||||||
|
|
||||||
|
i := end - 1
|
||||||
|
while i >= 0 && path[i] != '/'
|
||||||
|
i -= 1
|
||||||
|
|
||||||
|
return path->substr_n(i + 1, end - i - 1)
|
||||||
|
|
||||||
|
struct os.Timespec
|
||||||
|
tv_sec: i64
|
||||||
|
tv_nsec: i64
|
||||||
|
|
||||||
|
func os.sleep[ms: i64] : void
|
||||||
|
if ms < 0
|
||||||
|
panic("negative time passed to os.sleep")
|
||||||
|
req := new os.Timespec
|
||||||
|
req->tv_sec = ms / 1000
|
||||||
|
req->tv_nsec = (ms % 1000) * 1000000
|
||||||
|
_builtin_syscall(SYS_nanosleep, req, 0)
|
||||||
|
|
||||||
|
func os.time[] : i64
|
||||||
|
ts := new os.Timespec
|
||||||
|
_builtin_syscall(SYS_clock_gettime, CLOCK_REALTIME, ts)
|
||||||
|
out := ts->tv_sec * 1000 + ts->tv_nsec / 1000000
|
||||||
|
return out
|
||||||
|
|
||||||
|
func os.urandom_buf[buf: ptr, n: i64] : void
|
||||||
|
pos := 0
|
||||||
|
while pos < n
|
||||||
|
ret := _builtin_syscall(SYS_getrandom, buf + pos, n - pos, 0)
|
||||||
|
if ret <= 0
|
||||||
|
panic("os.urandom_buf: syscall failed")
|
||||||
|
pos += ret
|
||||||
|
|
||||||
|
func os.urandom[n: i64]: ptr
|
||||||
|
buf := mem.alloc(n)
|
||||||
|
os.urandom_buf(buf, n)
|
||||||
|
return buf
|
||||||
|
|
||||||
|
func os.urandom_i64[] : i64
|
||||||
|
n := 0
|
||||||
|
os.urandom_buf(^n, 8)
|
||||||
|
return n
|
||||||
|
|
||||||
|
func os.run_shell_command[command: str] : i64, bool
|
||||||
|
pid := _builtin_syscall(SYS_fork)
|
||||||
|
if pid < 0
|
||||||
|
return -1, false
|
||||||
|
|
||||||
|
if pid == 0
|
||||||
|
argv := ["sh", "-c", command, 0] // gets freed after child process exits
|
||||||
|
_builtin_syscall(SYS_execve, "/bin/sh", argv->data, _builtin_environ())
|
||||||
|
os.exit(1)
|
||||||
|
else
|
||||||
|
status := 0
|
||||||
|
wp := _builtin_syscall(SYS_wait4, pid, ^status, 0, 0)
|
||||||
|
if wp < 0
|
||||||
|
return -1, false
|
||||||
|
st := status & 0xffffffff
|
||||||
|
|
||||||
|
if (st & 0x7f) == 0
|
||||||
|
return (st >> 8) & 0xff, true
|
||||||
|
else
|
||||||
|
return -(st & 0x7f), true
|
||||||
|
|
||||||
|
func os.file_exists[path: str] : bool
|
||||||
|
return _builtin_syscall(SYS_faccessat, AT_FDCWD, path, F_OK, 0) == 0
|
||||||
|
|
||||||
|
func os.get_file_size[path: str] : i64, bool
|
||||||
|
st := _stackalloc(256) // it has 21 mixed-size fields so ptr for now
|
||||||
|
|
||||||
|
rc := _builtin_syscall(SYS_newfstatat, AT_FDCWD, path, st, 0)
|
||||||
|
if rc != 0
|
||||||
|
return 0, false
|
||||||
|
|
||||||
|
return mem.read64(st + 48), true
|
||||||
|
|
||||||
|
func os.is_a_directory[path: str] : bool
|
||||||
|
st := _stackalloc(256) // it has 21 mixed-size fields so ptr for now
|
||||||
|
|
||||||
|
rc := _builtin_syscall(SYS_newfstatat, AT_FDCWD, path, st, 0)
|
||||||
|
if rc != 0
|
||||||
|
return false
|
||||||
|
|
||||||
|
return (mem.read32(st + 24) & S_IFMT) == S_IFDIR
|
||||||
|
|
||||||
|
func os.list_directory[path: str] : Array, bool
|
||||||
|
fd := _builtin_syscall(SYS_openat, AT_FDCWD, path, O_RDONLY, 0)
|
||||||
|
if fd < 0
|
||||||
|
return 0 as Array, false
|
||||||
|
|
||||||
|
files := []
|
||||||
|
buf := _stackalloc(1024)
|
||||||
|
while true
|
||||||
|
n := _builtin_syscall(SYS_getdents64, fd, buf, 1024)
|
||||||
|
if n < 0
|
||||||
|
files->free_with_items()
|
||||||
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
return 0 as Array, false
|
||||||
|
else if n == 0
|
||||||
|
break
|
||||||
|
|
||||||
|
pos := 0
|
||||||
|
while pos < n
|
||||||
|
len := mem.read16(buf + pos + 16)
|
||||||
|
name : ptr = buf + pos + 19
|
||||||
|
if name[0]
|
||||||
|
skip := false
|
||||||
|
// skip if name is exactly '.' or '..'
|
||||||
|
if name[0] == '.'
|
||||||
|
if name[1] == 0
|
||||||
|
skip = true
|
||||||
|
else if name[1] == '.'
|
||||||
|
if name[2] == 0
|
||||||
|
skip = true
|
||||||
|
if !skip
|
||||||
|
files->push((name as str)->make_copy())
|
||||||
|
pos += len
|
||||||
|
|
||||||
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
return files, true
|
||||||
32
std/posix.zr
Normal file
32
std/posix.zr
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
const STDIN_FILENO = 0
|
||||||
|
const STDOUT_FILENO = 1
|
||||||
|
|
||||||
|
const SIGABRT = 6
|
||||||
|
|
||||||
|
const AT_FDCWD = -100
|
||||||
|
|
||||||
|
const S_IFDIR = 16384
|
||||||
|
const S_IFMT = 61440
|
||||||
|
|
||||||
|
const PROT_NONE = 0
|
||||||
|
const PROT_READ = 1
|
||||||
|
const PROT_WRITE = 2
|
||||||
|
const PROT_EXEC = 4
|
||||||
|
|
||||||
|
const MAP_FAILED = -1
|
||||||
|
const MAP_SHARED = 1
|
||||||
|
const MAP_PRIVATE = 2
|
||||||
|
const MAP_FIXED = 16
|
||||||
|
const MAP_ANONYMOUS = 32
|
||||||
|
|
||||||
|
const O_RDONLY = 0
|
||||||
|
const O_WRONLY = 1
|
||||||
|
const O_CREAT = 64
|
||||||
|
const O_TRUNC = 512
|
||||||
|
|
||||||
|
const SEEK_SET = 0
|
||||||
|
const SEEK_END = 2
|
||||||
|
|
||||||
|
const F_OK = 0
|
||||||
|
|
||||||
|
const CLOCK_REALTIME = 0
|
||||||
299
std/str.zr
Normal file
299
std/str.zr
Normal file
@@ -0,0 +1,299 @@
|
|||||||
|
include "num.zr"
|
||||||
|
include "mem.zr"
|
||||||
|
|
||||||
|
func u8.is_whitespace[x: u8] : bool
|
||||||
|
return x == ' ' || x == '\n' || x == '\t' || x == '\r'
|
||||||
|
|
||||||
|
func u8.is_digit[x: u8] : bool
|
||||||
|
return x >= '0' && x <= '9'
|
||||||
|
|
||||||
|
func u8.is_hex_digit[x: u8] : bool
|
||||||
|
return (x >= '0' && x <= '9') || (x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F')
|
||||||
|
|
||||||
|
func u8.is_lowercase[x: u8] : bool
|
||||||
|
return x >= 'a' && x <= 'z'
|
||||||
|
|
||||||
|
func u8.is_uppercase[x: u8] : bool
|
||||||
|
return x >= 'A' && x <= 'Z'
|
||||||
|
|
||||||
|
func u8.is_letter[x: u8] : bool
|
||||||
|
return x->is_uppercase() || x->is_lowercase()
|
||||||
|
|
||||||
|
func u8.is_alphanumeric[x: u8] : bool
|
||||||
|
return x->is_letter() || x->is_digit()
|
||||||
|
|
||||||
|
func u8.to_str[c: u8] : str
|
||||||
|
s := mem.alloc(2) as str
|
||||||
|
s[0] = c
|
||||||
|
s[1] = 0
|
||||||
|
return s
|
||||||
|
|
||||||
|
func str.free[s: str] : void
|
||||||
|
(s as ptr)->free()
|
||||||
|
|
||||||
|
func str.len[s: str] : i64
|
||||||
|
i := 0
|
||||||
|
while s[i]
|
||||||
|
i += 1
|
||||||
|
return i
|
||||||
|
|
||||||
|
func str.make_copy[s: str] : str
|
||||||
|
size := s->len() + 1
|
||||||
|
dup := mem.alloc(size) as str
|
||||||
|
mem.copy(s as ptr, dup as ptr, size)
|
||||||
|
return dup
|
||||||
|
|
||||||
|
func str.equal[a: str, b: str] : bool
|
||||||
|
i := 0
|
||||||
|
while a[i] != 0 && b[i] != 0
|
||||||
|
if a[i] != b[i]
|
||||||
|
return false
|
||||||
|
i += 1
|
||||||
|
return a[i] == b[i]
|
||||||
|
|
||||||
|
func str.equal_n[a: str, b: str, n: i64] : bool
|
||||||
|
i := 0
|
||||||
|
while i < n
|
||||||
|
if a[i] == 0 || b[i] == 0
|
||||||
|
return false
|
||||||
|
if a[i] != b[i]
|
||||||
|
return false
|
||||||
|
i += 1
|
||||||
|
return true
|
||||||
|
|
||||||
|
func str.format_into[..] : i64
|
||||||
|
buf := _var_arg(0) as ptr
|
||||||
|
size := _var_arg(1) as i64
|
||||||
|
if size <= 0
|
||||||
|
return 0
|
||||||
|
s := _var_arg(2) as ptr
|
||||||
|
i := 3
|
||||||
|
n := 0
|
||||||
|
|
||||||
|
tmp := _stackalloc(64) as str
|
||||||
|
|
||||||
|
while s[0]
|
||||||
|
if s[0] == '%'
|
||||||
|
s += 1
|
||||||
|
|
||||||
|
if s[0] == 'd'
|
||||||
|
(_var_arg(i) as i64)->to_str_buf(tmp as ptr)
|
||||||
|
tmp_len := tmp->len()
|
||||||
|
remaining := size - n - 1
|
||||||
|
mem.copy(tmp as ptr, buf + n, math.min(tmp_len, remaining))
|
||||||
|
n += tmp_len
|
||||||
|
i += 1
|
||||||
|
else if s[0] == 'x'
|
||||||
|
(_var_arg(i) as i64)->to_hex_str_buf(tmp as ptr)
|
||||||
|
tmp_len := tmp->len()
|
||||||
|
remaining := size - n - 1
|
||||||
|
mem.copy(tmp as ptr, buf + n, math.min(tmp_len, remaining))
|
||||||
|
n += tmp_len
|
||||||
|
i += 1
|
||||||
|
else if s[0] == 's'
|
||||||
|
tmp_str := _var_arg(i) as str
|
||||||
|
tmp_len := tmp_str->len()
|
||||||
|
remaining := size - n - 1
|
||||||
|
mem.copy(tmp_str as ptr, buf + n, math.min(tmp_len, remaining))
|
||||||
|
n += tmp_len
|
||||||
|
i += 1
|
||||||
|
else if s[0] == 'c'
|
||||||
|
if n < size - 1
|
||||||
|
buf[n] = _var_arg(i) as u8
|
||||||
|
n += 1
|
||||||
|
i += 1
|
||||||
|
else if s[0] == 'f'
|
||||||
|
// TODO: use arrow when we implement f64 params
|
||||||
|
f64.to_str_buf(_var_arg(i), tmp as ptr)
|
||||||
|
tmp_len := tmp->len()
|
||||||
|
remaining := size - n - 1
|
||||||
|
mem.copy(tmp as ptr, buf + n, math.min(tmp_len, remaining))
|
||||||
|
n += tmp_len
|
||||||
|
i += 1
|
||||||
|
else if s[0] == '%'
|
||||||
|
if n < size - 1
|
||||||
|
buf[n] = '%'
|
||||||
|
n += 1
|
||||||
|
else if s[0] == 0
|
||||||
|
break
|
||||||
|
else
|
||||||
|
panic("str.format_into: unrecognized format")
|
||||||
|
else
|
||||||
|
if n < size - 1
|
||||||
|
buf[n] = s[0]
|
||||||
|
n += 1
|
||||||
|
s += 1
|
||||||
|
|
||||||
|
if n < size
|
||||||
|
buf[n] = 0
|
||||||
|
else if size > 0
|
||||||
|
buf[size - 1] = 0
|
||||||
|
return n
|
||||||
|
|
||||||
|
func str.concat[a: str, b: str] : str
|
||||||
|
a_len := a->len()
|
||||||
|
b_len := b->len()
|
||||||
|
out := mem.alloc(a_len + b_len + 1) as str
|
||||||
|
mem.copy(a as ptr, out as ptr, a_len)
|
||||||
|
mem.copy(b as ptr, out as ptr + a_len, b_len)
|
||||||
|
out[a_len + b_len] = 0
|
||||||
|
return out
|
||||||
|
|
||||||
|
func str.contains[haystack: str, needle: str] : bool
|
||||||
|
return haystack->find(needle) != -1
|
||||||
|
|
||||||
|
func str.find[haystack: str, needle: str] : i64
|
||||||
|
haystack_len := haystack->len()
|
||||||
|
needle_len := needle->len()
|
||||||
|
|
||||||
|
if needle_len == 0
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if needle_len > haystack_len
|
||||||
|
return -1
|
||||||
|
|
||||||
|
for i in 0..(haystack_len - needle_len + 1)
|
||||||
|
if haystack[i] != needle[0]
|
||||||
|
continue
|
||||||
|
if needle_len == 1
|
||||||
|
return i
|
||||||
|
match := true
|
||||||
|
for j in 1..needle_len
|
||||||
|
if haystack[i + j] != needle[j]
|
||||||
|
match = false
|
||||||
|
break
|
||||||
|
if match
|
||||||
|
return i
|
||||||
|
return -1
|
||||||
|
|
||||||
|
func str.substr_n[s: str, start: i64, length: i64] : str
|
||||||
|
out := mem.alloc(length + 1) as str
|
||||||
|
mem.copy(s as ptr + start, out as ptr, length)
|
||||||
|
out[length] = 0
|
||||||
|
return out
|
||||||
|
|
||||||
|
func str.trim[s: str] : str
|
||||||
|
len := s->len()
|
||||||
|
if len == 0
|
||||||
|
out := mem.alloc(1) as str
|
||||||
|
out[0] = 0
|
||||||
|
return out
|
||||||
|
|
||||||
|
start := 0
|
||||||
|
end := len - 1
|
||||||
|
|
||||||
|
while start <= end && s[start]->is_whitespace()
|
||||||
|
start += 1
|
||||||
|
|
||||||
|
while end >= start && s[end]->is_whitespace()
|
||||||
|
end -= 1
|
||||||
|
|
||||||
|
return s->substr_n(start, end - start + 1)
|
||||||
|
|
||||||
|
func str.reverse[s: str] : str
|
||||||
|
len := s->len()
|
||||||
|
out := mem.alloc(len + 1) as str
|
||||||
|
|
||||||
|
for i in 0..len
|
||||||
|
out[i] = s[len - i - 1]
|
||||||
|
out[len] = 0
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
func str.parse_i64[s: str] : i64
|
||||||
|
len := s->len()
|
||||||
|
i := 0
|
||||||
|
|
||||||
|
sign := 1
|
||||||
|
if i < len && s[i] == '-'
|
||||||
|
sign = -1
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
num := 0
|
||||||
|
while i < len
|
||||||
|
d := s[i]
|
||||||
|
if !d->is_digit()
|
||||||
|
break
|
||||||
|
num = num * 10 + (d - '0')
|
||||||
|
i += 1
|
||||||
|
return num * sign
|
||||||
|
|
||||||
|
func str.hex_encode[s: str, s_len: i64] : str
|
||||||
|
hex_chars := "0123456789abcdef"
|
||||||
|
out := mem.alloc(s_len * 2 + 1) as str
|
||||||
|
|
||||||
|
for i in 0..s_len
|
||||||
|
b := s[i]
|
||||||
|
out[i * 2] = hex_chars[(b >> 4) & 15]
|
||||||
|
out[i * 2 + 1] = hex_chars[b & 15]
|
||||||
|
|
||||||
|
out[s_len * 2] = 0
|
||||||
|
return out
|
||||||
|
|
||||||
|
func str._hex_digit_to_int[d: u8] : u8
|
||||||
|
if d->is_digit()
|
||||||
|
return d - '0'
|
||||||
|
lower : u8 = d | 32
|
||||||
|
if lower >= 'a' && lower <= 'f'
|
||||||
|
return lower - 'a' + 10
|
||||||
|
panic("invalid hex digit passed to str._hex_digit_to_int")
|
||||||
|
|
||||||
|
func str.hex_decode[s: str] : str
|
||||||
|
s_len := s->len()
|
||||||
|
if s_len % 2 != 0
|
||||||
|
panic("invalid hex string passed to str.hex_decode")
|
||||||
|
|
||||||
|
out_len := s_len / 2
|
||||||
|
out := mem.alloc(out_len + 1) as str
|
||||||
|
|
||||||
|
for i in 0..out_len
|
||||||
|
high := str._hex_digit_to_int(s[i * 2])
|
||||||
|
low := str._hex_digit_to_int(s[i * 2 + 1])
|
||||||
|
out[i] = (high << 4) | low
|
||||||
|
|
||||||
|
out[out_len] = 0
|
||||||
|
return out
|
||||||
|
|
||||||
|
struct str.Builder
|
||||||
|
data: ptr
|
||||||
|
size: i64
|
||||||
|
capacity: i64
|
||||||
|
|
||||||
|
func str.Builder._grow[b: str.Builder, needed: i64] : void
|
||||||
|
if b->size + needed > b->capacity
|
||||||
|
new_capacity := 64
|
||||||
|
if b->capacity != 0
|
||||||
|
new_capacity = b->capacity * 2
|
||||||
|
while new_capacity < b->size + needed
|
||||||
|
new_capacity = new_capacity * 2
|
||||||
|
b->data = b->data->realloc(new_capacity)
|
||||||
|
b->capacity = new_capacity
|
||||||
|
|
||||||
|
func str.Builder.append_char[b: str.Builder, c: u8] : void
|
||||||
|
b->_grow(1)
|
||||||
|
b->data[b->size] = c
|
||||||
|
b->size += 1
|
||||||
|
|
||||||
|
func str.Builder.append[b: str.Builder, s: str] : void
|
||||||
|
len := s->len()
|
||||||
|
b->_grow(len)
|
||||||
|
mem.copy(s as ptr, b->data + b->size, len)
|
||||||
|
b->size += len
|
||||||
|
|
||||||
|
func str.Builder.build[b: str.Builder] : str
|
||||||
|
s := mem.alloc(b->size + 1) as str
|
||||||
|
mem.copy(b->data, s as ptr, b->size)
|
||||||
|
s[b->size] = 0
|
||||||
|
return s
|
||||||
|
|
||||||
|
func str.Builder.build_and_free_buffer[b: str.Builder] : str
|
||||||
|
s := b->build()
|
||||||
|
b->free_buffer()
|
||||||
|
return s
|
||||||
|
|
||||||
|
func str.Builder.free_buffer[b: str.Builder] : void
|
||||||
|
if b->data != 0
|
||||||
|
b->data->free()
|
||||||
|
b->size = 0
|
||||||
|
b->capacity = 0
|
||||||
|
|
||||||
76
test.zr
76
test.zr
@@ -1,35 +1,58 @@
|
|||||||
func run_test[x: str] : void
|
include "$/io.zr"
|
||||||
build_blacklist := ["examples/euler", "examples/raylib.zr", "examples/chip8.zr", "examples/sqlite_todo.zr"]
|
include "$/os.zr"
|
||||||
run_blacklist := ["guess_number.zr", "tcp_server.zr", "udp_server.zr"]
|
|
||||||
|
|
||||||
for i in 0..build_blacklist->size
|
struct TestRunner
|
||||||
if x->equal(build_blacklist->nth(i))
|
build_blacklist: Array
|
||||||
io.printf("Skipping %s...\n", x)
|
run_blacklist: Array
|
||||||
return
|
custom_build_flags: HashMap
|
||||||
|
custom_run_args: HashMap
|
||||||
|
|
||||||
|
func TestRunner.run_directory[tr: TestRunner, dir: str] : void
|
||||||
|
files, ok := os.list_directory(dir)
|
||||||
|
if !ok
|
||||||
|
panic("failed to open test directory")
|
||||||
|
for i in 0..files->size
|
||||||
|
joined := dir->concat("/")->concat(files->nth(i))
|
||||||
|
if os.is_a_directory(joined)
|
||||||
|
tr->run_directory(joined)
|
||||||
|
else
|
||||||
|
tr->run_test(joined)
|
||||||
|
|
||||||
|
func TestRunner.run_test[tr: TestRunner, x: str] : void
|
||||||
|
name := os.basename(x)
|
||||||
|
|
||||||
|
if tr->build_blacklist->contains_str(name)
|
||||||
|
io.printf("Skipping %s...\n", x)
|
||||||
|
return
|
||||||
|
|
||||||
io.printf("Building %s...\n", x)
|
io.printf("Building %s...\n", x)
|
||||||
|
|
||||||
|
build_cmd := "./target/release/zern "->concat(x)
|
||||||
|
custom_build_flags, ok := tr->custom_build_flags->get(name)
|
||||||
|
if ok
|
||||||
|
build_cmd = build_cmd->concat(" ")->concat(custom_build_flags)
|
||||||
|
|
||||||
build_start_time := os.time()
|
build_start_time := os.time()
|
||||||
~status, ok := os.run_shell_command("./target/release/zern "->concat(x))
|
status, ok := os.run_shell_command(build_cmd)
|
||||||
if !ok || status != 0
|
if !ok || status != 0
|
||||||
os.exit(1)
|
os.exit(1)
|
||||||
build_end_time := os.time()
|
build_end_time := os.time()
|
||||||
|
|
||||||
io.printf("%dms\n", build_end_time - build_start_time)
|
io.printf("%dms\n", build_end_time - build_start_time)
|
||||||
|
|
||||||
for i in 0..run_blacklist->size
|
if tr->run_blacklist->contains_str(name)
|
||||||
if x->contains(run_blacklist->nth(i))
|
io.printf("Skipping %s...\n", x)
|
||||||
io.printf("Skipping %s...\n", x)
|
return
|
||||||
return
|
|
||||||
|
|
||||||
io.printf("Running %s...\n", x)
|
io.printf("Running %s...\n", x)
|
||||||
|
|
||||||
run_cmd := "./out"
|
run_cmd := "./out"
|
||||||
if x->equal("examples/curl.zr")
|
custom_run_args, ok := tr->custom_run_args->get(name)
|
||||||
run_cmd = run_cmd->concat(" http://example.com")
|
if ok
|
||||||
|
run_cmd = run_cmd->concat(" ")->concat(custom_run_args)
|
||||||
|
|
||||||
run_start_time := os.time()
|
run_start_time := os.time()
|
||||||
~status, ok := os.run_shell_command(run_cmd)
|
status, ok := os.run_shell_command(run_cmd)
|
||||||
if !ok || status != 0
|
if !ok || status != 0
|
||||||
io.println("Test failed.")
|
io.println("Test failed.")
|
||||||
os.exit(1)
|
os.exit(1)
|
||||||
@@ -37,18 +60,21 @@ func run_test[x: str] : void
|
|||||||
|
|
||||||
io.printf("Running %s took %dms\n", x, run_end_time - run_start_time)
|
io.printf("Running %s took %dms\n", x, run_end_time - run_start_time)
|
||||||
|
|
||||||
func run_directory[dir: str] : void
|
|
||||||
~files, ok := os.list_directory(dir)
|
|
||||||
if !ok
|
|
||||||
panic("failed to open test directory")
|
|
||||||
for i in 0..files->size
|
|
||||||
run_test(dir->concat(files->nth(i)))
|
|
||||||
|
|
||||||
func main[] : i64
|
func main[] : i64
|
||||||
~status, ok := os.run_shell_command("cargo build --release")
|
status, ok := os.run_shell_command("cargo build --release")
|
||||||
if !ok || status != 0
|
if !ok || status != 0
|
||||||
os.exit(1)
|
os.exit(1)
|
||||||
|
|
||||||
run_directory("examples/")
|
tr := new TestRunner
|
||||||
run_directory("examples/euler/")
|
tr->build_blacklist = []
|
||||||
|
tr->run_blacklist = ["raylib.zr", "sqlite_todo.zr", "guess_number.zr", "udp_server.zr", "chip8.zr", "serve.zr"]
|
||||||
|
tr->custom_build_flags = HashMap.new()
|
||||||
|
tr->custom_build_flags->insert("raylib.zr", "-m -C \"-lraylib\"")
|
||||||
|
tr->custom_build_flags->insert("chip8.zr", "-m -C \"-lraylib\"")
|
||||||
|
tr->custom_build_flags->insert("sqlite_todo.zr", "-m -C \"-lsqlite3\"")
|
||||||
|
tr->custom_build_flags->insert("serve.zr", "-m -C \"-lpthread\"")
|
||||||
|
tr->custom_run_args = HashMap.new()
|
||||||
|
tr->custom_run_args->insert("curl.zr", "http://example.com")
|
||||||
|
|
||||||
|
tr->run_directory("examples")
|
||||||
io.println("DONE")
|
io.println("DONE")
|
||||||
|
|||||||
Reference in New Issue
Block a user