commit c7f7d297f83c885d47d0e0cad1b4579b7933a732
parent 5101702a6464961b16644b5d2a66359df4e5d418
Author: Drew DeVault <sir@cmpwn.com>
Date: Mon, 21 Dec 2020 14:34:33 -0500
parse: simple access expressions
Diffstat:
2 files changed, 56 insertions(+), 6 deletions(-)
diff --git a/include/ast.h b/include/ast.h
@@ -84,7 +84,11 @@ struct ast_type {
};
};
-struct ast_constant_expression {
+struct ast_expression_access {
+ struct identifier ident;
+};
+
+struct ast_expression_constant {
enum type_storage storage;
union {
intmax_t ival;
@@ -109,7 +113,8 @@ struct ast_return_expression {
struct ast_expression {
enum expr_type type;
union {
- struct ast_constant_expression constant;
+ struct ast_expression_access access;
+ struct ast_expression_constant constant;
struct ast_expression_list list;
struct ast_return_expression _return;
};
diff --git a/src/parse.c b/src/parse.c
@@ -335,12 +335,21 @@ parse_type(struct parser *par, struct ast_type *type)
}
static void
-parse_simple_expression(struct parser *par, struct ast_expression *exp)
+parse_access(struct parser *par, struct ast_expression *exp)
+{
+ trenter(TR_PARSE, "access");
+ exp->type = EXPR_ACCESS;
+ parse_identifier(par, &exp->access.ident);
+ trleave(TR_PARSE, NULL);
+}
+
+static void
+parse_constant(struct parser *par, struct ast_expression *exp)
{
- trenter(TR_PARSE, "simple-expression");
- struct token tok = {0};
- want(par, T_LITERAL, &tok); // TODO: other simple expressions
trenter(TR_PARSE, "constant");
+
+ struct token tok = {0};
+ want(par, T_LITERAL, &tok);
exp->type = EXPR_CONSTANT;
exp->constant.storage = tok.storage;
switch (tok.storage) {
@@ -372,6 +381,42 @@ parse_simple_expression(struct parser *par, struct ast_expression *exp)
assert(0); // TODO
}
trleave(TR_PARSE, "%s", token_str(&tok));
+
+ trleave(TR_PARSE, NULL);
+}
+
+static void
+parse_simple_expression(struct parser *par, struct ast_expression *exp)
+{
+ trenter(TR_PARSE, "simple-expression");
+
+ // TODO: This will need to be refactored once we finish the grammar
+ // connecting postfix-expression to logical-or-expression
+ struct token tok = {0};
+ lex(par->lex, &tok);
+ switch (tok.token) {
+ // plain-expression
+ case T_NAME:
+ unlex(par->lex, &tok);
+ parse_access(par, exp);
+ break;
+ case T_LITERAL:
+ unlex(par->lex, &tok);
+ parse_constant(par, exp);
+ break;
+ case T_LBRACKET:
+ assert(0); // TODO: array/slice literal
+ case T_LBRACE:
+ assert(0); // TODO: array/slice literal
+ // nested-expression
+ case T_LPAREN:
+ assert(0); // TODO: nested-expression
+ // syntax error
+ default:
+ synassert(false, &tok, T_LITERAL, T_NAME, T_EOF);
+ break;
+ }
+
trleave(TR_PARSE, NULL);
}