adds self reference and improve methods a little
This commit is contained in:
parent
db8b1488d0
commit
c7c4387e16
@ -34,6 +34,7 @@ module Ast
|
|||||||
# otherwise it's a method without args and a send is ussued.
|
# 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
|
# this makes the namespace static, ie when eval and co are implemented method needs recompilation
|
||||||
def compile frame , method
|
def compile frame , method
|
||||||
|
return Virtual::SelfReference.new() if name == :self
|
||||||
if method.has_var(name)
|
if method.has_var(name)
|
||||||
frame.compile_get(method , name )
|
frame.compile_get(method , name )
|
||||||
else
|
else
|
||||||
|
@ -5,6 +5,13 @@ module Ast
|
|||||||
args = params.collect{ |p| p.compile(frame , method )}
|
args = params.collect{ |p| p.compile(frame , method )}
|
||||||
r = receiver ? receiver.compile(frame,method) : Virtual::SelfReference.new
|
r = receiver ? receiver.compile(frame,method) : Virtual::SelfReference.new
|
||||||
method = Virtual::Method.new(name , params , r )
|
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
|
end
|
||||||
def scratch
|
def scratch
|
||||||
args = []
|
args = []
|
||||||
|
@ -49,9 +49,6 @@ module Boot
|
|||||||
fun
|
fun
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect
|
|
||||||
"MetaClass.new(:#{@me_self.name})"
|
|
||||||
end
|
|
||||||
def to_s
|
def to_s
|
||||||
"#{inspect} on #{@me_self}, #{@functions.length} functions"
|
"#{inspect} on #{@me_self}, #{@functions.length} functions"
|
||||||
end
|
end
|
||||||
|
@ -23,6 +23,10 @@ module Virtual
|
|||||||
end
|
end
|
||||||
attr_reader :next_normal, :next_exception, :me, :binding
|
attr_reader :next_normal, :next_exception, :me, :binding
|
||||||
|
|
||||||
|
# dummy for the eventual
|
||||||
|
def new_frame
|
||||||
|
self
|
||||||
|
end
|
||||||
#
|
#
|
||||||
def compile_get method , name
|
def compile_get method , name
|
||||||
method.add FrameGet.new(name)
|
method.add FrameGet.new(name)
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
require_relative "object"
|
||||||
|
|
||||||
module Virtual
|
module Virtual
|
||||||
|
|
||||||
# Instruction is an abstract for all the code of the object-machine. Derived classe make up the actual functionality
|
# 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.
|
# 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
|
# 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
|
attr_accessor :next
|
||||||
|
def attributes
|
||||||
def inspect
|
[:next]
|
||||||
self.class.name + ".new()"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ module Virtual
|
|||||||
Method.new(:main , [] , Virtual::SelfReference )
|
Method.new(:main , [] , Virtual::SelfReference )
|
||||||
end
|
end
|
||||||
def attributes
|
def attributes
|
||||||
[:name , :args , :receiver]
|
[:name , :args , :receiver , :start , :return_type]
|
||||||
end
|
end
|
||||||
def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Reference
|
def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Reference
|
||||||
@name = name.to_sym
|
@name = name.to_sym
|
||||||
@ -26,7 +26,8 @@ module Virtual
|
|||||||
@start = MethodEnter.new
|
@start = MethodEnter.new
|
||||||
@current = @start
|
@current = @start
|
||||||
end
|
end
|
||||||
attr_reader :name , :args , :receiver
|
attr_reader :name , :args , :receiver , :start
|
||||||
|
attr_accessor :return_type
|
||||||
|
|
||||||
def add instruction
|
def add instruction
|
||||||
@current.next = instruction
|
@current.next = instruction
|
||||||
|
@ -31,6 +31,12 @@ class TestBasic < MiniTest::Test
|
|||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_self
|
||||||
|
@string_input = 'self '
|
||||||
|
@output = [Virtual::SelfReference.new()]
|
||||||
|
check
|
||||||
|
end
|
||||||
|
|
||||||
def test_instance_variable
|
def test_instance_variable
|
||||||
@string_input = '@foo_bar '
|
@string_input = '@foo_bar '
|
||||||
@output = [Virtual::FrameSend.new(:_get_instance_variable , [ Virtual::StringConstant.new('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
|
def test_module_name
|
||||||
@string_input = 'FooBar '
|
@string_input = 'FooBar '
|
||||||
@output = [Boot::MetaClass.new(:FooBar)]
|
@output = [Boot::BootClass.new(:FooBar,:Object)]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user