commit 4c7c282cfa3e8b099910f5d06d721cbe46a6cc34
parent b6f4872e033667c40a7e37f54eeaba9c4241dd33
Author: Eyal Sawady <ecs@d2evs.net>
Date: Fri, 25 Dec 2020 17:11:00 -0500
parse: implement slice and array types
Diffstat:
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/include/ast.h b/include/ast.h
@@ -25,7 +25,7 @@ struct ast_imports {
struct ast_list_type {
struct ast_expression *length; // NULL for slices and unbounded arrays
- struct type *members;
+ struct ast_type *members;
};
struct ast_enum_field {
diff --git a/src/parse.c b/src/parse.c
@@ -556,7 +556,21 @@ parse_type(struct parser *par)
type = parse_tagged_union_type(par);
break;
case T_LBRACKET:
- assert(0); // TODO: Slices/arrays
+ type = xcalloc(sizeof(struct ast_type), 1);
+ switch (lex(par->lex, &tok)) {
+ case T_RBRACKET:
+ type->storage = TYPE_STORAGE_SLICE;
+ type->slice.members = parse_type(par);
+ break;
+ default:
+ type->storage = TYPE_STORAGE_ARRAY;
+ unlex(par->lex, &tok);
+ type->array.length = parse_simple_expression(par);
+ want(par, T_RBRACKET, NULL);
+ type->array.members = parse_type(par);
+ break;
+ }
+ break;
case T_ATTR_NORETURN:
noreturn = true;
want(par, T_FN, NULL);