diff options
| author | hachem <im@hachem.wtf> | 2026-09-03 03:36:06 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-09-03 03:36:06 +0200 |
| commit | 56ba8b2a373dd1386950592bda9c68ff9f25c203 (patch) | |
| tree | e33d46037b8a0cb25f5c9052d1f2ee3b143dff22 /src/parser | |
| parent | 9956f626110a5b55fc58049bbe5841a98e941614 (diff) | |
fix: i JUST started using a static analyzer for ts
Diffstat (limited to 'src/parser')
| -rw-r--r-- | src/parser/ast.c | 18 | ||||
| -rw-r--r-- | src/parser/parser.c | 25 |
2 files changed, 20 insertions, 23 deletions
diff --git a/src/parser/ast.c b/src/parser/ast.c index 788341b..a387533 100644 --- a/src/parser/ast.c +++ b/src/parser/ast.c @@ -150,10 +150,7 @@ void add_data(struct Program* program, struct DataDecl decl) struct EnumDecl create_enum(void) { - struct EnumDecl decl; - decl.members = NULL; - decl.member_count = 0; - decl.member_capacity = 0; + struct EnumDecl decl = { 0 }; return decl; } @@ -185,10 +182,7 @@ void add_enum(struct Program* program, struct EnumDecl decl) struct StructDecl create_struct(void) { - struct StructDecl decl; - decl.fields = NULL; - decl.field_count = 0; - decl.field_capacity = 0; + struct StructDecl decl = { 0 }; return decl; } @@ -220,13 +214,7 @@ void add_struct(struct Program* program, struct StructDecl decl) struct ProcDecl create_proc(void) { - struct ProcDecl proc; - proc.params = NULL; - proc.param_count = 0; - proc.param_capacity = 0; - proc.body = NULL; - proc.body_count = 0; - proc.body_capacity = 0; + struct ProcDecl proc = { 0 }; return proc; } diff --git a/src/parser/parser.c b/src/parser/parser.c index 3ed9fff..a83c3c4 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -209,13 +209,15 @@ static bool is_assign_op(enum TokenType type) || type == TOKEN_PLUS_EQUAL || type == TOKEN_MINUS_EQUAL || type == TOKEN_STAR_EQUAL - || type == TOKEN_SLASH_EQUAL; + || type == TOKEN_SLASH_EQUAL + || type == TOKEN_PERCENT_EQUAL; } static struct Expr* alloc_expr(enum ExprKind kind) { struct Expr* expr = malloc(sizeof(struct Expr)); - expr->kind = kind; + if (expr != NULL) + expr->kind = kind; return expr; } @@ -289,13 +291,14 @@ static struct Expr* parse_postfix(struct Parser* parser) return expr; } -static struct Expr* parse_binary(struct Parser* parser, struct Expr* (*operand)(struct Parser*), enum TokenType a, enum TokenType b) +static struct Expr* parse_binary(struct Parser* parser, struct Expr* (*operand)(struct Parser*), + enum TokenType a, enum TokenType b, enum TokenType c) { struct Expr* left = operand(parser); if (left == NULL) return NULL; - while (check(parser, a) || check(parser, b)) + while (check(parser, a) || check(parser, b) || check(parser, c)) { advance_parser(parser); struct Token op = parser->previous; @@ -319,12 +322,12 @@ static struct Expr* parse_binary(struct Parser* parser, struct Expr* (*operand)( static struct Expr* parse_multiplicative(struct Parser* parser) { - return parse_binary(parser, parse_postfix, TOKEN_STAR, TOKEN_SLASH); + return parse_binary(parser, parse_postfix, TOKEN_STAR, TOKEN_SLASH, TOKEN_PERCENT); } static struct Expr* parse_expression(struct Parser* parser) { - return parse_binary(parser, parse_multiplicative, TOKEN_PLUS, TOKEN_MINUS); + return parse_binary(parser, parse_multiplicative, TOKEN_PLUS, TOKEN_MINUS, TOKEN_MINUS); } static bool is_compare_op(enum TokenType type) @@ -354,7 +357,13 @@ static bool parse_call(struct Parser* parser, struct Token name, struct Statemen if (count == capacity) { capacity = capacity < 4 ? 4 : capacity * 2; - args = realloc(args, capacity * sizeof(struct Expr*)); + struct Expr** grown = realloc(args, capacity * sizeof(struct Expr*)); + if (grown == NULL) + { + free_expr(arg); + goto error; + } + args = grown; } args[count] = arg; count += 1; @@ -614,7 +623,7 @@ static bool parse_directive(struct Parser* parser, struct Program* program) bool parse_program(struct Lexer* lexer, struct Program* out) { - struct Parser parser; + struct Parser parser = { 0 }; parser.lexer = lexer; parser.source.name = lexer->name; parser.source.text = lexer->source; |
