mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 23:28:48 +00:00
Constant evaluation for in expression for bit_sets
This commit is contained in:
@@ -727,7 +727,7 @@ bit_set_type :: proc() {
|
|||||||
WEEKEND :: Days{Sunday, Saturday};
|
WEEKEND :: Days{Sunday, Saturday};
|
||||||
|
|
||||||
d: Days;
|
d: Days;
|
||||||
d = Days{Sunday} | Days{Monday};
|
d = {Sunday, Monday};
|
||||||
x := Tuesday;
|
x := Tuesday;
|
||||||
e := d | WEEKEND;
|
e := d | WEEKEND;
|
||||||
e |= {Monday};
|
e |= {Monday};
|
||||||
@@ -738,6 +738,8 @@ bit_set_type :: proc() {
|
|||||||
if Saturday in e {
|
if Saturday in e {
|
||||||
fmt.println("Saturday in", e);
|
fmt.println("Saturday in", e);
|
||||||
}
|
}
|
||||||
|
X :: Saturday in WEEKEND; // Constant evaluation
|
||||||
|
fmt.println(X);
|
||||||
}
|
}
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
|
|||||||
@@ -2041,6 +2041,22 @@ void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, bool use_lhs_as
|
|||||||
} else if (is_type_bit_set(y->type)) {
|
} else if (is_type_bit_set(y->type)) {
|
||||||
Type *yt = base_type(y->type);
|
Type *yt = base_type(y->type);
|
||||||
check_assignment(c, x, yt->BitSet.base, str_lit("bit_set 'in'"));
|
check_assignment(c, x, yt->BitSet.base, str_lit("bit_set 'in'"));
|
||||||
|
|
||||||
|
if (x->mode == Addressing_Constant && y->mode == Addressing_Constant) {
|
||||||
|
ExactValue k = exact_value_to_integer(x->value);
|
||||||
|
ExactValue v = exact_value_to_integer(y->value);
|
||||||
|
GB_ASSERT(k.kind == ExactValue_Integer);
|
||||||
|
GB_ASSERT(v.kind == ExactValue_Integer);
|
||||||
|
i64 bit = 1ll<<big_int_to_i64(&k.value_integer);
|
||||||
|
i64 bits = big_int_to_i64(&v.value_integer);
|
||||||
|
|
||||||
|
x->mode = Addressing_Constant;
|
||||||
|
x->type = t_untyped_bool;
|
||||||
|
x->value = exact_value_bool((bit & bits) != 0);
|
||||||
|
x->expr = node;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
gbString t = type_to_string(y->type);
|
gbString t = type_to_string(y->type);
|
||||||
error(x->expr, "expected either a map or bitset for 'in', got %s", t);
|
error(x->expr, "expected either a map or bitset for 'in', got %s", t);
|
||||||
|
|||||||
Reference in New Issue
Block a user