Skip to content

Commit 68d815c

Browse files
authored
Allocate memory for statement data
1 parent 987b4e7 commit 68d815c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

parser.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#include "bloat.h"
22
#include "lexer.h"
33
#include "parser.h"
4+
#include <stdlib.h>
45

56
static Parser parser;
67

8+
static Stmt* declaration();
9+
static Stmt* statement();
10+
static Expr* expression();
11+
static Expr* binary();
12+
static Expr* unary();
13+
static Expr* primary();
14+
715
static void error_at(Token* token, const char* message) {
816
if (parser.panic_mode) return;
917
parser.panic_mode = true;
@@ -27,11 +35,9 @@ static void error(const char* message) {
2735

2836
static void advance() {
2937
parser.previous = parser.current;
30-
3138
for (;;) {
3239
parser.current = scan_token();
3340
if (parser.current.type != TOKEN_ERROR) break;
34-
3541
error_at(&parser.current, parser.current.start);
3642
}
3743
}
@@ -41,7 +47,6 @@ static void consume(TokenType type, const char* message) {
4147
advance();
4248
return;
4349
}
44-
4550
error_at(&parser.current, message);
4651
}
4752

@@ -70,7 +75,6 @@ static Expr* unary() {
7075

7176
static Expr* binary() {
7277
Expr* expr = unary();
73-
7478
while (true) {
7579
Token operator = parser.current;
7680
switch (operator.type) {
@@ -102,19 +106,23 @@ static Expr* binary() {
102106
static Stmt* print_statement() {
103107
Expr* expr = expression();
104108
consume(TOKEN_SEMICOLON, "Expect ';' after value.");
105-
109+
106110
Stmt* stmt = malloc(sizeof(Stmt));
107111
stmt->type = STMT_PRINT;
112+
113+
stmt->data.print = malloc(sizeof(*(stmt->data.print)));
108114
stmt->data.print->expr = expr;
109115
return stmt;
110116
}
111117

112118
static Stmt* expression_statement() {
113119
Expr* expr = expression();
114120
consume(TOKEN_SEMICOLON, "Expect ';' after expression.");
115-
121+
116122
Stmt* stmt = malloc(sizeof(Stmt));
117123
stmt->type = STMT_EXPRESSION;
124+
125+
stmt->data.expression = malloc(sizeof(*(stmt->data.expression)));
118126
stmt->data.expression->expr = expr;
119127
return stmt;
120128
}

0 commit comments

Comments
 (0)