remove div and add div10

general division is for another day, the 10 version is manageable
also same code produces mod10
wip
This commit is contained in:
Torsten Ruger 2015-11-12 20:03:57 +02:00
parent a5afca10f6
commit 5c862111b9
6 changed files with 31 additions and 15 deletions

View File

@ -195,8 +195,6 @@ module Interpreter
result = left + right
when "-"
result = left - right
when "/"
result = left / right
when ">>"
result = left >> right
when "<<"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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