First crack at proxies. First code runs!

This commit is contained in:
Peter Camilleri 2014-10-14 21:37:53 -04:00
parent fe14b83d5f
commit 5877817486
6 changed files with 66 additions and 9 deletions

View file

@ -8,7 +8,6 @@ module XfOOrth
#for fOOrth commands and source code. The readline facility is used to enable
#editing and command history and retrieval.
class Console
include Readline
include ReadPoint
#Initialize a new console command source.
@ -25,7 +24,7 @@ module XfOOrth
#<br>Returns
#* The next character of user input as a string.
def get
read { puts; readline(prompt, true).rstrip }
read { puts; Readline.readline(prompt, true).rstrip }
end
#Has the scanning of the text reached the end of input?

View file

@ -1,9 +1,11 @@
# coding: utf-8
require_relative 'compiler/word_specs'
require_relative 'core/object'
require_relative 'core/class'
require_relative 'core/virtual_machine'
require_relative 'core/proxy'
#* core.rb - The fOOrth language OO core.
module XfOOrth
@ -62,9 +64,9 @@ module XfOOrth
#Predefine the default implementation of the init method. All classes
#inherit this simple method.
name = '.init'
sym = SymbolMap.add_entry(name, :init)
sym = SymbolMap.add_entry(name, :foorth_init)
spec = MethodWordSpec.new(name, sym, [], &lambda {|vm| })
@object_class.add_shared_method(:init, spec)
@object_class.add_shared_method(:foorth_init, spec)
#The Class class is a child of the Object class.
@object_class.children['Class'] = @class_class

View file

@ -80,7 +80,7 @@ module XfOOrth
#* vm - The current fOOrth virtual machine.
def create_foorth_instance(vm)
obj = @instance_template.new
obj.init(vm)
obj.foorth_init(vm)
obj
end

38
lib/fOOrth/core/proxy.rb Normal file
View file

@ -0,0 +1,38 @@
# coding: utf-8
#require_relative 'exclusive'
require_relative 'shared_cache'
require_relative 'method_missing'
require_relative 'shared'
#* core/proxy.rb - A module to allow existing Ruby classes to serve as fOOrth
# classes as well. This is done via a proxy mechanism.
module XfOOrth
#Wrap a proxy around an existing Ruby class so that it acts as a fOOrth class.
#<br>Parameters:
#* target_class - The Ruby class to be wrapped.
#* foorth_parent - The fOOrth class that serves as parent.
#* exclusives - Are exclusive methods to be allowed?
#<br>Returns:
#* The newly created proxy class.
def self.create_proxy(target_class, foorth_parent, exclusives=false)
target_class.define_singleton_method(:foorth_parent) {foorth_parent}
target_class.define_singleton_method(:foorth_class) {XfOOrth.class_class}
target_class.define_singleton_method(:shared) {@shared ||= {}}
target_class.send(:define_method, :foorth_class,
&lambda {target_class})
target_class.send(:define_method, :name,
&lambda {"#{foorth_class.name} instance <#{@name}>."})
target_class.extend(SharedCache)
target_class.extend(Shared)
target_class.send(:include, MethodMissing)
target_class
end
end

22
sire.rb
View file

@ -4,7 +4,7 @@
require 'readline'
require 'pp'
include Readline
class Object
#Generate the class lineage of the object.
@ -24,6 +24,24 @@ class Object
end
end
module Foobar
def hello
puts 'Hello!'
self
end
Fixnum.send(:include, self)
end
module Bluebar
def ahoy
puts 'Ahoy!'
self
end
Fixnum.extend(self)
end
class SIRE
#Set up the interactive session.
def initialize
@ -45,7 +63,7 @@ class SIRE
def run_sire
until @done
begin
line = readline('SIRE>', true)
line = Readline.readline('SIRE>', true)
@running = true
result = eval line
@running = false

View file

@ -41,8 +41,8 @@ class SymbolMapTester < MiniTest::Unit::TestCase
#Test the special mappings facility
def test_special_mappings
assert_equal(XfOOrth::SymbolMap.map('.init'), :init)
assert_equal(XfOOrth::SymbolMap.unmap(:init), ['.init'])
assert_equal(XfOOrth::SymbolMap.map('.init'), :foorth_init)
assert_equal(XfOOrth::SymbolMap.unmap(:foorth_init), ['.init'])
assert_raises(XfOOrth::XfOOrthError) do
XfOOrth::SymbolMap.add_entry('.init', :evil_method)