Compare commits
No commits in common. "d2597d2de72f998318942c47c4cd88f7aebc94a3" and "8f4d626968f51ba7193fbf1b2c6f313a48b7732c" have entirely different histories.
d2597d2de7
...
8f4d626968
|
|
@ -77,18 +77,6 @@ parse_func:
|
||||||
push rbp
|
push rbp
|
||||||
mov rbp, rsp
|
mov rbp, rsp
|
||||||
push rdi
|
push rdi
|
||||||
|
|
||||||
; start-structs
|
|
||||||
; struct AstFunction {
|
|
||||||
; name: *const u8,
|
|
||||||
; name_len: usize,
|
|
||||||
; args: *const Argument,
|
|
||||||
; args_len: usize,
|
|
||||||
; return_type: Type,
|
|
||||||
; body: u64,
|
|
||||||
; }
|
|
||||||
; end-structs
|
|
||||||
|
|
||||||
sub rsp, 48
|
sub rsp, 48
|
||||||
; name: *const u8 [0..8]
|
; name: *const u8 [0..8]
|
||||||
; name_len: usize [8..16]
|
; name_len: usize [8..16]
|
||||||
|
|
@ -392,16 +380,17 @@ parse_statement:
|
||||||
mov dil, TOKEN_RETURN
|
mov dil, TOKEN_RETURN
|
||||||
call expect_token
|
call expect_token
|
||||||
test rax, rax
|
test rax, rax
|
||||||
jnz .return
|
jz .return
|
||||||
jmp .panic
|
jmp .panic
|
||||||
|
|
||||||
.return:
|
.return:
|
||||||
mov rdi, [rsp + 24] ; Ast
|
|
||||||
call parse_expr
|
call parse_expr
|
||||||
|
mov [rsp + 16], rax ; expression
|
||||||
mov byte [rsp], AST_RETURN_STATEMENT ; kind
|
mov byte [rsp], AST_RETURN_STATEMENT ; kind
|
||||||
mov [rsp + 8], rax ; data
|
lea rax, [rsp + 16] ; data ptr
|
||||||
mov rdi, [rsp + 24] ; Ast
|
mov [rsp + 8], rax ; data
|
||||||
lea rsi, [rsp] ; AstNode
|
mov rdi, [rsp + 24] ; Ast
|
||||||
|
mov rsi, rsp ; AstNode
|
||||||
call vec_push
|
call vec_push
|
||||||
mov rdi, [rsp + 24] ; Ast
|
mov rdi, [rsp + 24] ; Ast
|
||||||
mov rax, [rdi + 8] ; Ast.nodes.len()
|
mov rax, [rdi + 8] ; Ast.nodes.len()
|
||||||
|
|
@ -422,6 +411,7 @@ parse_statement:
|
||||||
parse_block:
|
parse_block:
|
||||||
push rbp
|
push rbp
|
||||||
mov rbp, rsp
|
mov rbp, rsp
|
||||||
|
push rdi
|
||||||
|
|
||||||
; start-structs
|
; start-structs
|
||||||
; struct Block {
|
; struct Block {
|
||||||
|
|
@ -430,46 +420,31 @@ parse_block:
|
||||||
; }
|
; }
|
||||||
; end-structs
|
; end-structs
|
||||||
|
|
||||||
; Ast: *mut Ast [56..64]
|
sub rsp, 56
|
||||||
; statements: Vec<Statement> [8..56]
|
; statements: Vec<Statement> [0..40]
|
||||||
; statement: u64 [0..8]
|
; statement: u64 [40..48]
|
||||||
sub rsp, 64
|
|
||||||
mov [rsp + 56], rdi ; Ast
|
|
||||||
|
|
||||||
mov dil, TOKEN_LBRACE
|
mov dil, TOKEN_LBRACE
|
||||||
call unwrap_token
|
call unwrap_token
|
||||||
|
|
||||||
mov dil, TOKEN_RBRACE
|
|
||||||
call peek_expect_token
|
|
||||||
test rax, rax
|
|
||||||
jnz .done
|
|
||||||
|
|
||||||
lea rdi, [rsp + 8]
|
|
||||||
mov rsi, 8 ; size of statement
|
|
||||||
mov rdx, 0 ; drop = None
|
|
||||||
mov rcx, 64 ; capacity
|
|
||||||
call vec_init_with
|
|
||||||
|
|
||||||
.loop:
|
.loop:
|
||||||
mov dil, TOKEN_RBRACE
|
mov rdi, [rsp + 16] ; Ast
|
||||||
call peek_expect_token
|
|
||||||
test rax, rax
|
|
||||||
jnz .done
|
|
||||||
mov rdi, [rsp + 56] ; Ast
|
|
||||||
call parse_statement
|
call parse_statement
|
||||||
lea rdi, [rsp + 8] ; vec
|
test rax, rax
|
||||||
mov [rsp], rax ; statement
|
je .done
|
||||||
lea rsi, [rsp]
|
lea rdi, [rsp + 16] ; vec
|
||||||
|
mov [rsp + 8], rax ; statement
|
||||||
|
lea rsi, [rsp + 8]
|
||||||
call vec_push
|
call vec_push
|
||||||
jmp .loop
|
jmp .loop
|
||||||
.done:
|
.done:
|
||||||
mov rdi, [rsp + 56] ; Ast
|
mov rdi, [rsp + 56] ; Ast
|
||||||
lea rsi, [rsp + 8] ; statements vec-slice
|
lea rsi, [rsp + 16] ; statements vec-slice
|
||||||
call vec_push
|
call vec_push
|
||||||
mov rdi, [rsp + 56] ; Ast
|
mov rdi, [rsp + 56] ; Ast
|
||||||
mov rax, [rdi + 8] ; Ast.nodes.len()
|
mov rax, [rdi + 8] ; Ast.nodes.len()
|
||||||
dec rax
|
dec rax
|
||||||
add rsp, 64
|
add rsp, 56
|
||||||
|
pop rdi
|
||||||
pop rbp
|
pop rbp
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -318,11 +318,14 @@ is_number:
|
||||||
cmp r14, r13
|
cmp r14, r13
|
||||||
jge .number
|
jge .number
|
||||||
mov dil, [r12 + r14]
|
mov dil, [r12 + r14]
|
||||||
|
call is_whitespace
|
||||||
|
test rax, rax
|
||||||
|
jne .number
|
||||||
cmp dil, '_'
|
cmp dil, '_'
|
||||||
je .loop_next
|
je .loop_next
|
||||||
call is_numeric
|
call is_numeric
|
||||||
test rax, rax
|
test rax, rax
|
||||||
je .number
|
je .not_number
|
||||||
.loop_next:
|
.loop_next:
|
||||||
inc r14
|
inc r14
|
||||||
jmp .loop
|
jmp .loop
|
||||||
|
|
@ -557,13 +560,11 @@ unwrap_token:
|
||||||
.panic:
|
.panic:
|
||||||
call panic
|
call panic
|
||||||
|
|
||||||
;; returns 0 if token not found, else returns lexeme (ptr, len)
|
|
||||||
;; dil: expected token
|
;; dil: expected token
|
||||||
peek_expect_token:
|
peek_expect_token:
|
||||||
push rbp
|
push rbp
|
||||||
mov rbp, rsp
|
mov rbp, rsp
|
||||||
mov rax, [rel cursor]
|
push qword [rel cursor]
|
||||||
push rax
|
|
||||||
call expect_token
|
call expect_token
|
||||||
pop rdi
|
pop rdi
|
||||||
mov [rel cursor], rdi
|
mov [rel cursor], rdi
|
||||||
|
|
@ -575,8 +576,7 @@ peek_lexeme:
|
||||||
push rbp
|
push rbp
|
||||||
mov rbp, rsp
|
mov rbp, rsp
|
||||||
push rdi
|
push rdi
|
||||||
mov rax, [rel cursor] ; current cursor
|
push qword [rel cursor] ; save cursor
|
||||||
push rax
|
|
||||||
call find_lexeme
|
call find_lexeme
|
||||||
pop rdi
|
pop rdi
|
||||||
mov [rel cursor], rdi ; restore cursor
|
mov [rel cursor], rdi ; restore cursor
|
||||||
|
|
|
||||||
|
|
@ -102,7 +102,7 @@ LEXEME_LENS:
|
||||||
dq LEX_VOID_len
|
dq LEX_VOID_len
|
||||||
|
|
||||||
align 8
|
align 8
|
||||||
NUM_LEXEMES: dq 31
|
NUM_LEXEMES: dq 30
|
||||||
|
|
||||||
LEX_NOT_A_LEXEME db "<not a lexeme>", 0
|
LEX_NOT_A_LEXEME db "<not a lexeme>", 0
|
||||||
LEX_LET db "let"
|
LEX_LET db "let"
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ unsafe extern "C" {
|
||||||
unsafe fn tokeniser_init_buf(bytes: *const u8, len: usize) -> ();
|
unsafe fn tokeniser_init_buf(bytes: *const u8, len: usize) -> ();
|
||||||
}
|
}
|
||||||
|
|
||||||
use util::defs::{parse_expr, parse_func, Ast, AstNode};
|
use util::defs::{parse_expr, Ast, AstNode};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -18,30 +18,19 @@ fn main() {
|
||||||
let src = b"3 + 4";
|
let src = b"3 + 4";
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
// tokeniser_init_buf(src.as_ptr(), src.len());
|
|
||||||
// let mut ast = Ast {
|
|
||||||
// nodes: util::vec::Vec::new(),
|
|
||||||
// };
|
|
||||||
// let expr_id = parse_expr(&mut ast);
|
|
||||||
// println!("Parsed expression with ID: {}", expr_id);
|
|
||||||
// println!("{:#}", &ast);
|
|
||||||
|
|
||||||
let src = b"fn main() -> void { return 1 + 2; }";
|
|
||||||
tokeniser_init_buf(src.as_ptr(), src.len());
|
tokeniser_init_buf(src.as_ptr(), src.len());
|
||||||
let mut ast = Ast {
|
let mut ast = Ast {
|
||||||
nodes: util::vec::Vec::new(),
|
nodes: util::vec::Vec::new(),
|
||||||
};
|
};
|
||||||
let expr_id = parse_func(&mut ast);
|
let expr_id = parse_expr(&mut ast);
|
||||||
println!("Parsed function with ID: {}", expr_id);
|
println!("Parsed expression with ID: {}", expr_id);
|
||||||
println!("{:#}", &ast);
|
println!("{:#}", &ast);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for AstNode {
|
impl std::fmt::Display for AstNode {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
use util::defs::{
|
use util::defs::{BinaryExpr, AST_BINARY_OP, AST_NUMBER};
|
||||||
BinaryExpr, AST_BINARY_OP, AST_FUNCTION, AST_NUMBER, AST_RETURN_STATEMENT,
|
|
||||||
};
|
|
||||||
match self.kind as u32 {
|
match self.kind as u32 {
|
||||||
AST_NUMBER => {
|
AST_NUMBER => {
|
||||||
write!(f, "Number({})", self.data as usize)
|
write!(f, "Number({})", self.data as usize)
|
||||||
|
|
@ -58,22 +47,6 @@ impl std::fmt::Display for AstNode {
|
||||||
operator, left, right
|
operator, left, right
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
AST_RETURN_STATEMENT => {
|
|
||||||
let return_expr_id = self.data as usize;
|
|
||||||
write!(f, "ReturnStatement(expr: {})", return_expr_id)
|
|
||||||
}
|
|
||||||
AST_FUNCTION => {
|
|
||||||
let func = unsafe { self.data.cast::<util::defs::AstFunction>().read() };
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"Function(name: {:?}, return_type: {:?}, body: {})",
|
|
||||||
unsafe {
|
|
||||||
std::str::from_utf8(std::slice::from_raw_parts(func.name, func.name_len))
|
|
||||||
},
|
|
||||||
func.return_type,
|
|
||||||
func.body
|
|
||||||
)
|
|
||||||
}
|
|
||||||
_ => write!(f, "UnknownNode"),
|
_ => write!(f, "UnknownNode"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -86,17 +86,6 @@ pub struct Type {
|
||||||
pub kind: u8,
|
pub kind: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct AstFunction {
|
|
||||||
pub name: *const u8,
|
|
||||||
pub name_len: usize,
|
|
||||||
pub args: *const Argument,
|
|
||||||
pub args_len: usize,
|
|
||||||
pub return_type: Type,
|
|
||||||
pub body: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct BinaryExpr {
|
pub struct BinaryExpr {
|
||||||
|
|
|
||||||
|
|
@ -253,7 +253,11 @@ pub mod vec {
|
||||||
cmp_trampoline::<T, F>,
|
cmp_trampoline::<T, F>,
|
||||||
&raw mut cmp as *mut F as *mut (),
|
&raw mut cmp as *mut F as *mut (),
|
||||||
);
|
);
|
||||||
if vacant { Err(index) } else { Ok(index) }
|
if vacant {
|
||||||
|
Err(index)
|
||||||
|
} else {
|
||||||
|
Ok(index)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -239,30 +239,6 @@ fn main() {
|
||||||
&[Lexeme(32, "3"), Lexeme(16, "+"), Lexeme(32, "4")],
|
&[Lexeme(32, "3"), Lexeme(16, "+"), Lexeme(32, "4")],
|
||||||
);
|
);
|
||||||
|
|
||||||
eprint!("Initializing tokeniser.. ");
|
|
||||||
let src = b"fn main() -> void { return 1 + 2; }";
|
|
||||||
tokeniser_init_buf(src.as_ptr(), src.len());
|
|
||||||
eprintln!("ok.");
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
&collect_tokens()[..],
|
|
||||||
&[
|
|
||||||
Lexeme(4, "fn"),
|
|
||||||
Lexeme(31, "main"),
|
|
||||||
Lexeme(19, "("),
|
|
||||||
Lexeme(18, ")"),
|
|
||||||
Lexeme(12, "->"),
|
|
||||||
Lexeme(30, "void"),
|
|
||||||
Lexeme(21, "{"),
|
|
||||||
Lexeme(5, "return"),
|
|
||||||
Lexeme(32, "1"),
|
|
||||||
Lexeme(16, "+"),
|
|
||||||
Lexeme(32, "2"),
|
|
||||||
Lexeme(23, ";"),
|
|
||||||
Lexeme(20, "}"),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
eprintln!("Finished tokenising.");
|
eprintln!("Finished tokenising.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#[path = "shared/shared.rs"]
|
#[path = "shared/shared.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
use util::{BlobVec, ffi::*, vec::Vec};
|
use util::{ffi::*, vec::Vec, BlobVec};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
static mut DROPS: usize = 1;
|
static mut DROPS: usize = 1;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue