implemented member access parsing
This commit is contained in:
parent
c566f7d490
commit
d1b89939ae
4 changed files with 33 additions and 1 deletions
4
lc.c
4
lc.c
|
|
@ -72,6 +72,10 @@ void print_ast(ast_node *node, int depth) {
|
|||
printf("Cast: %.*s\n", (int)node->expr.cast.type_len, node->expr.cast.type);
|
||||
print_ast(node->expr.cast.value, depth + 1);
|
||||
break;
|
||||
case NODE_ACCESS:
|
||||
printf("Access: %.*s\n", (int)node->expr.access.member_len, node->expr.access.member);
|
||||
print_ast(node->expr.access.expr, depth + 1);
|
||||
break;
|
||||
case NODE_BINARY:
|
||||
printf("BinaryOp (%s)\n", get_op_str(node->expr.binary.operator));
|
||||
print_ast(node->expr.binary.left, depth + 1);
|
||||
|
|
|
|||
23
parser.c
23
parser.c
|
|
@ -233,6 +233,10 @@ ast_node *parse_expression(parser *p)
|
|||
left = node;
|
||||
}
|
||||
|
||||
/*
|
||||
* If after parsing an expression a `[` character
|
||||
* is found, it should be an array subscript expression.
|
||||
*/
|
||||
if (match(p, TOKEN_LSQUARE)) {
|
||||
ast_node *index = parse_expression(p);
|
||||
ast_node *node = arena_alloc(p->allocator, sizeof(ast_node));
|
||||
|
|
@ -247,6 +251,25 @@ ast_node *parse_expression(parser *p)
|
|||
return node;
|
||||
}
|
||||
|
||||
/*
|
||||
* If after parsing an expression a `.` character
|
||||
* is found, it should be a member access expression.
|
||||
*/
|
||||
if (match(p, TOKEN_DOT)) {
|
||||
if (!match_peek(p, TOKEN_IDENTIFIER)) {
|
||||
error(p, "expected identifier after member access.");
|
||||
return NULL;
|
||||
}
|
||||
ast_node *node = arena_alloc(p->allocator, sizeof(ast_node));
|
||||
node->type = NODE_ACCESS;
|
||||
node->expr.access.expr = left;
|
||||
node->expr.access.member = p->tokens->lexeme;
|
||||
node->expr.access.member_len = p->tokens->lexeme_len;
|
||||
advance(p);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
|
|
|
|||
5
parser.h
5
parser.h
|
|
@ -128,6 +128,11 @@ typedef struct _ast_node {
|
|||
struct _ast_node *expr;
|
||||
struct _ast_node *index;
|
||||
} subscript;
|
||||
struct {
|
||||
struct _ast_node *expr;
|
||||
char *member;
|
||||
usize member_len;
|
||||
} access;
|
||||
struct {
|
||||
struct _ast_node *expr;
|
||||
struct _ast_node *next;
|
||||
|
|
|
|||
2
test.c
2
test.c
|
|
@ -1 +1 @@
|
|||
x[4
|
||||
x.y
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue