aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/parse.c9
-rw-r--r--tests/118.sh19
2 files changed, 23 insertions, 5 deletions
diff --git a/src/parse.c b/src/parse.c
index 57dd316..ecd8e65 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -870,6 +870,8 @@ static Type* parse_pointer_opt(Parser* p, Type* ty) {
return ty;
}
+static int eval(AstNode* e);
+
// array-declarator:
// direct-declarator '[' TODO type-qualifier-list? TODO assignment-expression? ']'
// TODO direct-declarator '[' 'static' type-qualifier-list? assignment-expression? ']'
@@ -878,11 +880,8 @@ static Type* parse_pointer_opt(Parser* p, Type* ty) {
static Type* parse_array_declarator_suffix(Parser* p, Type* ty) {
next_token(p); // skip '['
- AstNode* size_expr = parse_expr(p);
- if (size_expr->kind != AstNodeKind_int_expr) {
- fatal_error("parse_var_decl: invalid array size");
- }
- int size = size_expr->node_int_value;
+ AstNode* size_expr = parse_assignment_expr(p);
+ int size = eval(size_expr);
expect(p, TokenKind_bracket_r);
return type_new_array(ty, size);
diff --git a/tests/118.sh b/tests/118.sh
new file mode 100644
index 0000000..48a8569
--- /dev/null
+++ b/tests/118.sh
@@ -0,0 +1,19 @@
+cat <<'EOF' > expected
+400
+80
+16
+EOF
+
+test_diff <<'EOF'
+int printf();
+
+int main() {
+ int a[10 * 10];
+ int b[10 + 10];
+ int c[1 << 2];
+
+ printf("%zu\n", sizeof(a));
+ printf("%zu\n", sizeof(b));
+ printf("%zu\n", sizeof(c));
+}
+EOF