adds self reference and improve methods a little

This commit is contained in:
Torsten Ruger 2014-07-14 14:29:33 +03:00
parent db8b1488d0
commit c7c4387e16
7 changed files with 27 additions and 10 deletions

View File

@ -34,6 +34,7 @@ module Ast
# otherwise it's a method without args and a send is ussued.
# this makes the namespace static, ie when eval and co are implemented method needs recompilation
def compile frame , method
return Virtual::SelfReference.new() if name == :self
if method.has_var(name)
frame.compile_get(method , name )
else

View File

@ -5,6 +5,13 @@ module Ast
args = params.collect{ |p| p.compile(frame , method )}
r = receiver ? receiver.compile(frame,method) : Virtual::SelfReference.new
method = Virtual::Method.new(name , params , r )
frame = frame.new_frame
return_type = nil
body.each do |ex|
return_type = ex.compile(frame , method )
end
method.return_type = return_type
method
end
def scratch
args = []

View File

@ -49,9 +49,6 @@ module Boot
fun
end
def inspect
"MetaClass.new(:#{@me_self.name})"
end
def to_s
"#{inspect} on #{@me_self}, #{@functions.length} functions"
end

View File

@ -23,6 +23,10 @@ module Virtual
end
attr_reader :next_normal, :next_exception, :me, :binding
# dummy for the eventual
def new_frame
self
end
#
def compile_get method , name
method.add FrameGet.new(name)

View File

@ -1,3 +1,5 @@
require_relative "object"
module Virtual
# Instruction is an abstract for all the code of the object-machine. Derived classe make up the actual functionality
@ -8,11 +10,10 @@ module Virtual
# defining a minimal set of instructions needed to implement oo.
# This is partly because jumping over this layer and doing in straight in assember was too big a step
class Instruction
class Instruction < Virtual::Object
attr_accessor :next
def inspect
self.class.name + ".new()"
def attributes
[:next]
end
end

View File

@ -15,7 +15,7 @@ module Virtual
Method.new(:main , [] , Virtual::SelfReference )
end
def attributes
[:name , :args , :receiver]
[:name , :args , :receiver , :start , :return_type]
end
def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Reference
@name = name.to_sym
@ -26,7 +26,8 @@ module Virtual
@start = MethodEnter.new
@current = @start
end
attr_reader :name , :args , :receiver
attr_reader :name , :args , :receiver , :start
attr_accessor :return_type
def add instruction
@current.next = instruction

View File

@ -31,6 +31,12 @@ class TestBasic < MiniTest::Test
check
end
def test_self
@string_input = 'self '
@output = [Virtual::SelfReference.new()]
check
end
def test_instance_variable
@string_input = '@foo_bar '
@output = [Virtual::FrameSend.new(:_get_instance_variable , [ Virtual::StringConstant.new('foo_bar')])]
@ -39,7 +45,7 @@ class TestBasic < MiniTest::Test
def test_module_name
@string_input = 'FooBar '
@output = [Boot::MetaClass.new(:FooBar)]
@output = [Boot::BootClass.new(:FooBar,:Object)]
check
end