Author | Jakob Wakeling <[email protected]> |
Date | 2023-05-02 04:34:10 |
Commit | 04569e6b84d3462bf923847abc39428e3c82fc21 |
Parent | 4dec857b6760907b45eb614c1ed8802fa5514d60 |
Fix not checking for null on operator stack
Diffstat
D | CHANGELOG | | | 6 | ------ |
M | examples/main.g | | | 2 | +- |
M | src/parse.c | | | 9 | ++++++++- |
M | src/parse.h | | | 3 | +-- |
4 files changed, 10 insertions, 10 deletions
diff --git a/CHANGELOG b/CHANGELOG deleted file mode 100644 index 26b45ca..0000000 --- a/CHANGELOG +++ /dev/null @@ -1,6 +0,0 @@ -0.1.0, -* Add Lexer -* Add Symbol Table -* Add Parser -* Add Abstract Syntax Tree -* Add LLVM Backend diff --git a/examples/main.g b/examples/main.g index 49aff02..97fa5ae 100644 --- a/examples/main.g +++ b/examples/main.g @@ -1,4 +1,4 @@ main :: proc() -> s64 { - var := 42; + var := 42 + 8; return var; } diff --git a/src/parse.c b/src/parse.c index eb60ea9..ad11863 100644 --- a/src/parse.c +++ b/src/parse.c @@ -15,6 +15,7 @@ #include <stdlib.h> #include <string.h> +/* tk : Token Kind, ak : AST Kind, o : Precedence, as : Left Associative */ typedef struct { tok_k tk; ast_k ak; s32 o; bool as; } op; char *ast_ks[] = { @@ -239,7 +240,13 @@ static ast *parse_expr(lex *l, syt *st) { default: /* Handle operators */ { op o1, o2; if ((o1 = op_lookup(T.k, false)).tk == TK_NULL) { goto eox; } - for (o2 = op_lookup(tok_a_peek(&ts).k, false);; o2 = op_lookup(tok_a_peek(&ts).k, false)) { + /* + If there is an operator at the top of the operator stack that is not + a left parenthesis, and has greater precedence than o1 or has the + same precedence as o1 and o1 is left-associative, then pop it from + the stack onto the output. + */ + for (o2 = op_lookup(tok_a_peek(&ts).k, false); o2.tk != NULL; o2 = op_lookup(tok_a_peek(&ts).k, false)) { if (o2.tk == TK_LPAREN || (o1.o < o2.o && (o1.o != o2.o || o1.as == true))) { break; } shunt(&as, tok_a_pop(&ts), o2); diff --git a/src/parse.h b/src/parse.h index 81be9df..fc7b5fd 100644 --- a/src/parse.h +++ b/src/parse.h @@ -25,8 +25,7 @@ typedef enum { AK_ID_VAR, AK_ID_PROC, AK_INT, } ast_k; -/* k: kind, ln: line, cl: column */ -/* t: type, c: children */ +/* k : Kind, ln : Line, cl : Column, t : Type, c : Children */ typedef struct ast_s { ast_k k; UINT ln, cl; u64 h; char *s; type *t; val v; syt st; struct ast_s *p; struct { struct ast_s **a; UINT al; } c;