1 Commits
v0.1 ... riscv

Author SHA1 Message Date
bdf8a671db borderline experimental riscv codegen 2025-07-28 17:43:37 +02:00
18 changed files with 868 additions and 446 deletions

View File

@@ -2,26 +2,20 @@
A very cool language A very cool language
## Features ## Huh?
* Clean indentation-based syntax * Clean indentation-based syntax
* Compiles to x86_64 Assembly * Compiles to x86_64 Assembly
* Sometimes works * Almost works
* Has the pipe operator * Has the pipe operator
## Syntax ## Syntax
```rust ```rust
func fib[n: I64] : I64
if n <= 1
return n
return fib(n-2) + fib(n-1)
func main[] : I64 func main[] : I64
let answer: I64 = math.abs(math.urandom()) % 100 for i in 0..20
fib(i) |> str.from_i64() |> io.print()
while true
io.print("Guess a number: ")
let guess: I64 = io.read_stdin() |> str.trim() |> str.parse_i64()
if guess == answer
io.print("You win!")
break
else if guess < answer
io.print("Too low!")
else
io.print("Too high!")
``` ```

View File

@@ -1,11 +1,10 @@
func main[] : I64 func main[] : I64
// https://brainfuck.org/sierpinski.b // https://brainfuck.org/sierpinski.b
let src: String = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]" let src: String = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
let src_len: I64 = str.len(src) let src_len: I64 = c.strlen(src)
let i: I64 = 0 let i: I64 = 0
let memory: Ptr = mem.alloc(30000) let memory: Ptr = c.calloc(1, 30000)
mem.zero(memory, 30000)
let p: I64 = 0 let p: I64 = 0
while i < src_len while i < src_len
@@ -16,15 +15,15 @@ func main[] : I64
else if op == '<' else if op == '<'
p = p - 1 p = p - 1
else if op == '+' else if op == '+'
str.set(memory, p, mem.read8(memory + p)+1) str.set(memory, p, str.nth(memory, p)+1)
else if op == '-' else if op == '-'
str.set(memory, p, mem.read8(memory + p)-1) str.set(memory, p, str.nth(memory, p)-1)
else if op == '.' else if op == '.'
c.printf("%c", mem.read8(memory + p)) c.printf("%c", str.nth(memory, p))
else if op == ',' else if op == ','
str.set(memory, p, c.getchar()) str.set(memory, p, c.getchar())
else if op == '[' else if op == '['
if !mem.read8(memory + p) if !str.nth(memory, p)
i = i + 1 i = i + 1
let opened: I64 = 0 let opened: I64 = 0
while i < src_len & !(str.nth(src, i) == ']' & !opened) while i < src_len & !(str.nth(src, i) == ']' & !opened)
@@ -34,7 +33,7 @@ func main[] : I64
opened = opened - 1 opened = opened - 1
i = i + 1 i = i + 1
else if op == ']' else if op == ']'
if mem.read8(memory + p) if str.nth(memory, p)
i = i - 1 i = i - 1
let closed: I64 = 0 let closed: I64 = 0
while i >= 0 & !(str.nth(src, i) == '[' & !closed) while i >= 0 & !(str.nth(src, i) == '[' & !closed)
@@ -45,5 +44,3 @@ func main[] : I64
i = i - 1 i = i - 1
i = i + 1 i = i + 1
mem.free(memory)

View File

@@ -1,38 +1,18 @@
func main[argc: I64, argv: Ptr] : I64 func main[] : I64
if argc < 2 // TODO: parse url
dbg.panic("url missing") let host: String = "devernay.free.fr"
let path: String = "/hacks/chip8/C8TECH10.HTM"
let url: String = mem.read64(argv + 8)
if str.len(url) <= 7
dbg.panic("missing url scheme")
if !str.equal(str.substr(url, 0, 7), "http://")
dbg.panic("invalid url scheme")
let url_len: I64 = str.len(url)
let host_start: I64 = 7
let i: I64 = host_start
while i < url_len
if str.nth(url, i) == '/'
break
i = i + 1
let host: String = str.substr(url, host_start, i - host_start)
let path: String = "/"
if i < url_len
path = str.substr(url, i, url_len - i)
let s: I64 = net.connect(host, 80) let s: I64 = net.connect(host, 80)
if s < 0 if s < 0
dbg.panic("failed to connect") dbg.panic("failed to connect")
let req: String = mem.alloc(2048) let req: String = c.malloc(2048)
c.snprintf(req, 2048, "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", path, host) c.snprintf(req, 2048, "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", path, host)
c.send(s, req, str.len(req), 0) c.send(s, req, c.strlen(req), 0)
mem.free(req) c.free(req)
let header_buf: String = mem.alloc(8192) let header_buf: Ptr = c.malloc(8192)
let header_size: I64 = 0 let header_size: I64 = 0
let found: Bool = false let found: Bool = false
let end_index: I64 = -1 let end_index: I64 = -1
@@ -42,9 +22,10 @@ func main[argc: I64, argv: Ptr] : I64
if n <= 0 if n <= 0
break break
let current_size: I64 = header_size + n let current_size: I64 = header_size + n
i = 0 let i: I64 = 0
while i <= current_size - 4 while i <= current_size - 4
if str.nth(header_buf, i) == 13 & str.nth(header_buf, i+1) == 10 & str.nth(header_buf, i+2) == 13 & str.nth(header_buf, i+3) == 10 let p: Ptr = header_buf + i
if str.nth(p, 0) == 13 & str.nth(p, 1) == 10 & str.nth(p, 2) == 13 & str.nth(p, 3) == 10
found = true found = true
end_index = i + 4 end_index = i + 4
break break
@@ -53,14 +34,14 @@ func main[argc: I64, argv: Ptr] : I64
if end_index < header_size if end_index < header_size
c.write(1, header_buf + end_index, header_size - end_index) c.write(1, header_buf + end_index, header_size - end_index)
mem.free(header_buf) c.free(header_buf)
let buffer: Ptr = mem.alloc(4096) let buffer: Ptr = c.malloc(4096)
while true while true
let n: I64 = c.read(s, buffer, 4096) let n: I64 = c.read(s, buffer, 4096)
if n <= 0 if n <= 0
break break
c.write(1, buffer, n) c.write(1, buffer, n)
mem.free(buffer) c.free(buffer)
c.close(s) c.close(s)

View File

@@ -1,5 +1,4 @@
func main[] : I64 func main[] : I64
// leaks a bit of memory but looks very cool
37107287533 + 46376937677 + 74324986199 + 91942213363 + 23067588207 + 89261670696 + 28112879812 + 44274228917 + 47451445736 + 70386486105 + 62176457141 + 64906352462 + 92575867718 + 58203565325 + 80181199384 + 35398664372 + 86515506006 + 71693888707 + 54370070576 + 53282654108 + 36123272525 + 45876576172 + 17423706905 + 81142660418 + 51934325451 + 62467221648 + 15732444386 + 55037687525 + 18336384825 + 80386287592 + 78182833757 + 16726320100 + 48403098129 + 87086987551 + 59959406895 + 69793950679 + 41052684708 + 65378607361 + 35829035317 + 94953759765 + 88902802571 + 25267680276 + 36270218540 + 24074486908 + 91430288197 + 34413065578 + 23053081172 + 11487696932 + 63783299490 + 67720186971 + 95548255300 + 76085327132 + 37774242535 + 23701913275 + 29798860272 + 18495701454 + 38298203783 + 34829543829 + 40957953066 + 29746152185 + 41698116222 + 62467957194 + 23189706772 + 86188088225 + 11306739708 + 82959174767 + 97623331044 + 42846280183 + 55121603546 + 32238195734 + 75506164965 + 62177842752 + 32924185707 + 99518671430 + 73267460800 + 76841822524 + 97142617910 + 87783646182 + 10848802521 + 71329612474 + 62184073572 + 66627891981 + 60661826293 + 85786944089 + 66024396409 + 64913982680 + 16730939319 + 94809377245 + 78639167021 + 15368713711 + 40789923115 + 44889911501 + 41503128880 + 81234880673 + 82616570773 + 22918802058 + 77158542502 + 72107838435 + 20849603980 + 53503534226 37107287533 + 46376937677 + 74324986199 + 91942213363 + 23067588207 + 89261670696 + 28112879812 + 44274228917 + 47451445736 + 70386486105 + 62176457141 + 64906352462 + 92575867718 + 58203565325 + 80181199384 + 35398664372 + 86515506006 + 71693888707 + 54370070576 + 53282654108 + 36123272525 + 45876576172 + 17423706905 + 81142660418 + 51934325451 + 62467221648 + 15732444386 + 55037687525 + 18336384825 + 80386287592 + 78182833757 + 16726320100 + 48403098129 + 87086987551 + 59959406895 + 69793950679 + 41052684708 + 65378607361 + 35829035317 + 94953759765 + 88902802571 + 25267680276 + 36270218540 + 24074486908 + 91430288197 + 34413065578 + 23053081172 + 11487696932 + 63783299490 + 67720186971 + 95548255300 + 76085327132 + 37774242535 + 23701913275 + 29798860272 + 18495701454 + 38298203783 + 34829543829 + 40957953066 + 29746152185 + 41698116222 + 62467957194 + 23189706772 + 86188088225 + 11306739708 + 82959174767 + 97623331044 + 42846280183 + 55121603546 + 32238195734 + 75506164965 + 62177842752 + 32924185707 + 99518671430 + 73267460800 + 76841822524 + 97142617910 + 87783646182 + 10848802521 + 71329612474 + 62184073572 + 66627891981 + 60661826293 + 85786944089 + 66024396409 + 64913982680 + 16730939319 + 94809377245 + 78639167021 + 15368713711 + 40789923115 + 44889911501 + 41503128880 + 81234880673 + 82616570773 + 22918802058 + 77158542502 + 72107838435 + 20849603980 + 53503534226
|> str.from_i64() |> str.from_i64()
|> str.substr(0, 10) |> str.substr(0, 10)

View File

@@ -6,8 +6,8 @@ func main[] : I64
if a * b > out if a * b > out
let s: String = str.from_i64(a * b) let s: String = str.from_i64(a * b)
let s_rev: String = str.reverse(s) let s_rev: String = str.reverse(s)
if str.equal(s, s_rev) if c.strcmp(s, s_rev) == 0
out = a * b out = a * b
mem.free(s) c.free(s)
mem.free(s_rev) c.free(s_rev)
io.print_i64(out) io.print_i64(out)

View File

@@ -2,7 +2,7 @@ func main[] : I64
let n: String = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" let n: String = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
let out: I64 = 0 let out: I64 = 0
let max: I64 = str.len(n) - 13 let max: I64 = c.strlen(n) - 13
for i in 0..max for i in 0..max
let s: I64 = 1 let s: I64 = 1
let j: I64 = 0 let j: I64 = 0

View File

@@ -1,3 +1,26 @@
func quicksort[arr: Array] : Void
do_quicksort(arr, 0, array.size(arr)-1)
func do_quicksort[arr: Array, low: I64, high: I64] : Void
if low < high
let i: I64 = partition(arr, low, high)
do_quicksort(arr, low, i - 1)
do_quicksort(arr, i + 1, high)
func partition[arr: Array, low: I64, high: I64] : I64
let pivot: I64 = arr[high]
let i: I64 = low - 1
for j in (low)..high
if arr[j] <= pivot
i = i + 1
let temp: I64 = arr[i]
array.set(arr, i, arr[j])
array.set(arr, j, temp)
let temp: I64 = arr[i + 1]
array.set(arr, i + 1, arr[high])
array.set(arr, high, temp)
return i + 1
func main[] : I64 func main[] : I64
let arr: Array = [] let arr: Array = []
for i in 0..10 for i in 0..10
@@ -7,9 +30,7 @@ func main[] : I64
io.print_i64(arr[i]) io.print_i64(arr[i])
io.print("------------") io.print("------------")
alg.quicksort(arr) quicksort(arr)
for i in 0..array.size(arr) for i in 0..array.size(arr)
io.print_i64(arr[i]) io.print_i64(arr[i])
array.free(arr)

View File

@@ -1,42 +0,0 @@
// musl doesnt like dlopen, needs to be compiled with -m
func main[] : I64
let rl: Ptr = c.dlopen("libraylib.so", 2)
let rl.InitWindow: Ptr = c.dlsym(rl, "InitWindow")
let rl.SetTargetFPS: Ptr = c.dlsym(rl, "SetTargetFPS")
let rl.WindowShouldClose: Ptr = c.dlsym(rl, "WindowShouldClose")
let rl.BeginDrawing: Ptr = c.dlsym(rl, "BeginDrawing")
let rl.EndDrawing: Ptr = c.dlsym(rl, "EndDrawing")
let rl.ClearBackground: Ptr = c.dlsym(rl, "ClearBackground")
let rl.CloseWindow: Ptr = c.dlsym(rl, "CloseWindow")
let rl.DrawRectangle: Ptr = c.dlsym(rl, "DrawRectangle")
let rl.IsKeyDown: Ptr = c.dlsym(rl, "IsKeyDown")
let rl.KEY_W: I64 = 87
let rl.KEY_S: I64 = 83
let rl.KEY_A: I64 = 65
let rl.KEY_D: I64 = 68
let x: I64 = 200
let y: I64 = 200
rl.InitWindow(800, 600, "Hello, World!")
rl.SetTargetFPS(60)
while !rl.WindowShouldClose()
if rl.IsKeyDown(rl.KEY_W) & 255
y = y - 10
if rl.IsKeyDown(rl.KEY_S) & 255
y = y + 10
if rl.IsKeyDown(rl.KEY_A) & 255
x = x - 10
if rl.IsKeyDown(rl.KEY_D) & 255
x = x + 10
rl.BeginDrawing()
rl.ClearBackground(0xffffffff)
rl.DrawRectangle(x, y, 100, 100, 0xff0000ff)
rl.EndDrawing()
rl.CloseWindow()

View File

@@ -15,13 +15,14 @@ func rule110_step[state: Array] : Array
return new_state return new_state
func print_state[state: Array]: Void func to_str[state: Array]: String
let out: String = malloc(array.size(state))
for i in 0..array.size(state) for i in 0..array.size(state)
if state[i] if state[i]
c.putchar('#') str.set(out, i, '#')
else else
c.putchar(' ') str.set(out, i, ' ')
io.print("") return out
func main[] : I64 func main[] : I64
let SIZE: I64 = 60 let SIZE: I64 = 60
@@ -31,9 +32,7 @@ func main[] : I64
array.push(state, false) array.push(state, false)
array.push(state, true) array.push(state, true)
print_state(state) io.print(to_str(state))
for i in 0..SIZE for i in 0..SIZE
state = rule110_step(state) state = rule110_step(state)
print_state(state) io.print(to_str(state))
array.free(state)

13
examples/tcp_client.zr Normal file
View File

@@ -0,0 +1,13 @@
func main[] : I64
let s: I64 = net.connect("devernay.free.fr", 80)
let req: String = "GET /hacks/chip8/C8TECH10.HTM HTTP/1.0\r\nHost: devernay.free.fr\r\nConnection: close\r\n\r\n"
c.send(s, req, c.strlen(req), 0)
let resp: String = c.malloc(60000)
let n: I64 = c.read(s, resp, 60000)
str.set(resp, n, 0)
io.print(resp)
c.free(resp)
c.close(s)

View File

@@ -1,12 +1,12 @@
func main[] : I64 func main[] : I64
let s: I64 = net.listen(8000) let s: I64 = net.listen(8000)
let resp: String = mem.alloc(60000) let resp: String = c.malloc(60000)
while true while true
let conn: I64 = c.accept(s, 0, 0) let c: I64 = c.accept(s, 0, 0)
if conn < 0 if c < 0
continue continue
let n: I64 = c.read(conn, resp, 60000) let n: I64 = c.read(c, resp, 60000)
c.send(conn, resp, n, 0) c.send(c, resp, n, 0)
c.close(conn) c.close(c)

501
src/codegen_riscv.rs Normal file
View File

@@ -0,0 +1,501 @@
use std::{collections::HashMap, fmt::Write};
use crate::{
parser::{Expr, Stmt},
tokenizer::{TokenType, ZernError, error},
};
pub struct Var {
// pub var_type: String,
pub stack_offset: usize,
}
pub struct Env {
scopes: Vec<HashMap<String, Var>>,
next_offset: usize,
loop_begin_label: String,
loop_end_label: String,
}
impl Env {
pub fn new() -> Env {
Env {
scopes: vec![HashMap::new()],
next_offset: 24,
loop_begin_label: String::new(),
loop_end_label: String::new(),
}
}
pub fn push_scope(&mut self) {
self.scopes.push(HashMap::new());
}
pub fn pop_scope(&mut self) {
self.scopes.pop();
}
pub fn define_var(&mut self, name: String, _var_type: String) -> usize {
let offset = self.next_offset;
self.next_offset += 16;
self.scopes.last_mut().unwrap().insert(name, Var {
stack_offset: offset,
});
offset
}
pub fn get_var(&self, name: &str) -> Option<&Var> {
for scope in self.scopes.iter().rev() {
if let Some(var) = scope.get(name) {
return Some(var);
}
}
None
}
}
macro_rules! emit {
($($arg:tt)*) => {
let _ = writeln!($($arg)*);
};
}
static REGISTERS: [&str; 8] = ["a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7"];
pub struct CodegenRISCV {
output: String,
data_section: String,
label_counter: usize,
data_counter: usize,
}
impl CodegenRISCV {
pub fn new() -> CodegenRISCV {
CodegenRISCV {
output: String::new(),
data_section: String::new(),
label_counter: 0,
data_counter: 1,
}
}
fn label(&mut self) -> String {
self.label_counter += 1;
format!(".L{}", self.label_counter)
}
pub fn get_output(&self) -> String {
format!(
".section .data
{}{}",
self.data_section, self.output
)
}
pub fn emit_prologue(&mut self) -> Result<(), ZernError> {
emit!(
&mut self.output,
".section .note.GNU-stack,\"\",@progbits
.section .text"
);
// take that rustfmt
for name in "malloc,calloc,realloc,free,puts,printf,sprintf,snprintf,strtol,strlen,strcmp,strcat,strcpy,strdup,strncpy,syscall,fopen,fseek,ftell,fread,fwrite,fclose,rewind,system,opendir,readdir,closedir,exit,gettimeofday,connect,socket,send,write,read,close,bind,listen,accept,getchar,gethostbyname".split(",")
{
emit!(&mut self.output, ".extern {}", name);
emit!(&mut self.output, ".equ c.{}, {}", name, name);
}
emit!(
&mut self.output,
"
.section .text._builtin_deref8
_builtin_deref8:
lbu a0, 0(a0)
ret
.section .text._builtin_deref64
_builtin_deref64:
ld a0, 0(a0)
ret
.section .text._builtin_set8
_builtin_set8:
sb a1, 0(a0)
ret
.section .text._builtin_set64
_builtin_set64:
sd a1, 0(a0)
ret
.section .text._builtin_lshift
_builtin_lshift:
sll a0, a0, a1
ret
.section .text._builtin_rshift
_builtin_rshift:
sra a0, a0, a1
ret
"
);
Ok(())
}
pub fn compile_stmt(&mut self, env: &mut Env, stmt: Stmt) -> Result<(), ZernError> {
match stmt {
Stmt::Expression(expr) => self.compile_expr(env, expr)?,
Stmt::Let {
name,
var_type,
initializer,
} => {
// TODO: move to analyzer
if env.get_var(&name.lexeme).is_some() {
return error!(
name.loc,
format!("variable already defined: {}", &name.lexeme)
);
}
self.compile_expr(env, initializer)?;
let offset = env.define_var(name.lexeme.clone(), var_type.lexeme);
emit!(&mut self.output, " sd a0, -{}(fp)", offset);
}
Stmt::Block(statements) => {
env.push_scope();
for stmt in statements {
self.compile_stmt(env, stmt)?;
}
env.pop_scope();
}
Stmt::If {
condition,
then_branch,
else_branch,
} => {
let else_label = self.label();
let end_label = self.label();
self.compile_expr(env, condition)?;
emit!(&mut self.output, " beqz a0, {}", else_label);
self.compile_stmt(env, *then_branch.clone())?;
emit!(&mut self.output, " j {}", end_label);
emit!(&mut self.output, "{}:", else_label);
self.compile_stmt(env, *else_branch.clone())?;
emit!(&mut self.output, "{}:", end_label);
}
Stmt::While { condition, body } => {
let old_loop_begin_label = env.loop_begin_label.clone();
let old_loop_end_label = env.loop_end_label.clone();
env.loop_begin_label = self.label();
env.loop_end_label = self.label();
emit!(&mut self.output, "{}:", env.loop_begin_label);
self.compile_expr(env, condition)?;
emit!(&mut self.output, " beqz a0, {}", env.loop_end_label);
self.compile_stmt(env, *body.clone())?;
emit!(&mut self.output, " j {}", env.loop_begin_label);
emit!(&mut self.output, "{}:", env.loop_end_label);
env.loop_begin_label = old_loop_begin_label;
env.loop_end_label = old_loop_end_label;
}
Stmt::Function {
name,
params,
return_type: _,
body,
} => {
if name.lexeme == "main" {
emit!(&mut self.output, ".globl {}", name.lexeme);
}
emit!(&mut self.output, ".section .text.{}", name.lexeme);
emit!(&mut self.output, "{}:", name.lexeme);
emit!(&mut self.output, " addi sp, sp, -16");
emit!(&mut self.output, " sd ra, 8(sp)");
emit!(&mut self.output, " sd fp, 0(sp)");
emit!(&mut self.output, " addi fp, sp, 16");
emit!(&mut self.output, " addi sp, sp, -256"); // TODO
for (i, param) in params.iter().enumerate() {
let offset = env
.define_var(param.var_name.lexeme.clone(), param.var_type.lexeme.clone());
let reg = match REGISTERS.get(i) {
Some(x) => x,
None => return error!(&name.loc, "only up to 8 params allowed"),
};
emit!(&mut self.output, " sd {}, -{}(fp)", reg, offset);
}
self.compile_stmt(env, *body)?;
if name.lexeme == "main" {
emit!(&mut self.output, " li a0, 0");
}
emit!(&mut self.output, " addi sp, fp, -16");
emit!(&mut self.output, " ld ra, 8(sp)");
emit!(&mut self.output, " ld fp, 0(sp)");
emit!(&mut self.output, " addi sp, sp, 16");
emit!(&mut self.output, " ret");
}
Stmt::Return(expr) => {
self.compile_expr(env, expr)?;
emit!(&mut self.output, " addi sp, fp, -16");
emit!(&mut self.output, " ld ra, 8(sp)");
emit!(&mut self.output, " ld fp, 0(sp)");
emit!(&mut self.output, " addi sp, sp, 16");
emit!(&mut self.output, " ret");
}
Stmt::For {
var,
start,
end,
body,
} => {
let old_loop_begin_label = env.loop_begin_label.clone();
let old_loop_end_label = env.loop_end_label.clone();
env.loop_begin_label = self.label();
env.loop_end_label = self.label();
env.push_scope();
let offset = env.define_var(var.lexeme, "I64".into());
self.compile_expr(env, start)?;
emit!(&mut self.output, " sd a0, -{}(fp)", offset);
emit!(&mut self.output, "{}:", env.loop_begin_label);
emit!(&mut self.output, " ld a0, -{}(fp)", offset);
emit!(&mut self.output, " addi sp, sp, -16");
emit!(&mut self.output, " sd a0, 0(sp)");
self.compile_expr(env, end)?;
emit!(&mut self.output, " ld a1, 0(sp)");
emit!(&mut self.output, " addi sp, sp, 16");
emit!(&mut self.output, " bge a1, a0, {}", env.loop_end_label);
self.compile_stmt(env, *body)?;
emit!(&mut self.output, " ld a0, -{}(fp)", offset);
emit!(&mut self.output, " addi a0, a0, 1");
emit!(&mut self.output, " sd a0, -{}(fp)", offset);
emit!(&mut self.output, " j {}", env.loop_begin_label);
emit!(&mut self.output, "{}:", env.loop_end_label);
env.pop_scope();
env.loop_begin_label = old_loop_begin_label;
env.loop_end_label = old_loop_end_label;
}
Stmt::Break => {
emit!(&mut self.output, " j {}", env.loop_end_label);
}
Stmt::Continue => {
// TODO: skips incrementing when used in a for loop
emit!(&mut self.output, " j {}", env.loop_begin_label);
}
}
Ok(())
}
pub fn compile_expr(&mut self, env: &mut Env, expr: Expr) -> Result<(), ZernError> {
match expr {
Expr::Binary { left, op, right } => {
self.compile_expr(env, *left)?;
emit!(&mut self.output, " addi sp, sp, -16");
emit!(&mut self.output, " sd a0, 0(sp)");
self.compile_expr(env, *right)?;
emit!(&mut self.output, " mv a1, a0");
emit!(&mut self.output, " ld a0, 0(sp)");
emit!(&mut self.output, " addi sp, sp, 16");
match op.token_type {
TokenType::Plus => {
emit!(&mut self.output, " add a0, a0, a1");
}
TokenType::Minus => {
emit!(&mut self.output, " sub a0, a0, a1");
}
TokenType::Star => {
emit!(&mut self.output, " mul a0, a0, a1");
}
TokenType::Slash => {
emit!(&mut self.output, " div a0, a0, a1");
}
TokenType::Mod => {
emit!(&mut self.output, " rem a0, a0, a1");
}
TokenType::Xor => {
emit!(&mut self.output, " xor a0, a0, a1");
}
TokenType::And => {
emit!(&mut self.output, " and a0, a0, a1");
}
TokenType::Or => {
emit!(&mut self.output, " or a0, a0, a1");
}
TokenType::DoubleEqual => {
emit!(&mut self.output, " xor a0, a0, a1");
emit!(&mut self.output, " seqz a0, a0");
}
TokenType::NotEqual => {
emit!(&mut self.output, " xor a0, a0, a1");
emit!(&mut self.output, " snez a0, a0");
}
TokenType::Greater => {
emit!(&mut self.output, " slt a0, a1, a0");
}
TokenType::GreaterEqual => {
emit!(&mut self.output, " slt a0, a0, a1");
emit!(&mut self.output, " xori a0, a0, 1");
}
TokenType::Less => {
emit!(&mut self.output, " slt a0, a0, a1");
}
TokenType::LessEqual => {
emit!(&mut self.output, " slt a0, a1, a0");
emit!(&mut self.output, " xori a0, a0, 1");
}
_ => unreachable!(),
}
}
Expr::Grouping(expr) => self.compile_expr(env, *expr)?,
Expr::Literal(token) => match token.token_type {
TokenType::Number => {
emit!(&mut self.output, " li a0, {}", token.lexeme);
}
TokenType::Char => {
emit!(
&mut self.output,
" li a0, {}",
token.lexeme.chars().nth(1).unwrap() as u8
);
}
TokenType::String => {
// TODO: actual string parsing in the tokenizer
let value = &token.lexeme[1..token.lexeme.len() - 1]
.replace("\\n", "\n")
.replace("\\r", "\r")
.replace("\\t", "\t")
.replace("\\033", "\x1b")
.replace("\\0", "\0");
let label = format!("str_{:03}", self.data_counter);
if value.is_empty() {
emit!(&mut self.data_section, " {}: .byte 0", label);
} else {
let charcodes = value
.chars()
.map(|x| (x as u8).to_string())
.collect::<Vec<String>>()
.join(",");
emit!(
&mut self.data_section,
" {}: .byte {},0",
label,
charcodes,
);
}
emit!(&mut self.output, " la a0, {}", label);
self.data_counter += 1;
}
TokenType::True => {
emit!(&mut self.output, " li a0, 1");
}
TokenType::False => {
emit!(&mut self.output, " li a0, 0");
}
_ => unreachable!(),
},
Expr::Unary { op, right } => {
self.compile_expr(env, *right)?;
match op.token_type {
TokenType::Minus => {
emit!(&mut self.output, " sub a0, x0, a0");
}
TokenType::Bang => {
emit!(&mut self.output, " seqz a0, a0");
}
_ => unreachable!(),
}
}
Expr::Variable(name) => {
// TODO: move to analyzer
let var = match env.get_var(&name.lexeme) {
Some(x) => x,
None => {
return error!(name.loc, format!("undefined variable: {}", &name.lexeme));
}
};
emit!(&mut self.output, " ld a0, -{}(fp)", var.stack_offset);
}
Expr::Assign { name, value } => {
self.compile_expr(env, *value)?;
// TODO: move to analyzer
let var = match env.get_var(&name.lexeme) {
Some(x) => x,
None => {
return error!(name.loc, format!("undefined variable: {}", &name.lexeme));
}
};
emit!(&mut self.output, " sd a0, -{}(fp)", var.stack_offset);
}
Expr::Call {
callee,
paren,
args,
} => {
let callee = match *callee {
Expr::Variable(name) => name.lexeme,
_ => return error!(&paren.loc, "tried to call a non-constant expression"),
};
for arg in &args {
self.compile_expr(env, arg.clone())?;
emit!(&mut self.output, " addi sp, sp, -16");
emit!(&mut self.output, " sd a0, 0(sp)");
}
for i in (0..args.len()).rev() {
let reg = match REGISTERS.get(i) {
Some(x) => x,
None => return error!(&paren.loc, "only up to 8 args allowed"),
};
emit!(&mut self.output, " ld {}, 0(sp)", reg);
emit!(&mut self.output, " addi sp, sp, 16");
}
emit!(&mut self.output, " call {}", callee);
}
Expr::ArrayLiteral(exprs) => {
emit!(&mut self.output, " call array.new");
emit!(&mut self.output, " addi sp, sp, -16");
emit!(&mut self.output, " sd a0, 0(sp)");
for expr in exprs {
self.compile_expr(env, expr)?;
emit!(&mut self.output, " mv a1, a0");
emit!(&mut self.output, " ld a0, 0(sp)");
emit!(&mut self.output, " sd a0, 0(sp)");
emit!(&mut self.output, " call array.push");
}
emit!(&mut self.output, " ld a0, 0(sp)");
emit!(&mut self.output, " addi sp, sp, 16");
}
Expr::Index { expr, index } => {
self.compile_expr(env, *expr)?;
emit!(&mut self.output, " addi sp, sp, -16");
emit!(&mut self.output, " sd a0, 0(sp)");
self.compile_expr(env, *index)?;
emit!(&mut self.output, " ld t0, 0(sp)");
emit!(&mut self.output, " addi sp, sp, 16");
emit!(&mut self.output, " ld t0, 0(t0)");
emit!(&mut self.output, " li t1, 8");
emit!(&mut self.output, " mul t2, a0, t1");
emit!(&mut self.output, " add t0, t0, t2");
emit!(&mut self.output, " ld a0, 0(t0)");
}
}
Ok(())
}
}

View File

@@ -38,12 +38,9 @@ impl Env {
pub fn define_var(&mut self, name: String, _var_type: String) -> usize { pub fn define_var(&mut self, name: String, _var_type: String) -> usize {
let offset = self.next_offset; let offset = self.next_offset;
self.next_offset += 8; self.next_offset += 8;
self.scopes.last_mut().unwrap().insert( self.scopes.last_mut().unwrap().insert(name, Var {
name,
Var {
stack_offset: offset, stack_offset: offset,
}, });
);
offset offset
} }
@@ -106,7 +103,7 @@ section .text
); );
// take that rustfmt // take that rustfmt
for name in "malloc,memset,realloc,free,puts,putchar,printf,snprintf,strtol,strlen,strcmp,strcat,strcpy,strncpy,fopen,fseek,ftell,fread,fwrite,fclose,rewind,system,opendir,readdir,closedir,exit,gettimeofday,connect,socket,send,write,read,close,bind,listen,accept,getchar,gethostbyname,dlopen,dlsym".split(",") for name in "malloc,calloc,realloc,free,puts,printf,sprintf,snprintf,strtol,strlen,strcmp,strcat,strcpy,strdup,strncpy,syscall,fopen,fseek,ftell,fread,fwrite,fclose,rewind,system,opendir,readdir,closedir,exit,gettimeofday,connect,socket,send,write,read,close,bind,listen,accept,getchar,gethostbyname".split(",")
{ {
emit!(&mut self.output, "extern {}", name); emit!(&mut self.output, "extern {}", name);
emit!(&mut self.output, "c.{} equ {}", name, name); emit!(&mut self.output, "c.{} equ {}", name, name);
@@ -115,14 +112,14 @@ section .text
emit!( emit!(
&mut self.output, &mut self.output,
" "
section .text._builtin_read8 section .text._builtin_deref8
_builtin_read8: _builtin_deref8:
xor rax, rax xor rax, rax
mov al, byte [rdi] mov al, byte [rdi]
ret ret
section .text._builtin_read64 section .text._builtin_deref64
_builtin_read64: _builtin_deref64:
mov rax, qword [rdi] mov rax, qword [rdi]
ret ret
@@ -135,6 +132,20 @@ section .text._builtin_set64
_builtin_set64: _builtin_set64:
mov [rdi], rsi mov [rdi], rsi
ret ret
section .text._builtin_lshift
_builtin_lshift:
mov rcx, rsi
mov rax, rdi
shl rax, cl
ret
section .text._builtin_rshift
_builtin_rshift:
mov rcx, rsi
mov rax, rdi
sar rax, cl
ret
" "
); );
Ok(()) Ok(())
@@ -228,8 +239,10 @@ _builtin_set64:
self.compile_stmt(env, *body)?; self.compile_stmt(env, *body)?;
// fallback to returning null if name.lexeme == "main" {
emit!(&mut self.output, " mov rax, 0"); emit!(&mut self.output, " mov rax, 0");
}
emit!(&mut self.output, " mov rsp, rbp"); emit!(&mut self.output, " mov rsp, rbp");
emit!(&mut self.output, " pop rbp"); emit!(&mut self.output, " pop rbp");
emit!(&mut self.output, " ret"); emit!(&mut self.output, " ret");
@@ -316,10 +329,10 @@ _builtin_set64:
TokenType::Xor => { TokenType::Xor => {
emit!(&mut self.output, " xor rax, rbx"); emit!(&mut self.output, " xor rax, rbx");
} }
TokenType::BitAnd => { TokenType::And => {
emit!(&mut self.output, " and rax, rbx"); emit!(&mut self.output, " and rax, rbx");
} }
TokenType::BitOr => { TokenType::Or => {
emit!(&mut self.output, " or rax, rbx"); emit!(&mut self.output, " or rax, rbx");
} }
TokenType::DoubleEqual => { TokenType::DoubleEqual => {
@@ -352,14 +365,6 @@ _builtin_set64:
emit!(&mut self.output, " setle al"); emit!(&mut self.output, " setle al");
emit!(&mut self.output, " movzx rax, al"); emit!(&mut self.output, " movzx rax, al");
} }
TokenType::ShiftLeft => {
emit!(&mut self.output, " mov rcx, rbx");
emit!(&mut self.output, " sal rax, cl");
}
TokenType::ShiftRight => {
emit!(&mut self.output, " mov rcx, rbx");
emit!(&mut self.output, " sar rax, cl");
}
_ => unreachable!(), _ => unreachable!(),
} }
} }
@@ -474,20 +479,8 @@ _builtin_set64:
emit!(&mut self.output, " pop {}", reg); emit!(&mut self.output, " pop {}", reg);
} }
match env.get_var(&callee) {
Some(var) => {
emit!(
&mut self.output,
" mov rax, QWORD [rbp-{}]",
var.stack_offset,
);
emit!(&mut self.output, " call rax");
}
None => {
emit!(&mut self.output, " call {}", callee); emit!(&mut self.output, " call {}", callee);
} }
};
}
Expr::ArrayLiteral(exprs) => { Expr::ArrayLiteral(exprs) => {
emit!(&mut self.output, " call array.new"); emit!(&mut self.output, " call array.new");
emit!(&mut self.output, " push rax"); emit!(&mut self.output, " push rax");

View File

@@ -77,26 +77,11 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
run_command(format!("nasm -f elf64 -o {}.o {}.s", args.out, args.out)); run_command(format!("nasm -f elf64 -o {}.o {}.s", args.out, args.out));
if args.no_musl { // TODO: drop libc entirely
run_command(format!( run_command(format!(
"gcc -no-pie -o {} {}.o -flto -Wl,--gc-sections {}", "./musl-1.2.4/root/bin/musl-gcc -static -o {} {}.o -flto -Wl,--gc-sections {}",
args.out, args.out, args.cflags args.out, args.out, args.cflags
)); ));
} else {
run_command(format!(
"musl-gcc -static -o {} {}.o -flto -Wl,--gc-sections {}",
args.out, args.out, args.cflags
));
}
if args.run_exe {
run_command(
std::fs::canonicalize(args.out)
.unwrap()
.to_string_lossy()
.into_owned(),
);
}
} else { } else {
fs::write(&args.out, codegen.get_output()).unwrap(); fs::write(&args.out, codegen.get_output()).unwrap();
} }
@@ -115,12 +100,6 @@ struct Args {
#[arg(short = 'S', help = "Only generate assembly")] #[arg(short = 'S', help = "Only generate assembly")]
output_asm: bool, output_asm: bool,
#[arg(short = 'r', help = "Run the compiled executable")]
run_exe: bool,
#[arg(short = 'm', help = "Don't use musl")]
no_musl: bool,
#[arg(short = 'C', default_value = "", help = "Extra flags to pass to gcc")] #[arg(short = 'C', default_value = "", help = "Extra flags to pass to gcc")]
cflags: String, cflags: String,
} }

View File

@@ -115,6 +115,7 @@ impl Parser {
} }
} }
// TOOD: parse return type
fn func_declaration(&mut self) -> Result<Stmt, ZernError> { fn func_declaration(&mut self) -> Result<Stmt, ZernError> {
let name = self.consume(TokenType::Identifier, "expected function name")?; let name = self.consume(TokenType::Identifier, "expected function name")?;
self.consume(TokenType::LeftBracket, "expected '[' after function name")?; self.consume(TokenType::LeftBracket, "expected '[' after function name")?;
@@ -303,7 +304,7 @@ impl Parser {
fn logical_or(&mut self) -> Result<Expr, ZernError> { fn logical_or(&mut self) -> Result<Expr, ZernError> {
let mut expr = self.logical_and()?; let mut expr = self.logical_and()?;
while self.match_token(&[TokenType::BitOr]) { while self.match_token(&[TokenType::Or]) {
let op = self.previous().clone(); let op = self.previous().clone();
let right = self.logical_and()?; let right = self.logical_and()?;
expr = Expr::Binary { expr = Expr::Binary {
@@ -319,7 +320,7 @@ impl Parser {
fn logical_and(&mut self) -> Result<Expr, ZernError> { fn logical_and(&mut self) -> Result<Expr, ZernError> {
let mut expr = self.equality()?; let mut expr = self.equality()?;
while self.match_token(&[TokenType::BitAnd]) { while self.match_token(&[TokenType::And]) {
let op = self.previous().clone(); let op = self.previous().clone();
let right = self.equality()?; let right = self.equality()?;
expr = Expr::Binary { expr = Expr::Binary {
@@ -388,13 +389,7 @@ impl Parser {
fn factor(&mut self) -> Result<Expr, ZernError> { fn factor(&mut self) -> Result<Expr, ZernError> {
let mut expr = self.unary()?; let mut expr = self.unary()?;
while self.match_token(&[ while self.match_token(&[TokenType::Star, TokenType::Slash, TokenType::Mod]) {
TokenType::Star,
TokenType::Slash,
TokenType::Mod,
TokenType::ShiftLeft,
TokenType::ShiftRight,
]) {
let op = self.previous().clone(); let op = self.previous().clone();
let right = self.unary()?; let right = self.unary()?;
expr = Expr::Binary { expr = Expr::Binary {

View File

@@ -2,27 +2,6 @@ func dbg.panic[msg: String] : Void
c.printf("PANIC: %s\n", msg) c.printf("PANIC: %s\n", msg)
c.exit(1) c.exit(1)
func mem.alloc[x: I64] : Ptr
return c.malloc(x)
func mem.free[x: Ptr] : Void
c.free(x)
func mem.zero[x: I64, size: I64] : Void
c.memset(x, 0, size)
func mem.read8[x: Ptr] : U8
return _builtin_read8(x)
func mem.read64[x: Ptr] : I64
return _builtin_read64(x)
func mem.write8[x: Ptr, d: U8] : Void
_builtin_set8(x, d)
func mem.write64[x: Ptr, d: I64] : Void
_builtin_set64(x, d)
func io.print[x: String] : Void func io.print[x: String] : Void
c.puts(x) c.puts(x)
@@ -30,10 +9,8 @@ func io.print_i64[x: I64] : Void
c.printf("%ld\n", x) c.printf("%ld\n", x)
func io.read_stdin[]: String func io.read_stdin[]: String
let buffer: String = mem.alloc(1025) let buffer: String = c.malloc(1025)
let n: I64 = c.read(0, buffer, 1024) let n: I64 = c.syscall(0, 0, buffer, 1024)
if n < 0
return ""
str.set(buffer, n, 0) str.set(buffer, n, 0)
return buffer return buffer
@@ -46,9 +23,8 @@ func io.read_file[path: String]: String
let size: I64 = c.ftell(file) let size: I64 = c.ftell(file)
c.rewind(file) c.rewind(file)
let buffer: String = mem.alloc(size + 1) let buffer: String = c.malloc(size + 1)
// TODO: check if works with huge files
let n: I64 = c.fread(buffer, 1, size, file) let n: I64 = c.fread(buffer, 1, size, file)
str.set(buffer, n, 0) str.set(buffer, n, 0)
c.fclose(file) c.fclose(file)
@@ -59,56 +35,43 @@ func io.write_file[path: String, content: String] : Void
if !file if !file
dbg.panic("failed to open file") dbg.panic("failed to open file")
c.fwrite(content, 1, str.len(content), file) c.fwrite(content, 1, c.strlen(content), file)
c.fclose(file) c.fclose(file)
func str.len[s: String] : I64
return c.strlen(s)
func str.nth[s: String, n: I64] : U8 func str.nth[s: String, n: I64] : U8
return mem.read8(s + n) return _builtin_deref8(s + n)
func str.copy[s: String] : String
let size: I64 = str.len(s) + 1
let dup: String = mem.alloc(size)
for i in 0..size+1
str.set(dup, i, str.nth(s, i))
return dup
func str.set[s: String, n: I64, c: U8] : Void func str.set[s: String, n: I64, c: U8] : Void
mem.write8(s+n, c) _builtin_set8(s+n, c)
func str.equal[a: String, b: String] : Bool func str.is_whitespace[c: U8] : Bool
return c.strcmp(a, b) == 0 return c == ' ' | c == 10 | c == 13 | c == 9
func str.is_whitespace[x: U8] : Bool
return x == ' ' | x == 10 | x == 13 | x == 9
func str.concat[a: String, b: String] : String func str.concat[a: String, b: String] : String
let out: String = mem.alloc(str.len(a) + str.len(b) + 1) let c: String = c.malloc(c.strlen(a) + c.strlen(b) + 1)
c.strcpy(out, a) c.strcpy(c, a)
c.strcat(out, b) c.strcat(c, b)
return out return c
func str.find[s: String, c: U8] : I64 func str.find[s: String, c: U8] : I64
let s_len: I64 = str.len(s) let s_len: I64 = c.strlen(s)
for i in 0..s_len for i in 0..s_len
if str.nth(s, i) == c if str.nth(s, i) == c
return i return i
return -1 return -1
func str.substr[s: String, start: I64, length: I64] : String func str.substr[s: String, start: I64, length: I64] : String
if start < 0 | length < 0 | start + length > str.len(s) if start < 0 | length < 0 | start + length > c.strlen(s)
dbg.panic("str.substr out of bounds") dbg.panic("String.substr out of bounds")
let out: String = mem.alloc(length + 1) let out: String = c.malloc(length + 1)
c.strncpy(out, s + start, length) c.strncpy(out, s + start, length)
str.set(out, length, 0) str.set(out, length, 0)
return out return out
func str.trim[s: String] : String func str.trim[s: String] : String
let start: I64 = 0 let start: I64 = 0
let end: I64 = str.len(s) - 1 let end: I64 = c.strlen(s) - 1
while start <= end & str.is_whitespace(str.nth(s, start)) while start <= end & str.is_whitespace(str.nth(s, start))
start = start + 1 start = start + 1
@@ -118,41 +81,9 @@ func str.trim[s: String] : String
return str.substr(s, start, end - start + 1) return str.substr(s, start, end - start + 1)
func str.split[haystack: String, needle: String]: Array
let haystack_len: I64 = str.len(haystack)
let needle_len: I64 = str.len(needle)
let result: Array = []
if !needle_len
if !haystack_len
return result
else
for i in 0..haystack_len
array.push(result, str.substr(haystack, i, 1))
return result
let start: I64 = 0
let i: I64 = 0
while i < haystack_len
if i <= haystack_len - needle_len
let match: Bool = true
for j in 0..needle_len
if str.nth(haystack, i+j) != str.nth(needle, j)
match = false
break
if match
array.push(result, str.substr(haystack, start, i - start))
start = i + needle_len
i = i + needle_len
continue
i = i + 1
array.push(result, str.substr(haystack, start, haystack_len - start))
return result
func str.reverse[s: String] : String func str.reverse[s: String] : String
let len: I64 = str.len(s) let len: I64 = c.strlen(s)
let out: String = mem.alloc(len + 1) let out: String = c.malloc(len + 1)
for i in 0..len for i in 0..len
str.set(out, i, str.nth(s, len - i - 1)) str.set(out, i, str.nth(s, len - i - 1))
@@ -160,50 +91,13 @@ func str.reverse[s: String] : String
return out return out
func str.from_i64[n: I64] : String func str.from_i64[n: I64] : String
let x: String = mem.alloc(21) let x: String = c.malloc(21)
c.snprintf(x, 21, "%ld", n) c.sprintf(x, "%ld", n)
return x return x
func str.parse_i64[s: String] : I64 func str.parse_i64[s: String] : I64
return c.strtol(s, 0, 0) return c.strtol(s, 0, 0)
func str.hex_encode[s: String] : String
let hex_chars: String = "0123456789abcdef"
let s_len: I64 = str.len(s)
let j: I64 = 0
let out: String = mem.alloc(s_len*2+1)
for i in 0..s_len
let high: U8 = (str.nth(s, i) >> 4) & 15
let low: U8 = str.nth(s, i) & 15
str.set(out, j, str.nth(hex_chars, high))
str.set(out, j+1, str.nth(hex_chars, low))
j = j + 2
str.set(out, j, 0)
return out
func str._from_hex_digit[d: U8] : I64
if d >= 'a' & d <= 'f'
return d - 'a' + 10
if d >= 'A' & d <= 'F'
return d - 'A' + 10
return d - '0'
func str.hex_decode[s: String] : String
let s_len: I64 = str.len(s)
let i: I64 = 0
let j: I64 = 0
let out: String = mem.alloc(s_len/2+1)
while i < s_len
str.set(out, j, str._from_hex_digit(str.nth(s, i)) * 16 + str._from_hex_digit(str.nth(s, i+1)))
i = i + 2
j = j + 1
str.set(out, j, 0)
return out
func math.gcd[a: I64, b: I64] : I64 func math.gcd[a: I64, b: I64] : I64
while b != 0 while b != 0
let tmp: I64 = b let tmp: I64 = b
@@ -266,87 +160,51 @@ func math.is_prime[n: I64]: Bool
return true return true
func math.urandom[]: I64 func math.urandom[]: I64
let buffer: Ptr = mem.alloc(8) let buffer: Ptr = c.malloc(8)
let file: Ptr = c.fopen("/dev/urandom", "rb") let file: Ptr = c.fopen("/dev/urandom", "rb")
c.fread(buffer, 8, 1, file) c.fread(buffer, 8, 1, file)
c.fclose(file) c.fclose(file)
let n: I64 = mem.read64(buffer) let n: I64 = _builtin_deref64(buffer)
mem.free(buffer) c.free(buffer)
return n return n
func array.new[] : Array func array.new[] : Array
let arr: Ptr = mem.alloc(24) return c.calloc(1, 24)
mem.zero(arr, 24)
return arr
func array.nth[xs: Array, n: I64] : I64
// this probably should be implemented in the codegen
if n < 0 | n >= array.size(xs)
dbg.panic("array.nth out of bounds")
return xs[n]
func array.set[xs: Array, n: I64, x: I64] : Void func array.set[xs: Array, n: I64, x: I64] : Void
let data: Ptr = mem.read64(xs) let data: Ptr = _builtin_deref64(xs)
mem.write64(data+n*8, x) _builtin_set64(data+n*8, x)
func array.push[xs: Array, x: I64] : Void func array.push[xs: Array, x: I64] : Void
let data: Ptr = mem.read64(xs) let data: Ptr = _builtin_deref64(xs)
let capacity: I64 = mem.read64(xs+8) let capacity: I64 = _builtin_deref64(xs+8)
let size: I64 = mem.read64(xs+16) let size: I64 = _builtin_deref64(xs+16)
if size == capacity if size == capacity
let new_capacity: I64 = 4 let new_capacity: I64 = 4
if capacity != 0 if capacity != 0
new_capacity = capacity * 2 new_capacity = capacity * 2
let new_data: Ptr = c.realloc(data, new_capacity * 8) let new_data: Ptr = c.realloc(data, new_capacity * 8)
mem.write64(xs, new_data) _builtin_set64(xs, new_data)
mem.write64(xs+8, new_capacity) _builtin_set64(xs+8, new_capacity)
data = new_data data = new_data
mem.write64(data+size*8, x) _builtin_set64(data+size*8, x)
mem.write64(xs+16, size + 1) _builtin_set64(xs+16, size + 1)
func array.size[xs: Array] : I64 func array.size[xs: Array] : I64
return mem.read64(xs+16) return _builtin_deref64(xs+16)
func array.free[xs: Array] : Void func array.free[xs: Array] : Void
let data: Ptr = mem.read64(xs) c.free(_builtin_deref64(xs))
if data != 0 c.free(xs)
mem.free(data)
mem.free(xs)
func alg.quicksort[arr: Array] : Void
alg._do_quicksort(arr, 0, array.size(arr)-1)
func alg._do_quicksort[arr: Array, low: I64, high: I64] : Void
if low < high
let i: I64 = alg._partition(arr, low, high)
alg._do_quicksort(arr, low, i - 1)
alg._do_quicksort(arr, i + 1, high)
func alg._partition[arr: Array, low: I64, high: I64] : I64
let pivot: I64 = arr[high]
let i: I64 = low - 1
for j in (low)..high
if arr[j] <= pivot
i = i + 1
let temp: I64 = arr[i]
array.set(arr, i, arr[j])
array.set(arr, j, temp)
let temp: I64 = arr[i + 1]
array.set(arr, i + 1, arr[high])
array.set(arr, high, temp)
return i + 1
func os.exit[code: I64] : Void
c.exit(code)
func os.time[] : I64 func os.time[] : I64
let tv: Ptr = mem.alloc(16) let tv: Ptr = c.malloc(16)
c.gettimeofday(tv, 0) c.gettimeofday(tv, 0)
let seconds: I64 = mem.read64(tv) let seconds: I64 = _builtin_deref64(tv)
let microseconds: I64 = mem.read64(tv+8) let microseconds: I64 = _builtin_deref64(tv+8)
mem.free(tv) c.free(tv)
return seconds * 1000 + microseconds / 1000 return seconds * 1000 + microseconds / 1000
func os.listdir[path: String] : Array func os.listdir[path: String] : Array
@@ -359,34 +217,179 @@ func os.listdir[path: String] : Array
break break
let skip: Bool = false let skip: Bool = false
if mem.read8(entry + 19) == '.' if str.nth(entry, 19) == '.'
if mem.read8(entry + 20) == 0 if str.nth(entry, 20) == 0
skip = true skip = true
else if mem.read8(entry + 20) == '.' else if str.nth(entry, 20) == '.'
if mem.read8(entry + 21) == 0 if str.nth(entry, 21) == 0
skip = true skip = true
if !skip if !skip
array.push(files, str.copy(entry + 19)) array.push(files, c.strdup(entry + 19))
c.closedir(dir) c.closedir(dir)
return files return files
func bit.lshift[a: I64, b: I64] : I64
return _builtin_lshift(a, b)
func bit.rshift[a: I64, b: I64] : I64
return _builtin_rshift(a, b)
func crypto.hex_encode[s: String] : String
let hex_chars: String = "0123456789abcdef"
let s_len: I64 = c.strlen(s)
let j: I64 = 0
let out: String = c.malloc(s_len*2+1)
for i in 0..s_len
let high: U8 = bit.rshift(str.nth(s, i), 4) & 15
let low: U8 = str.nth(s, i) & 15
str.set(out, j, str.nth(hex_chars, high))
str.set(out, j+1, str.nth(hex_chars, low))
j = j + 2
str.set(out, j, 0)
return out
func crypto.from_hex_digit[d: U8] : I64
if d >= 'a' & d <= 'f'
return d - 'a' + 10
if d >= 'A' & d <= 'F'
return d - 'A' + 10
return d - '0'
func crypto.hex_decode[s: String] : String
let s_len: I64 = c.strlen(s)
let i: I64 = 0
let j: I64 = 0
let out: String = c.malloc(s_len/2+1)
while i < s_len
str.set(out, j, crypto.from_hex_digit(str.nth(s, i)) * 16 + crypto.from_hex_digit(str.nth(s, i+1)))
i = i + 2
j = j + 1
str.set(out, j, 0)
return out
func crypto.rc4[key: String, plaintext: String]: String
let S: String = c.malloc(256)
for i in 0..256
str.set(S, i, i)
let j: I64 = 0
let key_len: I64 = c.strlen(key)
for i in 0..256
j = (j + str.nth(S, i) + str.nth(key, i % key_len)) % 256
let tmp: U8 = str.nth(S, i)
str.set(S, i, str.nth(S, j))
str.set(S, j, tmp)
let i: I64 = 0
j = 0
let plaintext_len: I64 = c.strlen(plaintext)
let ciphertext: String = c.malloc(plaintext_len+1)
for n in 0..plaintext_len
i = (i + 1) % 256
j = (j + str.nth(S, i)) % 256
let tmp: U8 = str.nth(S, i)
str.set(S, i, str.nth(S, j))
str.set(S, j, tmp)
let r: I64 = str.nth(S, (str.nth(S, i) + str.nth(S, j)) % 256)
str.set(ciphertext, n, r ^ str.nth(plaintext, n))
str.set(ciphertext, plaintext_len, 0)
c.free(S)
return ciphertext
func crypto.base64_encode[s: String] : String
let chars: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
let s_len: I64 = c.strlen(s)
let out: String = c.malloc(s_len*2)
let i: I64 = 0
let j: I64 = 0
while i < s_len
let b1: U8 = str.nth(s, i)
let b2: U8 = 0
if i + 1 < s_len
b2 = str.nth(s, i+1)
let b3: U8 = 0
if i + 2 < s_len
b3 = str.nth(s, i+2)
i = i + 3
let triple: I64 = bit.lshift(b1, 16) | bit.lshift(b2, 8) | b3
str.set(out, j, str.nth(chars, bit.rshift(triple, 18) & 63))
str.set(out, j+1, str.nth(chars, bit.rshift(triple, 12) & 63))
str.set(out, j+2, str.nth(chars, bit.rshift(triple, 6) & 63))
str.set(out, j+3, str.nth(chars, triple & 63))
j = j + 4
let padding: I64 = s_len % 3
if padding == 1
str.set(out, j-2, '=')
str.set(out, j-1, '=')
else if padding == 2
str.set(out, j-1, '=')
str.set(out, j, 0)
return out
func crypto.base64_decode[s: String] : String
let chars: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
let s_len: I64 = c.strlen(s)
let out: String = c.malloc(s_len)
let i: I64 = 0
let j: I64 = 0
while str.nth(s, s_len-1) == '='
s_len = s_len - 1
while i < s_len
let s1: U8 = str.find(chars, str.nth(s, i))
let s2: U8 = 0
if i + 1 < s_len
s2 = str.find(chars, str.nth(s, i+1))
let s3: U8 = 0
if i + 2 < s_len
s3 = str.find(chars, str.nth(s, i+2))
let s4: U8 = 0
if i + 3 < s_len
s4 = str.find(chars, str.nth(s, i+3))
i = i + 4
let triple: I64 = bit.lshift(s1, 18) | bit.lshift(s2, 12) | bit.lshift(s3, 6) | s4
str.set(out, j, bit.rshift(triple, 16) & 255)
j = j + 1
if s3 != 64
str.set(out, j, bit.rshift(triple, 8) & 255)
j = j + 1
if s4 != 64
str.set(out, j, triple & 255)
j = j + 1
str.set(out, j, 0)
return out
func net.listen[port: I64] : I64 func net.listen[port: I64] : I64
let s: I64 = c.socket(2, 1, 0) let s: I64 = c.socket(2, 1, 0)
if s < 0 if s < 0
return -1 return -1
let sa: Ptr = mem.alloc(16) let sa: Ptr = c.calloc(1, 16)
mem.zero(sa, 16) str.set(sa, 0, 2)
mem.write8(sa + 0, 2) str.set(sa, 1, 0)
mem.write8(sa + 1, 0) str.set(sa, 2, bit.rshift(port, 8) & 255)
mem.write8(sa + 2, (port >> 8) & 255) str.set(sa, 3, port & 255)
mem.write8(sa + 3, port & 255)
if c.bind(s, sa, 16) < 0 if c.bind(s, sa, 16) < 0
c.close(s) c.close(s)
return -1 return -1
mem.free(sa) c.free(sa)
if c.listen(s, 1) < 0 if c.listen(s, 1) < 0
c.close(s) c.close(s)
@@ -399,26 +402,25 @@ func net.connect[host: String, port: I64] : I64
if he == 0 if he == 0
return -1 return -1
let ip_ptr: Ptr = mem.read64(mem.read64(he + 24)) let ip_ptr: Ptr = _builtin_deref64(_builtin_deref64(he + 24))
let s: I64 = c.socket(2, 1, 0) let s: I64 = c.socket(2, 1, 0)
if s < 0 if s < 0
return -1 return -1
let sa: Ptr = mem.alloc(16) let sa: Ptr = c.calloc(1, 16)
mem.zero(sa, 16) str.set(sa, 0, 2)
mem.write8(sa + 0, 2) str.set(sa, 2, bit.rshift(port, 8) & 255)
mem.write8(sa + 2, (port >> 8) & 255) str.set(sa, 3, port & 255)
mem.write8(sa + 3, port & 255) str.set(sa, 4, _builtin_deref8(ip_ptr + 0))
mem.write8(sa + 4, mem.read8(ip_ptr + 0)) str.set(sa, 5, _builtin_deref8(ip_ptr + 1))
mem.write8(sa + 5, mem.read8(ip_ptr + 1)) str.set(sa, 6, _builtin_deref8(ip_ptr + 2))
mem.write8(sa + 6, mem.read8(ip_ptr + 2)) str.set(sa, 7, _builtin_deref8(ip_ptr + 3))
mem.write8(sa + 7, mem.read8(ip_ptr + 3))
if c.connect(s, sa, 16) < 0 if c.connect(s, sa, 16) < 0
mem.free(sa) c.free(sa)
c.close(s) c.close(s)
return -1 return -1
mem.free(sa) c.free(sa)
return s return s

View File

@@ -15,12 +15,10 @@ pub enum TokenType {
Xor, Xor,
Bang, Bang,
Colon, Colon,
BitAnd, And,
BitOr, Or,
Pipe, Pipe,
DoubleDot, DoubleDot,
ShiftLeft,
ShiftRight,
Equal, Equal,
DoubleEqual, DoubleEqual,
@@ -168,12 +166,12 @@ impl Tokenizer {
self.add_token(TokenType::Slash) self.add_token(TokenType::Slash)
} }
} }
'&' => self.add_token(TokenType::BitAnd), '&' => self.add_token(TokenType::And),
'|' => { '|' => {
if self.match_char('>') { if self.match_char('>') {
self.add_token(TokenType::Pipe); self.add_token(TokenType::Pipe);
} else { } else {
self.add_token(TokenType::BitOr); self.add_token(TokenType::Or);
} }
} }
'!' => { '!' => {
@@ -191,18 +189,14 @@ impl Tokenizer {
} }
} }
'>' => { '>' => {
if self.match_char('>') { if self.match_char('=') {
self.add_token(TokenType::ShiftRight);
} else if self.match_char('=') {
self.add_token(TokenType::GreaterEqual) self.add_token(TokenType::GreaterEqual)
} else { } else {
self.add_token(TokenType::Greater) self.add_token(TokenType::Greater)
} }
} }
'<' => { '<' => {
if self.match_char('<') { if self.match_char('=') {
self.add_token(TokenType::ShiftLeft);
} else if self.match_char('=') {
self.add_token(TokenType::LessEqual) self.add_token(TokenType::LessEqual)
} else { } else {
self.add_token(TokenType::Less) self.add_token(TokenType::Less)
@@ -232,7 +226,7 @@ impl Tokenizer {
self.advance(); self.advance();
self.add_token(TokenType::String); self.add_token(TokenType::String);
} }
' ' | '\r' => {} ' ' | '\t' | '\r' => {}
'\n' => { '\n' => {
self.loc.line += 1; self.loc.line += 1;
self.loc.column = 1; self.loc.column = 1;
@@ -286,25 +280,25 @@ impl Tokenizer {
fn count_indentation(&mut self) -> usize { fn count_indentation(&mut self) -> usize {
let mut count = 0; let mut count = 0;
while self.peek() == ' ' { while self.peek() == ' ' || self.peek() == '\t' {
if self.peek() == ' ' {
count += 1; count += 1;
}
if self.peek() == '\t' {
count += 4;
}
self.advance(); self.advance();
} }
count count
} }
fn scan_number(&mut self) { fn scan_number(&mut self) {
if self.match_char('x') {
while self.peek().is_ascii_hexdigit() {
self.advance();
}
} else {
while self.peek().is_ascii_digit() { while self.peek().is_ascii_digit() {
self.advance(); self.advance();
} }
if self.peek() == '.' if self.peek() == '.'
&& self.current + 1 < self.source.len() && (self.current + 1 >= self.source.len())
&& self.source[self.current + 1].is_ascii_digit() && self.source[self.current + 1].is_ascii_digit()
{ {
self.advance(); self.advance();
@@ -312,13 +306,16 @@ impl Tokenizer {
self.advance(); self.advance();
} }
} }
}
self.add_token(TokenType::Number); self.add_token(TokenType::Number);
} }
fn scan_identifier(&mut self) { fn scan_identifier(&mut self) {
while self.peek().is_alphanumeric() || self.peek() == '_' || self.peek() == '.' { while self.peek().is_alphanumeric()
|| self.peek() == '_'
|| self.peek() == '.'
|| self.peek() == '!'
{
self.advance(); self.advance();
} }
@@ -345,7 +342,6 @@ impl Tokenizer {
false false
} else { } else {
self.current += 1; self.current += 1;
self.loc.column += 1;
true true
} }
} }

14
test.zr
View File

@@ -4,22 +4,18 @@ func run_test[x: String] : Void
let build_start_time: I64 = os.time() let build_start_time: I64 = os.time()
if c.system(cmd) != 0 if c.system(cmd) != 0
os.exit(1) c.exit(1)
let build_end_time: I64 = os.time() let build_end_time: I64 = os.time()
mem.free(cmd) c.free(cmd)
c.printf(" %ldms\n", build_end_time - build_start_time) c.printf(" %ldms\n", build_end_time - build_start_time)
if str.equal(x, "guess_number.zr") | str.equal(x, "tcp_server.zr") | str.equal(x, "raylib.zr") if c.strcmp(x, "guess_number.zr") == 0 | c.strcmp(x, "tcp_server.zr") == 0
c.printf("\033[93mSkipping %s...\033[0m\n", x) c.printf("\033[93mSkipping %s...\033[0m\n", x)
else else
let run_start_time: I64 = os.time() let run_start_time: I64 = os.time()
if str.equal(x, "curl.zr")
if c.system("./out http://example.com") != 0
os.exit(1)
else
if c.system("./out") != 0 if c.system("./out") != 0
os.exit(1) c.exit(1)
let run_end_time: I64 = os.time() let run_end_time: I64 = os.time()
c.printf("\033[93mRunning %s...\033[0m %ldms\n", x, run_end_time - run_start_time) c.printf("\033[93mRunning %s...\033[0m %ldms\n", x, run_end_time - run_start_time)
@@ -30,5 +26,3 @@ func main[] : I64
let files: Array = os.listdir("examples/") let files: Array = os.listdir("examples/")
for i in 0..array.size(files) for i in 0..array.size(files)
run_test(files[i]) run_test(files[i])
array.free(files)