diff --git a/lib/ast/basic_expressions.rb b/lib/ast/basic_expressions.rb index afa30178..4d4a9194 100644 --- a/lib/ast/basic_expressions.rb +++ b/lib/ast/basic_expressions.rb @@ -5,7 +5,7 @@ module Ast class IntegerExpression < Expression # attr_reader :value def compile context - Vm::IntegerConstant.new value + Virtual::IntegerConstant.new value end end @@ -34,7 +34,7 @@ module Ast class StringExpression < Expression # attr_reader :string def compile context - value = Vm::StringConstant.new(string) + value = Virtual::StringConstant.new(string) context.object_space.add_object value value end diff --git a/lib/ast/call_site_expression.rb b/lib/ast/call_site_expression.rb index 7497ae63..add393fe 100644 --- a/lib/ast/call_site_expression.rb +++ b/lib/ast/call_site_expression.rb @@ -20,7 +20,7 @@ module Ast elsif receiver.is_a?(NameExpression) if(receiver.name == :self) function = context.current_class.resolve_function(name) - value_receiver = Vm::Integer.new(Vm::RegisterMachine.instance.receiver_register) + value_receiver = Virtual::Integer.new(Virtual::RegisterMachine.instance.receiver_register) else value_receiver = receiver.compile(context) # TODO HACK warning: should determine class dynamically @@ -35,7 +35,7 @@ module Ast end raise "No such method error #{inspect}" if (function.nil?) raise "No receiver error #{inspect}:#{receiver}" if (value_receiver.nil?) - call = Vm::CallSite.new( name , value_receiver , params , function) + call = Virtual::CallSite.new( name , value_receiver , params , function) current_function = context.function into.push([]) unless current_function.nil? call.load_args into diff --git a/lib/ast/expression_list.rb b/lib/ast/expression_list.rb new file mode 100644 index 00000000..a33e909e --- /dev/null +++ b/lib/ast/expression_list.rb @@ -0,0 +1,10 @@ +module Ast + class ExpressionList < Expression +# attr_reader :expressions + def compile binding + expressions.each do |part| + expr = part.compile( binding ) + end + end + end +end \ No newline at end of file diff --git a/lib/ast/function_expression.rb b/lib/ast/function_expression.rb index f406fbd0..3ce8bbd9 100644 --- a/lib/ast/function_expression.rb +++ b/lib/ast/function_expression.rb @@ -6,13 +6,13 @@ module Ast locals = {} params.each_with_index do |param , index| arg = param.name - register = Vm::RegisterReference.new(Vm::RegisterMachine.instance.receiver_register).next_reg_use(index + 1) - arg_value = Vm::Integer.new(register) + register = Virtual::RegisterReference.new(Virtual::RegisterMachine.instance.receiver_register).next_reg_use(index + 1) + arg_value = Virtual::Integer.new(register) locals[arg] = arg_value args << arg_value end # class depends on receiver - me = Vm::Integer.new( Vm::RegisterMachine.instance.receiver_register ) + me = Virtual::Integer.new( Virtual::RegisterMachine.instance.receiver_register ) if receiver.nil? clazz = context.current_class else @@ -20,7 +20,7 @@ module Ast clazz = c.meta_class end - function = Vm::Function.new(name , me , args ) + function = Virtual::Function.new(name , me , args ) clazz.add_function function parent_locals = context.locals @@ -32,11 +32,11 @@ module Ast body.each do |b| puts "compiling in function #{b}" last_compiled = b.compile(context) - raise "alarm #{last_compiled} \n #{b}" unless last_compiled.is_a? Vm::Word + raise "alarm #{last_compiled} \n #{b}" unless last_compiled.is_a? Virtual::Word end - return_reg = Vm::Integer.new(Vm::RegisterMachine.instance.return_register) - if last_compiled.is_a?(Vm::IntegerConstant) or last_compiled.is_a?(Vm::ObjectConstant) + return_reg = Virtual::Integer.new(Virtual::RegisterMachine.instance.return_register) + if last_compiled.is_a?(Virtual::IntegerConstant) or last_compiled.is_a?(Virtual::ObjectConstant) return_reg.load function , last_compiled if last_compiled.register_symbol != return_reg.register_symbol else return_reg.move( function, last_compiled ) if last_compiled.register_symbol != return_reg.register_symbol diff --git a/lib/ast/if_expression.rb b/lib/ast/if_expression.rb index a52d7f40..6e4eacda 100644 --- a/lib/ast/if_expression.rb +++ b/lib/ast/if_expression.rb @@ -11,7 +11,7 @@ module Ast puts "compiling if condition #{cond}" cond_val = cond.compile(context) - unless cond_val.is_a? Vm::BranchCondition + unless cond_val.is_a? Virtual::BranchCondition cond_val = cond_val.is_true? f end f.b true_block , condition_code: cond_val.operator diff --git a/lib/ast/return_expression.rb b/lib/ast/return_expression.rb index 44761a01..18be2a74 100644 --- a/lib/ast/return_expression.rb +++ b/lib/ast/return_expression.rb @@ -7,8 +7,8 @@ module Ast expression_value = expression.compile(context) # copied from function expression: TODO make function - return_reg = Vm::Integer.new(Vm::RegisterMachine.instance.return_register) - if expression_value.is_a?(Vm::IntegerConstant) or expression_value.is_a?(Vm::ObjectConstant) + return_reg = Virtual::Integer.new(Virtual::RegisterMachine.instance.return_register) + if expression_value.is_a?(Virtual::IntegerConstant) or expression_value.is_a?(Virtual::ObjectConstant) return_reg.load into , expression_value else return_reg.move( into, expression_value ) if expression_value.register_symbol != return_reg.register_symbol diff --git a/lib/boot/boot_class.rb b/lib/boot/boot_class.rb index c4f911fa..ec92ca96 100644 --- a/lib/boot/boot_class.rb +++ b/lib/boot/boot_class.rb @@ -3,7 +3,7 @@ require_relative "meta_class" module Boot # class is mainly a list of functions with a name (for now) # layout of object is seperated into Layout - class BootClass < Vm::ObjectConstant + class BootClass < Virtual::ObjectConstant def initialize name , context , super_class = :Object super() @context = context @@ -16,7 +16,7 @@ module Boot attr_reader :name , :functions , :meta_class , :context , :super_class def add_function function - raise "not a function #{function}" unless function.is_a? Vm::Function + raise "not a function #{function}" unless function.is_a? Virtual::Function raise "syserr " unless function.name.is_a? Symbol @functions << function end diff --git a/lib/boot/boot_space.rb b/lib/boot/boot_space.rb index ae339f2f..1b6039e0 100644 --- a/lib/boot/boot_space.rb +++ b/lib/boot/boot_space.rb @@ -18,28 +18,28 @@ module Boot # throwing in a context for unspecified use (well one is to pass the programm/globals around) - class BootSpace < Vm::Code + class BootSpace < Virtual::Code # Initialize with a string for cpu. Naming conventions are: for Machine XXX there exists a module XXX - # with a XXXMachine in it that derives from Vm::RegisterMachine + # with a XXXMachine in it that derives from Virtual::RegisterMachine def initialize machine = nil super() machine = RbConfig::CONFIG["host_cpu"] unless machine machine = "intel" if machine == "x86_64" machine = machine.capitalize - Vm::RegisterMachine.instance = eval("#{machine}::#{machine}Machine").new + Virtual::RegisterMachine.instance = eval("#{machine}::#{machine}Machine").new @classes = {} - @context = Vm::Context.new(self) + @context = Virtual::Context.new(self) @context.current_class = get_or_create_class :Object - @main = Vm::Function.new("main") + @main = Virtual::Function.new("main") @context.function = @main #global objects (data) @objects = [] - @entry = Vm::RegisterMachine.instance.main_start @context + @entry = Virtual::RegisterMachine.instance.main_start @context #main gets executed between entry and exit - @exit = Vm::RegisterMachine.instance.main_exit @context + @exit = Virtual::RegisterMachine.instance.main_exit @context boot_classes - @passes = [ Vm::MoveMoveReduction.new , Vm::LogicMoveReduction.new, Vm::NoopReduction.new, Vm::SaveLocals.new ] + @passes = [ Virtual::MoveMoveReduction.new , Virtual::LogicMoveReduction.new, Virtual::NoopReduction.new, Virtual::SaveLocals.new ] end attr_reader :context , :main , :classes , :entry , :exit @@ -81,7 +81,7 @@ module Boot # Objects are data and get assembled after functions def add_object o return if @objects.include? o - raise "must be derived from Code #{o.inspect}" unless o.is_a? Vm::Code + raise "must be derived from Code #{o.inspect}" unless o.is_a? Virtual::Code @objects << o # TODO check type , no basic values allowed (must be wrapped) end diff --git a/lib/boot/meta_class.rb b/lib/boot/meta_class.rb index e40149e4..3931d7ab 100644 --- a/lib/boot/meta_class.rb +++ b/lib/boot/meta_class.rb @@ -8,7 +8,7 @@ module Boot # PS: can't say i fancy the << self syntax and am considerernig adding a keyword for it, like meta # In effect it is a very similar construct to def self.function(...) # So one could write def meta.function(...) and thus define on the meta-class - class MetaClass < Vm::ObjectConstant + class MetaClass < Virtual::ObjectConstant # no name, nor nothing. as this is just the object really def initialize(object) @@ -19,7 +19,7 @@ module Boot # in a non-booting version this should map to _add_singleton_method def add_function function - raise "not a function #{function}" unless function.is_a? Vm::Function + raise "not a function #{function}" unless function.is_a? Virtual::Function raise "syserr " unless function.name.is_a? Symbol @functions << function end diff --git a/lib/boot/object.rb b/lib/boot/object.rb index 9fe0095e..0e5270a3 100644 --- a/lib/boot/object.rb +++ b/lib/boot/object.rb @@ -5,13 +5,13 @@ module Boot # return the index of the variable. Now "normal" code can't really do anything with that, but # set/get instance variable use it. # This is just a placeholder, as we code this in ruby, but the instance methods need the definition before. - def index_of context , name = Vm::Integer - index_function = Vm::Function.new(:index_of , Vm::Reference , [Vm::Reference] , Vm::Integer ) + def index_of context , name = Virtual::Integer + index_function = Virtual::Function.new(:index_of , Virtual::Reference , [Virtual::Reference] , Virtual::Integer ) return index_function end def self.layout - layout_function = Vm::Function.new(:layout , Vm::Reference , [ ] , Vm::Reference ) + layout_function = Virtual::Function.new(:layout , Virtual::Reference , [ ] , Virtual::Reference ) layout_function.at_index 2 layout_function end @@ -22,8 +22,8 @@ module Boot # return at_index(i) # end # 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 = Vm::Integer - get_function = Vm::Function.new(:_get_instance_variable , Vm::Reference , [ Vm::Reference ] , Vm::Mystery ) + def _get_instance_variable context , name = Virtual::Integer + get_function = Virtual::Function.new(:_get_instance_variable , Virtual::Reference , [ Virtual::Reference ] , Virtual::Mystery ) me = get_function.receiver var_name = get_function.args.first return_to = get_function.return_type @@ -42,8 +42,8 @@ module Boot return get_function end - def _set_instance_variable(context , name = Vm::Integer , value = Vm::Integer ) - set_function = Vm::Function.new(:_set_instance_variable , Vm::Reference ,[Vm::Reference ,Vm::Reference], Vm::Mystery ) + def _set_instance_variable(context , name = Virtual::Integer , value = Virtual::Integer ) + set_function = Virtual::Function.new(:_set_instance_variable , Virtual::Reference ,[Virtual::Reference ,Virtual::Reference], Virtual::Mystery ) me = set_function.receiver var_name = set_function.args.first return_to = set_function.return_type diff --git a/lib/boot/string.rb b/lib/boot/string.rb index b1642ee9..05f1256c 100644 --- a/lib/boot/string.rb +++ b/lib/boot/string.rb @@ -1,12 +1,12 @@ module Boot class String module ClassMethods - def get context , index = Vm::Integer - get_function = Vm::Function.new(:get , Vm::Integer , [ Vm::Integer] , Vm::Integer ) + def get context , index = Virtual::Integer + get_function = Virtual::Function.new(:get , Virtual::Integer , [ Virtual::Integer] , Virtual::Integer ) return get_function end - def set context , index = Vm::Integer , char = Vm::Integer - set_function = Vm::Function.new(:set , Vm::Integer ,[Vm::Integer, Vm::Integer] , Vm::Integer ) + def set context , index = Virtual::Integer , char = Virtual::Integer + set_function = Virtual::Function.new(:set , Virtual::Integer ,[Virtual::Integer, Virtual::Integer] , Virtual::Integer ) return set_function end end diff --git a/lib/kernel/integer.rb b/lib/kernel/integer.rb index 6ed03dfb..37ff1ca7 100644 --- a/lib/kernel/integer.rb +++ b/lib/kernel/integer.rb @@ -7,11 +7,11 @@ module Crystal # As we write before we recurse (save a push) we write the number backwards # arguments: string address , integer def self.utoa context - utoa_function = Vm::Function.new(:utoa , Vm::Integer , [ Vm::Integer ] , Vm::Integer ) + utoa_function = Virtual::Function.new(:utoa , Virtual::Integer , [ Virtual::Integer ] , Virtual::Integer ) str_addr = utoa_function.receiver number = utoa_function.args.first remainder = utoa_function.new_local - Vm::RegisterMachine.instance.div10( utoa_function , number , remainder ) + Virtual::RegisterMachine.instance.div10( utoa_function , number , remainder ) # make char out of digit (by using ascii encoding) 48 == "0" utoa_function.instance_eval do add( remainder , remainder , 48) @@ -24,8 +24,8 @@ module Crystal end def self.putint context - putint_function = Vm::Function.new(:putint , Vm::Integer , [] , Vm::Integer ) - buffer = Vm::StringConstant.new(" ") # create a buffer + putint_function = Virtual::Function.new(:putint , Virtual::Integer , [] , Virtual::Integer ) + buffer = Virtual::StringConstant.new(" ") # create a buffer context.object_space.add_object buffer # and save it (function local variable: a no no) int = putint_function.receiver moved_int = putint_function.new_local @@ -41,7 +41,7 @@ module Crystal add( int , buffer , nil ) # string to write to mov( moved_int , buffer.length ) end - Vm::RegisterMachine.instance.write_stdout(putint_function) + Virtual::RegisterMachine.instance.write_stdout(putint_function) putint_function end @@ -50,7 +50,7 @@ module Crystal # a hand coded version of the fibonachi numbers # not my hand off course, found in the net http://www.peter-cockerell.net/aalp/html/ch-5.html def self.fibo context - fibo_function = Vm::Function.new(:fibo , Vm::Integer , [] , Vm::Integer ) + fibo_function = Virtual::Function.new(:fibo , Virtual::Integer , [] , Virtual::Integer ) result = fibo_function.return_type int = fibo_function.receiver diff --git a/lib/kernel/string.rb b/lib/kernel/string.rb index f9aeda93..c725d35e 100644 --- a/lib/kernel/string.rb +++ b/lib/kernel/string.rb @@ -1,8 +1,8 @@ module Crystal module Kernel def self.putstring context - function = Vm::Function.new(:putstring , Vm::Reference , [] ) - ret = Vm::RegisterMachine.instance.write_stdout(function) + function = Virtual::Function.new(:putstring , Virtual::Reference , [] ) + ret = Virtual::RegisterMachine.instance.write_stdout(function) function.set_return ret function end diff --git a/lib/kernel/system.rb b/lib/kernel/system.rb index 8f97d66c..a113763b 100644 --- a/lib/kernel/system.rb +++ b/lib/kernel/system.rb @@ -1,8 +1,8 @@ module Crystal module Kernel def self.exit context - function = Vm::Function.new(:exit , Vm::Integer , [] ) - ret = Vm::RegisterMachine.instance.exit(function) + function = Virtual::Function.new(:exit , Virtual::Integer , [] ) + ret = Virtual::RegisterMachine.instance.exit(function) function.set_return ret function end diff --git a/lib/virtual/integer.rb b/lib/virtual/integer.rb index b4564742..45cb1f4a 100644 --- a/lib/virtual/integer.rb +++ b/lib/virtual/integer.rb @@ -7,19 +7,19 @@ module Vm def less_or_equal block , right block.cmp( self , right ) - Vm::BranchCondition.new :le + Virtual::BranchCondition.new :le end def greater_or_equal block , right block.cmp( self , right ) - Vm::BranchCondition.new :ge + Virtual::BranchCondition.new :ge end def greater_than block , right block.cmp( self , right ) - Vm::BranchCondition.new :gt + Virtual::BranchCondition.new :gt end def less_than block , right block.cmp( self , right ) - Vm::BranchCondition.new :lt + Virtual::BranchCondition.new :lt end def plus block , first , right block.add( self , left , right ) @@ -35,12 +35,12 @@ module Vm end def equals block , right block.cmp( self , right ) - Vm::BranchCondition.new :eq + Virtual::BranchCondition.new :eq end def is_true? function function.cmp( self , 0 ) - Vm::BranchCondition.new :ne + Virtual::BranchCondition.new :ne end def move block , right diff --git a/test/virtual/test_basic.rb b/test/virtual/test_basic.rb new file mode 100644 index 00000000..063ef302 --- /dev/null +++ b/test/virtual/test_basic.rb @@ -0,0 +1,70 @@ +require_relative "virtual_helper" + +class TestBasic < MiniTest::Test + # include the magic (setup and parse -> test method translation), see there + include VirtualHelper + + def test_number + @string_input = '42 ' + @output = Ast::IntegerExpression.new(42) + check + end + + def test_name + @string_input = 'foo ' + @output = Ast::NameExpression.new('foo') + @parser = @parser.name + end + + def test_name_underscode_start + @string_input = '_bar ' + @output = Ast::NameExpression.new('_bar') + @parser = @parser.name + end + + def test_name_underscode_middle + @string_input = 'foo_bar ' + @parse_output = {:name => 'foo_bar'} + @output = Ast::NameExpression.new('foo_bar') + @parser = @parser.name + end + + def test_instance_variable + @string_input = '@foo_bar ' + @parse_output = {:instance_variable=>{:name=>"foo_bar"}} + @output = Ast::VariableExpression.new(:foo_bar) + @parser = @parser.instance_variable + end + + def test_module_name + @string_input = 'FooBar ' + @parse_output = {:module_name=>"FooBar"} + @output = Ast::ModuleName.new("FooBar") + @parser = @parser.module_name + end + + def test_comment + out = "# i am a comment \n" + @string_input = out.dup #NEEDS the return, which is what delimits the comment + @parse_output = out + @output = @parse_output #dont transform + @parser = @parser.comment + end + + def test_string + @string_input = "\"hello\"" + @parse_output = {:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]} + @output = Ast::StringExpression.new('hello') + @parser = @parser.string + end + + def test_string_escapes + out = 'hello \nyou' + @string_input = '"' + out + '"' + @parse_output = {:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}, + {:char=>" "}, {:char=>" "}, {:esc=>"n"}, {:char=>"y"}, {:char=>"o"}, {:char=>"u"}]} + @output = Ast::StringExpression.new(out) + @parser = @parser.string + end + +end \ No newline at end of file