@@ -2,6 +2,7 @@
2#include <stdio.h>
2#include <stdio.h>
3#include <string.h>
3#include <string.h>
4#include <unistd.h>
4#include <unistd.h>
6#include "closure.h"
7#include "closure.h"
@@ -12,13 +13,15 @@ VM* init_vm(int stack_size, size_t heap_size) {
13 VM* vm = malloc(sizeof(VM));
14 VM* vm = malloc(sizeof(VM));
14 vm -> valstack = valstack;
15 vm -> valstack = valstack;
15− vm -> valstack_ptr = valstack;
16+ vm -> valstack_top = valstack;
17+ vm -> valstack_base = valstack;
16 vm -> intstack = intstack;
18 vm -> intstack = intstack;
17 vm -> intstack_ptr = intstack;
19 vm -> intstack_ptr = intstack;
18 vm -> floatstack = floatstack;
20 vm -> floatstack = floatstack;
19 vm -> floatstack_ptr = floatstack;
21 vm -> floatstack_ptr = floatstack;
20 vm -> stack_max = stack_size;
22 vm -> stack_max = stack_size;
21 vm -> heap = malloc(heap_size);
23 vm -> heap = malloc(heap_size);
24+ vm -> heap_next = vm -> heap;
22 vm -> ret = NULL;
25 vm -> ret = NULL;
23 return vm;
26 return vm;
@@ -27,78 +30,80 @@ void* allocate(VM* vm, int size) {
27 return malloc(size); // TMP!
30 return malloc(size); // TMP!
30−void* allocThunk(VM* vm, int argspace) {
31− Closure* cl = allocate(vm, sizeof(ClosureType) +
32− sizeof(VAL) * (argspace + 1) +
38void* allocCon(VM* vm, int arity) {
33void* allocCon(VM* vm, int arity) {
39 Closure* cl = allocate(vm, sizeof(ClosureType) +
34 Closure* cl = allocate(vm, sizeof(ClosureType) +
40− sizeof(int) +
35+ sizeof(con));
41− sizeof(VAL) * arity);
42 cl -> ty = CON;
36 cl -> ty = CON;
38+ cl -> info.c.args = NULL;
40+ cl -> info.c.args = allocate(vm, sizeof(VAL)*arity);
43 return (void*)cl;
42 return (void*)cl;
46−VAL mkInt(VM* vm, int val) {
47− Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(int));
53−VAL mkFloat(VM* vm, double val) {
45+VAL MKFLOAT(VM* vm, double val) {
54 Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(double));
46 Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(double));
55 cl -> ty = FLOAT;
47 cl -> ty = FLOAT;
56 cl -> info.f = val;
48 cl -> info.f = val;
57 return cl;
49 return cl;
60−VAL mkStr(VM* vm, char* str) {
52+VAL MKSTR(VM* vm, char* str) {
61− Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(char*) * strlen(str));
53+ Closure* cl = allocate(vm, sizeof(ClosureType) + sizeof(char*));
62 cl -> ty = STRING;
54 cl -> ty = STRING;
55+ cl -> info.str = allocate(vm, sizeof(char)*strlen(str)+1);
63 strcpy(cl -> info.str, str);
56 strcpy(cl -> info.str, str);
64 return cl;
57 return cl;
67−VAL mkThunk(VM* vm, func fn, int args, int arity) {
60+VAL MKCON(VM* vm, int tag, int arity, ...) {
69− Closure* cl = allocThunk(vm, arity);
63+ va_list args;
70− cl -> info.t.fn = fn;
71− cl -> info.t.arity = arity;
72− cl -> info.t.numargs = args;
73− VAL** argptr = &(cl -> info.t.args);
75− for (i = 0; i < args; ++i) {
81−VAL mkCon(VM* vm, int tag, int arity) {
65+ va_start(args, arity);
82− Closure* cl = allocCon(vm, arity);
84− int i;
67+ cl = allocCon(vm, arity);
85 cl -> info.c.tag = tag;
68 cl -> info.c.tag = tag;
69+ cl -> info.c.arity = arity;
86− VAL** argptr = &(cl -> info.c.args);
70+ VAL* argptr = (VAL*)(cl -> info.c.args);
88 for (i = 0; i < arity; ++i) {
72 for (i = 0; i < arity; ++i) {
89− VAL* v = POP;
73+ VAL v = va_arg(args, VAL);
90− *argptr++ = v;
74+ *argptr = v;
94−// if 'update' is set, update the value at the top of the stack
79+ return cl;
95−// otherwise, replace it with a new value
80+}
97−void EVAL(int update) {
82+void PROJECT(VM* vm, VAL r, int loc, int arity) {
84+ VAL* argptr = (VAL*)(r -> info.c.args);
86+ for(i = 0; i < arity; ++i) {
87+ LOC(i+loc) = *argptr++;
95+ printf("%d[", v->info.c.tag);
96+ for(i = 0; i < v->info.c.arity; ++i) {
97+ VAL* args = (VAL*)v->info.c.args;
101void stackOverflow() {
105void stackOverflow() {
102 fprintf(stderr, "Stack overflow");
106 fprintf(stderr, "Stack overflow");
103 exit(-1);
107 exit(-1);