add constant expression evaluation for negation

master
Andrew Kelley 2016-01-25 23:44:16 -07:00
parent a37bb4a4da
commit b215a3e0b6
2 changed files with 34 additions and 9 deletions

View File

@ -3627,22 +3627,25 @@ static TypeTableEntry *analyze_prefix_op_expr(CodeGen *g, ImportTableEntry *impo
expr_node);
if (expr_type->id == TypeTableEntryIdInvalid) {
return expr_type;
} else if (expr_type->id == TypeTableEntryIdInt &&
expr_type->data.integral.is_signed)
} else if ((expr_type->id == TypeTableEntryIdInt &&
expr_type->data.integral.is_signed) ||
expr_type->id == TypeTableEntryIdFloat ||
expr_type->id == TypeTableEntryIdNumLitInt ||
expr_type->id == TypeTableEntryIdNumLitFloat)
{
return expr_type;
} else if (expr_type->id == TypeTableEntryIdFloat) {
return expr_type;
} else if (expr_type->id == TypeTableEntryIdNumLitInt) {
return expr_type;
} else if (expr_type->id == TypeTableEntryIdNumLitFloat) {
ConstExprValue *target_const_val = &get_resolved_expr(expr_node)->const_val;
if (!target_const_val->ok) {
return expr_type;
}
ConstExprValue *const_val = &get_resolved_expr(node)->const_val;
const_val->ok = true;
bignum_negate(&const_val->data.x_bignum, &target_const_val->data.x_bignum);
return expr_type;
} else {
add_node_error(g, node, buf_sprintf("invalid negation type: '%s'",
buf_ptr(&expr_type->name)));
return g->builtin_types.entry_invalid;
}
// TODO const expr eval
}
case PrefixOpAddressOf:
case PrefixOpConstAddressOf:

View File

@ -1286,6 +1286,28 @@ pub fn main(args: [][]u8) -> %void {
%%stdout.printf("OK" ++ " IT " ++ "WORKED\n");
}
)SOURCE", "OK IT WORKED\n");
add_simple_case("constant struct with negation", R"SOURCE(
import "std.zig";
struct Vertex {
x: f32,
y: f32,
r: f32,
g: f32,
b: f32,
}
const vertices = []Vertex {
Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 },
Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 },
Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 },
};
pub fn main(args: [][]u8) -> %void {
if (vertices[0].x != -0.6) {
%%stdout.printf("BAD\n");
}
%%stdout.printf("OK\n");
}
)SOURCE", "OK\n");
}