start on class compiler

idea is to get cleaner layer seperation
reduce machine and rework builtin boot
This commit is contained in:
Torsten Ruger 2018-06-30 19:20:17 +03:00
parent 4a7cc72732
commit daf1b56062
11 changed files with 51 additions and 12 deletions

10
lib/mom/class_compiler.rb Normal file
View File

@ -0,0 +1,10 @@
module Mom
class ClassCompiler
attr_reader :clazz , :methods
def initialize(clazz , methods)
@clazz = clazz
@methods = methods
end
end
end

View File

@ -15,3 +15,4 @@ module Mom
end end
require_relative "instruction/instruction.rb" require_relative "instruction/instruction.rb"
require_relative "class_compiler"

View File

@ -20,6 +20,7 @@ module Parfait
raise "args_type must be type" unless args_type.is_a?(Parfait::Type) raise "args_type must be type" unless args_type.is_a?(Parfait::Type)
raise "frame_type must be type" unless frame_type.is_a?(Parfait::Type) raise "frame_type must be type" unless frame_type.is_a?(Parfait::Type)
raise "source must be vool" unless source.is_a?(Vool::Statement) raise "source must be vool" unless source.is_a?(Vool::Statement)
raise "Empty bod" if(source.is_a?(Vool::Statements) and source.empty?)
end end
def create_typed_method( type ) def create_typed_method( type )
@ -27,6 +28,11 @@ module Parfait
type.create_method( @name , @args_type , @frame_type) type.create_method( @name , @args_type , @frame_type)
end end
def compile_to_mom(for_type)
typed_method = create_typed_method(for_type)
source.to_mom( typed_method )
end
def compile_to_risc(for_type) def compile_to_risc(for_type)
typed_method = create_typed_method(for_type) typed_method = create_typed_method(for_type)
head = source.to_mom( typed_method ) head = source.to_mom( typed_method )

View File

@ -24,12 +24,12 @@ module Vool
def to_mom( _ ) def to_mom( _ )
create_class_object create_class_object
mom = [] methods = []
body.statements.each do |node| body.statements.each do |node|
raise "Only methods for now #{node}" unless node.is_a?(MethodStatement) raise "Only methods for now #{node}" unless node.is_a?(MethodStatement)
mom << node.to_mom(@clazz) methods << node.to_mom(@clazz)
end end
mom Mom::ClassCompiler.new(@clazz , methods)
end end
def each(&block) def each(&block)

View File

@ -31,8 +31,9 @@ module Vool
# create mom instructions # create mom instructions
def to_mom( method ) def to_mom( method )
raise "Empty list ? #{statements.length}" if empty? raise "Empty list ? #{statements.length}" if empty?
flat = @statements.shift.to_mom(method) stats = @statements.dup
while( nekst = @statements.shift ) flat = stats.shift.to_mom(method)
while( nekst = stats.shift )
flat.append nekst.to_mom(method) flat.append nekst.to_mom(method)
end end
flat flat

View File

@ -10,6 +10,7 @@ module Risc
class TestRegisterValue < MiniTest::Test class TestRegisterValue < MiniTest::Test
def setup def setup
Parfait.boot!
Risc.machine.boot Risc.machine.boot
@r0 = RegisterValue.new(:r0 , :Message) @r0 = RegisterValue.new(:r0 , :Message)
@r1 = RegisterValue.new(:r1 , :Space) @r1 = RegisterValue.new(:r1 , :Space)

View File

@ -21,17 +21,17 @@ end
module MomCompile module MomCompile
include ScopeHelper include ScopeHelper
def compile_first_method( input ) def compile_mom(input)
# works a lot like Vool.ruby_to_vool statements = RubyX::RubyXCompiler.ruby_to_vool input
# but here we return the intermediate mom instructions that are otherwise not available
statements = RubyX::RubyCompiler.compile as_test_main( input )
statements = statements.normalize
res = statements.to_mom(nil) res = statements.to_mom(nil)
assert_equal Parfait::Class , statements.clazz.class , statements assert_equal Parfait::Class , statements.clazz.class , statements
@method = statements.clazz.get_method(:main) @method = statements.clazz.get_method(:main)
assert_equal Parfait::VoolMethod , @method.class assert_equal Parfait::VoolMethod , @method.class
#puts "#{res.class}" res
res.first end
def compile_first_method( input )
res = compile_mom( as_test_main( input ))
res.clazz.instance_methods.first.compile_to_mom(res.clazz.instance_type)
end end
def check_array( should , is ) def check_array( should , is )

View File

@ -6,6 +6,7 @@ module Vool
include Mom include Mom
def setup def setup
Parfait.boot!
Risc.machine.boot Risc.machine.boot
@ins = compile_first_method( "a = main(1 + 2)" ) @ins = compile_first_method( "a = main(1 + 2)" )
end end

View File

@ -6,6 +6,7 @@ module Vool
include Mom include Mom
def setup def setup
Parfait.boot!
Risc.machine.boot Risc.machine.boot
@ins = compile_first_method( "a = 5; a.div4") @ins = compile_first_method( "a = 5; a.div4")
end end

View File

@ -0,0 +1,17 @@
require_relative "helper"
module Vool
class TestClassStatement < MiniTest::Test
include MomCompile
def setup
Parfait.boot!
@ins = compile_mom( "class Test ; def main(); return 1; end; end;")
end
def test_return
assert_equal Mom::ClassCompiler , @ins.class
end
end
end

View File

@ -43,6 +43,7 @@ module Vool
include Mom include Mom
def setup def setup
Parfait.boot!
Risc.machine.boot Risc.machine.boot
@ins = compile_first_method( "return 5.div4") @ins = compile_first_method( "return 5.div4")
end end