Permalink
Browse files

Iteratorssss

  • Loading branch information...
1 parent d4bcee5 commit b3491f8e10855ea8bf1ba14768ec49a873eb2971 @txus committed Dec 31, 2012
View
@@ -1,8 +1,7 @@
-Cn=clang
+CC=clang
CFLAGS=-g -std=c11 -O3 -Wall -Werror -Isrc -DNDEBUG $(OPTFLAGS)
LIBS=$(OPTLIBS)
PREFIX?=/usr/local
-VPATH=vendor
SOURCES=$(wildcard src/**/*.c src/*.c)
OBJECTS=$(patsubst %.c,%.o,$(SOURCES))
@@ -32,7 +31,7 @@ examples: rubinius
rm -rf `find examples -name "*.tvm" -print`
cd compiler && rake examples
-$(TARGET): CFLAGS += -fPIC $(LIBS)
+$(TARGET): CFLAGS += -fPIC -fblocks $(LIBS)
$(TARGET): build $(OBJECTS)
ar rcs $@ $(OBJECTS)
ranlib $@
@@ -0,0 +1,6 @@
+default_greeting = 'hello'
+greeter = {}
+greeter.greet = -> name {
+ puts default_greeting + ' ' + name + '!'
+}
+greeter.greet('world')
@@ -1,2 +1,7 @@
-a = ['hello world',2,3]
-puts a
+symbol = '* '
+greetings = ['hello', 'world', 'to', 'everyone!']
+cls = -> greeting {
+ puts symbol + greeting
+}
+greetings.each(cls)
+puts 'done!'
@@ -7,5 +7,9 @@
VM.types[:number][:/] = VM.primitives[:'number_/']
VM.types[:number][:*] = VM.primitives[:'number_*']
-VM.types[:vector][:[]] = VM.primitives[:'vector_[]']
-VM.types[:vector][:to_map] = VM.primitives[:vector_to_map]
+VM.types[:string][:+] = VM.primitives[:'string_+']
+
+VM.types[:vector][:[]] = VM.primitives[:'vector_[]']
+VM.types[:vector].to_map = VM.primitives[:vector_to_map]
+VM.types[:vector].each = VM.primitives[:vector_each]
+VM.types[:vector].each_with_index = VM.primitives[:vector_each_with_index]
@@ -165,6 +165,24 @@ def compiles_block(code, &block)
_send 0, 1
end
end
+
+ it 'work with iterators' do
+ code = "[1,2,3].each(-> num { puts num })"
+ compiles(code) do
+ _push 0
+ _push 1
+ _push 2
+ _makevec 3
+ _defn 3
+ _send 4, 1
+ end
+
+ compiles_block(code) do
+ _pushself
+ _pushlocal 0
+ _send 0, 1
+ end
+ end
end
describe 'vectors' do
View
@@ -0,0 +1,63 @@
+_main
+:5:26
+"hello
+"to_map
+"block_524
+"greet
+"world
+17 PUSH
+0
+33 SETLOCAL
+0
+68 MAKEVEC
+0
+128 SEND
+1
+0
+33 SETLOCAL
+1
+32 PUSHLOCAL
+1
+67 DEFN
+2
+65 SETSLOT
+3
+32 PUSHLOCAL
+1
+17 PUSH
+4
+128 SEND
+3
+1
+20 PUSHNIL
+144 RET
+_block_524
+:4:23
+"
+"+
+"!
+"puts
+16 PUSHSELF
+34 PUSHLOCALDEPTH
+1
+0
+17 PUSH
+0
+128 SEND
+1
+1
+32 PUSHLOCAL
+0
+128 SEND
+1
+1
+17 PUSH
+2
+128 SEND
+1
+1
+128 SEND
+3
+1
+144 RET
+
View
@@ -1,25 +1,64 @@
_main
-:4:18
-3
-2
-"hello world
+:9:35
+"*
+"everyone!
+"to
+"world
+"hello
+"block_524
+"each
+"done!
"puts
17 PUSH
0
+33 SETLOCAL
+0
17 PUSH
1
17 PUSH
2
-68 MAKEVEC
+17 PUSH
3
+17 PUSH
+4
+68 MAKEVEC
+4
33 SETLOCAL
-0
+1
+67 DEFN
+5
+33 SETLOCAL
+2
+32 PUSHLOCAL
+1
+32 PUSHLOCAL
+2
+128 SEND
+6
+1
+16 PUSHSELF
+17 PUSH
+7
+128 SEND
+8
+1
+20 PUSHNIL
+144 RET
+_block_524
+:2:13
+"+
+"puts
16 PUSHSELF
+34 PUSHLOCALDEPTH
+1
+0
32 PUSHLOCAL
0
128 SEND
-3
+0
+1
+128 SEND
+1
1
-20 PUSHNIL
144 RET
View
@@ -1,5 +1,5 @@
_main
-:21:218
+:27:290
"VM
"types
"object
@@ -17,10 +17,16 @@ _main
"/
"number_*
"*
+"string
+"string_+
"vector
"vector_[]
"vector_to_map
"to_map
+"vector_each
+"each
+"vector_each_with_index
+"each_with_index
16 PUSHSELF
64 GETSLOT
0
@@ -212,6 +218,30 @@ _main
3
1
65 SETSLOT
+10
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
+19
+128 SEND
+3
+1
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+4
+0
+17 PUSH
+20
+128 SEND
+3
+1
+65 SETSLOT
3
16 PUSHSELF
64 GETSLOT
@@ -220,7 +250,31 @@ _main
1
0
17 PUSH
-17
+19
+128 SEND
+3
+1
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+4
+0
+17 PUSH
+21
+128 SEND
+3
+1
+65 SETSLOT
+22
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
+19
128 SEND
3
1
@@ -231,12 +285,36 @@ _main
4
0
17 PUSH
+23
+128 SEND
+3
+1
+65 SETSLOT
+24
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
19
128 SEND
3
1
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+4
+0
+17 PUSH
+25
+128 SEND
+3
+1
65 SETSLOT
-20
+26
20 PUSHNIL
144 RET
View
@@ -50,13 +50,18 @@ expose_VM(VALUE lobby)
// Vector
DEFPRIM(primitives, "vector_[]", Primitive_Vector_at);
DEFPRIM(primitives, "vector_to_map", Primitive_Vector_to_map);
+ DEFPRIM(primitives, "vector_each", Primitive_Vector_each);
+ DEFPRIM(primitives, "vector_each_with_index", Primitive_Vector_each_with_index);
// Number
DEFPRIM(primitives, "number_+", Primitive_Number_add);
DEFPRIM(primitives, "number_-", Primitive_Number_sub);
DEFPRIM(primitives, "number_*", Primitive_Number_mul);
DEFPRIM(primitives, "number_/", Primitive_Number_div);
+ // String
+ DEFPRIM(primitives, "string_+", Primitive_String_concat);
+
Value_set(vm, "primitives", Map_new(primitives));
// VM.types map
View
@@ -3,6 +3,7 @@
#include <terror/stack.h>
#include <terror/state.h>
#include <terror/call_frame.h>
+#include <terror/vm.h>
Function*
Function_new(int *code, DArray *literals)
@@ -45,6 +46,7 @@ Function_call(
DArray *locals) // the arguments to the function call
{
int *ret = state->ret;
+ if(!receiver) receiver = CURR_FRAME->self;
// Native function dispatch
if(fn->c_fn) {
@@ -66,3 +68,11 @@ Function_call(
return new_frame->fn->code;
}
+
+VALUE
+Closure_invoke(STATE, VALUE closure, VALUE receiver, DArray *args)
+{
+ state->ret = NULL; // don't return anywhere
+ Function_call(state, VAL2FN(closure), receiver, args);
+ return VM_run(state);
+}
View
@@ -22,5 +22,6 @@ Function* Function_native_new(native_fn);
struct val_s* Function_native_call(struct state_s*, Function*, struct val_s*, DArray*);
int* Function_call(struct state_s*, Function*, struct val_s*, DArray*);
+struct val_s* Closure_invoke(struct state_s*, struct val_s* closure, struct val_s* receiver, DArray *args);
#endif
Oops, something went wrong.

0 comments on commit b3491f8

Please sign in to comment.