removed the structs (code smell)
This commit is contained in:
@ -6,4 +6,6 @@ require "asm/stack_instruction"
|
||||
require "asm/arm_assembler"
|
||||
require "elf/object_writer"
|
||||
require 'vm/parser'
|
||||
require 'vm/nodes'
|
||||
require 'vm/transform'
|
||||
|
||||
|
@ -54,7 +54,7 @@ module Vm
|
||||
end
|
||||
|
||||
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
|
||||
exprs = first_expr ? tree[first_expr..-1] : []
|
||||
|
||||
|
@ -1,11 +1,27 @@
|
||||
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)
|
||||
builder.mov "r0" , value
|
||||
end
|
||||
end
|
||||
|
||||
class Name < Struct.new :name
|
||||
class NameExpression < Expression
|
||||
attr_reader :name
|
||||
def initialize name
|
||||
@name = name
|
||||
end
|
||||
def eval(context, builder)
|
||||
param_names = context[:params] || []
|
||||
position = param_names.index(name)
|
||||
@ -15,7 +31,11 @@ module Vm
|
||||
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)
|
||||
args.each { |a| a.eval(context, builder) }
|
||||
types = [builder.int] * (args.length + 1)
|
||||
@ -23,7 +43,11 @@ module Vm
|
||||
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)
|
||||
cond.eval context, builder
|
||||
|
||||
@ -39,7 +63,11 @@ module Vm
|
||||
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)
|
||||
param_names = [params].flatten.map(&:name)
|
||||
context[:params] = param_names
|
||||
|
@ -3,31 +3,31 @@ require 'vm/nodes'
|
||||
|
||||
module Vm
|
||||
class Transform < Parslet::Transform
|
||||
rule(:number => simple(:value)) { Number.new(value.to_i) }
|
||||
rule(:name => simple(:name)) { Name.new(name.to_s) }
|
||||
rule(:number => simple(:value)) { NumberExpression.new(value.to_i) }
|
||||
rule(:name => simple(:name)) { NameExpression.new(name.to_s) }
|
||||
|
||||
rule(:arg => simple(:arg)) { arg }
|
||||
rule(:args => sequence(:args)) { args }
|
||||
|
||||
rule(:funcall => simple(:funcall),
|
||||
:args => simple(:args)) { Funcall.new(funcall.name, [args]) }
|
||||
:args => simple(:args)) { FuncallExpression.new(funcall.name, [args]) }
|
||||
|
||||
rule(:funcall => simple(:funcall),
|
||||
:args => sequence(:args)) { Funcall.new(funcall.name, args) }
|
||||
:args => sequence(:args)) { FuncallExpression.new(funcall.name, args) }
|
||||
|
||||
rule(:cond => simple(:cond),
|
||||
: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(:params => sequence(:params)) { params }
|
||||
|
||||
rule(:func => simple(:func),
|
||||
: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),
|
||||
:params => sequence(:params),
|
||||
:body => simple(:body)) { Function.new(func.name, params, body) }
|
||||
:body => simple(:body)) { FunctionExpression.new(func.name, params, body) }
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user