commit 2668d426f005dd158b5c1cf1d8d1110677e2322c
parent fb9fa98670cc9e6c8278da8e708404b5441caf39
Author: Drew DeVault <sir@cmpwn.com>
Date: Wed, 30 Dec 2020 14:52:16 -0500
parse: implement cast expressions
Diffstat:
3 files changed, 34 insertions(+), 1 deletion(-)
diff --git a/include/ast.h b/include/ast.h
@@ -146,6 +146,12 @@ struct ast_expression_call {
struct ast_call_argument *args;
};
+struct ast_expression_cast {
+ enum cast_kind kind;
+ struct ast_expression *value;
+ struct ast_type *type;
+};
+
struct ast_array_constant {
struct ast_expression *value;
struct ast_array_constant *next;
@@ -237,6 +243,7 @@ struct ast_expression {
struct ast_expression_binarithm binarithm;
struct ast_expression_binding binding;
struct ast_expression_call call;
+ struct ast_expression_cast cast;
struct ast_expression_constant constant;
struct ast_expression_for _for;
struct ast_expression_if _if;
diff --git a/include/expr.h b/include/expr.h
@@ -92,6 +92,18 @@ struct expression_binding {
struct expression_binding *next;
};
+enum cast_kind {
+ C_CAST,
+ C_ASSERTION,
+ C_TEST,
+};
+
+struct expression_cast {
+ enum cast_kind kind;
+ struct expression *value;
+ const struct type *type;
+};
+
struct call_argument {
bool variadic;
struct expression *value;
@@ -190,6 +202,7 @@ struct expression {
struct expression_binarithm binarithm;
struct expression_binding binding;
struct expression_call call;
+ struct expression_cast cast;
union expression_constant constant;
struct expression_for _for;
struct expression_if _if;
diff --git a/src/parse.c b/src/parse.c
@@ -1197,17 +1197,30 @@ parse_cast_expression(struct lexer *lexer)
{
trace(TR_PARSE, "cast");
struct ast_expression *value = parse_unary_expression(lexer);
+ enum cast_kind kind;
struct token tok;
switch (lex(lexer, &tok)) {
case T_COLON:
+ kind = C_CAST;
+ break;
case T_AS:
+ kind = C_ASSERTION;
+ break;
case T_IS:
- assert(0); // TODO
+ kind = C_TEST;
+ break;
default:
unlex(lexer, &tok);
return value;
}
+
+ struct ast_expression *exp = xcalloc(1, sizeof(struct ast_expression));
+ exp->type = EXPR_CAST;
+ exp->cast.kind = kind;
+ exp->cast.value = value;
+ exp->cast.type = parse_type(lexer);
+ return exp;
}
static int