starting with own node tests
This commit is contained in:
parent
c411ac5df8
commit
2b1a56b4fe
@ -45,7 +45,7 @@ module Vm
|
||||
((name.as(:param) >> (comma >> name.as(:param)).repeat(0)).maybe).as(:params) >>
|
||||
rparen
|
||||
}
|
||||
|
||||
rule(:root) { func.repeat(0) >> expression }
|
||||
root :args
|
||||
# rule(:root) { func.repeat(0) >> expression }
|
||||
end
|
||||
end
|
||||
|
@ -1,98 +1,33 @@
|
||||
require_relative "helper"
|
||||
require 'minitest/spec'
|
||||
|
||||
include Vm
|
||||
|
||||
class FakeBuilder
|
||||
attr_reader :result
|
||||
#testing that parsing strings that we know to be correct returns the nodes we expect
|
||||
# in a way the combination of test_parser and test_transform
|
||||
|
||||
Asm::InstructionTools::REGISTERS.each do |reg , number|
|
||||
define_method(reg) { Asm::Register.new(reg , number) }
|
||||
class NodesCase < MiniTest::Test
|
||||
|
||||
def setup
|
||||
@parser = Vm::Parser.new
|
||||
@transform = Vm::Transform.new
|
||||
end
|
||||
|
||||
def initialize
|
||||
@result = ''
|
||||
def parse string
|
||||
syntax = @parser.parse(string)
|
||||
tree = @transform.apply(syntax)
|
||||
tree
|
||||
end
|
||||
|
||||
def class_builder
|
||||
'example'
|
||||
def test_number
|
||||
tree = parse "42"
|
||||
assert tree.is_a? Vm::NumberExpression
|
||||
assert_equal 42 , tree.value
|
||||
end
|
||||
|
||||
def int
|
||||
'int'
|
||||
end
|
||||
|
||||
def method_missing(name, *args, &block)
|
||||
@result += ([name] + args.flatten).join(', ').sub(',', '')
|
||||
@result += "\n"
|
||||
block.call(self) if name.to_s == 'public_static_method'
|
||||
def test_args
|
||||
tree = parse "( 42 )"
|
||||
assert tree.is_a? Hash
|
||||
assert tree[:args].is_a? Vm::NumberExpression
|
||||
assert_equal 42 , tree[:args].value
|
||||
end
|
||||
end
|
||||
|
||||
describe 'Nodes' do
|
||||
before do
|
||||
@context = Hash.new
|
||||
@builder = FakeBuilder.new
|
||||
end
|
||||
|
||||
it 'emits a number' do
|
||||
input = Vm::NumberExpression.new 42
|
||||
expected = <<HERE
|
||||
mov r0, 42
|
||||
HERE
|
||||
input.eval @context, @builder
|
||||
|
||||
@builder.result.must_equal expected
|
||||
end
|
||||
|
||||
it 'emits a function call' do
|
||||
@context[:params] = ['foo']
|
||||
|
||||
input = Vm::FuncallExpression.new 'baz', [Vm::NumberExpression.new(42),
|
||||
Vm::NameExpression.new('foo')]
|
||||
expected = <<HERE
|
||||
mov r0, 42
|
||||
iload 0
|
||||
invokestatic example, baz, int, int, int
|
||||
HERE
|
||||
|
||||
input.eval @context, @builder
|
||||
|
||||
@builder.result.must_equal expected
|
||||
end
|
||||
|
||||
it 'emits a conditional' do
|
||||
input = Vm::ConditionalExpression.new \
|
||||
Vm::NumberExpression.new(0),
|
||||
Vm::NumberExpression.new(42),
|
||||
Vm::NumberExpression.new(667)
|
||||
expected = <<HERE
|
||||
mov r0, 0
|
||||
ifeq else
|
||||
mov r0, 42
|
||||
goto endif
|
||||
label else
|
||||
mov r0, 667
|
||||
label endif
|
||||
HERE
|
||||
|
||||
input.eval @context, @builder
|
||||
@builder.result.must_equal expected
|
||||
end
|
||||
|
||||
it 'emits a function definition' do
|
||||
input = Vm::FunctionExpression.new \
|
||||
'foo',
|
||||
Vm::NameExpression.new('x'),
|
||||
Vm::NumberExpression.new(5)
|
||||
|
||||
expected = <<HERE
|
||||
public_static_method foo, int, int
|
||||
mov r0, 5
|
||||
ireturn
|
||||
HERE
|
||||
|
||||
input.eval @context, @builder
|
||||
@builder.result.must_equal expected
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user