summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--eval.c31
-rw-r--r--main.c1
-rw-r--r--test.toc23
-rw-r--r--types.c2
4 files changed, 32 insertions, 25 deletions
diff --git a/eval.c b/eval.c
index 1d893f4..a3c2c83 100644
--- a/eval.c
+++ b/eval.c
@@ -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:
diff --git a/main.c b/main.c
index 889b9e4..88ac5f7 100644
--- a/main.c
+++ b/main.c
@@ -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
diff --git a/test.toc b/test.toc
index d17b226..65f9f30 100644
--- a/test.toc
+++ b/test.toc
@@ -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);
};
diff --git a/types.c b/types.c
index 4a07afd..337b97e 100644
--- a/types.c
+++ b/types.c
@@ -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)) {