From: momoyon Date: Thu, 15 May 2025 05:46:10 +0000 (+0500) Subject: [main.c] Can parse_prefix(). X-Git-Url: https://www.git.momoyon.org/?a=commitdiff_plain;h=f3f772a38aa6426ca131998cd784f4d3f0e42f63;p=lang.git [main.c] Can parse_prefix(). NOTE: Skipped Compound Literals. - [tests] Add new test parse_prefix.momo --- diff --git a/main.c b/main.c index 62f05b3..6047862 100644 --- a/main.c +++ b/main.c @@ -33,6 +33,8 @@ static bool DEBUG_PRINT = false; // factor -> unary ( ( "/" | "*" ) unary )* ; // unary -> ( "!" | "-" ) unary // | primary ; +// prefix -> ( "++" | "--" ) IDENT +// comp.lit -> Skipped... // access -> IDENT "." ( access | IDENT ) // subscript -> IDENT "[" ast "]" // funcalls -> IDENT "(" ( ast "," )* ")" | IDENT "(" ast ")" @@ -98,9 +100,9 @@ static bool DEBUG_PRINT = false; * --------------------+-------------------------------------+-----------+----- * Prefix Inc/Dec | ++ -- | Right | * --------------------+-------------------------------------+-----------+----- - * Compound Lit | (type){list} | Left | + * Compound Lit | (type){list} | Left | // TODO: We skipped this * --------------------+-------------------------------------+-----------+----- - * Struct/Union access | . | Left | NOTE: We use . to access through pointers as well + * Struct/Union access | . | Left | X NOTE: We use . to access through pointers as well * --------------------+-------------------------------------+-----------+----- * Array Subscripting | [] | Left | X * --------------------+-------------------------------------+-----------+----- @@ -795,6 +797,7 @@ AST *parse_suffix(Arena *arena, Parser *p); AST *parse_funcall(Arena *arena, Parser *p); AST *parse_subscript(Arena *arena, Parser *p); AST *parse_access(Arena *arena, Parser *p); +AST *parse_prefix(Arena *arena, Parser *p); AST *parse_unary(Arena *arena, Parser *p); AST *parse_factor(Arena *arena, Parser *p); AST *parse_comparision(Arena *arena, Parser *p); @@ -898,10 +901,8 @@ AST *parse_suffix(Arena *arena, Parser *p) { if (t.type == TK_IDENT && (parser_peek_by(p, 1).type == TK_PLUS_PLUS || parser_peek_by(p, 1).type == TK_MINUS_MINUS)) { - // AST *operand = parse_primary(arena, p); ASSERT(operand, "We should be able to parse identifiers using parse_primary()!"); - // printf("INFO: CURRENT TOKEN: "); print_token(stdout, parser_peek(p)); AST *ast = (AST *)arena_alloc(arena, sizeof(AST)); ast->loc = t.loc; @@ -1027,6 +1028,27 @@ AST *parse_access(Arena *arena, Parser *p) { return parse_subscript(arena, p); } +AST *parse_prefix(Arena *arena, Parser *p) { + Token t = parser_peek(p); + + if (t.type == TK_PLUS_PLUS || + t.type == TK_MINUS_MINUS) { + + AST *ast = (AST *)arena_alloc(arena, sizeof(AST)); + ast->loc = t.loc; + ast->unary_expr = (Unary_expr *)arena_alloc(arena, sizeof(Unary_expr)); + ast->kind = AST_UNARY; + Unary_expr *unary_expr = ast->unary_expr; + unary_expr->operator = parser_advance(p); + unary_expr->operand = parse_primary(arena, p); + ASSERT(unary_expr->operand, "We should be able to parse identifiers using parse_primary()!"); + unary_expr->suffix = false; + return ast; + } + + return parse_access(arena, p); +} + AST *parse_unary(Arena *arena, Parser *p) { Token t = parser_peek(p); @@ -1041,7 +1063,7 @@ AST *parse_unary(Arena *arena, Parser *p) { return ast; } - return parse_access(arena, p); + return parse_prefix(arena, p); } AST *parse_factor(Arena *arena, Parser *p) { diff --git a/tests/.parse_prefix.build.code.expected b/tests/.parse_prefix.build.code.expected new file mode 100644 index 0000000..c227083 --- /dev/null +++ b/tests/.parse_prefix.build.code.expected @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/tests/.parse_prefix.build.err.expected b/tests/.parse_prefix.build.err.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.parse_prefix.build.in.expected b/tests/.parse_prefix.build.in.expected new file mode 100644 index 0000000..c4d955f --- /dev/null +++ b/tests/.parse_prefix.build.in.expected @@ -0,0 +1 @@ +dump_ast \ No newline at end of file diff --git a/tests/.parse_prefix.build.out.expected b/tests/.parse_prefix.build.out.expected new file mode 100644 index 0000000..51c5a6d --- /dev/null +++ b/tests/.parse_prefix.build.out.expected @@ -0,0 +1,4 @@ +parse_prefix.momo:1:0 [UNARY] ' ++ ('foo': ???)' +parse_prefix.momo:2:0 [UNARY] ' -- ('bar': ???)' +parse_prefix.momo:3:0 [UNARY] ' ++ ('a': ???)' +parse_prefix.momo:3:5 [UNARY] ' -- ('b': ???)' diff --git a/tests/.parse_prefix.code.expected b/tests/.parse_prefix.code.expected new file mode 100644 index 0000000..d7d17fc --- /dev/null +++ b/tests/.parse_prefix.code.expected @@ -0,0 +1 @@ +-1 \ No newline at end of file diff --git a/tests/.parse_prefix.err.expected b/tests/.parse_prefix.err.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.parse_prefix.in.expected b/tests/.parse_prefix.in.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.parse_prefix.out.expected b/tests/.parse_prefix.out.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/parse_prefix.momo b/tests/parse_prefix.momo new file mode 100644 index 0000000..1d39757 --- /dev/null +++ b/tests/parse_prefix.momo @@ -0,0 +1,3 @@ +++foo; +--bar; +++a; --b;