fix ast gen with nested binary ops
This commit is contained in:
parent
a0f4b56c64
commit
b9712dacfb
|
|
@ -4,11 +4,11 @@ default rel
|
|||
%include "src/ast.inc"
|
||||
|
||||
section .rdata
|
||||
PRECEDENCE_ADD equ 90
|
||||
PRECEDENCE_SUB equ 90
|
||||
PRECEDENCE_MUL equ 100
|
||||
PRECEDENCE_DIV equ 100
|
||||
PRECEDENCE_REM equ 100
|
||||
PRECEDENCE_ADD dw 90
|
||||
PRECEDENCE_SUB dw 90
|
||||
PRECEDENCE_MUL dw 100
|
||||
PRECEDENCE_DIV dw 100
|
||||
PRECEDENCE_REM dw 100
|
||||
|
||||
section .text
|
||||
extern vec_init_with
|
||||
|
|
@ -28,6 +28,7 @@ extern tokeniser_init
|
|||
extern find_lexeme
|
||||
extern peek_lexeme
|
||||
extern expect_token
|
||||
extern skip_token
|
||||
extern unwrap_token
|
||||
extern peek_expect_token
|
||||
|
||||
|
|
@ -400,6 +401,7 @@ parse_primary_expr:
|
|||
parse_binary_expr:
|
||||
push rbp
|
||||
mov rbp, rsp
|
||||
push rbx
|
||||
|
||||
; size: 24, align: 8
|
||||
; start-structs
|
||||
|
|
@ -438,40 +440,19 @@ parse_binary_expr:
|
|||
lea rdi, [rsp + 32] ; lexeme
|
||||
call peek_lexeme
|
||||
mov rax, [rsp + 32]
|
||||
mov byte [rsp + 16], al ; operator
|
||||
mov bx, -1
|
||||
cmp al, TOKEN_PLUS
|
||||
je .add
|
||||
cmove bx, word [rel PRECEDENCE_ADD]
|
||||
cmp al, TOKEN_MINUS
|
||||
je .sub
|
||||
cmove bx, word [rel PRECEDENCE_SUB]
|
||||
cmp al, TOKEN_STAR
|
||||
je .mul
|
||||
cmove bx, word [rel PRECEDENCE_MUL]
|
||||
cmp al, TOKEN_SLASH
|
||||
je .div
|
||||
jmp .done
|
||||
|
||||
.add:
|
||||
mov dil, TOKEN_PLUS
|
||||
call unwrap_token
|
||||
mov byte [rsp + 16], TOKEN_PLUS
|
||||
mov byte [rsp + 18], PRECEDENCE_ADD
|
||||
jmp .right
|
||||
.sub:
|
||||
mov dil, TOKEN_MINUS
|
||||
call unwrap_token
|
||||
mov byte [rsp + 16], TOKEN_MINUS
|
||||
mov byte [rsp + 18], PRECEDENCE_SUB
|
||||
jmp .right
|
||||
.mul:
|
||||
mov dil, TOKEN_STAR
|
||||
call unwrap_token
|
||||
mov byte [rsp + 16], TOKEN_STAR
|
||||
mov byte [rsp + 18], PRECEDENCE_MUL
|
||||
jmp .right
|
||||
.div:
|
||||
mov dil, TOKEN_SLASH
|
||||
call unwrap_token
|
||||
mov byte [rsp + 16], TOKEN_SLASH
|
||||
mov byte [rsp + 18], PRECEDENCE_DIV
|
||||
jmp .right
|
||||
cmove bx, word [rel PRECEDENCE_DIV]
|
||||
cmp bx, -1
|
||||
je .done
|
||||
mov byte [rsp + 18], bl
|
||||
|
||||
.right:
|
||||
mov dil, [rsp + 17]
|
||||
|
|
@ -479,6 +460,8 @@ parse_binary_expr:
|
|||
cmp al, dil ; our_precedence <= upper_precedence
|
||||
jle .done
|
||||
|
||||
call skip_token ; consume operator
|
||||
|
||||
mov rdi, [rsp] ; Ast
|
||||
mov sil, [rsp + 18]
|
||||
call parse_binary_expr
|
||||
|
|
@ -529,6 +512,7 @@ parse_binary_expr:
|
|||
mov rax, [rsp + 8] ; left
|
||||
movzx rdx, byte [rsp + 19] ; left_placeness
|
||||
add rsp, 64
|
||||
pop rbx
|
||||
pop rbp
|
||||
ret
|
||||
|
||||
|
|
|
|||
|
|
@ -60,6 +60,11 @@ fn main() {
|
|||
};
|
||||
}
|
||||
|
||||
print_ast(
|
||||
b"fn main() -> void { return 1 * 2 + 3 * 4; }",
|
||||
|ast| unsafe { parse_func(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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue