get thnads tests to work
This commit is contained in:
parent
d90ea3dd26
commit
601dc22b2e
@ -1,3 +1,9 @@
|
|||||||
|
# parslet is assumed to be checked out at the same level as crystal for now
|
||||||
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', ".." , "parslet",'lib'))
|
||||||
|
require 'parslet'
|
||||||
|
|
||||||
require "asm/stack_instruction"
|
require "asm/stack_instruction"
|
||||||
require "asm/arm_assembler"
|
require "asm/arm_assembler"
|
||||||
require "elf/object_writer"
|
require "elf/object_writer"
|
||||||
|
require 'vm/parser'
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ require 'java'
|
|||||||
java_import java.lang.System
|
java_import java.lang.System
|
||||||
java_import java.io.PrintStream
|
java_import java.io.PrintStream
|
||||||
|
|
||||||
module Thnad
|
module Vm
|
||||||
module Builtins
|
module Builtins
|
||||||
def add_builtins
|
def add_builtins
|
||||||
public_static_method 'print', [], int, int do
|
public_static_method 'print', [], int, int do
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
require 'bitescript'
|
require 'bitescript'
|
||||||
require 'thnad/parser'
|
require 'vm/parser'
|
||||||
require 'thnad/transform'
|
require 'vm/transform'
|
||||||
require 'thnad/builtins'
|
require 'vm/builtins'
|
||||||
|
|
||||||
module Thnad
|
module Vm
|
||||||
class Compiler
|
class Compiler
|
||||||
def initialize(filename)
|
def initialize(filename)
|
||||||
@filename = filename
|
@filename = filename
|
||||||
@classname = File.basename(@filename, '.thnad')
|
@classname = File.basename(@filename, '.vm')
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile
|
def compile
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
module Thnad
|
module Vm
|
||||||
class Number < Struct.new :value
|
class Number < Struct.new :value
|
||||||
def eval(context, builder)
|
def eval(context, builder)
|
||||||
builder.ldc value
|
builder.ldc value
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
require 'parslet'
|
|
||||||
|
|
||||||
module Thnad
|
module Vm
|
||||||
class Parser < Parslet::Parser
|
class Parser < Parslet::Parser
|
||||||
rule(:name) { match('[a-z]').repeat(1).as(:name) >> space? }
|
rule(:name) { match('[a-z]').repeat(1).as(:name) >> space? }
|
||||||
rule(:number) { match('[0-9]').repeat(1).as(:number) >> space? }
|
rule(:number) { match('[0-9]').repeat(1).as(:number) >> space? }
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
require 'parslet'
|
require 'parslet'
|
||||||
require 'thnad/nodes'
|
require 'vm/nodes'
|
||||||
|
|
||||||
module Thnad
|
module Vm
|
||||||
class Transform < Parslet::Transform
|
class Transform < Parslet::Transform
|
||||||
rule(:number => simple(:value)) { Number.new(value.to_i) }
|
rule(:number => simple(:value)) { Number.new(value.to_i) }
|
||||||
rule(:name => simple(:name)) { Name.new(name.to_s) }
|
rule(:name => simple(:name)) { Name.new(name.to_s) }
|
||||||
|
@ -3,10 +3,10 @@ $: << File.expand_path(File.dirname(__FILE__))
|
|||||||
|
|
||||||
require 'minitest/autorun'
|
require 'minitest/autorun'
|
||||||
require 'minitest/spec'
|
require 'minitest/spec'
|
||||||
require 'thnad/nodes'
|
require 'vm/nodes'
|
||||||
require 'fake_builder'
|
require 'fake_builder'
|
||||||
|
|
||||||
include Thnad
|
include Vm
|
||||||
|
|
||||||
describe 'Nodes' do
|
describe 'Nodes' do
|
||||||
before do
|
before do
|
||||||
@ -15,7 +15,7 @@ describe 'Nodes' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'emits a number' do
|
it 'emits a number' do
|
||||||
input = Thnad::Number.new 42
|
input = Vm::Number.new 42
|
||||||
expected = <<HERE
|
expected = <<HERE
|
||||||
ldc 42
|
ldc 42
|
||||||
HERE
|
HERE
|
||||||
@ -27,8 +27,8 @@ HERE
|
|||||||
it 'emits a function call' do
|
it 'emits a function call' do
|
||||||
@context[:params] = ['foo']
|
@context[:params] = ['foo']
|
||||||
|
|
||||||
input = Thnad::Funcall.new 'baz', [Thnad::Number.new(42),
|
input = Vm::Funcall.new 'baz', [Vm::Number.new(42),
|
||||||
Thnad::Name.new('foo')]
|
Vm::Name.new('foo')]
|
||||||
expected = <<HERE
|
expected = <<HERE
|
||||||
ldc 42
|
ldc 42
|
||||||
iload 0
|
iload 0
|
||||||
@ -41,10 +41,10 @@ HERE
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'emits a conditional' do
|
it 'emits a conditional' do
|
||||||
input = Thnad::Conditional.new \
|
input = Vm::Conditional.new \
|
||||||
Thnad::Number.new(0),
|
Vm::Number.new(0),
|
||||||
Thnad::Number.new(42),
|
Vm::Number.new(42),
|
||||||
Thnad::Number.new(667)
|
Vm::Number.new(667)
|
||||||
expected = <<HERE
|
expected = <<HERE
|
||||||
ldc 0
|
ldc 0
|
||||||
ifeq else
|
ifeq else
|
||||||
@ -60,10 +60,10 @@ HERE
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'emits a function definition' do
|
it 'emits a function definition' do
|
||||||
input = Thnad::Function.new \
|
input = Vm::Function.new \
|
||||||
'foo',
|
'foo',
|
||||||
Thnad::Name.new('x'),
|
Vm::Name.new('x'),
|
||||||
Thnad::Number.new(5)
|
Vm::Number.new(5)
|
||||||
|
|
||||||
expected = <<HERE
|
expected = <<HERE
|
||||||
public_static_method foo, int, int
|
public_static_method foo, int, int
|
@ -1,14 +1,11 @@
|
|||||||
$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
require_relative 'helper'
|
||||||
|
|
||||||
require 'minitest/autorun'
|
|
||||||
require 'minitest/spec'
|
require 'minitest/spec'
|
||||||
require 'thnad/parser'
|
|
||||||
|
|
||||||
include Thnad
|
include Vm
|
||||||
|
|
||||||
describe Parser do
|
describe Parser do
|
||||||
before do
|
before do
|
||||||
@parser = Thnad::Parser.new
|
@parser = Vm::Parser.new
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'reads a number' do
|
it 'reads a number' do
|
@ -1,26 +1,24 @@
|
|||||||
$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
require_relative 'helper'
|
||||||
|
|
||||||
require 'minitest/autorun'
|
|
||||||
require 'minitest/spec'
|
require 'minitest/spec'
|
||||||
require 'thnad/transform'
|
require 'vm/transform'
|
||||||
|
|
||||||
include Thnad
|
include Vm
|
||||||
|
|
||||||
describe Transform do
|
describe Transform do
|
||||||
before do
|
before do
|
||||||
@transform = Thnad::Transform.new
|
@transform = Vm::Transform.new
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'transforms a number' do
|
it 'transforms a number' do
|
||||||
input = {:number => '42'}
|
input = {:number => '42'}
|
||||||
expected = Thnad::Number.new(42)
|
expected = Vm::Number.new(42)
|
||||||
|
|
||||||
@transform.apply(input).must_equal expected
|
@transform.apply(input).must_equal expected
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'transforms a name' do
|
it 'transforms a name' do
|
||||||
input = {:name => 'foo'}
|
input = {:name => 'foo'}
|
||||||
expected = Thnad::Name.new('foo')
|
expected = Vm::Name.new('foo')
|
||||||
|
|
||||||
@transform.apply(input).must_equal expected
|
@transform.apply(input).must_equal expected
|
||||||
end
|
end
|
||||||
@ -28,8 +26,8 @@ describe Transform do
|
|||||||
it 'transforms an argument list' do
|
it 'transforms an argument list' do
|
||||||
input = {:args => [{:arg => {:number => '42'}},
|
input = {:args => [{:arg => {:number => '42'}},
|
||||||
{:arg => {:name => 'foo'}}]}
|
{:arg => {:name => 'foo'}}]}
|
||||||
expected = [Thnad::Number.new(42),
|
expected = [Vm::Number.new(42),
|
||||||
Thnad::Name.new('foo')]
|
Vm::Name.new('foo')]
|
||||||
|
|
||||||
@transform.apply(input).must_equal expected
|
@transform.apply(input).must_equal expected
|
||||||
end
|
end
|
||||||
@ -37,7 +35,7 @@ describe Transform do
|
|||||||
it 'transforms a single-argument function call' do
|
it 'transforms a single-argument function call' do
|
||||||
input = {:funcall => {:name => 'foo'},
|
input = {:funcall => {:name => 'foo'},
|
||||||
:args => [{:arg => {:number => '42'}}]}
|
:args => [{:arg => {:number => '42'}}]}
|
||||||
expected = Thnad::Funcall.new 'foo', [Thnad::Number.new(42)]
|
expected = Vm::Funcall.new 'foo', [Vm::Number.new(42)]
|
||||||
|
|
||||||
@transform.apply(input).must_equal expected
|
@transform.apply(input).must_equal expected
|
||||||
end
|
end
|
||||||
@ -46,8 +44,8 @@ describe Transform do
|
|||||||
input = {:funcall => {:name => 'baz'},
|
input = {:funcall => {:name => 'baz'},
|
||||||
:args => [{:arg => {:number => '42'}},
|
:args => [{:arg => {:number => '42'}},
|
||||||
{:arg => {:name => 'foo'}}]}
|
{:arg => {:name => 'foo'}}]}
|
||||||
expected = Thnad::Funcall.new 'baz', [Thnad::Number.new(42),
|
expected = Vm::Funcall.new 'baz', [Vm::Number.new(42),
|
||||||
Thnad::Name.new('foo')]
|
Vm::Name.new('foo')]
|
||||||
|
|
||||||
@transform.apply(input).must_equal expected
|
@transform.apply(input).must_equal expected
|
||||||
end
|
end
|
||||||
@ -56,10 +54,10 @@ describe Transform do
|
|||||||
input = {:cond => {:number => '0'},
|
input = {:cond => {:number => '0'},
|
||||||
:if_true => {:body => {:number => '42'}},
|
:if_true => {:body => {:number => '42'}},
|
||||||
:if_false => {:body => {:number => '667'}}}
|
:if_false => {:body => {:number => '667'}}}
|
||||||
expected = Thnad::Conditional.new \
|
expected = Vm::Conditional.new \
|
||||||
Thnad::Number.new(0),
|
Vm::Number.new(0),
|
||||||
Thnad::Number.new(42),
|
Vm::Number.new(42),
|
||||||
Thnad::Number.new(667)
|
Vm::Number.new(667)
|
||||||
|
|
||||||
@transform.apply(input).must_equal expected
|
@transform.apply(input).must_equal expected
|
||||||
end
|
end
|
||||||
@ -68,10 +66,10 @@ describe Transform do
|
|||||||
input = {:func => {:name => 'foo'},
|
input = {:func => {:name => 'foo'},
|
||||||
:params => {:param => {:name => 'x'}},
|
:params => {:param => {:name => 'x'}},
|
||||||
:body => {:number => '5'}}
|
:body => {:number => '5'}}
|
||||||
expected = Thnad::Function.new \
|
expected = Vm::Function.new \
|
||||||
'foo',
|
'foo',
|
||||||
[Thnad::Name.new('x')],
|
[Vm::Name.new('x')],
|
||||||
Thnad::Number.new(5)
|
Vm::Number.new(5)
|
||||||
|
|
||||||
@transform.apply(input).must_equal expected
|
@transform.apply(input).must_equal expected
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user