From 5c862111b92475aa68271a8a7f3ba135856f2902 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 12 Nov 2015 20:03:57 +0200 Subject: [PATCH] remove div and add div10 general division is for another day, the 10 version is manageable also same code produces mod10 wip --- lib/interpreter/interpreter.rb | 2 -- lib/register/boot.rb | 2 +- lib/register/builtin/integer.rb | 16 ++++++++++++++++ lib/soml/parfait/integer.soml | 13 +++---------- lib/soml/parfait/word.soml | 4 ++-- test/parfait/helper.rb | 9 +++++++++ 6 files changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/interpreter/interpreter.rb b/lib/interpreter/interpreter.rb index 5a519e6e..89d9b836 100644 --- a/lib/interpreter/interpreter.rb +++ b/lib/interpreter/interpreter.rb @@ -195,8 +195,6 @@ module Interpreter result = left + right when "-" result = left - right - when "/" - result = left / right when ">>" result = left >> right when "<<" diff --git a/lib/register/boot.rb b/lib/register/boot.rb index ac1ace6d..d2977b64 100644 --- a/lib/register/boot.rb +++ b/lib/register/boot.rb @@ -152,7 +152,7 @@ module Register @space.get_class_by_name(:Word).add_instance_method Builtin::Word.send(:putstring , nil) obj = @space.get_class_by_name(:Integer) - [:mod , :putint].each do |f| + [:mod , :putint , :div10 , :mod10].each do |f| obj.add_instance_method Builtin::Integer.send(f , nil) end end diff --git a/lib/register/builtin/integer.rb b/lib/register/builtin/integer.rb index 878d8abc..ba7cb684 100644 --- a/lib/register/builtin/integer.rb +++ b/lib/register/builtin/integer.rb @@ -5,6 +5,22 @@ module Register module ClassMethods include AST::Sexp + def div10 context + compiler = Soml::Compiler.new.create_method(:Integer,:div10 ).init_method + do_div10(compiler) + # return div + return compiler.method + end + def mod10 context + compiler = Soml::Compiler.new.create_method(:Integer,:mod10 ).init_method + do_div10(compiler) + #return mod + return compiler.method + end + + def do_div10 compiler + + end # The conversion to base10 is quite a bit more complicated than i thought. # The bulk of it is in div10 # We set up variables, do the devision and write the result to the string diff --git a/lib/soml/parfait/integer.soml b/lib/soml/parfait/integer.soml index 43c5d66d..d2f0e9df 100644 --- a/lib/soml/parfait/integer.soml +++ b/lib/soml/parfait/integer.soml @@ -13,7 +13,7 @@ class Integer < Value int num = as_char() str = str.push_char( num ) else - int rest = self / 10 + int rest = self.div10() str = rest.as_string( str ) rest = rest * 10 rest = self - rest @@ -33,14 +33,7 @@ class Integer < Value return self end - int div( Integer by) - int ret = self / by - return ret - end - - int mod(Integer by) - int base = self / by - base = base * by - return self - base + int mod4() + return self & 3 end end diff --git a/lib/soml/parfait/word.soml b/lib/soml/parfait/word.soml index 80c945de..04c79aca 100644 --- a/lib/soml/parfait/word.soml +++ b/lib/soml/parfait/word.soml @@ -2,7 +2,7 @@ class Word < Object int char_at(int index) int word_index = index - 1 - word_index = word_index / 4 + word_index = word_index >> 2 word_index = word_index + 3 int rest = index - 1 rest = rest.mod(4) @@ -23,7 +23,7 @@ class Word < Object self.set_length(index) int word_index = index - 1 - word_index = word_index / 4 + word_index = word_index >> 2 word_index = word_index + 3 int rest = index - 1 diff --git a/test/parfait/helper.rb b/test/parfait/helper.rb index 7979b7b0..8d570411 100644 --- a/test/parfait/helper.rb +++ b/test/parfait/helper.rb @@ -29,6 +29,15 @@ HERE @interpreter.tick end while( ! @interpreter.instruction.nil?) assert_equal @stdout , @interpreter.stdout +# write_file if true + end + + def write_file + file_name = caller(3).first.split("in ").last.chop.reverse.chop.reverse + file_name = File.dirname(__FILE__) + "/" + file_name + ".o" + Register.machine.translate_arm + writer = Elf::ObjectWriter.new + writer.save file_name end def check_return val