@@ -20,7 +20,7 @@ VM* init_vm(int stack_size, size_t heap_size) {
2020 vm -> intstack_ptr = intstack;
2121 vm -> floatstack = floatstack;
2222 vm -> floatstack_ptr = floatstack;
23− vm -> stack_max = stack_size;
23+ vm -> stack_max = valstack + stack_size;
2424 vm -> heap = malloc(heap_size);
2525 vm -> oldheap = NULL;
2626 vm -> heap_next = vm -> heap;
@@ -30,18 +30,21 @@ VM* init_vm(int stack_size, size_t heap_size) {
3030 vm -> allocations = 0;
3131 vm -> heap_growth = heap_size;
3232 vm -> ret = NULL;
33+ vm -> reg1 = NULL;
3334 return vm;
3435}
3536
3637void* allocate(VM* vm, size_t size) {
38+// return malloc(size);
3739 if ((size & 7)!=0) {
3840 size = 8 + ((size >> 3) << 3);
3941 }
40− vm->allocations += size+sizeof(size_t)*2;
42+ vm->allocations += size + sizeof(size_t);
4143 if (vm -> heap_next + size < vm -> heap_end) {
42− void* ptr = (void*)(((size_t*)(vm->heap_next))+2);
43− *((size_t*)(vm->heap_next)) = size+sizeof(size_t)*2;
44− vm -> heap_next += size+sizeof(size_t)*2;
44+ void* ptr = (void*)(vm->heap_next + sizeof(size_t));
45+ *((size_t*)(vm->heap_next)) = size + sizeof(size_t);
46+ vm -> heap_next += size + sizeof(size_t);
47+ bzero(ptr, size);
4548 return ptr;
4649 } else {
4750 gc(vm);
@@ -57,6 +60,9 @@ void* allocCon(VM* vm, int arity) {
5760 } else {
5861 cl -> info.c.args = (void*)((char*)cl + sizeof(Closure));
5962 }
63+ cl -> info.c.arity = arity;
64+// cl -> info.c.tag = 42424242;
65+// printf("%p\n", cl);
6066 return (void*)cl;
6167}
6268
@@ -84,17 +90,17 @@ VAL MKPTR(VM* vm, void* ptr) {
8490 return cl;
8591}
8692
87−VAL MKCON(VM* vm, int tag, int arity, ...) {
88− Closure* cl;
93+VAL MKCON(VM* vm, VAL cl, int tag, int arity, ...) {
8994 int i;
9095 va_list args;
9196
9297 va_start(args, arity);
9398
94− cl = allocCon(vm, arity);
99+// Closure* cl = allocCon(vm, arity);
95100 cl -> info.c.tag = tag;
96101 cl -> info.c.arity = arity;
97102 VAL* argptr = (VAL*)(cl -> info.c.args);
103+ printf("... %p %p\n", cl, argptr);
98104
99105 for (i = 0; i < arity; ++i) {
100106 VAL v = va_arg(args, VAL);
@@ -102,7 +108,6 @@ VAL MKCON(VM* vm, int tag, int arity, ...) {
102108 argptr++;
103109 }
104110 va_end(args);
105−
106111 return cl;
107112}
108113
@@ -122,7 +127,23 @@ void SLIDE(VM* vm, int args) {
122127 }
123128}
124129
130+void dumpStack(VM* vm) {
131+ int i = 0;
132+ VAL* root;
133+
134+ for (root = vm->valstack; root < vm->valstack_top; ++root, ++i) {
135+ printf("%d: ", i);
136+ dumpVal(*root);
137+ if (*root >= (VAL)(vm->heap) && *root < (VAL)(vm->heap_end)) { printf("OK"); }
138+ printf("\n");
139+ }
140+ printf("RET: ");
141+ dumpVal(vm->ret);
142+ printf("\n");
143+}
144+
125145void dumpVal(VAL v) {
146+ if (v==NULL) return;
126147 int i;
127148 if (ISINT(v)) {
128149 printf("%ld ", GETINT(v));
@@ -137,6 +158,10 @@ void dumpVal(VAL v) {
137158 }
138159 printf("] ");
139160 break;
161+ case FWD:
162+ printf("FWD ");
163+ dumpVal((VAL)(v->info.ptr));
164+ break;
140165 default:
141166 printf("val");
142167 }
@@ -154,7 +179,10 @@ VAL idris_castIntStr(VM* vm, VAL i) {
154179VAL idris_castStrInt(VM* vm, VAL i) {
155180 char *end;
156181 i_int v = strtol(GETSTR(i), &end, 10);
157− if (*end != '\0') return MKINT(0); else return MKINT(v);
182+ if (*end == '\0' || *end == '\n' || *end == '\r')
183+ return MKINT(v);
184+ else
185+ return MKINT(0);
158186}
159187
160188VAL idris_castFloatStr(VM* vm, VAL i) {
@@ -170,8 +198,10 @@ VAL idris_castStrFloat(VM* vm, VAL i) {
170198}
171199
172200VAL idris_concat(VM* vm, VAL l, VAL r) {
173− char *ls = GETSTR(l);
174201 char *rs = GETSTR(r);
202+ char *ls = GETSTR(l);
203+ // dumpVal(l);
204+ // printf("\n");
175205 Closure* cl = allocate(vm, sizeof(Closure) + strlen(ls) + strlen(rs) + 1);
176206 cl -> ty = STRING;
177207 cl -> info.str = (char*)cl + sizeof(Closure);
@@ -246,10 +276,10 @@ VAL idris_strTail(VM* vm, VAL str) {
246276
247277VAL idris_strCons(VM* vm, VAL x, VAL xs) {
248278 char *xstr = GETSTR(xs);
249− Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(char*) +
279+ Closure* cl = allocate(vm, sizeof(Closure) +
250280 strlen(xstr) + 2);
251281 cl -> ty = STRING;
252− cl -> info.str = (char*)cl + sizeof(ClosureType) + sizeof(char*);
282+ cl -> info.str = (char*)cl + sizeof(Closure);
253283 cl -> info.str[0] = (char)(GETINT(x));
254284 strcpy(cl -> info.str+1, xstr);
255285 return cl;
@@ -261,10 +291,10 @@ VAL idris_strIndex(VM* vm, VAL str, VAL i) {
261291
262292VAL idris_strRev(VM* vm, VAL str) {
263293 char *xstr = GETSTR(str);
264− Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(char*) +
294+ Closure* cl = allocate(vm, sizeof(Closure) +
265295 strlen(xstr) + 1);
266296 cl -> ty = STRING;
267− cl -> info.str = (char*)cl + sizeof(ClosureType) + sizeof(char*);
297+ cl -> info.str = (char*)cl + sizeof(Closure);
268298 int y = 0;
269299 int x = strlen(xstr);
270300