Skip to content

Commit

Permalink
Iteratorssss
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Dec 31, 2012
1 parent d4bcee5 commit b3491f8
Show file tree
Hide file tree
Showing 24 changed files with 562 additions and 41 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -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 $@
Expand Down
6 changes: 6 additions & 0 deletions compiler/examples/hello_world.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
default_greeting = 'hello'
greeter = {}
greeter.greet = -> name {
puts default_greeting + ' ' + name + '!'
}
greeter.greet('world')
9 changes: 7 additions & 2 deletions compiler/examples/vectors.rb
Original file line number Diff line number Diff line change
@@ -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!'
8 changes: 6 additions & 2 deletions compiler/kernel/prelude.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
18 changes: 18 additions & 0 deletions compiler/test/terror/visitor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions examples/hello_world.tvm
Original file line number Diff line number Diff line change
@@ -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

55 changes: 47 additions & 8 deletions examples/vectors.tvm
Original file line number Diff line number Diff line change
@@ -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

84 changes: 81 additions & 3 deletions kernel/prelude.tvm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
_main
:21:218
:27:290
"VM
"types
"object
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

5 changes: 5 additions & 0 deletions src/terror/bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/terror/function.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
1 change: 1 addition & 0 deletions src/terror/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Loading

0 comments on commit b3491f8

Please sign in to comment.