a lot of work to get one more test to pass

This commit is contained in:
Torsten Ruger 2014-07-12 21:59:17 +03:00
parent 842c4e3044
commit dc6cb2bb52
16 changed files with 89 additions and 153 deletions

View File

@ -4,25 +4,25 @@ module Ast
class IntegerExpression < Expression
# attr_reader :value
def compile frame
def compile frame , method
Virtual::IntegerConstant.new value
end
end
class TrueExpression
def compile frame
def compile frame , method
Virtual::TrueValue.new
end
end
class FalseExpression
def compile frame
def compile frame , method
Virtual::FalseValue.new
end
end
class NilExpression
def compile frame
def compile frame , method
Virtual::NilValue.new
end
end
@ -44,7 +44,7 @@ module Ast
class ModuleName < NameExpression
def compile context
def compile frame , method
clazz = context.object_space.get_or_create_class name
raise "uups #{clazz}.#{name}" unless clazz
#class qualifier, means call from metaclass
@ -56,9 +56,9 @@ module Ast
class StringExpression < Expression
# attr_reader :string
def compile context
def compile frame , method
value = Virtual::StringConstant.new(string)
context.object_space.add_object value
::Virtual::Object.space.add_object value
value
end
end

View File

@ -4,7 +4,7 @@ module Ast
class CallSiteExpression < Expression
# attr_reader :name, :args , :receiver
@@counter = 0
def compile context
def compile frame , method
into = context.function
params = args.collect{ |a| a.compile(context) }
puts "compiling receiver #{receiver} (call #{name})"

View File

@ -1,73 +1,51 @@
require_relative "meta_class"
module Boot
# class is mainly a list of functions with a name (for now)
# class is mainly a list of methods with a name (for now)
# layout of object is seperated into Layout
class BootClass < Virtual::ObjectConstant
def initialize name , context , super_class = :Object
def initialize name , scope , super_class = :Object
super()
@context = context
# class functions
@functions = []
@scope = scope
# class methods
@methods = []
@name = name.to_sym
@super_class = super_class
@meta_class = MetaClass.new(self)
end
attr_reader :name , :functions , :meta_class , :context , :super_class
attr_reader :name , :methods , :meta_class , :context , :super_class
def add_function function
raise "not a function #{function}" unless function.is_a? Virtual::Function
raise "syserr " unless function.name.is_a? Symbol
@functions << function
def add_method method
raise "not a method #{method}" unless method.is_a? Virtual::Method
raise "syserr " unless method.name.is_a? Symbol
@methods << method
end
def get_function fname
def get_method fname
fname = fname.to_sym
f = @functions.detect{ |f| f.name == fname }
names = @functions.collect{|f| f.name }
f = @methods.detect{ |f| f.name == fname }
names = @methods.collect{|f| f.name }
f
end
# get the function and if not found, try superclasses. raise error if not found
def resolve_function name
fun = get_function name
# get the method and if not found, try superclasses. raise error if not found
def resolve_method name
fun = get_method name
unless fun or name == :Object
supr = @context.object_space.get_or_create_class(@super_class)
fun = supr.get_function name
puts "#{supr.functions.collect(&:name)} for #{name} GOT #{fun.class}" if name == :index_of
fun = supr.get_method name
puts "#{supr.methods.collect(&:name)} for #{name} GOT #{fun.class}" if name == :index_of
end
raise "Method not found :#{name}, for #{inspect}" unless fun
fun
end
def inspect
"BootClass #{@name} < #{@super_class}:#{@functions.collect(&:name)}"
"BootClass #{@name} < #{@super_class}:#{@methods.collect(&:name)}"
end
def to_s
inspect
end
# Code interface follows. Note position is inheitted as is from Code
# length of the class is the length of it's functions
def length
@functions.inject(0) {| sum , item | sum + item.length}
end
# linking functions
def link_at( start , context)
super
@functions.each do |function|
function.link_at(start , context)
start += function.length
end
end
# assemble functions
def assemble( io )
@functions.each do |function|
function.assemble(io)
end
io
end
end
end

View File

@ -1,7 +1,5 @@
require "vm/context"
require "boot/boot_class"
require "vm/call_site"
require "arm/arm_machine"
#require "vm/call_site"
require "kernel/all"
require "boot/object"
require "boot/string"
@ -16,30 +14,18 @@ module Boot
# While data "ususally" would live in a .data section, we may also "inline" it into the code
# in an oo system all data is represented as objects
# throwing in a context for unspecified use (well one is to pass the programm/globals around)
class BootSpace < Virtual::Code
class BootSpace
# 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 Virtual::RegisterMachine
def initialize machine = nil
super()
machine = RbConfig::CONFIG["host_cpu"] unless machine
machine = "intel" if machine == "x86_64"
machine = machine.capitalize
Virtual::RegisterMachine.instance = eval("#{machine}::#{machine}Machine").new
@classes = {}
@context = Virtual::Context.new(self)
@context.current_class = get_or_create_class :Object
@main = Virtual::Function.new("main")
@context.function = @main
@main = Virtual::Method.new("main")
#global objects (data)
@objects = []
@entry = Virtual::RegisterMachine.instance.main_start @context
#main gets executed between entry and exit
@exit = Virtual::RegisterMachine.instance.main_exit @context
boot_classes
@passes = [ Virtual::MoveMoveReduction.new , Virtual::LogicMoveReduction.new, Virtual::NoopReduction.new, Virtual::SaveLocals.new ]
@passes = [ ]
end
attr_reader :context , :main , :classes , :entry , :exit
@ -65,27 +51,27 @@ module Boot
obj = get_or_create_class :Object
[:index_of , :_get_instance_variable , :_set_instance_variable].each do |f|
#puts "Boot Object::#{f}"
obj.add_function Boot::Object.send(f , @context)
obj.add_method Boot::Object.send(f , @context)
end
[:utoa, :putstring,:putint,:fibo,:exit].each do |f|
[:putstring,:putint,:fibo,:exit].each do |f|
#puts "Boot Kernel::#{f}"
obj.add_function Crystal::Kernel.send(f , @context)
obj.add_method Crystal::Kernel.send(f , @context)
end
obj = get_or_create_class :String
[:get , :set].each do |f|
#puts "Boot String::#{f}"
obj.add_function Boot::String.send(f , @context)
obj.add_method Boot::String.send(f , @context)
end
end
# 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? Virtual::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
# this is the way to instantiate classes (not BootBlass.new)
# this is the way to instantiate classes (not BootClass.new)
# so we get and keep exactly one per name
def get_or_create_class name
raise "uups #{name}.#{name.class}" unless name.is_a? Symbol
@ -96,39 +82,5 @@ module Boot
end
c
end
# linking entry , exit , main , classes , objects
def link_at( start , context)
super
@entry.link_at( start , context )
start += @entry.length
@exit.link_at( start , context)
start += @exit.length
@main.link_at( start , context )
start += @main.length
@classes.values.each do |clazz|
clazz.link_at(start , context)
start += clazz.length
end
@objects.each do |o|
o.link_at(start , context)
start += o.length
end
end
# assemble in the same order as linked
def assemble( io )
link_at( @position , nil) #second link in case of forward declarations
@entry.assemble( io )
@exit.assemble( io )
@main.assemble( io )
@classes.values.each do |clazz|
clazz.assemble(io)
end
@objects.each do |o|
o.assemble(io)
end
io
end
end
end

View File

@ -6,12 +6,12 @@ module Boot
# 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 = Virtual::Integer
index_function = Virtual::Function.new(:index_of , Virtual::Reference , [Virtual::Reference] , Virtual::Integer )
index_function = Virtual::Method.new(:index_of , Virtual::Reference , [Virtual::Reference] , Virtual::Integer )
return index_function
end
def self.layout
layout_function = Virtual::Function.new(:layout , Virtual::Reference , [ ] , Virtual::Reference )
layout_function = Virtual::Function.new(:layout , [ ] , Virtual::Reference , Virtual::Reference )
layout_function.at_index 2
layout_function
end
@ -23,7 +23,8 @@ module Boot
# 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 = Virtual::Integer
get_function = Virtual::Function.new(:_get_instance_variable , Virtual::Reference , [ Virtual::Reference ] , Virtual::Mystery )
get_function = Virtual::Method.new(:_get_instance_variable , [ Virtual::Reference ] , Virtual::Reference ,Virtual::Mystery )
return get_function
me = get_function.receiver
var_name = get_function.args.first
return_to = get_function.return_type
@ -43,7 +44,9 @@ module Boot
end
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 )
set_function = Virtual::Method.new(:_set_instance_variable ,[Virtual::Reference ,Virtual::Reference], Virtual::Reference ,Virtual::Mystery )
return set_function
receiver set_function
me = set_function.receiver
var_name = set_function.args.first
return_to = set_function.return_type

View File

@ -2,11 +2,11 @@ module Boot
class String
module ClassMethods
def get context , index = Virtual::Integer
get_function = Virtual::Function.new(:get , Virtual::Integer , [ Virtual::Integer] , Virtual::Integer )
get_function = Virtual::Method.new(:get , [ Virtual::Integer] , Virtual::Integer , Virtual::Integer )
return get_function
end
def set context , index = Virtual::Integer , char = Virtual::Integer
set_function = Virtual::Function.new(:set , Virtual::Integer ,[Virtual::Integer, Virtual::Integer] , Virtual::Integer )
set_function = Virtual::Method.new(:set , [Virtual::Integer, Virtual::Integer] , Virtual::Integer ,Virtual::Integer )
return set_function
end
end

View File

@ -7,7 +7,8 @@ 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 = Virtual::Function.new(:utoa , Virtual::Integer , [ Virtual::Integer ] , Virtual::Integer )
utoa_function = Virtual::Method.new(:utoa , [ Virtual::Integer ] , Virtual::Integer ,Virtual::Integer )
return utoa_function
str_addr = utoa_function.receiver
number = utoa_function.args.first
remainder = utoa_function.new_local
@ -24,7 +25,8 @@ module Crystal
end
def self.putint context
putint_function = Virtual::Function.new(:putint , Virtual::Integer , [] , Virtual::Integer )
putint_function = Virtual::Method.new(:putint , [] , Virtual::Integer ,Virtual::Integer )
return putint_function
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
@ -50,7 +52,8 @@ 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 = Virtual::Function.new(:fibo , Virtual::Integer , [] , Virtual::Integer )
fibo_function = Virtual::Method.new(:fibo , [] , Virtual::Integer ,Virtual::Integer )
return fibo_function
result = fibo_function.return_type
int = fibo_function.receiver

View File

@ -1,7 +1,8 @@
module Crystal
module Kernel
def self.putstring context
function = Virtual::Function.new(:putstring , Virtual::Reference , [] )
function = Virtual::Method.new(:putstring , [] )
return function
ret = Virtual::RegisterMachine.instance.write_stdout(function)
function.set_return ret
function

View File

@ -1,7 +1,8 @@
module Crystal
module Kernel
def self.exit context
function = Virtual::Function.new(:exit , Virtual::Integer , [] )
function = Virtual::Method.new(:exit , [] , Virtual::Integer)
return function
ret = Virtual::RegisterMachine.instance.exit(function)
function.set_return ret
function

View File

@ -28,28 +28,18 @@ module Virtual
class StringConstant < ObjectConstant
attr_reader :string
# currently aligned to 4 (ie padded with 0) and off course 0 at the end
def attributes
[:string]
end
def initialize str
str = str.to_s if str.is_a? Symbol
length = str.length
# rounding up to the next 4 (always adding one for zero pad)
pad = ((length / 4 ) + 1 ) * 4 - length
raise "#{pad} #{self}" unless pad >= 1
@string = str + " " * pad
@string = str
end
def result= value
class_for(MoveInstruction).new(value , self , :opcode => :mov)
end
# the strings length plus padding
def length
string.length
end
# just writing the string
def assemble(io)
io << string
def inspect
self.class.name + ".new('#{@string}')"
end
end

View File

@ -1,4 +1,6 @@
module Vm
require_relative "value"
module Virtual
class Integer < Value
def initialize

View File

@ -31,10 +31,6 @@ module Virtual
# that the the model is such that what is a variable in ruby, never ends up being just on the pysical stack.
#
class Machine
# the main machine is the one that runs on the main thread, if it exists or receives an uncaught exception, that's it.
def self.main
@main || @main = Machine.new
end
def initialize
the_end = Halt.new
@ -62,3 +58,4 @@ require_relative "value"
require_relative "mystery"
require_relative "object"
require_relative "constants"
require "boot/boot_space"

View File

@ -12,10 +12,12 @@ module Virtual
def Method.main
Method.new(:main)
end
def initialize name , args = []
def initialize name , args = [] , receiver = Virtual::Reference , return_type = Virtual::Reference
@name = name
@args = args
@locals = []
@receiver = receiver
@return_type = return_type
@start = MethodEnter.new
@current = @start
end

View File

@ -1,3 +1,7 @@
module Boot
class BootSoace
end
end
module Virtual
# our machine is made up of objects, some of which are code, some data
#
@ -21,6 +25,14 @@ module Virtual
end
return true
end
def self.space
if defined? @@space
@@space
else
@@space = ::Boot::BootSpace.new
end
end
end
class Layout < Object
@ -38,5 +50,7 @@ module Virtual
@super_class = sup
end
end
end
require_relative "integer"
require_relative "reference"

View File

@ -1,4 +1,6 @@
module Vm
require_relative "value"
module Virtual
class Reference < Value
def initialize clazz = nil

View File

@ -46,18 +46,9 @@ class TestBasic < MiniTest::Test
check
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
check
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')
@output = [Virtual::StringConstant.new('hello')]
check
end