removed the structs (code smell)

This commit is contained in:
Torsten Ruger 2014-04-24 17:38:06 +03:00
parent 305f2380a9
commit c411ac5df8
6 changed files with 72 additions and 42 deletions

View File

@ -6,4 +6,6 @@ require "asm/stack_instruction"
require "asm/arm_assembler" require "asm/arm_assembler"
require "elf/object_writer" require "elf/object_writer"
require 'vm/parser' require 'vm/parser'
require 'vm/nodes'
require 'vm/transform'

View File

@ -54,7 +54,7 @@ module Vm
end end
def split_functions(tree) def split_functions(tree)
first_expr = tree.index { |t| ! t.is_a?(Function) } first_expr = tree.index { |t| ! t.is_a?(FunctionExpression) }
funcs = first_expr ? tree[0...first_expr] : tree funcs = first_expr ? tree[0...first_expr] : tree
exprs = first_expr ? tree[first_expr..-1] : [] exprs = first_expr ? tree[first_expr..-1] : []

View File

@ -1,11 +1,27 @@
module Vm module Vm
class Number < Struct.new :value
class Expression
# evey Expression has a eval function that returns a value
def eval
raise "abstract"
end
end
class NumberExpression < Expression
attr_reader :value
def initialize val
@value = val
end
def eval(context, builder) def eval(context, builder)
builder.mov "r0" , value builder.mov "r0" , value
end end
end end
class Name < Struct.new :name class NameExpression < Expression
attr_reader :name
def initialize name
@name = name
end
def eval(context, builder) def eval(context, builder)
param_names = context[:params] || [] param_names = context[:params] || []
position = param_names.index(name) position = param_names.index(name)
@ -15,7 +31,11 @@ module Vm
end end
end end
class Funcall < Struct.new :name, :args class FuncallExpression < Expression
attr_reader :name, :args
def initialize name, args
@name , @args = name , args
end
def eval(context, builder) def eval(context, builder)
args.each { |a| a.eval(context, builder) } args.each { |a| a.eval(context, builder) }
types = [builder.int] * (args.length + 1) types = [builder.int] * (args.length + 1)
@ -23,7 +43,11 @@ module Vm
end end
end end
class Conditional < Struct.new :cond, :if_true, :if_false class ConditionalExpression < Expression
attr_reader :cond, :if_true, :if_false
def initialize cond, if_true, if_false
@cond, @if_true, @if_false = cond, if_true, if_false
end
def eval(context, builder) def eval(context, builder)
cond.eval context, builder cond.eval context, builder
@ -39,7 +63,11 @@ module Vm
end end
end end
class Function < Struct.new :name, :params, :body class FunctionExpression < Expression
attr_reader :name, :params, :body
def initialize name, params, body
@name, @params, @body = name, params, body
end
def eval(context, builder) def eval(context, builder)
param_names = [params].flatten.map(&:name) param_names = [params].flatten.map(&:name)
context[:params] = param_names context[:params] = param_names

View File

@ -3,31 +3,31 @@ require 'vm/nodes'
module Vm module Vm
class Transform < Parslet::Transform class Transform < Parslet::Transform
rule(:number => simple(:value)) { Number.new(value.to_i) } rule(:number => simple(:value)) { NumberExpression.new(value.to_i) }
rule(:name => simple(:name)) { Name.new(name.to_s) } rule(:name => simple(:name)) { NameExpression.new(name.to_s) }
rule(:arg => simple(:arg)) { arg } rule(:arg => simple(:arg)) { arg }
rule(:args => sequence(:args)) { args } rule(:args => sequence(:args)) { args }
rule(:funcall => simple(:funcall), rule(:funcall => simple(:funcall),
:args => simple(:args)) { Funcall.new(funcall.name, [args]) } :args => simple(:args)) { FuncallExpression.new(funcall.name, [args]) }
rule(:funcall => simple(:funcall), rule(:funcall => simple(:funcall),
:args => sequence(:args)) { Funcall.new(funcall.name, args) } :args => sequence(:args)) { FuncallExpression.new(funcall.name, args) }
rule(:cond => simple(:cond), rule(:cond => simple(:cond),
:if_true => {:body => simple(:if_true)}, :if_true => {:body => simple(:if_true)},
:if_false => {:body => simple(:if_false)}) { Conditional.new(cond, if_true, if_false) } :if_false => {:body => simple(:if_false)}) { ConditionalExpression.new(cond, if_true, if_false) }
rule(:param => simple(:param)) { param } rule(:param => simple(:param)) { param }
rule(:params => sequence(:params)) { params } rule(:params => sequence(:params)) { params }
rule(:func => simple(:func), rule(:func => simple(:func),
:params => simple(:name), :params => simple(:name),
:body => simple(:body)) { Function.new(func.name, [name], body) } :body => simple(:body)) { FunctionExpression.new(func.name, [name], body) }
rule(:func => simple(:func), rule(:func => simple(:func),
:params => sequence(:params), :params => sequence(:params),
:body => simple(:body)) { Function.new(func.name, params, body) } :body => simple(:body)) { FunctionExpression.new(func.name, params, body) }
end end
end end

View File

@ -1,15 +1,15 @@
$: << File.expand_path(File.dirname(__FILE__) + '/../lib') require_relative "helper"
$: << File.expand_path(File.dirname(__FILE__))
require 'minitest/autorun'
require 'minitest/spec' require 'minitest/spec'
require 'vm/nodes'
include Vm include Vm
class FakeBuilder class FakeBuilder
attr_reader :result attr_reader :result
Asm::InstructionTools::REGISTERS.each do |reg , number|
define_method(reg) { Asm::Register.new(reg , number) }
end
def initialize def initialize
@result = '' @result = ''
end end
@ -36,7 +36,7 @@ describe 'Nodes' do
end end
it 'emits a number' do it 'emits a number' do
input = Vm::Number.new 42 input = Vm::NumberExpression.new 42
expected = <<HERE expected = <<HERE
mov r0, 42 mov r0, 42
HERE HERE
@ -48,8 +48,8 @@ HERE
it 'emits a function call' do it 'emits a function call' do
@context[:params] = ['foo'] @context[:params] = ['foo']
input = Vm::Funcall.new 'baz', [Vm::Number.new(42), input = Vm::FuncallExpression.new 'baz', [Vm::NumberExpression.new(42),
Vm::Name.new('foo')] Vm::NameExpression.new('foo')]
expected = <<HERE expected = <<HERE
mov r0, 42 mov r0, 42
iload 0 iload 0
@ -62,10 +62,10 @@ HERE
end end
it 'emits a conditional' do it 'emits a conditional' do
input = Vm::Conditional.new \ input = Vm::ConditionalExpression.new \
Vm::Number.new(0), Vm::NumberExpression.new(0),
Vm::Number.new(42), Vm::NumberExpression.new(42),
Vm::Number.new(667) Vm::NumberExpression.new(667)
expected = <<HERE expected = <<HERE
mov r0, 0 mov r0, 0
ifeq else ifeq else
@ -81,10 +81,10 @@ HERE
end end
it 'emits a function definition' do it 'emits a function definition' do
input = Vm::Function.new \ input = Vm::FunctionExpression.new \
'foo', 'foo',
Vm::Name.new('x'), Vm::NameExpression.new('x'),
Vm::Number.new(5) Vm::NumberExpression.new(5)
expected = <<HERE expected = <<HERE
public_static_method foo, int, int public_static_method foo, int, int

View File

@ -11,14 +11,14 @@ describe Transform do
it 'transforms a number' do it 'transforms a number' do
input = {:number => '42'} input = {:number => '42'}
expected = Vm::Number.new(42) expected = Vm::NumberExpression.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 = Vm::Name.new('foo') expected = Vm::NameExpression.new('foo')
@transform.apply(input).must_equal expected @transform.apply(input).must_equal expected
end end
@ -26,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 = [Vm::Number.new(42), expected = [Vm::NumberExpression.new(42),
Vm::Name.new('foo')] Vm::NameExpression.new('foo')]
@transform.apply(input).must_equal expected @transform.apply(input).must_equal expected
end end
@ -35,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 = Vm::Funcall.new 'foo', [Vm::Number.new(42)] expected = Vm::FuncallExpression.new 'foo', [Vm::NumberExpression.new(42)]
@transform.apply(input).must_equal expected @transform.apply(input).must_equal expected
end end
@ -44,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 = Vm::Funcall.new 'baz', [Vm::Number.new(42), expected = Vm::FuncallExpression.new 'baz', [Vm::NumberExpression.new(42),
Vm::Name.new('foo')] Vm::NameExpression.new('foo')]
@transform.apply(input).must_equal expected @transform.apply(input).must_equal expected
end end
@ -54,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 = Vm::Conditional.new \ expected = Vm::ConditionalExpression.new \
Vm::Number.new(0), Vm::NumberExpression.new(0),
Vm::Number.new(42), Vm::NumberExpression.new(42),
Vm::Number.new(667) Vm::NumberExpression.new(667)
@transform.apply(input).must_equal expected @transform.apply(input).must_equal expected
end end
@ -66,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 = Vm::Function.new \ expected = Vm::FunctionExpression.new \
'foo', 'foo',
[Vm::Name.new('x')], [Vm::NameExpression.new('x')],
Vm::Number.new(5) Vm::NumberExpression.new(5)
@transform.apply(input).must_equal expected @transform.apply(input).must_equal expected
end end