first tests comping through after ast/compile change

This commit is contained in:
Torsten Ruger 2015-05-04 23:03:52 +03:00
parent ff22c17784
commit e4c799ecb6
17 changed files with 72 additions and 75 deletions

View File

@ -1,5 +0,0 @@
lib/**/*.rb
bin/*
-
features/**/*.feature
LICENSE.txt

View File

@ -2,7 +2,7 @@ require_relative "instruction"
module Arm module Arm
# A Machines main responsibility in the framework is to instantiate Instruction # A Machines main responsibility in the framework is to instantiate Instructions
# Value functions are mapped to machines by concatenating the values class name + the methd name # Value functions are mapped to machines by concatenating the values class name + the methd name
# Example: IntegerValue.plus( value ) -> Machine.signed_plus (value ) # Example: IntegerValue.plus( value ) -> Machine.signed_plus (value )
@ -53,7 +53,6 @@ module Arm
end end
def self.class_for clazz def self.class_for clazz
c_name = clazz.name
my_module = self.class.name.split("::").first my_module = self.class.name.split("::").first
clazz_name = clazz.name.split("::").last clazz_name = clazz.name.split("::").last
if(my_module != Register ) if(my_module != Register )

View File

@ -1,9 +1,9 @@
module Compiler module Compiler
def self.compile expression , method , message def self.compile expression , method , message
exp_name = expression.class.split("::").last.sub("Expression","").downcase exp_name = expression.class.name.split("::").last.sub("Expression","").downcase
puts "Expression #{exp_name}" puts "Expression #{exp_name}"
self.send exp_name.to_sym , method , message self.send "compile_#{exp_name}".to_sym , expression, method , message
end end
end end

View File

@ -11,28 +11,28 @@ module Compiler
# But in the future (in the one that holds great things) we optimize those unneccesay moves away # But in the future (in the one that holds great things) we optimize those unneccesay moves away
# attr_reader :value # attr_reader :value
def compile_integer expession , method , message def self.compile_integer expession , method , message
int = Virtual::IntegerConstant.new(value) int = Virtual::IntegerConstant.new(value)
to = Virtual::NewReturn.new(Virtual::Integer , int) to = Virtual::NewReturn.new(Virtual::Integer , int)
method.add_code Virtual::Set.new( to , int) method.add_code Virtual::Set.new( to , int)
to to
end end
def compile_true expession , method , message def self.compile_true expession , method , message
value = Virtual::TrueConstant.new value = Virtual::TrueConstant.new
to = Virtual::Return.new(Virtual::Reference , value) to = Virtual::Return.new(Virtual::Reference , value)
method.add_code Virtual::Set.new( to , value ) method.add_code Virtual::Set.new( to , value )
to to
end end
def compile_false expession , method , message def self.compile_false expession , method , message
value = Virtual::FalseConstant.new value = Virtual::FalseConstant.new
to = Virtual::Return.new(Virtual::Reference , value) to = Virtual::Return.new(Virtual::Reference , value)
method.add_code Virtual::Set.new( to , value ) method.add_code Virtual::Set.new( to , value )
to to
end end
def compile_nil expession , method , message def self.compile_nil expession , method , message
value = Virtual::NilConstant.new value = Virtual::NilConstant.new
to = Virtual::Return.new(Virtual::Reference , value) to = Virtual::Return.new(Virtual::Reference , value)
method.add_code Virtual::Set.new( to , value ) method.add_code Virtual::Set.new( to , value )
@ -43,7 +43,7 @@ module Compiler
# compiling name needs to check if it's a variable and if so resolve it # compiling name needs to check if it's a variable and if so resolve it
# otherwise it's a method without args and a send is ussued. # otherwise it's a method without args and a send is ussued.
# this makes the namespace static, ie when eval and co are implemented method needs recompilation # this makes the namespace static, ie when eval and co are implemented method needs recompilation
def compile_name expession , method , message def self.compile_name expession , method , message
return Virtual::Self.new( Virtual::Mystery ) if expession.name == :self return Virtual::Self.new( Virtual::Mystery ) if expession.name == :self
if method.has_var(expession.name) if method.has_var(expession.name)
message.compile_get(method , expession.name ) message.compile_get(method , expession.name )
@ -54,7 +54,7 @@ module Compiler
end end
def compile_module expession , method , message def self.compile_module expession , method , message
clazz = Virtual::BootSpace.space.get_or_create_class name clazz = Virtual::BootSpace.space.get_or_create_class name
raise "uups #{clazz}.#{name}" unless clazz raise "uups #{clazz}.#{name}" unless clazz
to = Virtual::Return.new(Virtual::Reference , clazz ) to = Virtual::Return.new(Virtual::Reference , clazz )
@ -63,7 +63,7 @@ module Compiler
end end
# attr_reader :string # attr_reader :string
def compile_string expession , method , message def self.compile_string expession , method , message
value = Virtual::StringConstant.new(expession.string) value = Virtual::StringConstant.new(expession.string)
to = Virtual::Return.new(Virtual::Reference , value) to = Virtual::Return.new(Virtual::Reference , value)
Virtual::BootSpace.space.add_object value Virtual::BootSpace.space.add_object value
@ -72,14 +72,14 @@ module Compiler
end end
#attr_reader :left, :right #attr_reader :left, :right
def compile_assignment expession , method , message def self.compile_assignment expession , method , message
raise "must assign to NameExpression , not #{expession.left}" unless expession.left.instance_of? NameExpression raise "must assign to NameExpression , not #{expession.left}" unless expession.left.instance_of? NameExpression
r = right.compile(method,message) r = right.compile(method,message)
raise "oh noo, nil from where #{expession.right.inspect}" unless r raise "oh noo, nil from where #{expession.right.inspect}" unless r
message.compile_set( method , expession.left.name , r ) message.compile_set( method , expession.left.name , r )
end end
def compile_variable expession, method , message def self.compile_variable expession, method , message
method.add_code Virtual::InstanceGet.new(expession.name) method.add_code Virtual::InstanceGet.new(expession.name)
Virtual::NewReturn.new( Virtual::Mystery ) Virtual::NewReturn.new( Virtual::Mystery )
end end

View File

@ -3,7 +3,7 @@ module Compiler
# call_site - attr_reader :name, :args , :receiver # call_site - attr_reader :name, :args , :receiver
def compile_call_site expession , method , message def self.compile_call_site expession , method , message
me = expession.receiver.compile( method, message ) me = expession.receiver.compile( method, message )
method.add_code Virtual::NewMessage.new method.add_code Virtual::NewMessage.new
method.add_code Virtual::Set.new(Virtual::NewSelf.new(me.type), me) method.add_code Virtual::Set.new(Virtual::NewSelf.new(me.type), me)

View File

@ -1,14 +1,14 @@
module Compiler module Compiler
# attr_reader :values # attr_reader :values
def compile_array expession, context def self.compile_array expession, context
to.do to.do
end end
# attr_reader :key , :value # attr_reader :key , :value
def compile_association context def self.compile_association context
to.do to.do
end end
def compile_hash context def self.compile_hash context
to.do to.do
end end
end end

View File

@ -1,6 +1,8 @@
module Compiler module Compiler
# list - attr_reader :expressions # list - attr_reader :expressions
def compile_list expession , method , message def self.compile_list expession , method , message
expession.expressions.collect { |part| part.compile( method, message ) } expession.expressions.collect do |part|
Compiler.compile( part , method, message )
end
end end
end end

View File

@ -1,6 +1,6 @@
module Compiler module Compiler
# function attr_reader :name, :params, :body , :receiver # function attr_reader :name, :params, :body , :receiver
def compile_function expression, method , message def self.compile_function expression, method , message
args = expession.params.collect do |p| args = expession.params.collect do |p|
raise "error, argument must be a identifier, not #{p}" unless p.is_a? NameExpression raise "error, argument must be a identifier, not #{p}" unless p.is_a? NameExpression
p.name p.name

View File

@ -1,7 +1,7 @@
module Compiler module Compiler
# if - attr_reader :cond, :if_true, :if_false # if - attr_reader :cond, :if_true, :if_false
def compile_if expression , method , message def self.compile_if expression , method , message
# to execute the logic as the if states it, the blocks are the other way around # to execute the logic as the if states it, the blocks are the other way around
# so we can the jump over the else if true ,and the else joins unconditionally after the true_block # so we can the jump over the else if true ,and the else joins unconditionally after the true_block
merge_block = method.new_block "if_merge" # last one, created first merge_block = method.new_block "if_merge" # last one, created first

View File

@ -1,10 +1,10 @@
module Compiler module Compiler
# module attr_reader :name ,:expressions # module attr_reader :name ,:expressions
def compile_module expression , context def self.compile_module expression , context
return clazz return clazz
end end
def compile_class expression , method , message def self.compile_class expression , method , message
clazz = ::Virtual::BootSpace.space.get_or_create_class name clazz = ::Virtual::BootSpace.space.get_or_create_class name
puts "Created class #{clazz.name.inspect}" puts "Created class #{clazz.name.inspect}"
expression.expressions.each do |expr| expression.expressions.each do |expr|

View File

@ -1,6 +1,6 @@
module Compiler module Compiler
# operator attr_reader :operator, :left, :right # operator attr_reader :operator, :left, :right
def compile_operator expression, method , message def self.compile_operator expression, method , message
call = CallSiteExpression.new( operator , [right] , left ) call = CallSiteExpression.new( operator , [right] , left )
call.compile(method,message) call.compile(method,message)
end end

View File

@ -1,7 +1,7 @@
module Compiler module Compiler
# return attr_reader :expression # return attr_reader :expression
def compile_return expression, scope ,method def self.compile_return expression, scope ,method
Virtual::Reference.new Virtual::Reference.new
end end
def old def old

View File

@ -1,7 +1,7 @@
module Compiler module Compiler
# while- attr_reader :condition, :body # while- attr_reader :condition, :body
def compile_while expression, method , message def self.compile_while expression, method , message
start = Virtual::Label.new("while_start") start = Virtual::Label.new("while_start")
method.add_code start method.add_code start
is = expression.condition.compile(method,message) is = expression.condition.compile(method,message)

View File

@ -24,7 +24,7 @@ module Builtin
# The at_index is just "below" the api, something we need but don't want to expose, so we can't code the above in ruby # The at_index is just "below" the api, something we need but don't want to expose, so we can't code the above in ruby
def _get_instance_variable context , name = Virtual::Integer def _get_instance_variable context , name = Virtual::Integer
get_function = Virtual::CompiledMethod.new(:_get_instance_variable , [ Virtual::Reference ] , Virtual::Reference ,Virtual::Mystery ) get_function = Virtual::CompiledMethod.new(:_get_instance_variable , [ Virtual::Reference ] , Virtual::Reference ,Virtual::Mystery )
# return get_function return get_function
me = get_function.receiver me = get_function.receiver
var_name = get_function.args.first var_name = get_function.args.first
return_to = get_function.return_type return_to = get_function.return_type

View File

@ -63,7 +63,7 @@ module Virtual
def locals_at l_block def locals_at l_block
used =[] used =[]
# call assigns the return register, but as it is in l_block, it is not asked. # call assigns the return register, but as it is in l_block, it is not asked.
assigned = [ RegisterReference.new(Register::RegisterMachine.instance.return_register) ] assigned = [ RegisterReference.new(Virtual::RegisterMachine.instance.return_register) ]
l_block.reachable.each do |b| l_block.reachable.each do |b|
b.uses.each {|u| b.uses.each {|u|
(used << u) unless assigned.include?(u) (used << u) unless assigned.include?(u)
@ -158,11 +158,11 @@ module Virtual
# mov( r1 , r2 ) # mov( r1 , r2 )
# add( r1 , r2 , 4) # add( r1 , r2 , 4)
# end # end
# mov and add will be called on Machine and generate Inststuction that are then added # mov and add will be called on Machine and generate Instructions that are then added
# to the current block # to the current block
# also symbols are supported and wrapped as register usages (for bare metal programming) # also symbols are supported and wrapped as register usages (for bare metal programming)
def method_missing(meth, *arg_names, &block) def method_missing(meth, *arg_names, &block)
add_code ::Register::RegisterMachine.instance.send(meth , *arg_names) add_code ::Arm::ArmMachine.send(meth , *arg_names)
end end
def mem_length def mem_length

View File

@ -50,7 +50,8 @@ module Virtual
# read all the files needed for a minimal system at compile # read all the files needed for a minimal system at compile
classes = ["object"] classes = ["object"]
classes.each do |clazz| classes.each do |clazz|
bytes = File.read(File.join( File.dirname( __FILE__ ) , ".." , "parfait" , "#{clazz}.rb") ) bytes = File.read(File.join( File.dirname( __FILE__ ) , ".." , "parfait" , "#{clazz}.rb") )
bytes = 0 #shuts up my atom linter
# expression = compile_main(bytes) # expression = compile_main(bytes)
end end
end end
@ -59,7 +60,7 @@ module Virtual
syntax = @parser.parse_with_debug(bytes) syntax = @parser.parse_with_debug(bytes)
parts = Parser::Transform.new.apply(syntax) parts = Parser::Transform.new.apply(syntax)
main = Virtual::CompiledMethod.main main = Virtual::CompiledMethod.main
parts.compile( main , self.message ) Compiler.compile( parts , main , self.message )
end end
end end

View File

@ -30,12 +30,12 @@ HERE
# a hand coded version of the fibonachi numbers (moved to kernel to be able to call it) # a hand coded version of the fibonachi numbers (moved to kernel to be able to call it)
# not my hand off course, found in the net from a basic introduction # not my hand off course, found in the net from a basic introduction
def test_kernel_fibo def test_kernel_fibo
int = Register::Integer.new(Register::RegisterMachine.instance.receiver_register) int = Register::Integer.new(Virtual::RegisterMachine.instance.receiver_register)
fibo = @object_space.get_or_create_class(:Object).resolve_method(:fibo) fibo = @object_space.get_or_create_class(:Object).resolve_method(:fibo)
main = @object_space.main main = @object_space.main
main.mov int , 10 main.mov int , 10
main.call( fibo ) main.call( fibo )
main.mov( Register::RegisterMachine.instance.receiver_register , Register::RegisterMachine.instance.return_register ) main.mov( Virtual::RegisterMachine.instance.receiver_register , Virtual::RegisterMachine.instance.return_register )
putint = @object_space.get_or_create_class(:Object).resolve_method(:putint) putint = @object_space.get_or_create_class(:Object).resolve_method(:putint)
main.call( putint ) main.call( putint )
@should = [0x0,0x40,0x2d,0xe9,0x1,0x0,0x52,0xe3,0x2,0x0,0xa0,0xd1,0x7,0x0,0x0,0xda,0x1,0x30,0xa0,0xe3,0x0,0x40,0xa0,0xe3,0x4,0x30,0x83,0xe0,0x4,0x40,0x43,0xe0,0x1,0x20,0x42,0xe2,0x1,0x0,0x52,0xe3,0xfa,0xff,0xff,0x1a,0x3,0x0,0xa0,0xe1,0x0,0x80,0xbd,0xe8] @should = [0x0,0x40,0x2d,0xe9,0x1,0x0,0x52,0xe3,0x2,0x0,0xa0,0xd1,0x7,0x0,0x0,0xda,0x1,0x30,0xa0,0xe3,0x0,0x40,0xa0,0xe3,0x4,0x30,0x83,0xe0,0x4,0x40,0x43,0xe0,0x1,0x20,0x42,0xe2,0x1,0x0,0x52,0xe3,0xfa,0xff,0xff,0x1a,0x3,0x0,0xa0,0xe1,0x0,0x80,0xbd,0xe8]