This commit is contained in:
Nigel Thorne 2014-06-04 21:49:06 +10:00
commit 5145d21e37
22 changed files with 184 additions and 107 deletions

View File

@ -63,6 +63,7 @@ module Arm
# return the block of the given name
# or raise an exception, as this is meant to be called when the block is available
def get_block name
name = name.to_sym
block = @blocks.find {|b| b.name == name}
raise "No block found for #{name} (in #{blocks.collect{|b|b.name}.join(':')})" unless block
block

View File

@ -11,7 +11,7 @@ module Arm
@attributes[:update_status] = 0 if @attributes[:update_status] == nil
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
@operand = 0
raise "alert" if right.is_a? Vm::Block
@pre_post_index = 0 #P flag
@add_offset = 0 #U flag
@is_load = opcode.to_s[0] == "l" ? 1 : 0 #L (load) flag
@ -31,7 +31,10 @@ module Arm
@rn = arg
if @right
@operand = @right
#TODO better test, this operand integer (register) does not work. but sleep first
@operand = @operand.register if @operand.is_a? Vm::Integer
unless( @operand.is_a? Symbol)
puts "operand #{@operand.inspect}"
if (@operand < 0)
@add_offset = 0
#TODO test/check/understand

View File

@ -24,7 +24,7 @@ module Ast
class NameExpression < Expression
attr_reader :name
def initialize name
@name = name
@name = name.to_sym
end
# compiling a variable resolves it.
# if it wasn't defined, nli is returned
@ -32,10 +32,10 @@ module Ast
context.locals[name]
end
def inspect
self.class.name + '.new("' + name + '")'
"#{self.class.name}.new(#{name})"
end
def to_s
name
name.to_s
end
def attributes
[:name]
@ -54,7 +54,7 @@ module Ast
self.class.name + '.new("' + string + '")'
end
def to_s
'"' + string + '"'
'"' + string.to_s + '"'
end
def compile context , into
value = Vm::StringConstant.new(string)

View File

@ -4,15 +4,30 @@ module Ast
class CallSiteExpression < Expression
attr_reader :name, :args , :receiver
def initialize name, args , receiver = Ast::NameExpression.new("self")
@name , @args , @receiver = name.to_sym , args , receiver
def initialize name, args , receiver = Ast::NameExpression.new(:self)
@name = name.to_sym
@args = args
@receiver = receiver
end
def compile context , into
params = args.collect{ |a| a.compile(context, into) }
#TOOD, this needs dynamic resolution
if receiver.name == :self
function = context.current_class.get_or_create_function(name)
raise "Forward declaration not implemented (#{name}) #{inspect}" if function == nil
elsif receiver.is_a? ModuleName
c_name = receiver.name
clazz = context.object_space.get_or_create_class c_name
raise "uups #{clazz}.#{c_name}" unless clazz
#class qualifier, means call from metaclass
clazz = clazz.meta_class
function = clazz.get_or_create_function(name)
elsif receiver.is_a? VariableExpression
raise "not implemented instance var:#{receiver}"
else
raise "not implemented case (dare i say system error):#{receiver}"
end
raise "No such method error #{clazz.to_s}:#{name}" if function == nil
call = Vm::CallSite.new( name , params , function)
current_function = context.function
current_function.save_locals(context , into) if current_function

View File

@ -31,10 +31,16 @@ module Ast
locals[arg] = arg_value
args << arg_value
end
class_name = context.current_class.name
function = Vm::Function.new(name , args )
# class depends on receiver
context.current_class.add_function function
if receiver.nil?
clazz = context.current_class
else
c = context.object_space.get_or_create_class receiver.name.to_sym
clazz = c.meta_class
end
function = Vm::Function.new(name , args )
clazz.add_function function
parent_locals = context.locals
parent_function = context.function

View File

@ -1,6 +1,8 @@
module Ast
class ModuleExpression < Expression
attr_reader :name ,:expressions
def initialize name , expressions
@name = name.to_sym
@expressions = expressions

51
lib/boot/object.rb Normal file
View File

@ -0,0 +1,51 @@
module Boot
class Object
module ClassMethods
# 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::Integer] , Vm::Integer )
return index_function
end
# in ruby, how this goes is
# def _get_instance_variable var
# i = self.index_of(var)
# return at_index(i)
# end
# The at_index is just "below" the api, somehting 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::Integer , Vm::Integer ] , Vm::Integer )
me = get_function.args[0]
var_name = get_function.args[1]
return_to = get_function.return_type
index_function = context.object_space.get_or_create_class(:Object).get_or_create_function(:index_of)
b = get_function.body
b.push( [me] )
b.call( index_function )
b.pop([me])
return_to.at_index( get_function.body , me , return_to )
get_function.set_return return_to
return get_function
end
def _set_instance_variable(name , value)
raise name
end
def _get_singleton_method(name )
raise name
end
def _add_singleton_method(method)
raise "4"
end
def initialize()
raise "4"
end
end
extend ClassMethods
end
end

View File

@ -1,26 +1,6 @@
# this is not a "normal" ruby file, ie it is not required by crystal
# instead it is parsed by crystal to define part of the crystal that runs
class BaseObject
def _set_instance_variable(name , value)
end
def _get_instance_variable( name )
end
def _get_singleton_method(name )
end
def _add_singleton_method(method)
end
def initialize
end
end
class Array < BaseObject
def initialize size

View File

@ -1,23 +0,0 @@
module Core
class BaseObject
def _set_instance_variable(name , value)
return name
end
def _get_instance_variable( name )
return name
end
def _get_singleton_method(name )
return name
end
def _add_singleton_method(method)
return 4
end
def initialize()
return 4
end
end
end

View File

@ -21,27 +21,6 @@ module Core
Vm::RegisterMachine.instance.function_exit block , f_name
end
# in ruby, how this goes is
# def _get_instance_variable var
# i = self.index_of(var)
# return at_index(i)
# end
# The at_index is just "below" the api, somehting 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::Integer , Vm::Integer ] , Vm::Integer )
me = get_function.args[0]
var_name = get_function.args[1]
return_to = get_function.return_type
index_function = context.object_space.get_or_create_class(:Object).get_or_create_function(:index_of)
b = get_function.body
b.push( me )
index = b.call( index_function )
b.pop(me)
return_to.at_index( get_function.body , me , index )
get_function.set_return return_to
return get_function
end
#TODO this is in the wrong place. It is a function that returns a function object
# while all other methods add their code into some block. --> kernel
def putstring context , string = Vm::Integer , length = Vm::Integer

View File

@ -1,15 +1,19 @@
require_relative "meta_class"
module Vm
# class is mainly a list of functions with a name (for now)
# layout of object is seperated into Layout
class BootClass < Code
def initialize name , context , superclass = :Object
def initialize name , context , super_class = :Object
super()
@context = context
# class functions
@functions = []
@name = name.to_sym
@superclass = superclass
@super_class = super_class
@meta_class = MetaClass.new(self)
end
attr_reader :name , :functions
attr_reader :name , :functions , :meta_class
def add_function function
raise "not a function #{function}" unless function.is_a? Function
@ -17,27 +21,32 @@ module Vm
@functions << function
end
def get_function name
name = name.to_sym
@functions.detect{ |f| f.name == name }
def get_function fname
fname = fname.to_sym
f = @functions.detect{ |f| f.name == fname }
names = @functions.collect{|f| f.name }
f
end
# preferred way of creating new functions (also forward declarations, will flag unresolved later)
# way of creating new functions that have not been parsed.
def get_or_create_function name
fun = get_function name
unless fun or name == :Object
supr = @context.object_space.get_or_create_class(@superclass)
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
end
unless fun
fun = Core::Kernel.send(name , @context)
raise "no such function #{name}, #{name.class}" if fun == nil
return nil if fun == nil
@functions << fun
end
fun
end
def inspect
"BootClass #{@name} , super #{@super_class} #{@functions.length} functions"
end
# Code interface follows. Note position is inheitted as is from Code
# length of the class is the length of it's functions

View File

@ -3,6 +3,7 @@ require_relative "boot_class"
require_relative "call_site"
require "arm/arm_machine"
require "core/kernel"
require "boot/object"
module Vm
# The BootSpace is contains all objects for a program. In functional terms it is a program, but on oo
@ -35,9 +36,19 @@ module Vm
#main gets executed between entry and exit
@main = Block.new("main",nil)
@exit = Core::Kernel::main_exit Vm::Block.new("main_exit",nil)
boot_classes
end
attr_reader :context , :main , :classes , :entry , :exit
def boot_classes
# very fiddly chicken 'n egg problem. Functions need to be in the right order, and in fact we have to define some
# dummies, just for the other to compile
obj = get_or_create_class :Object
[:index_of , :_get_instance_variable].each do |f|
puts "adding #{f}"
obj.add_function Boot::Object.send(f , @context)
end
end
def add_object o
return if @objects.include? o
raise "must be derived from Code #{o.inspect}" unless o.is_a? Code
@ -45,7 +56,7 @@ module Vm
end
def get_or_create_class name
raise "uups #{name}" unless name.is_a? Symbol
raise "uups #{name}.#{name.class}" unless name.is_a? Symbol
c = @classes[name]
unless c
c = BootClass.new(name,@context)

View File

@ -26,6 +26,7 @@ module Vm
attr_reader :string
# currently aligned to 4 (ie padded with 0) and off course 0 at the 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

37
lib/vm/meta_class.rb Normal file
View File

@ -0,0 +1,37 @@
module Vm
# class that acts like a class, but is really the object
# described in the ruby language book as the eigenclass, what you get with
# class MyClass
# class << self <--- this is called the eigenclass, or metaclass, and really is just the class object
# .... but gives us the ability to use the syntax as if it were a class
# 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 < Code
# no name, nor nothing. as this is just the object really
def initialize(object)
super()
@functions = []
@me_self = object
end
# 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? Function
raise "syserr " unless function.name.is_a? Symbol
@functions << function
end
def get_function name
name = name.to_sym
f = @functions.detect{ |f| f.name == name }
puts "no function for #{name} in Meta #{@me_self.inspect}" unless f
f
end
def inspect
"#{@me_self}, #{@functions.length} functions"
end
end
end

View File

@ -16,20 +16,25 @@ class Object
return @layout.class()
end
end
class Class
def Class.new_object( length )
return 4
end
end
class String
def String.new_string( len )
return Class.new_object( len << 2 )
end
def length()
return @length
end
def self.new_string( len )
4
end
def plus(str)
my_length = @length
str_len = str.length()
new_string = String.new_string(my_length + str_len)
i = 0
while( i < my_length) do
char = self.get(i)
char = get(i)
new_string.set(i , char)
i = i + 1
end

View File

@ -3,7 +3,7 @@ require_relative 'helper'
class TestWhileFragment < MiniTest::Test
include Fragments
def test_while
def test_while_fibo
@string_input = <<HERE
def fibonaccit(n) # n == r0
a = 0 # a == r1

View File

@ -35,7 +35,7 @@ class TestBasic < MiniTest::Test
def test_instance_variable
@string_input = '@foo_bar '
@parse_output = {:instance_variable=>{:name=>"foo_bar"}}
@transform_output = Ast::VariableExpression.new('foo_bar')
@transform_output = Ast::VariableExpression.new(:foo_bar)
@parser = @parser.instance_variable
end

View File

@ -22,7 +22,7 @@ class TestCallSite < MiniTest::Test
def test_single_instance
@string_input = '@var.foo(42)'
@parse_output = {:receiver=>{:instance_variable=>{:name=>"var"}}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]}
@transform_output = Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::VariableExpression.new("var"))
@transform_output = Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::VariableExpression.new(:var))
@parser = @parser.call_site
end
@ -96,7 +96,7 @@ class TestCallSite < MiniTest::Test
def test_call_chaining_instance
@string_input = '@class.new(self.get(4))'
@parse_output = {:receiver=>{:instance_variable=>{:name=>"class"}}, :call_site=>{:name=>"new"}, :argument_list=>[{:argument=>{:receiver=>{:name=>"self"}, :call_site=>{:name=>"get"}, :argument_list=>[{:argument=>{:integer=>"4"}}]}}]}
@transform_output = Ast::CallSiteExpression.new(:new, [Ast::CallSiteExpression.new(:get, [Ast::IntegerExpression.new(4)] ,Ast::NameExpression.new("self"))] ,Ast::VariableExpression.new("class"))
@transform_output = Ast::CallSiteExpression.new(:new, [Ast::CallSiteExpression.new(:get, [Ast::IntegerExpression.new(4)] ,Ast::NameExpression.new("self"))] ,Ast::VariableExpression.new(:class))
@parser = @parser.call_site
end

View File

@ -25,7 +25,7 @@ class Opers
end
HERE
@parse_output = {:module_name=>"Opers", :class_expressions=>[{:function_name=>{:name=>"foo"}, :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:l=>{:instance_variable=>{:name=>"abba"}}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}], :end=>"end"}
@transform_output = Ast::ClassExpression.new(:Opers ,[Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::OperatorExpression.new("=", Ast::VariableExpression.new("abba"),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] )] )
@transform_output = Ast::ClassExpression.new(:Opers ,[Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::OperatorExpression.new("=", Ast::VariableExpression.new(:abba),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] )] )
@parser = @parser.class_definition
end
@ -56,7 +56,7 @@ class Pifi
end
HERE
@parse_output = {:module_name=>"Pifi", :class_expressions=>[{:call_site=>{:name=>"ofthen"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}, {:function_name=>{:name=>"ofthen"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}, {:parmeter=>{:name=>"m"}}], :expressions=>[{:integer=>"44"}], :end=>"end"}], :end=>"end"}
@transform_output =Ast::ClassExpression.new(:Pifi ,[Ast::CallSiteExpression.new(:ofthen, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new("var")] ), Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new("n"),Ast::NameExpression.new("m")] , [Ast::IntegerExpression.new(44)] )] )
@transform_output =Ast::ClassExpression.new(:Pifi ,[Ast::CallSiteExpression.new(:ofthen, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] ), Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new("n"),Ast::NameExpression.new("m")] , [Ast::IntegerExpression.new(44)] )] )
@parser = @parser.class_definition
end
def test_class_module
@ -68,7 +68,7 @@ class Foo
end
HERE
@parse_output = {:module_name=>"Foo", :class_expressions=>[{:module_name=>"Boo", :module_expressions=>[{:call_site=>{:name=>"funcall"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}], :end=>"end"}], :end=>"end"}
@transform_output = Ast::ClassExpression.new(:Foo ,[Ast::ModuleExpression.new(:Boo ,[Ast::CallSiteExpression.new(:funcall, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new("var")] )] )] )
@transform_output = Ast::ClassExpression.new(:Foo ,[Ast::ModuleExpression.new(:Boo ,[Ast::CallSiteExpression.new(:funcall, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] )] )] )
@parser = @parser.class_definition
end
end

View File

@ -22,7 +22,7 @@ def String.length(x)
end
HERE
@parse_output = {:receiver=>{:module_name=>"String"}, :function_name=>{:name=>"length"}, :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:instance_variable=>{:name=>"length"}}], :end=>"end"}
@transform_output = Ast::FunctionExpression.new(:length, [Ast::NameExpression.new("x")] , [Ast::VariableExpression.new("length")] ,Ast::ModuleName.new("String") )
@transform_output = Ast::FunctionExpression.new(:length, [Ast::NameExpression.new("x")] , [Ast::VariableExpression.new(:length)] ,Ast::ModuleName.new("String") )
@parser = @parser.function_definition
end

View File

@ -36,7 +36,7 @@ module Opers
end
HERE
@parse_output = {:module_name=>"Opers", :module_expressions=>[{:l=>{:instance_variable=>{:name=>"abba"}}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"}
@transform_output = Ast::ModuleExpression.new(:Opers ,[Ast::OperatorExpression.new("=", Ast::VariableExpression.new("abba"),Ast::IntegerExpression.new(5))] )
@transform_output = Ast::ModuleExpression.new(:Opers ,[Ast::OperatorExpression.new("=", Ast::VariableExpression.new(:abba),Ast::IntegerExpression.new(5))] )
@parser = @parser.module_definition
end

View File

@ -31,25 +31,25 @@ class TestExpressions < MiniTest::Test
def test_op_variable
@string_input = "a + 35"
@parse_output = {:l=>{:name=>"a"}, :o=>"+ ", :r=>{:integer=>"35"}}
@transform_output = Ast::OperatorExpression.new("+", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(35))
@transform_output = Ast::OperatorExpression.new("+", Ast::NameExpression.new(:a),Ast::IntegerExpression.new(35))
@parser = @parser.operator_expression
end
def test_op_two_variable
@string_input = "a - b"
@parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:name=>"b"}}
@transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::NameExpression.new("b"))
@transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::NameExpression.new("b"))
@parser = @parser.operator_expression
end
def test_op_instance_variable
@string_input = "@a - 5"
@parse_output = {:l=>{:instance_variable=>{:name=>"a"}}, :o=>"- ", :r=>{:integer=>"5"}}
@transform_output = Ast::OperatorExpression.new("-", Ast::VariableExpression.new("a"),Ast::IntegerExpression.new(5))
@transform_output = Ast::OperatorExpression.new("-", Ast::VariableExpression.new(:a),Ast::IntegerExpression.new(5))
@parser = @parser.operator_expression
end
def test_op_variable_string
@string_input = 'a - "st"'
@parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:string=>[{:char=>"s"}, {:char=>"t"}]}}
@transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::StringExpression.new("st"))
@transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::StringExpression.new("st"))
@parser = @parser.operator_expression
end
def test_two_same_ops
@ -73,13 +73,13 @@ class TestExpressions < MiniTest::Test
def test_assignment
@string_input = "a = 5"
@parse_output = {:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}
@transform_output = Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(5))
@transform_output = Ast::OperatorExpression.new("=", Ast::NameExpression.new(:a),Ast::IntegerExpression.new(5))
@parser = @parser.operator_expression
end
def test_assignment_instance
@string_input = "@a = 5"
@parse_output = {:l=>{:instance_variable=>{:name=>"a"}}, :o=>"= ", :r=>{:integer=>"5"}}
@transform_output = Ast::OperatorExpression.new("=", Ast::VariableExpression.new("a"),Ast::IntegerExpression.new(5))
@transform_output = Ast::OperatorExpression.new("=", Ast::VariableExpression.new(:a),Ast::IntegerExpression.new(5))
@parser = @parser.operator_expression
end