support parenthesised expressions
This commit is contained in:
parent
c609fe4ec6
commit
886525cf7e
|
|
@ -10,6 +10,8 @@ section .rdata
|
|||
AST_NUMBER equ 4
|
||||
AST_BINARY_OP equ 5
|
||||
AST_RETURN_STATEMENT equ 6
|
||||
AST_VALUE_TO_PLACE equ 7
|
||||
AST_PLACE_TO_VALUE equ 8
|
||||
|
||||
TYPE_VOID equ 1
|
||||
TYPE_BOOL equ 2
|
||||
|
|
@ -268,6 +270,10 @@ parse_primary_expr:
|
|||
call expect_token
|
||||
test rax, rax
|
||||
jnz .number
|
||||
mov dil, TOKEN_LPARENS
|
||||
call expect_token
|
||||
test rax, rax
|
||||
jnz .paren_expr
|
||||
jmp .panic
|
||||
.number:
|
||||
mov rdi, rax ; lexeme ptr
|
||||
|
|
@ -282,6 +288,15 @@ parse_primary_expr:
|
|||
mov rdi, [rsp] ; Ast
|
||||
mov rax, [rdi + 8] ; return Ast.nodes.len()
|
||||
dec rax
|
||||
jmp .epilogue
|
||||
.paren_expr:
|
||||
mov rdi, [rsp] ; Ast
|
||||
call parse_expr
|
||||
mov [rsp + 8], rax ; expr
|
||||
mov dil, TOKEN_RPARENS
|
||||
call unwrap_token
|
||||
mov rax, [rsp + 8] ; expr
|
||||
.epilogue:
|
||||
add rsp, 32
|
||||
pop rbp
|
||||
ret
|
||||
|
|
|
|||
|
|
@ -17,24 +17,26 @@ fn main() {
|
|||
|
||||
let src = b"3 + 4";
|
||||
|
||||
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());
|
||||
let mut ast = Ast {
|
||||
nodes: util::vec::Vec::new(),
|
||||
fn print_ast(src: &[u8], parser: impl FnOnce(&mut Ast)) {
|
||||
unsafe {
|
||||
tokeniser_init_buf(src.as_ptr(), src.len());
|
||||
let mut ast = Ast {
|
||||
nodes: util::vec::Vec::new(),
|
||||
};
|
||||
let expr_id = parser(&mut ast);
|
||||
println!("{:#}", &ast);
|
||||
};
|
||||
let expr_id = parse_func(&mut ast);
|
||||
println!("Parsed function with ID: {}", expr_id);
|
||||
println!("{:#}", &ast);
|
||||
}
|
||||
|
||||
print_ast(b"3 + 4", |ast| unsafe {
|
||||
parse_expr(ast);
|
||||
});
|
||||
print_ast(b"fn main() -> void { return 1 + 2; }", |ast| unsafe {
|
||||
parse_func(ast);
|
||||
});
|
||||
print_ast(b"fn main() -> void { return (1 + (2)); }", |ast| unsafe {
|
||||
parse_func(ast);
|
||||
});
|
||||
}
|
||||
|
||||
impl std::fmt::Display for AstNode {
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ pub const AST_VARIABLE: u32 = 3;
|
|||
pub const AST_NUMBER: u32 = 4;
|
||||
pub const AST_BINARY_OP: u32 = 5;
|
||||
pub const AST_RETURN_STATEMENT: u32 = 6;
|
||||
pub const AST_VALUE_TO_PLACE: u32 = 7;
|
||||
pub const AST_PLACE_TO_VALUE: u32 = 8;
|
||||
pub const TYPE_VOID: u32 = 1;
|
||||
pub const TYPE_BOOL: u32 = 2;
|
||||
pub const TYPE_I32: u32 = 3;
|
||||
|
|
|
|||
Loading…
Reference in a new issue