diff options
-rw-r--r-- | eval.c | 31 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | test.toc | 23 | ||||
-rw-r--r-- | types.c | 2 |
4 files changed, 32 insertions, 25 deletions
@@ -965,13 +965,24 @@ static void eval_numerical_bin_op(Value lhs, Type *lhs_type, BinaryOp op, Value out->boolv = lhs.ptr op rhs.ptr; \ else { eval_binary_bool_op_nums_only(op); } - assert(out_type->kind == TYPE_BUILTIN); BuiltinType builtin = out_type->builtin; switch (op) { case BINARY_ADD: - eval_binary_op_nums_only(+); break; + if (lhs_type->kind == TYPE_PTR) { + out->ptr = (char *)lhs.ptr + val_to_i64(&rhs, rhs_type->builtin) + * (I64)compiler_sizeof(lhs_type->ptr); + } else { + eval_binary_op_nums_only(+); + } + break; case BINARY_SUB: - eval_binary_op_nums_only(-); break; + if (lhs_type->kind == TYPE_PTR) { + out->ptr = (char *)lhs.ptr - val_to_i64(&rhs, rhs_type->builtin) + * (I64)compiler_sizeof(lhs_type->ptr); + } else { + eval_binary_op_nums_only(-); + } + break; case BINARY_MUL: eval_binary_op_nums_only(*); break; case BINARY_DIV: @@ -1097,21 +1108,7 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { eval_deref(v, ptr, &e->type); } break; case BINARY_ADD: - if (e->binary.lhs->type.kind == TYPE_PTR) { - v->ptr = (char *)lhs.ptr + val_to_i64(&rhs, e->binary.rhs->type.builtin) - * (I64)compiler_sizeof(e->binary.lhs->type.ptr); - } else { - eval_numerical_bin_op(lhs, &e->binary.lhs->type, BINARY_ADD, rhs, &e->binary.rhs->type, v, &e->type); - } - break; case BINARY_SUB: - if (e->binary.lhs->type.kind == TYPE_PTR) { - v->ptr = (char *)lhs.ptr - val_to_i64(&rhs, e->binary.rhs->type.builtin) - * (I64)compiler_sizeof(e->binary.lhs->type.ptr); - } else { - eval_numerical_bin_op(lhs, &e->binary.lhs->type, BINARY_SUB, rhs, &e->binary.rhs->type, v, &e->type); - } - break; case BINARY_MUL: case BINARY_DIV: case BINARY_LT: @@ -1,6 +1,5 @@ /* TODO: -+=, -=, *=, /= compile-time arguments don't allow while {3; 5} (once break is added) any odd number of "s for a string @@ -9,15 +9,26 @@ putf @= fn(x: float) { "); }; -lsh @= fn(x: int, bits: int) int { - each 1..bits { - x *= 2; +sum @= fn(x: []int) int { + total := 0; + p := &x[0]; + while p < &x[0] + x.len { + total += *p; + p += 1; + } + total +}; + +some_sum @= fn() int { + foo := new(int, 10); + each _, i := foo { + foo[i] = i; } - x + sum(foo) }; main @= fn() { - puti(lsh(2, 15)); - X @= lsh(2, 15); + puti(some_sum()); + X @= some_sum(); puti(X); }; @@ -1208,7 +1208,7 @@ static bool types_expr(Typer *tr, Expression *e) { && type_builtin_is_numerical(lhs_type->builtin) && lhs_type->builtin == rhs_type->builtin) { valid = true; } - if (o == BINARY_ADD || o == BINARY_SUB) { + if (o == BINARY_ADD || o == BINARY_SUB || o == BINARY_SET_ADD || o == BINARY_SET_SUB) { if (lhs_type->kind == TYPE_PTR && rhs_type->kind == TYPE_BUILTIN && type_builtin_is_numerical(rhs_type->builtin)) { |