Permalink
Browse files

Compile vectors

  • Loading branch information...
1 parent ecfee11 commit 1a3f8c30e9956f9c4f31b0389d762bdb0e799514 @txus committed Dec 29, 2012
@@ -0,0 +1,2 @@
+a = ['hello world',2,3]
+puts a[0]
@@ -115,6 +115,11 @@ def defn(name)
_defn literal(name)
end
+ def makevec(count)
+ @ip += 1
+ _makevec count
+ end
+
def ret
@ip += 1
_ret
@@ -128,6 +128,7 @@ def emit(instruction)
op :pop, 0x42, 0
op :defn, 0x43, 1
+ op :makevec, 0x44, 1
op :send, 0x80, 2
op :ret, 0x90, 0
@@ -124,19 +124,16 @@ def attribute_assignment(node, parent)
g.setslot attribute_name
end
- # def array_literal(node, parent)
- # first = nil
- # node.body.each do |element|
- # slot = element.lazy_visit self
- # # Save the register of only the first element
- # first ||= slot
- # end
- # g.makearray first, node.body.count
- # end
+ def array_literal(node, parent)
+ node.body.reverse.each do |element|
+ element.lazy_visit self
+ end
+ g.makevec node.body.count
+ end
- # def empty_array(node, parent)
- # g.makearray 0, 0
- # end
+ def empty_array(node, parent)
+ g.makevec 0
+ end
# def hash_literal(node, parent)
# slf = g.loadself
@@ -100,6 +100,12 @@ module Terror
end
end
+ describe '#defn' do
+ it 'defines a closure' do
+ @g.defn :my_closure
+ end
+ end
+
describe '#send' do
it 'sends a message to an object' do
@g.send_message(:foo, 3)
@@ -23,7 +23,7 @@ module Terror
end
describe 'instructions with 1 operand' do
- %w(push pushlocal setlocal jmp jif jit getslot setslot).each do |instruction|
+ %w(push pushlocal setlocal jmp jif jit getslot setslot makevec).each do |instruction|
describe instruction do
it "#{instruction} is encoded correctly" do
inst = @g.__send__ "_#{instruction}", 2
@@ -162,5 +162,16 @@ def compiles(code, &block)
end
end
end
+
+ describe 'vectors' do
+ it 'are compiled' do
+ compiles("a = [false,true]") do
+ _pushtrue
+ _pushfalse
+ _makevec 2
+ _setlocal 0
+ end
+ end
+ end
end
end
View
@@ -0,0 +1,32 @@
+_main
+:6:23
+3
+2
+"hello world
+0
+"[]
+"puts
+17 PUSH
+0
+17 PUSH
+1
+17 PUSH
+2
+68 MAKEVEC
+3
+33 SETLOCAL
+0
+16 PUSHSELF
+32 PUSHLOCAL
+0
+17 PUSH
+3
+128 SEND
+4
+1
+128 SEND
+5
+1
+20 PUSHNIL
+144 RET
+

0 comments on commit 1a3f8c3

Please sign in to comment.