Permalink
Browse files

So far so good

  • Loading branch information...
1 parent 0c20e25 commit 806c92d330856a5211ccae5d458f60677b7c260e @txus committed Jan 11, 2013
View
@@ -205,6 +205,7 @@ That's it! :)
#### Values
* **PUSHSELF**: pushes the current self to the stack.
+* **PUSHLOBBY**: pushes the Lobby (toplevel object) to the stack.
* **PUSH A**: pushes the literal at index `A` to the stack.
* **PUSHTRUE**: pushes the `true` object to the stack.
* **PUSHFALSE**: pushes the `false` object to the stack.
@@ -220,11 +221,11 @@ That's it! :)
#### Branching
-* **JMP A**: Jumps forward as much as `A` instructions.
-* **JIF A**: Jumps forward as much as `A` instructions if the top of the stack
- is falsy (`false` or `nil`).
-* **JIT A**: Jumps forward as much as `A` instructions if the top of the stack
- is truthy (any value other than `false` or `nil`).
+* **JMP A**: Skips as much as `A` instructions.
+* **JIF A**: Skips as much as `A` instructions if the top of the stack is falsy
+ (`false` or `nil`).
+* **JIT A**: Skips as much as `A` instructions if the top of the stack is truthy
+ (any value other than `false` or `nil`).
#### Slots (attributes)
View
@@ -1,5 +1,6 @@
# The alpha prelude wires up all the primitives to the types.
+VM.types[:object].prototype = VM.primitives[:prototype]
VM.types[:object][:or] = VM.primitives[:or]
VM.types[:object][:==] = VM.primitives[:equals]
VM.types[:object][:===] = VM.primitives[:is]
@@ -21,3 +22,10 @@
VM.types[:vector].each_with_index = VM.primitives[:vector_each_with_index]
VM.types[:map].each = VM.primitives[:map_each]
+
+Object = VM.types[:object]
+Number = VM.types[:number]
+String = VM.types[:string]
+Vector = VM.types[:vector]
+Closure = VM.types[:closure]
+Map = VM.types[:map]
View
@@ -5,4 +5,10 @@
new.push fn.apply(element)
})
new
+}
+
+VM.types[:vector].length = -> fn {
+ len = 0
+ each(-> element { len += 1 })
+ len
}
@@ -34,14 +34,14 @@ def encode(name)
instrs = instructions.map(&:encode).flatten
output = "_#{name}\n"
- output << ":%i:%i\n" % [@literals.size, instrs.size]
+ output << ":%i:%i\n" % [@literals.size, @ip]
output << [@literals, instrs].flatten.join("\n")
output
end
def push(value)
- @ip += 1
+ @ip += 2
_push literal(value)
end
@@ -66,20 +66,20 @@ def pushnil
end
def jmp(label)
+ @ip += 2
label.start!
- @ip += 1
_jmp label
end
def jif(label)
+ @ip += 2
label.start!
- @ip += 1
_jif label
end
def jit(label)
+ @ip += 2
label.start!
- @ip += 1
_jit label
end
@@ -88,48 +88,55 @@ def pushself
_pushself
end
- def pushlocal(name)
+ def pushlobby
@ip += 1
+ _pushlobby
+ end
+
+ def pushlocal(name)
l = local(name)
if l.depth > 0
+ @ip += 3
_pushlocaldepth l.depth, l.index
else
+ @ip += 2
_pushlocal l.index
end
end
def setlocal(name)
- @ip += 1
l = local(name)
if l.depth > 0
+ @ip += 3
_setlocaldepth l.depth, l.index
else
+ @ip += 2
_setlocal l.index
end
end
def getslot(name)
- @ip += 1
+ @ip += 2
_getslot literal(name)
end
def setslot(name)
- @ip += 1
+ @ip += 2
_setslot literal(name)
end
def send_message(msg, argc)
- @ip += 1
+ @ip += 3
_send literal(msg), argc
end
def defn(name)
- @ip += 1
+ @ip += 2
_defn literal(name)
end
def makevec(count)
- @ip += 1
+ @ip += 2
_makevec count
end
@@ -115,6 +115,7 @@ def emit(instruction)
op :pushtrue, 0x12, 0
op :pushfalse, 0x13, 0
op :pushnil, 0x14, 0
+ op :pushlobby, 0x15, 0
op :pushlocal, 0x20, 1
op :setlocal, 0x21, 1
@@ -138,12 +138,12 @@ def attribute_assignment(node, parent)
end
def constant_assignment(node, parent)
- receiver_name = :self
+ receiver_name = :lobby
attribute_name = node.constant.name.to_sym
@slots[receiver_name] ||= []
@slots[receiver_name] << attribute_name
- g.pushself
+ g.pushlobby
node.value.lazy_visit self
g.setslot attribute_name
end
@@ -188,7 +188,7 @@ def symbol_literal(node, parent)
end
def constant_access(node, parent)
- g.pushself
+ g.pushlobby
g.getslot node.name
end
View
@@ -0,0 +1,5 @@
+if 2 == 3
+ puts 'equal!'
+else
+ puts 'unequal!'
+end
@@ -1,48 +1,57 @@
-TestCase = {}
-
-TestCase.setup = -> {}
-TestCase.teardown = -> {}
-
-TestCase.assert = {
- equal: -> exp, act, msg {
- if !msg
- msg = "Expected " + act.to_s + " to equal " + exp.to_s
+TestCase = {
+ assert_equal: -> exp, act, msg {
+ unless msg
+ msg = "Expected actual to equal expected."
end
if exp == act
true
else
self.failures.push msg
false
end
- }
-}
+ },
+ setup: -> {},
+ teardown: -> {},
+ run: -> {
+ self.tests = []
+ self.failures = []
+ that = self;
+ self.each(-> k, v {
+ unless (v.prototype === Closure)
+ puts 'executing!'
+ tests.push k
-TestCase.run = -> {
- tests = []
- failures = []
- self.each(-> k, v {
- unless (k == 'setup' || k == 'teardown')
- tests.push k
+ that.setup()
+ result = v.apply()
+ that.teardown()
- setup()
- result = v.apply(assert)
- teardown()
-
- if result
- print "\e[32m.\e[0m"
- else
- failures.push k
- print "\e[31mF\e[0m"
+ if result
+ print "\e[32m.\e[0m"
+ else
+ failures.push k
+ print "\e[31mF\e[0m"
+ end
end
+ })
+
+ puts ""
+ if failures.length > 0
+ failures.each(-> failure {
+ puts "* " + failure
+ })
+ puts ""
end
- })
+ }
}
mycase = TestCase.clone
-mycase.test_foo = -> assert {
- assert.equal 2 == 2
+mycase.test_foo = -> {
+ assert_equal 2, 2
+}
+mycase.test_bar = -> {
+ assert_equal 2, 3
}
-mycase.test_bar = -> assert {
- assert.equal 2 == 3
+mycase.test_baz = -> {
+ assert_equal 2, 3, "Failed man!!!"
}
mycase.run
@@ -18,7 +18,7 @@ module Terror
@g.push(3)
label.set!
- label.offset.must_equal 3
+ label.offset.must_equal 4
end
end
@@ -37,7 +37,7 @@ module Terror
end
describe 'instructions with no operands' do
- %w(noop pushself pushtrue pushfalse pushnil pop ret dump).each do |instruction|
+ %w(noop pushself pushlobby pushtrue pushfalse pushnil pop ret dump).each do |instruction|
describe instruction do
it "#{instruction} is encoded correctly" do
inst = @g.__send__ :"_#{instruction}"
@@ -102,17 +102,17 @@ def compiles_block(code, &block)
it 'compiles if' do
compiles("if 1 then 3; end") do
_push 0
- _jif 3
+ _jif 4
_push 1
- _jmp 2
+ _jmp 1
_pushnil
end
end
it 'compiles if-else' do
compiles("if 1 then 3 else 4 end") do
_push 0 # condition
- _jif 3
+ _jif 4
_push 1 # body
_jmp 2
_push 2 # else body
@@ -133,14 +133,22 @@ def compiles_block(code, &block)
end
describe 'constants' do
- it 'are compiled down to normal identifiers' do
+ it 'are compiled down to normal slots on lobby' do
compiles("Object.clone") do
- _pushself
+ _pushlobby
_getslot 0
_send 1, 0
end
end
+
+ it 'compiles assignments too' do
+ compiles("Object = 3") do
+ _pushlobby
+ _push 0
+ _setslot 1
+ end
+ end
end
describe 'functions' do
View
@@ -1,7 +1,7 @@
_main
:5:25
123
-"block_c12f1b0be80decf2727037d66c2617b2
+"block_9f8765f64c42ec0b2c499597d199b177
"fn
2
"puts
@@ -30,7 +30,7 @@ _main
1
20 PUSHNIL
144 RET
-_block_c12f1b0be80decf2727037d66c2617b2
+_block_9f8765f64c42ec0b2c499597d199b177
:1:9
"+
34 PUSHLOCALDEPTH
View
@@ -2,7 +2,7 @@ _main
:5:26
"hello
"to_map
-"block_1db718c551ada4f2bdcda621f818fc2c
+"block_20915b4a4c9dabb4c467b5361606dfb9
"greet
"world
17 PUSH
@@ -31,7 +31,7 @@ _main
1
20 PUSHNIL
144 RET
-_block_1db718c551ada4f2bdcda621f818fc2c
+_block_20915b4a4c9dabb4c467b5361606dfb9
:4:23
"
"+
Oops, something went wrong.

0 comments on commit 806c92d

Please sign in to comment.