Skip to content

Commit 7163055

Browse files
author
TheDevConnor
committed
Bug fixes with the error system in the parser and updated the char to byte to make it clear that it is one byte
1 parent 6dcd8ba commit 7163055

File tree

8 files changed

+329
-123
lines changed

8 files changed

+329
-123
lines changed

src/helper/help.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121

2222
/** Macro to access token at index in a token growable array */
2323
#define TOKEN_AT(i) (((Token *)tokens.data)[(i)])
24-
#define MAX_TOKENS 100
24+
#define MAX_TOKENS 300
2525

2626
#define BAR_WIDTH 40
2727

2828
/** Enable debug logs for arena allocator (comment to disable) */
2929
#define DEBUG_ARENA_ALLOC 1
3030

31-
#define Luma_Compiler_version "v0.1.0"
31+
#define Luma_Compiler_version "v0.1.2"
3232

3333
/** Error codes returned by the compiler */
3434
typedef enum {

src/parser/expr.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,14 @@ Expr *unary(Parser *parser) {
9292
UnaryOp op = TOKEN_TO_UNOP_MAP[current.type_];
9393

9494
if (op) {
95-
p_advance(parser); // Consume the token
95+
p_advance(parser);
9696
Expr *operand = parse_expr(parser, BP_UNARY);
97+
if (!operand) {
98+
parser_error(parser, "SyntaxError", parser->file_path,
99+
"Expected expression after unary operator", line, col,
100+
current.length);
101+
return NULL;
102+
}
97103
return create_unary_expr(parser->arena, op, operand, line, col);
98104
}
99105

@@ -116,9 +122,16 @@ Expr *binary(Parser *parser, Expr *left, BindingPower bp) {
116122

117123
Token current = p_current(parser);
118124
BinaryOp op = TOKEN_TO_BINOP_MAP[current.type_];
119-
p_advance(parser); // Consume the token
125+
p_advance(parser);
120126
Expr *right = parse_expr(parser, bp);
121127

128+
if (!right) {
129+
parser_error(parser, "SyntaxError", parser->file_path,
130+
"Expected expression after binary operator", line, col,
131+
current.length);
132+
return NULL;
133+
}
134+
122135
return create_binary_expr(parser->arena, op, left, right, line, col);
123136
}
124137

@@ -509,24 +522,24 @@ Expr *free_expr(Parser *parser) {
509522
Expr *cast_expr(Parser *parser) {
510523
int line = p_current(parser).line;
511524
int col = p_current(parser).col;
512-
525+
513526
p_advance(parser); // Advance past 'cast'
514527

515528
p_consume(parser, TOK_LT,
516529
"Expected a '<' before you declare the type you want to cast to.");
517-
530+
518531
Type *cast_type = parse_type(parser);
519532
// parse_type() has already advanced past the type
520-
533+
521534
p_consume(parser, TOK_GT,
522535
"Expected a '>' after defining the type you want to cast to, but "
523536
"before defining what you are casting");
524-
537+
525538
p_consume(parser, TOK_LPAREN,
526539
"Expected a '(' before defining what you are casting");
527-
540+
528541
Expr *castee = parse_expr(parser, BP_NONE);
529-
542+
530543
p_consume(parser, TOK_RPAREN,
531544
"Expected a ')' after defining what you are casting");
532545

@@ -537,23 +550,23 @@ Expr *cast_expr(Parser *parser) {
537550
Expr *input_expr(Parser *parser) {
538551
int line = p_current(parser).line;
539552
int col = p_current(parser).col;
540-
553+
541554
p_advance(parser); // Advance past 'input'
542555

543556
p_consume(parser, TOK_LT,
544557
"Expected a '<' before you declare the type you want to input.");
545-
558+
546559
Type *type = parse_type(parser);
547560
// parse_type() has already advanced past the type
548-
561+
549562
p_consume(parser, TOK_GT,
550563
"Expected a '>' after defining the type you want to input");
551-
564+
552565
p_consume(parser, TOK_LPAREN,
553566
"Expected a '(' before defining the input message");
554-
567+
555568
Expr *msg = parse_expr(parser, BP_NONE);
556-
569+
557570
p_consume(parser, TOK_RPAREN,
558571
"Expected a ')' after defining the input message");
559572

@@ -622,13 +635,13 @@ Expr *syscall_expr(Parser *parser) {
622635
Expr *sizeof_expr(Parser *parser) {
623636
int line = p_current(parser).line;
624637
int col = p_current(parser).col;
625-
638+
626639
p_advance(parser); // Advance past 'sizeof'
627-
640+
628641
p_consume(parser, TOK_LT,
629642
"Expected a '<' before defining the var or type you want to get "
630643
"the size of.");
631-
644+
632645
AstNode *object = NULL;
633646
bool is_type = false;
634647

@@ -643,7 +656,7 @@ Expr *sizeof_expr(Parser *parser) {
643656
object = (AstNode *)parse_expr(parser, BP_NONE);
644657
is_type = false;
645658
}
646-
659+
647660
p_consume(parser, TOK_GT,
648661
"Expected a '>' after defining the var or type you want to get the "
649662
"size of.");

src/parser/parser.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Stmt *parse(GrowableArray *tks, ArenaAllocator *arena, BuildConfig *config) {
109109
Token module_tok = p_current(&parser);
110110
const char *module_name = parse_module_declaration(&parser);
111111
if (!module_name) {
112+
error_report(); // CRITICAL: Report errors before returning
112113
return NULL;
113114
}
114115

@@ -127,13 +128,15 @@ Stmt *parse(GrowableArray *tks, ArenaAllocator *arena, BuildConfig *config) {
127128
while (p_current(&parser).type_ != TOK_EOF) {
128129
Stmt *stmt = parse_stmt(&parser);
129130
if (!stmt) {
130-
// Error already reported in parse_stmt
131+
// CRITICAL: Report accumulated errors before returning
132+
error_report();
131133
return NULL;
132134
}
133135

134136
Stmt **slot = (Stmt **)growable_array_push(&stmts);
135137
if (!slot) {
136138
fprintf(stderr, "Out of memory while growing statements array\n");
139+
error_report(); // Report any errors before returning
137140
return NULL;
138141
}
139142
*slot = stmt;
@@ -148,6 +151,7 @@ Stmt *parse(GrowableArray *tks, ArenaAllocator *arena, BuildConfig *config) {
148151
return create_program_node(parser.arena, (AstNode **)modules.data,
149152
modules.count, 0, 0);
150153
}
154+
151155
/**
152156
* @brief Gets the binding power (precedence) for a given token type
153157
*
@@ -524,8 +528,8 @@ Type *parse_type(Parser *parser) {
524528
case TOK_STRINGT:
525529
case TOK_VOID:
526530
case TOK_CHAR:
527-
case TOK_STAR: // Pointer type
528-
case TOK_LBRACKET: // Array type
531+
case TOK_STAR: // Pointer type
532+
case TOK_LBRACKET: // Array type
529533
case TOK_IDENTIFIER: // Could be simple type or namespace::Type
530534
return tnud(parser);
531535

src/parser/parser_utils.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,17 @@ Token p_consume(Parser *psr, LumaTokenType type, const char *error_msg) {
158158
int line = p_current(psr).line;
159159
int col = p_current(psr).col;
160160

161-
if (p_current(psr).type_ == type)
161+
if (p_current(psr).type_ == type) {
162162
return p_advance(psr);
163-
else {
164-
parser_error(psr, "SyntaxError", "unknown_file", error_msg, line, col,
163+
} else {
164+
parser_error(psr, "SyntaxError", psr->file_path, error_msg, line, col,
165165
CURRENT_TOKEN_LENGTH(psr));
166-
return (Token){.type_ = TOK_EOF}; // Return an error token
166+
167+
// CRITICAL: Set a flag or return a recognizable error token
168+
// The parser should check for this and abort
169+
Token error_tok = {
170+
.type_ = TOK_EOF, .line = line, .col = col, .length = 0, .value = NULL};
171+
return error_tok;
167172
}
168173
}
169174

0 commit comments

Comments
 (0)