Skip to content
Browse files

Oh my god

  • Loading branch information...
1 parent 93f95ed commit 2aead052c6f656fc9ca23a4c9d51e8a92e1ca303 @txus committed Dec 29, 2012
View
3 bin/fxvm.c
@@ -7,7 +7,6 @@ int
main(int argc, char *argv[])
{
bstring filename = bfromcstr(argv[1]);
- BytecodeFile *file = BytecodeFile_new(filename);
- VM_start(file);
+ VM_start(filename);
return 0;
}
View
2 compiler/examples/functions.rb
@@ -1 +1 @@
--> { 3 }
+puts -> a { a + 3 }.apply(nil, 123)
View
4 compiler/examples/primitives.rb
@@ -1,4 +0,0 @@
-puts VM.primitives[:+].apply(3, 4)
-puts VM.primitives[:-].apply(4, 3)
-puts VM.primitives[:*].apply(4, 3)
-puts VM.primitives[:/].apply(4, 2)
View
1 compiler/examples/vm.rb
@@ -1 +0,0 @@
-puts VM.primitives
View
13 compiler/kernel/prelude.rb
@@ -1,2 +1,11 @@
-self.print = VM.primitives[:print]
-self.puts = VM.primitives[:puts]
+VM.types[:object].clone = VM.primitives[:clone]
+VM.types[:object].print = VM.primitives[:print]
+VM.types[:object].puts = VM.primitives[:puts]
+
+VM.types[:integer][:+] = VM.primitives[:'integer_+']
+VM.types[:integer][:-] = VM.primitives[:'integer_-']
+VM.types[:integer][:/] = VM.primitives[:'integer_/']
+VM.types[:integer][:*] = VM.primitives[:'integer_*']
+
+VM.types[:vector][:[]] = VM.primitives[:'vector_[]']
+VM.types[:vector][:to_map] = VM.primitives[:vector_to_map]
View
17 compiler/lib/terror/visitor.rb
@@ -115,7 +115,7 @@ def slot_retrieval(node, parent)
def attribute_assignment(node, parent)
receiver_name = if node.receiver.is_a?(Rubinius::AST::Self)
- 'self'
+ :self
else
node.receiver.name
end
@@ -128,6 +128,21 @@ def attribute_assignment(node, parent)
g.setslot attribute_name
end
+ def element_assignment(node, parent)
+ receiver_name = if node.receiver.is_a?(Rubinius::AST::Self)
+ :self
+ else
+ node.receiver.name
+ end
+ attribute_name = node.arguments.array.first.value
+ @slots[receiver_name] ||= []
+ @slots[receiver_name] << attribute_name
+
+ node.receiver.lazy_visit self
+ node.arguments.array[1].lazy_visit self
+ g.setslot attribute_name
+ end
+
def array_literal(node, parent)
node.body.reverse.each do |element|
element.lazy_visit self
View
32 compiler/test/terror/visitor_test.rb
@@ -124,23 +124,6 @@ def compiles(code, &block)
_setslot 2
end
end
-
- it 'compiles objects with getters and setters' do
- compiles("a = 3; a.foo = 9; a.foo") do
- # a = 3
- _push 0
- _setlocal 0
-
- # a.foo = 9
- _pushlocal 0
- _push 1
- _setslot 2
-
- # a.foo
- _pushlocal 0
- _getslot 2
- end
- end
end
describe 'constants' do
@@ -194,6 +177,21 @@ def compiles(code, &block)
_setslot 1
end
end
+
+ it 'is compiled with hash syntax' do
+ compiles("a = 2; a[:foo] = 'bar'; a[:foo]") do
+ _push 0
+ _setlocal 0
+
+ _pushlocal 0
+ _push 1
+ _setslot 2
+
+ _pushlocal 0
+ _push 1
+ _send 2, 1
+ end
+ end
end
end
end
View
23 examples/functions.tvm
@@ -1,14 +1,33 @@
_main
-:1:4
+:4:14
"block_508
+123
+"apply
+"puts
+16 PUSHSELF
67 DEFN
0
20 PUSHNIL
+17 PUSH
+1
+128 SEND
+2
+2
+128 SEND
+3
+1
+20 PUSHNIL
144 RET
_block_508
-:1:3
+:2:8
3
+"+
+32 PUSHLOCAL
+0
17 PUSH
0
+128 SEND
+1
+1
144 RET
View
56 examples/objects.tvm
@@ -1,7 +1,6 @@
_main
-:14:92
-"Object
-"new
+:13:91
+"to_map
50
"age
1000
@@ -14,95 +13,94 @@ _main
"Parent money:
"Child age:
"Child money:
-16 PUSHSELF
-64 GETSLOT
+68 MAKEVEC
0
128 SEND
-1
+0
0
33 SETLOCAL
0
32 PUSHLOCAL
0
17 PUSH
-2
+1
65 SETSLOT
-3
+2
32 PUSHLOCAL
0
17 PUSH
-4
+3
65 SETSLOT
-5
+4
32 PUSHLOCAL
0
128 SEND
-6
+5
0
33 SETLOCAL
1
32 PUSHLOCAL
1
17 PUSH
-7
+6
65 SETSLOT
-3
+2
16 PUSHSELF
17 PUSH
-8
+7
128 SEND
-9
+8
1
16 PUSHSELF
32 PUSHLOCAL
0
64 GETSLOT
-3
+2
128 SEND
-10
+9
1
16 PUSHSELF
17 PUSH
-11
+10
128 SEND
-9
+8
1
16 PUSHSELF
32 PUSHLOCAL
0
64 GETSLOT
-5
+4
128 SEND
-10
+9
1
16 PUSHSELF
17 PUSH
-12
+11
128 SEND
-9
+8
1
16 PUSHSELF
32 PUSHLOCAL
1
64 GETSLOT
-3
+2
128 SEND
-10
+9
1
16 PUSHSELF
17 PUSH
-13
+12
128 SEND
-9
+8
1
16 PUSHSELF
32 PUSHLOCAL
1
128 SEND
-5
+4
0
128 SEND
-10
+9
1
20 PUSHNIL
144 RET
View
105 examples/primitives.tvm
@@ -1,105 +0,0 @@
-_main
-:12:90
-"VM
-"primitives
-"+
-"[]
-3
-4
-"apply
-"puts
-"-
-"*
-"/
-2
-16 PUSHSELF
-16 PUSHSELF
-64 GETSLOT
-0
-128 SEND
-1
-0
-17 PUSH
-2
-128 SEND
-3
-1
-17 PUSH
-4
-17 PUSH
-5
-128 SEND
-6
-2
-128 SEND
-7
-1
-16 PUSHSELF
-16 PUSHSELF
-64 GETSLOT
-0
-128 SEND
-1
-0
-17 PUSH
-8
-128 SEND
-3
-1
-17 PUSH
-5
-17 PUSH
-4
-128 SEND
-6
-2
-128 SEND
-7
-1
-16 PUSHSELF
-16 PUSHSELF
-64 GETSLOT
-0
-128 SEND
-1
-0
-17 PUSH
-9
-128 SEND
-3
-1
-17 PUSH
-5
-17 PUSH
-4
-128 SEND
-6
-2
-128 SEND
-7
-1
-16 PUSHSELF
-16 PUSHSELF
-64 GETSLOT
-0
-128 SEND
-1
-0
-17 PUSH
-10
-128 SEND
-3
-1
-17 PUSH
-5
-17 PUSH
-11
-128 SEND
-6
-2
-128 SEND
-7
-1
-20 PUSHNIL
-144 RET
-
View
18 examples/vm.tvm
@@ -1,18 +0,0 @@
-_main
-:3:12
-"VM
-"primitives
-"puts
-16 PUSHSELF
-16 PUSHSELF
-64 GETSLOT
-0
-128 SEND
-1
-0
-128 SEND
-2
-1
-20 PUSHNIL
-144 RET
-
View
208 kernel/prelude.tvm
@@ -1,11 +1,50 @@
_main
-:5:30
+:21:218
"VM
+"types
+"object
+"[]
"primitives
+"clone
"print
-"[]
"puts
+"integer
+"integer_+
+"+
+"integer_-
+"-
+"integer_/
+"/
+"integer_*
+"*
+"vector
+"vector_[]
+"vector_to_map
+"to_map
16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
+2
+128 SEND
+3
+1
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+4
+0
+17 PUSH
+5
+128 SEND
+3
+1
+65 SETSLOT
+5
16 PUSHSELF
64 GETSLOT
0
@@ -17,22 +56,187 @@ _main
128 SEND
3
1
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+4
+0
+17 PUSH
+6
+128 SEND
+3
+1
65 SETSLOT
+6
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
2
+128 SEND
+3
+1
16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+4
+0
+17 PUSH
+7
+128 SEND
+3
+1
+65 SETSLOT
+7
16 PUSHSELF
64 GETSLOT
0
128 SEND
1
0
17 PUSH
+8
+128 SEND
+3
+1
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
4
+0
+17 PUSH
+9
128 SEND
3
1
65 SETSLOT
+10
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
+8
+128 SEND
+3
+1
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
4
+0
+17 PUSH
+11
+128 SEND
+3
+1
+65 SETSLOT
+12
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
+8
+128 SEND
+3
+1
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+4
+0
+17 PUSH
+13
+128 SEND
+3
+1
+65 SETSLOT
+14
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
+8
+128 SEND
+3
+1
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+4
+0
+17 PUSH
+15
+128 SEND
+3
+1
+65 SETSLOT
+16
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
+17
+128 SEND
+3
+1
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+4
+0
+17 PUSH
+18
+128 SEND
+3
+1
+65 SETSLOT
+3
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
+17
+128 SEND
+3
+1
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+4
+0
+17 PUSH
+19
+128 SEND
+3
+1
+65 SETSLOT
+20
20 PUSHNIL
144 RET
View
16 src/forkix/bootstrap.c
@@ -41,22 +41,35 @@ expose_VM(VALUE lobby)
// VM.primitives map
DArray *primitives = DArray_create(sizeof(VALUE), 10);
+ // Object
DEFPRIM(primitives, "print", Primitive_print);
DEFPRIM(primitives, "puts", Primitive_puts);
DEFPRIM(primitives, "require", Primitive_require);
+ DEFPRIM(primitives, "clone", Primitive_clone);
+
+ // Vector
+ DEFPRIM(primitives, "vector_[]", Primitive_Vector_at);
+ DEFPRIM(primitives, "vector_to_map", Primitive_Vector_to_map);
+
+ // Integer
+ DEFPRIM(primitives, "integer_+", Primitive_Integer_add);
+ DEFPRIM(primitives, "integer_-", Primitive_Integer_sub);
+ DEFPRIM(primitives, "integer_*", Primitive_Integer_mul);
+ DEFPRIM(primitives, "integer_/", Primitive_Integer_div);
Value_set(vm, "primitives", Map_new(primitives));
// VM.types map
DArray *types = DArray_create(sizeof(VALUE), 10);
+ DEFVALUE(types, "object", Object_bp);
DEFVALUE(types, "integer", Integer_bp);
DEFVALUE(types, "string", String_bp);
DEFVALUE(types, "vector", Vector_bp);
DEFVALUE(types, "map", Map_bp);
DEFVALUE(types, "closure", Closure_bp);
- Value_set(vm, "types", Map_new(primitives));
+ Value_set(vm, "types", Map_new(types));
}
void
@@ -76,4 +89,5 @@ State_bootstrap(STATE)
debug("[BOOTSTRAP] Loading %s...", bdata(path));
Primitive_require(state, String_new(bdata(path)), NULL, NULL);
}
+ debug("[BOOTSTRAP] Done!");
}
View
3 src/forkix/gc.c
@@ -5,7 +5,10 @@ VALUE
gc_alloc(size_t size)
{
VALUE val = calloc(1, size);
+ check(val, "Out of memory.");
return val;
+error:
+ return NULL;
}
void
View
14 src/forkix/primitives.c
@@ -12,6 +12,13 @@
*/
VALUE
+Primitive_clone(STATE, void *a, void *_, void *__)
+{
+ VALUE obj = (VALUE)a;
+ return Value_from_prototype(obj->type, obj);
+}
+
+VALUE
Primitive_print(STATE, void *_, void *b, void *__)
{
Value_print((VALUE)b);
@@ -149,13 +156,15 @@ Primitive_Map_get(STATE, void *a, void *b, void *_)
VALUE map = (VALUE)a;
VALUE key = (VALUE)b;
- CHECK_TYPE(map, MapType);
CHECK_TYPE(key, StringType);
VALUE result = Value_get(map, VAL2STR(key));
- if(!result) result = NilObject;
+ check(result, "No member named %s.", VAL2STR(key));
return result;
+
+error:
+ return NULL;
}
VALUE
@@ -165,7 +174,6 @@ Primitive_Map_set(STATE, void *a, void *b, void *c)
VALUE key = (VALUE)b;
VALUE value = (VALUE)c;
- CHECK_TYPE(map, MapType);
CHECK_TYPE(key, StringType);
CHECK_PRESENCE(value);
View
1 src/forkix/primitives.h
@@ -10,6 +10,7 @@
#define PRIMITIVE_ARGS STATE, void*, void*, void*
// Generic primitive methods
+VALUE Primitive_clone(PRIMITIVE_ARGS);
VALUE Primitive_print(PRIMITIVE_ARGS);
VALUE Primitive_puts(PRIMITIVE_ARGS);
VALUE Primitive_require(PRIMITIVE_ARGS);
View
24 src/forkix/runtime.c
@@ -1,6 +1,10 @@
#include <forkix/runtime.h>
+#include <forkix/value.h>
+#include <forkix/function.h>
+#include <forkix/primitives.h>
// Blueprints
+VALUE Object_bp = NULL;
VALUE Integer_bp = NULL;
VALUE String_bp = NULL;
VALUE Vector_bp = NULL;
@@ -12,12 +16,20 @@ VALUE TrueObject = NULL;
VALUE FalseObject = NULL;
VALUE NilObject = NULL;
+#define DEFNATIVE(V, N, F) Value_set((V), (N), Closure_new(Function_native_new((F))))
+
void Runtime_init() {
- Integer_bp = Value_new(ObjectType);
- String_bp = Value_new(ObjectType);
- Vector_bp = Value_new(ObjectType);
- Map_bp = Value_new(ObjectType);
- Closure_bp = Value_new(ObjectType);
+ Object_bp = Value_new(ObjectType);
+
+ // These primitives cannot go in the prelude because they are used there.
+ DEFNATIVE(Object_bp, "[]", Primitive_Map_get);
+ DEFNATIVE(Object_bp, "[]=", Primitive_Map_set);
+
+ Integer_bp = Value_from_prototype(IntegerType, Object_bp);
+ String_bp = Value_from_prototype(StringType, Object_bp);
+ Vector_bp = Value_from_prototype(VectorType, Object_bp);
+ Map_bp = Value_from_prototype(MapType, Object_bp);
+ Closure_bp = Value_from_prototype(ClosureType, Object_bp);
// Init extern constants
TrueObject = Value_new(TrueType);
@@ -31,11 +43,13 @@ void Runtime_destroy() {
Value_destroy(Vector_bp);
Value_destroy(Map_bp);
Value_destroy(Closure_bp);
+ Value_destroy(Object_bp);
Integer_bp = NULL;
String_bp = NULL;
Vector_bp = NULL;
Map_bp = NULL;
Closure_bp = NULL;
+ Object_bp = NULL;
Value_destroy(TrueObject);
TrueObject = NULL;
View
1 src/forkix/runtime.h
@@ -3,6 +3,7 @@
#include <forkix/value.h>
+extern VALUE Object_bp;
extern VALUE Integer_bp;
extern VALUE String_bp;
extern VALUE Vector_bp;
View
109 src/forkix/value.c
@@ -5,6 +5,13 @@
#include <forkix/primitives.h>
#include <forkix/gc.h>
+VALUE Object_bp;
+VALUE Integer_bp;
+VALUE String_bp;
+VALUE Vector_bp;
+VALUE Map_bp;
+VALUE Closure_bp;
+
VALUE NilObject;
VALUE
@@ -13,59 +20,108 @@ Value_new(ValueType type)
VALUE val = gc_alloc(sizeof(val_t));
val->type = type;
val->table = Hashmap_create(NULL, NULL);
+ val->prototype = Object_bp;
return val;
};
+VALUE
+Value_from_prototype(ValueType type, VALUE prototype)
+{
+ VALUE val = Value_new(type);
+ val->prototype = prototype;
+ return val;
+}
+
void
Value_destroy(VALUE o)
{
gc_dealloc(o);
}
-void
-Value_print(VALUE o)
+static inline int
+Hashmap_print_cb(HashmapNode *node) {
+ char *name = bdata((bstring)(node->key));
+ VALUE obj = (VALUE)(node->data);
+ Value_print(String_new(name));
+ printf(" => ");
+ Value_print(obj);
+ printf(", ");
+ return 0;
+}
+
+static inline void
+__Value_print(VALUE o)
{
+ check(o, "Cannot print NULL value");
+
+ // Blueprints
+ if(o == Object_bp) { printf("Object"); return;
+ } else if (o == Integer_bp) { printf("Integer"); return;
+ } else if (o == String_bp) { printf("String"); return;
+ } else if (o == Closure_bp) { printf("Closure"); return;
+ } else if (o == Vector_bp) { printf("Vector"); return;
+ } else if (o == Map_bp) { printf("Map"); return;
+ }
+
switch(o->type) {
case IntegerType: {
- printf("%i\n", VAL2INT(o));
+ printf("%i", VAL2INT(o));
break;
}
case StringType: {
- printf("\"%s\"\n", VAL2STR(o));
+ printf("\"%s\"", VAL2STR(o));
break;
}
case TrueType: {
- printf("true\n");
+ printf("true");
break;
}
case FalseType: {
- printf("false\n");
+ printf("false");
break;
}
case NilType: {
- printf("nil\n");
+ printf("nil");
break;
}
case ClosureType: {
- printf("#<Closure %p>\n", o);
+ printf("#<Closure %p>", o);
break;
}
case VectorType: {
- printf("#<Vector %p>\n", o);
+ printf("#<Vector %p>", o);
break;
}
case MapType: {
- printf("#<Map %p>\n", o);
+ printf("{");
+ Hashmap_traverse(o->table, Hashmap_print_cb);
+ printf("}");
break;
}
default: {
- printf("#<Object %p>\n", o);
+ printf("#<Object %p ", o);
+ printf("{");
+ Hashmap_traverse(o->table, Hashmap_print_cb);
+ printf("}>");
break;
}
}
+error:
+ return;
}
-#define DEFNATIVE(V, N, F) Value_set((V), (N), Closure_new(Function_native_new((F))))
+void
+Value_print(VALUE o)
+{
+ __Value_print(o);
+
+ return; // for now
+ if(o->prototype) {
+ printf(" (");
+ __Value_print(o->prototype);
+ printf(")");
+ }
+}
VALUE
Lobby_new()
@@ -77,49 +133,41 @@ Lobby_new()
VALUE
Integer_new(int num)
{
- VALUE val = Value_new(IntegerType);
+ VALUE val = Value_from_prototype(IntegerType, Integer_bp);
val->data.as_int = num;
- DEFNATIVE(val, "+", Primitive_Integer_add);
- DEFNATIVE(val, "-", Primitive_Integer_sub);
- DEFNATIVE(val, "*", Primitive_Integer_mul);
- DEFNATIVE(val, "/", Primitive_Integer_div);
-
return val;
}
VALUE
String_new(char* value)
{
- VALUE val = Value_new(StringType);
+ VALUE val = Value_from_prototype(StringType, String_bp);
val->data.as_str = value;
return val;
}
VALUE
Closure_new(Function *fn)
{
- VALUE val = Value_new(ClosureType);
+ VALUE val = Value_from_prototype(ClosureType, Closure_bp);
val->data.as_data = fn;
return val;
}
VALUE
Vector_new(DArray *array)
{
- VALUE val = Value_new(VectorType);
+ VALUE val = Value_from_prototype(VectorType, Vector_bp);
val->data.as_data = array;
- DEFNATIVE(val, "[]", Primitive_Vector_at);
- DEFNATIVE(val, "to_map", Primitive_Vector_to_map);
-
return val;
}
VALUE
Map_new(DArray *array)
{
- VALUE val = Value_new(MapType);
+ VALUE val = Value_from_prototype(MapType, Map_bp);
int count = DArray_count(array);
assert(count % 2 == 0 && "Map element count must be even.");
@@ -136,9 +184,6 @@ Map_new(DArray *array)
Hashmap_set(hash, bfromcstr(VAL2STR(key)), value);
}
- DEFNATIVE(val, "[]", Primitive_Map_get);
- DEFNATIVE(val, "[]=", Primitive_Map_set);
-
return val;
};
@@ -153,6 +198,12 @@ Value_set(VALUE receiver, char *key, VALUE value)
VALUE
Value_get(VALUE receiver, char *key)
{
- return (VALUE)Hashmap_get(receiver->table, bfromcstr(key));
+ VALUE result = (VALUE)Hashmap_get(receiver->table, bfromcstr(key));
+
+ if(!result && receiver->prototype) {
+ result = Value_get(receiver->prototype, key);
+ }
+
+ return result;
}
View
2 src/forkix/value.h
@@ -15,12 +15,14 @@ struct val_s {
void *as_data;
} data;
Hashmap *table;
+ struct val_s *prototype;
};
typedef struct val_s val_t;
#define VALUE val_t*
VALUE Value_new(ValueType);
+VALUE Value_from_prototype(ValueType, VALUE);
void Value_destroy(VALUE);
void Value_print(VALUE);
View
52 src/forkix/vm.c
@@ -35,14 +35,16 @@ static inline void dump(Stack* stack)
#define LOCAL(A) (VALUE)DArray_at(CURR_FRAME->locals, (A))
#define LOCALSET(A, B) DArray_set(CURR_FRAME->locals, (A), (B))
-void VM_start(BytecodeFile *file)
+void VM_start(bstring filename)
{
+ Runtime_init();
+
+ BytecodeFile *file = BytecodeFile_new(filename);
+
STATE = State_new(file->functions);
VALUE lobby = Lobby_new(); // toplevel object
state->lobby = lobby;
-
- Runtime_init();
CallFrame *top_frame = CallFrame_new(lobby, STATE_FN("main"), NULL);
Stack_push(FRAMES, top_frame);
@@ -129,10 +131,11 @@ VALUE VM_run(STATE)
VALUE receiver = Stack_pop(STACK);
VALUE slot = LITERAL(*ip);
+ check(receiver->type != NilType, "Tried to get a slot from nil.");
check(slot->type == StringType, "Slot name must be a String.");
VALUE value = Value_get(receiver, VAL2STR(slot));
- check(value, "Undefined slot %s.", VAL2STR(slot));
+ check(value, "Undefined slot %s on object type %i.", VAL2STR(slot), receiver->type);
Stack_push(STACK, value);
break;
@@ -144,6 +147,7 @@ VALUE VM_run(STATE)
VALUE receiver = Stack_pop(STACK);
VALUE slot = LITERAL(*ip);
+ check(receiver->type != NilType, "Tried to set a slot on nil.");
check(slot->type == StringType, "Slot name must be a String.");
Value_set(receiver, VAL2STR(slot), value);
@@ -196,27 +200,51 @@ VALUE VM_run(STATE)
if(receiver->type == ClosureType &&
strcmp(VAL2STR(name), "apply") == 0) {
- DArray *apply_locals = DArray_create(sizeof(VALUE), op2 + 1);
- int argc = op2;
- VALUE rcv = DArray_at(locals, 0);
- for(int i=0; i < argc; i++) {
- DArray_push(apply_locals, DArray_at(locals, i+1));
- }
state->ret = ip; // save where we want to return
- ip = Function_call(state, VAL2FN(receiver), rcv, apply_locals);
+ ip = Function_call(state, VAL2FN(receiver), CURR_FRAME->self, locals);
ip--;
break;
}
+ /* printf("Trying to get slot %s.", VAL2STR(name)); */
VALUE closure = Value_get(receiver, VAL2STR(name));
- check(closure, "Undefined slot %s.", VAL2STR(name));
+ /* printf("Tried to get slot %s.", VAL2STR(name)); */
+ check(closure, "Undefined slot %s on object type %i.", VAL2STR(name), receiver->type);
if (op2 == 0 && closure->type != ClosureType && closure != NilObject) {
// GETSLOT
Stack_push(STACK, closure);
break;
}
+ /* if(op2 == 1 && strcmp(VAL2STR(name), "[]") == 0) { // getslot */
+ /* VALUE key = (VALUE)DArray_at(locals, 0); */
+ /* Stack_push(STACK, Value_get(receiver, VAL2STR(key))); */
+ /* break; */
+ /* } */
+
+ /* if(op2 == 2 && strcmp(VAL2STR(name), "[]=") == 0) { // setslot */
+ /* VALUE key = (VALUE)DArray_at(locals, 0); */
+ /* VALUE value = (VALUE)DArray_at(locals, 1); */
+ /* Value_set(receiver, VAL2STR(key), value); */
+ /* Stack_push(STACK, value); */
+ /* break; */
+ /* } */
+
+ if(closure->type == StringType) {
+ printf("BUG\n");
+
+ assert(receiver == Integer_bp && "Not the droids we're looking for");
+ VALUE cls = Value_get(receiver, "[]");
+ printf("SEND: Integer#[]\n\t");
+ Value_print(cls);
+ printf("\n");
+
+ printf("Calling %s on ", VAL2STR(name));
+ Value_print(receiver);
+ printf(". The closure is a string: %s.\n", VAL2STR(closure));
+ }
+
state->ret = ip; // save where we want to return
ip = Function_call(state, VAL2FN(closure), receiver, locals);
ip--; // because we increment after each while cycle
View
2 src/forkix/vm.h
@@ -7,6 +7,6 @@
#include <forkix/input_reader.h>
VALUE VM_run(STATE);
-void VM_start(BytecodeFile *file);
+void VM_start(bstring filename);
#endif
View
5 tests/value_tests.c
@@ -72,8 +72,9 @@ char *test_destroy()
char *test_get()
{
VALUE obj = Integer_new(123);
- VALUE closure = Value_get(obj, "+");
- mu_assert(closure->type == ClosureType, "failed getting closure");
+ Value_set(obj, "foo", Integer_new(99));
+ VALUE number = Value_get(obj, "foo");
+ mu_assert(number->type == IntegerType, "failed getting member of integer");
return NULL;
}
View
2 tests/vm_tests.c
@@ -324,7 +324,7 @@ char *test_send_apply()
DEFN(
"echo",
- PUSHSELF,
+ PUSHLOCAL, 0,
RET
);

0 comments on commit 2aead05

Please sign in to comment.
Something went wrong with that request. Please try again.