From 305f2380a982b617f1c03930827433071d9ad6a8 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 24 Apr 2014 17:17:17 +0300 Subject: [PATCH] folded fake builder, started adapting tests and added example file --- lib/vm/nodes.rb | 2 +- test/example.vm | 29 +++++++++++++++++++++++++++++ test/fake_builder.rb | 21 --------------------- test/test_complier.rb | 7 +++++++ test/test_nodes.rb | 35 ++++++++++++++++++++++++++++------- 5 files changed, 65 insertions(+), 29 deletions(-) create mode 100644 test/example.vm delete mode 100644 test/fake_builder.rb create mode 100755 test/test_complier.rb diff --git a/lib/vm/nodes.rb b/lib/vm/nodes.rb index 20231938..500f3aec 100644 --- a/lib/vm/nodes.rb +++ b/lib/vm/nodes.rb @@ -1,7 +1,7 @@ module Vm class Number < Struct.new :value def eval(context, builder) - builder.ldc value + builder.mov "r0" , value end end diff --git a/test/example.vm b/test/example.vm new file mode 100644 index 00000000..d350e57a --- /dev/null +++ b/test/example.vm @@ -0,0 +1,29 @@ +function plus(a, b) { + minus(a, minus(0, b)) +} + +function times(a, b) { + if (eq(b, 0)) { + 0 + } else { + plus(a, times(a, minus(b, 1))) + } +} + +function eq(a, b) { + if (minus(a, b)) { + 0 + } else { + 1 + } +} + +function factorial(n) { + if (eq(n, 1)) { + 1 + } else { + times(n, factorial(minus(n, 1))) + } +} + +print(factorial(4)) diff --git a/test/fake_builder.rb b/test/fake_builder.rb deleted file mode 100644 index b8765b56..00000000 --- a/test/fake_builder.rb +++ /dev/null @@ -1,21 +0,0 @@ -class FakeBuilder - attr_reader :result - - def initialize - @result = '' - end - - def class_builder - 'example' - 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' - end -end diff --git a/test/test_complier.rb b/test/test_complier.rb new file mode 100755 index 00000000..a7698f8f --- /dev/null +++ b/test/test_complier.rb @@ -0,0 +1,7 @@ + +(puts("Usage: #{} SOURCE"); exit) if ARGV.empty? + +require_relative 'helper' +require 'vm/compiler' + +Thnad::Compiler.new(ARGV.first).compile diff --git a/test/test_nodes.rb b/test/test_nodes.rb index fff10838..d7fa992c 100644 --- a/test/test_nodes.rb +++ b/test/test_nodes.rb @@ -4,10 +4,31 @@ $: << File.expand_path(File.dirname(__FILE__)) require 'minitest/autorun' require 'minitest/spec' require 'vm/nodes' -require 'fake_builder' include Vm +class FakeBuilder + attr_reader :result + + def initialize + @result = '' + end + + def class_builder + 'example' + 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' + end +end + describe 'Nodes' do before do @context = Hash.new @@ -17,7 +38,7 @@ describe 'Nodes' do it 'emits a number' do input = Vm::Number.new 42 expected = <