authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-25 23:44:16-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-25 23:44:16-07:00
logb215a3e0b6b3d79f4ffdaf3a092b8aa283aafb7b
tree39ebaeb1276982a2df727cdf01cf3273aba33b74
parenta37bb4a4dafa8e3e3b1e9e3bbd07e5cf4eee129b

add constant expression evaluation for negation


2 files changed, 34 insertions(+), 9 deletions(-)

src/analyze.cpp+12-9
...@@ -3627,22 +3627,25 @@ static TypeTableEntry *analyze_prefix_op_expr(CodeGen *g, ImportTableEntry *impo...@@ -3627,22 +3627,25 @@ static TypeTableEntry *analyze_prefix_op_expr(CodeGen *g, ImportTableEntry *impo
3627 expr_node);3627 expr_node);
3628 if (expr_type->id == TypeTableEntryIdInvalid) {3628 if (expr_type->id == TypeTableEntryIdInvalid) {
3629 return expr_type;3629 return expr_type;
3630 } else if (expr_type->id == TypeTableEntryIdInt &&3630 } else if ((expr_type->id == TypeTableEntryIdInt &&
3631 expr_type->data.integral.is_signed)3631 expr_type->data.integral.is_signed) ||
3632 expr_type->id == TypeTableEntryIdFloat ||
3633 expr_type->id == TypeTableEntryIdNumLitInt ||
3634 expr_type->id == TypeTableEntryIdNumLitFloat)
3632 {3635 {
3633 return expr_type;3636 ConstExprValue *target_const_val = &get_resolved_expr(expr_node)->const_val;
3634 } else if (expr_type->id == TypeTableEntryIdFloat) {3637 if (!target_const_val->ok) {
3635 return expr_type;3638 return expr_type;
3636 } else if (expr_type->id == TypeTableEntryIdNumLitInt) {3639 }
3637 return expr_type;3640 ConstExprValue *const_val = &get_resolved_expr(node)->const_val;
3638 } else if (expr_type->id == TypeTableEntryIdNumLitFloat) {3641 const_val->ok = true;
3642 bignum_negate(&const_val->data.x_bignum, &target_const_val->data.x_bignum);
3639 return expr_type;3643 return expr_type;
3640 } else {3644 } else {
3641 add_node_error(g, node, buf_sprintf("invalid negation type: '%s'",3645 add_node_error(g, node, buf_sprintf("invalid negation type: '%s'",
3642 buf_ptr(&expr_type->name)));3646 buf_ptr(&expr_type->name)));
3643 return g->builtin_types.entry_invalid;3647 return g->builtin_types.entry_invalid;
3644 }3648 }
3645 // TODO const expr eval
3646 }3649 }
3647 case PrefixOpAddressOf:3650 case PrefixOpAddressOf:
3648 case PrefixOpConstAddressOf:3651 case PrefixOpConstAddressOf:
test/run_tests.cpp+22
...@@ -1286,6 +1286,28 @@ pub fn main(args: [][]u8) -> %void {...@@ -1286,6 +1286,28 @@ pub fn main(args: [][]u8) -> %void {
1286 %%stdout.printf("OK" ++ " IT " ++ "WORKED\n");1286 %%stdout.printf("OK" ++ " IT " ++ "WORKED\n");
1287}1287}
1288 )SOURCE", "OK IT WORKED\n");1288 )SOURCE", "OK IT WORKED\n");
1289
1290 add_simple_case("constant struct with negation", R"SOURCE(
1291import "std.zig";
1292struct Vertex {
1293 x: f32,
1294 y: f32,
1295 r: f32,
1296 g: f32,
1297 b: f32,
1298}
1299const vertices = []Vertex {
1300 Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 },
1301 Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 },
1302 Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 },
1303};
1304pub fn main(args: [][]u8) -> %void {
1305 if (vertices[0].x != -0.6) {
1306 %%stdout.printf("BAD\n");
1307 }
1308 %%stdout.printf("OK\n");
1309}
1310 )SOURCE", "OK\n");
1289}1311}
12901312
12911313