implemented assignment parsing
This commit is contained in:
parent
5902ec8dbc
commit
dddb3f2954
2 changed files with 26 additions and 1 deletions
23
parser.c
23
parser.c
|
|
@ -973,6 +973,24 @@ static ast_node *parse_statement(parser *p)
|
||||||
else if (match(p, TOKEN_IF)) {
|
else if (match(p, TOKEN_IF)) {
|
||||||
return parse_if(p);
|
return parse_if(p);
|
||||||
}
|
}
|
||||||
|
else if (match_peek(p, TOKEN_IDENTIFIER) && p->tokens->next && p->tokens->next->type == TOKEN_EQ)
|
||||||
|
{
|
||||||
|
/* Variable declaration. */
|
||||||
|
ast_node *node = arena_alloc(p->allocator, sizeof(ast_node));
|
||||||
|
node->type = NODE_BINARY;
|
||||||
|
node->expr.binary.left = parse_factor(p);
|
||||||
|
advance(p);
|
||||||
|
node->expr.binary.right = parse_expression(p);
|
||||||
|
node->expr.binary.operator = OP_ASSIGN;
|
||||||
|
|
||||||
|
if (!match(p, TOKEN_SEMICOLON))
|
||||||
|
{
|
||||||
|
error(p, "expected `;` after statement.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
else if (match_peek(p, TOKEN_IDENTIFIER) && p->tokens->next && p->tokens->next->type == TOKEN_IDENTIFIER)
|
else if (match_peek(p, TOKEN_IDENTIFIER) && p->tokens->next && p->tokens->next->type == TOKEN_IDENTIFIER)
|
||||||
{
|
{
|
||||||
/* Variable declaration. */
|
/* Variable declaration. */
|
||||||
|
|
@ -1037,6 +1055,11 @@ static void parse(parser *p)
|
||||||
ast_node *tail = p->ast;
|
ast_node *tail = p->ast;
|
||||||
ast_node *expr = parse_statement(p);
|
ast_node *expr = parse_statement(p);
|
||||||
while (expr) {
|
while (expr) {
|
||||||
|
if (expr->type != NODE_FUNCTION && expr->type != NODE_VAR_DECL && expr->type != NODE_IMPORT &&
|
||||||
|
expr->type != NODE_STRUCT && expr->type != NODE_ENUM && expr->type != NODE_ENUM) {
|
||||||
|
error(p, "expected function, struct, enum, union, global variable or import statement.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
tail->expr.unit_node.next = arena_alloc(p->allocator, sizeof(ast_node));
|
tail->expr.unit_node.next = arena_alloc(p->allocator, sizeof(ast_node));
|
||||||
tail->expr.unit_node.next->expr.unit_node.expr = expr;
|
tail->expr.unit_node.next->expr.unit_node.expr = expr;
|
||||||
tail = tail->expr.unit_node.next;
|
tail = tail->expr.unit_node.next;
|
||||||
|
|
|
||||||
4
test.c
4
test.c
|
|
@ -1,5 +1,7 @@
|
||||||
enum {
|
enum {
|
||||||
a = 3,
|
a = 4,
|
||||||
b,
|
b,
|
||||||
c
|
c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a = 3;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue