Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make it oo

  • Loading branch information...
commit 3d279e21628a762a1fd9000f0cfa109670d1b220 1 parent 4091a8d
@txus authored
View
BIN  bin/fxvm
Binary file not shown
View
3  src/forkix/call_frame.c
@@ -2,9 +2,10 @@
#include <forkix/call_frame.h>
CallFrame*
-CallFrame_new(int *ip, int *ret)
+CallFrame_new(VALUE self, int *ip, int *ret)
{
CallFrame* cf = calloc(1, sizeof(CallFrame));
+ cf->self = self;
cf->ip = ip;
cf->ret = ret;
cf->locals = DArray_create(sizeof(VALUE), 10);
View
3  src/forkix/call_frame.h
@@ -5,6 +5,7 @@
#include <forkix/darray.h>
struct call_frame_s {
+ VALUE self;
DArray *locals;
int *ip;
int *ret;
@@ -12,6 +13,6 @@ struct call_frame_s {
typedef struct call_frame_s CallFrame;
-CallFrame* CallFrame_new(int *ip, int *ret);
+CallFrame* CallFrame_new(VALUE self, int *ip, int *ret);
#endif
View
2  src/forkix/value.c
@@ -7,6 +7,7 @@ Value_new(ValueType type)
{
VALUE val = gc_alloc(sizeof(val_t));
val->type = type;
+ val->table = Hashmap_create(NULL, NULL);
return val;
};
@@ -39,7 +40,6 @@ Integer_new(int num)
return val;
}
-
VALUE
String_new(char* value)
{
View
5 src/forkix/value.h
@@ -3,6 +3,7 @@
#include <forkix/value_type.h>
#include <forkix/gc_header.h>
+#include <forkix/hashmap.h>
struct val_s {
GCHeader gc;
@@ -12,6 +13,7 @@ struct val_s {
char *as_str;
void *as_data;
} data;
+ Hashmap *table;
};
typedef struct val_s val_t;
@@ -27,5 +29,8 @@ VALUE Integer_new(int);
VALUE String_new(char*);
#define VAL2STR(o) (o->data.as_str)
+#define VALSET(S, K, V) Hashmap_set((S)->table, bfromcstr((K)), (V));
+#define VALGET(S, K) (VALUE)Hashmap_get((S)->table, bfromcstr((K)))
+
#endif
View
3  src/forkix/value_type.h
@@ -9,7 +9,8 @@ typedef enum {
ClosureType,
TrueType,
FalseType,
- NilType
+ NilType,
+ MainType,
} ValueType;
#endif
View
26 src/forkix/vm.c
@@ -25,30 +25,31 @@ static void dump(Stack* stack)
void VM_start()
{
+
int program[] = {
// main
PUSHINT, 1,
PUSHINT, 4,
- SEND, 0, 2,
+ SEND, 0, 1,
DUMP,
RET,
// add
+ PUSHSELF,
PUSHLOCAL, 0,
- PUSHLOCAL, 1,
ADD,
RET
};
Hashmap *fns = Hashmap_create(NULL, NULL);
- int *main = &program[0];
- int *add = &program[9];
- Hashmap_set(fns, bfromcstr("main"), main);
- Hashmap_set(fns, bfromcstr("add"), add);
+ Hashmap_set(fns, bfromcstr("main"), &program[0]);
+ Hashmap_set(fns, bfromcstr("add"), &program[9]);
STATE state = State_new(fns);
+ VALUE main = Value_new(MainType); // toplevel object
+
Stack *frames = Stack_create();
- CallFrame *top_frame = CallFrame_new(STATE_FN("main"), NULL);
+ CallFrame *top_frame = CallFrame_new(main, STATE_FN("main"), NULL);
Stack_push(frames, top_frame);
VM_run(state, frames);
}
@@ -61,7 +62,6 @@ void VM_run(STATE state, Stack *frames)
DArray_push(literals, String_new("add"));
CallFrame *current_frame = (CallFrame*)(Stack_peek(frames));
- printf("Starting at %i\n", *current_frame->ip);
int *ip = current_frame->ip;
while(1) {
@@ -92,11 +92,14 @@ void VM_run(STATE state, Stack *frames)
int argcount = op2;
int *fn = STATE_FN(VAL2STR(name));
- CallFrame *new_frame = CallFrame_new(fn, ip++);
+ CallFrame *new_frame = CallFrame_new(NULL, fn, ip++);
while(argcount--) {
DArray_push(new_frame->locals, Stack_pop(stack));
}
+
+ new_frame->self = Stack_pop(stack);
+
Stack_push(frames, new_frame);
current_frame = (CallFrame*)(Stack_peek(frames));
@@ -106,6 +109,11 @@ void VM_run(STATE state, Stack *frames)
debug("SEND %i %i", op1, op2);
break;
}
+ case PUSHSELF: {
+ debug("PUSHSELF");
+ Stack_push(stack, current_frame->self);
+ break;
+ }
case PUSHLOCAL: {
ip++;
Stack_push(stack, LOCAL(*ip));
View
1  src/forkix/vm.h
@@ -6,6 +6,7 @@
typedef enum {
NOOP = 0,
+ PUSHSELF,
PUSHINT,
PUSHLOCAL = 0x20,
Please sign in to comment.
Something went wrong with that request. Please try again.