mirror of
https://github.com/PeterCamilleri/fOOrth
synced 2024-11-16 07:47:56 +01:00
Added the .append{ } method to OutStream
This commit is contained in:
parent
89277ac510
commit
a09d1b3bb1
4 changed files with 37 additions and 0 deletions
|
@ -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}")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue