Permalink
Browse files

Map implement natively

  • Loading branch information...
1 parent b3491f8 commit 8b057eac25b6be675284e5f532a37457e479c975 @txus committed Dec 31, 2012
@@ -4,4 +4,8 @@
puts symbol + greeting
}
greetings.each(cls)
-puts 'done!'
+puts 'done!'
+
+puts(greetings.map(-> greeting {
+ greeting + '!!!!'
+}))
@@ -1,3 +1,5 @@
+# The alpha prelude wires up all the primitives to the types.
+
VM.types[:object].clone = VM.primitives[:clone]
VM.types[:object].print = VM.primitives[:print]
VM.types[:object].puts = VM.primitives[:puts]
@@ -10,6 +12,7 @@
VM.types[:string][:+] = VM.primitives[:'string_+']
VM.types[:vector][:[]] = VM.primitives[:'vector_[]']
+VM.types[:vector].push = VM.primitives[:vector_push]
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]
@@ -0,0 +1,8 @@
+# Vector#map
+VM.types[:vector].map = -> fn {
+ new = []
+ each(-> element {
+ new.push fn.apply(element)
+ })
+ new
+}
@@ -29,19 +29,14 @@ def disassemble
end
def encode(name)
- ret
+ ret unless instructions[-1].inspect == "RET"
instrs = instructions.map(&:encode).flatten
output = "_#{name}\n"
output << ":%i:%i\n" % [@literals.size, instrs.size]
- output << @literals.join("\n")
- output << "\n"
- # instructions.each do |i|
- # p i
- # end
- output << instrs.join("\n")
+ output << [@literals, instrs].flatten.join("\n")
output
end
@@ -1,4 +1,5 @@
require 'terror/generator'
+require 'securerandom'
module Terror
class Visitor
@@ -12,7 +13,7 @@ def initialize(parent=nil)
alias_method :g, :generator
def name
- "block_#{hash}"
+ @name ||= "block_#{SecureRandom.hex}"
end
def fixnum_literal(node, parent)
@@ -182,8 +183,9 @@ def constant_access(node, parent)
def finalize(name)
out = g.encode(name)
- fns = @fns.map { |fn| fn.finalize(fn.name) }.join("\n")
- [out, fns].join("\n")
+ fns = @fns.map { |fn| fn.finalize(fn.name) }
+ [out, fns].flatten.join("\n")
end
end
end
+
@@ -1,7 +1,7 @@
_main
:5:25
123
-"block_524
+"block_c12f1b0be80decf2727037d66c2617b2
"fn
2
"puts
@@ -30,7 +30,7 @@ _main
1
20 PUSHNIL
144 RET
-_block_524
+_block_c12f1b0be80decf2727037d66c2617b2
:1:9
"+
34 PUSHLOCALDEPTH
@@ -2,7 +2,7 @@ _main
:5:26
"hello
"to_map
-"block_524
+"block_1db718c551ada4f2bdcda621f818fc2c
"greet
"world
17 PUSH
@@ -31,7 +31,7 @@ _main
1
20 PUSHNIL
144 RET
-_block_524
+_block_1db718c551ada4f2bdcda621f818fc2c
:4:23
"
"+
View
@@ -1,14 +1,16 @@
_main
-:9:35
+:11:46
"*
"everyone!
"to
"world
"hello
-"block_524
+"block_d06d80fd4f6cc4d0a62c4e547b0d82a6
"each
"done!
"puts
+"block_49f68350b370269ba275ec8f2590154e
+"map
17 PUSH
0
33 SETLOCAL
@@ -42,9 +44,20 @@ _main
128 SEND
8
1
+16 PUSHSELF
+32 PUSHLOCAL
+1
+67 DEFN
+9
+128 SEND
+10
+1
+128 SEND
+8
+1
20 PUSHNIL
144 RET
-_block_524
+_block_d06d80fd4f6cc4d0a62c4e547b0d82a6
:2:13
"+
"puts
@@ -61,4 +74,16 @@ _block_524
1
1
144 RET
+_block_49f68350b370269ba275ec8f2590154e
+:2:8
+"!!!!
+"+
+32 PUSHLOCAL
+0
+17 PUSH
+0
+128 SEND
+1
+1
+144 RET
@@ -1,5 +1,5 @@
_main
-:27:290
+:29:314
"VM
"types
"object
@@ -21,6 +21,8 @@ _main
"string_+
"vector
"vector_[]
+"vector_push
+"push
"vector_to_map
"to_map
"vector_each
@@ -315,6 +317,30 @@ _main
1
65 SETSLOT
26
+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
+27
+128 SEND
+3
+1
+65 SETSLOT
+28
20 PUSHNIL
144 RET
View
@@ -0,0 +1,64 @@
+_main
+:6:17
+"VM
+"types
+"vector
+"[]
+"block_3319debf35b52bc1dc4372eb64231136
+"map
+16 PUSHSELF
+64 GETSLOT
+0
+128 SEND
+1
+0
+17 PUSH
+2
+128 SEND
+3
+1
+67 DEFN
+4
+65 SETSLOT
+5
+20 PUSHNIL
+144 RET
+_block_3319debf35b52bc1dc4372eb64231136
+:2:15
+"block_9eb07e8a6f0594c80cada6d95bb425fe
+"each
+68 MAKEVEC
+0
+35 SETLOCALDEPTH
+1
+0
+16 PUSHSELF
+67 DEFN
+0
+128 SEND
+1
+1
+34 PUSHLOCALDEPTH
+1
+0
+144 RET
+_block_9eb07e8a6f0594c80cada6d95bb425fe
+:2:15
+"apply
+"push
+34 PUSHLOCALDEPTH
+2
+0
+34 PUSHLOCALDEPTH
+1
+0
+32 PUSHLOCAL
+0
+128 SEND
+0
+1
+128 SEND
+1
+1
+144 RET
+
@@ -49,6 +49,7 @@ expose_VM(VALUE lobby)
// Vector
DEFPRIM(primitives, "vector_[]", Primitive_Vector_at);
+ DEFPRIM(primitives, "vector_push", Primitive_Vector_push);
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);
@@ -218,6 +218,20 @@ Primitive_Vector_at(STATE, void *a, void *b, void *_)
}
VALUE
+Primitive_Vector_push(STATE, void *a, void *b, void *_)
+{
+ VALUE vector = (VALUE)a;
+ VALUE element = (VALUE)b;
+
+ CHECK_TYPE(vector, VectorType);
+ CHECK_PRESENCE(element);
+
+ Vector_push(vector, element);
+
+ return vector;
+}
+
+VALUE
Primitive_Vector_each(STATE, void *a, void *b, void *_)
{
VALUE vector = (VALUE)a;
@@ -28,6 +28,7 @@ VALUE Primitive_String_concat(PRIMITIVE_ARGS);
// Vector primitive methods
VALUE Primitive_Vector_at(PRIMITIVE_ARGS);
+VALUE Primitive_Vector_push(PRIMITIVE_ARGS);
VALUE Primitive_Vector_to_map(PRIMITIVE_ARGS);
VALUE Primitive_Vector_each(PRIMITIVE_ARGS);
VALUE Primitive_Vector_each_with_index(PRIMITIVE_ARGS);
View
@@ -12,5 +12,6 @@ void Vector_each_with_index(VALUE, Vector_iter_idx);
#define Vector_first(V) (VALUE)(DArray_first(VAL2ARY((V))))
#define Vector_last(V) (VALUE)(DArray_last(VAL2ARY((V))))
#define Vector_count(V) DArray_count(VAL2ARY((V)))
+#define Vector_push(V, E) DArray_push(VAL2ARY((V)), (E))
#endif
View
@@ -283,7 +283,6 @@ VALUE VM_run(STATE)
CallFrame *old_frame = Stack_pop(FRAMES);
ip = old_frame->ret;
- if(ip) debug("Returning to %i", *ip);
if (ip == NULL) return Stack_pop(STACK); // if there's nowhere to return, exit
break;

0 comments on commit 8b057ea

Please sign in to comment.