summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--eval.c21
-rw-r--r--out.c17
-rw-r--r--test.toc22
3 files changed, 44 insertions, 16 deletions
diff --git a/eval.c b/eval.c
index 9f8209c..f171a71 100644
--- a/eval.c
+++ b/eval.c
@@ -44,7 +44,7 @@ static size_t compiler_sizeof(Type *t) {
case TYPE_PTR:
return sizeof t->ptr;
case TYPE_ARR:
- return sizeof t->arr;
+ return compiler_sizeof(t->arr.of) * t->arr.n;
case TYPE_TUPLE:
return sizeof t->tuple;
case TYPE_SLICE:
@@ -502,7 +502,9 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) {
eval_binary_op_one(f32, F32, op); \
eval_binary_op_one(f64, F64, op)
+
#define eval_binary_op_nums_only(op) \
+ /* fix casting to bool */
val_cast(&lhs, &e->binary.lhs->type, &lhs, &e->type); \
val_cast(&rhs, &e->binary.rhs->type, &rhs, &e->type); \
assert(e->type.kind == TYPE_BUILTIN); \
@@ -534,7 +536,8 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) {
assert(e->type.kind == TYPE_BUILTIN); \
switch (builtin) { \
eval_binary_bool_op_nums(builtin, op); \
- default: assert(0); break; \
+ default:printf("%d\n",(int)builtin); \
+ assert(!"Invalid builtin to "#op); break; \
}
@@ -629,7 +632,7 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) {
if (e->binary.op != BINARY_SET)
if (!eval_expr(ev, e->binary.lhs, &lhs)) return false;
if (!eval_expr(ev, e->binary.rhs, &rhs)) return false;
- BuiltinType builtin = e->type.builtin;
+ BuiltinType builtin = e->binary.lhs->type.builtin;
switch (e->binary.op) {
case BINARY_ADD:
eval_binary_op_nums_only(+); break;
@@ -787,10 +790,16 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) {
} break;
case EXPR_NEW:
/* it's not strictly necessary to do the if here */
- if (e->new.type.kind == TYPE_ARR)
- v->arr = err_calloc(1, compiler_sizeof(&e->new.type));
- else
+ if (e->new.n) {
+ Value n;
+ if (!eval_expr(ev, e->new.n, &n))
+ return false;
+ U64 n64 = val_to_u64(&n, e->new.n->type.builtin);
+ v->slice.data = err_calloc(n64, compiler_sizeof(&e->new.type));
+ v->slice.n = n64;
+ } else {
v->ptr = err_calloc(1, compiler_sizeof(&e->new.type));
+ }
break;
case EXPR_CALL: {
Value fnv;
diff --git a/out.c b/out.c
index d165db8..31f95d2 100644
--- a/out.c
+++ b/out.c
@@ -17,7 +17,7 @@ typedef struct { void *data; u64 n; } slice_;
/* declarations */
-void puti(i64 x);
+i64 foo(void);
void main__(void);
/* code */
int main() {
@@ -25,17 +25,20 @@ int main() {
return 0;
}
-void puti(i64 x) {
+i64 foo(void) {
{
- }}
+ i64 i; {
+ i64 expr__; expr__ = 0;i = expr__;}
+ while ((i<100)) {
+ i = (i+1);;
+ };
+ }return i;
+}
void main__(void) {
{
- i64((* Ar)[5]); {
- i64((* expr__)[5]); expr__ = ((i64((*)[5]))calloc(1, sizeof(i64([5]))));Ar = expr__;}
- slice_ A; {
- slice_ expr__; slice_ a0_; a0_.data = calloc(100, sizeof(i64)); a0_.n = 100;expr__ = a0_;A = expr__;}
+ i64( Ar[1]) = {0};
}}
diff --git a/test.toc b/test.toc
index 3346b9e..8b5c7fc 100644
--- a/test.toc
+++ b/test.toc
@@ -1,11 +1,27 @@
// #C("#include <stdio.h>
// #define kasfdhkjasdfhjk ");
-puti @= fn(x: int) {
+// puti @= fn(x: int) {
// #C("printf(\"%ld\\n\", (long)x)");
+// };
+
+foo @= fn() int {
+ // X := new([100]int);
+ i := 0;
+ while i < 100 {
+ // (*X)[i] = i;
+ i = i + 1;
+ }
+ i
+ // total := 0;
+ // i = 0;
+ // while i < 100 {
+ // total = total + (*X)[i];
+ // i = i + 1;
+ // }
+ // total
};
main @= fn() {
- Ar := new([5]int);
- A:=new(int, 100);
+ Ar : [foo()]int;
};