Added the .append{ } method to OutStream

This commit is contained in:
Peter Camilleri 2015-01-25 18:05:28 -05:00
parent 89277ac510
commit a09d1b3bb1
4 changed files with 37 additions and 0 deletions

View file

@ -126,6 +126,17 @@ class OutStreamLibraryTester < MiniTest::Unit::TestCase
do_cleanup
end
def test_that_we_can_block_append_a_character
foorth_equal($osfn + 'OutStream .create 65 over .emit .close')
assert_equal(["A"], IO.readlines($osfn[1...-2]))
foorth_equal($osfn + 'OutStream .append{ 66 ~emit } ')
assert_equal(["AB"], IO.readlines($osfn[1...-2]))
do_cleanup
end
def do_cleanup
name = $osfn[1...-2]
system("rm #{name}")

View file

@ -149,4 +149,13 @@ module XfOOrth
&lambda {|vm| vm.resume_execute_mode('}; ', [:create_block]) })
})
#The object oriented .append{ } construct.
VirtualMachine.create_shared_method('.append{', VmSpec, [:immediate], &lambda {|vm|
suspend_execute_mode('vm.pop.do_foorth_append_block(vm){ ', :append_block)
context.create_local_method('}', [:immediate],
&lambda {|vm| vm.resume_execute_mode('}; ', [:append_block]) })
})
end

View file

@ -33,6 +33,18 @@ module XfOOrth
end
end
# Runtime support for the .append{ } construct.
def self.do_foorth_append_block(vm, &block)
file_name = vm.pop.to_s
out_stream = XfOOrth_OutStream.new(file_name, 'a')
begin
out_stream.instance_exec(vm, &block)
ensure
out_stream.file.close
end
end
end
#The .new method is stubbed out.

View file

@ -89,4 +89,9 @@ class Object
error "A #{self.foorth_name} does not support .create{ ... }."
end
# Runtime stub for the .append{ } construct.
def do_foorth_append_block(_vm, &block)
error "A #{self.foorth_name} does not support .append{ ... }."
end
end