From 7caa12285c4d51a2b50c8cd40e73a5e8bcff6b7f Mon Sep 17 00:00:00 2001 From: Peter Camilleri Date: Wed, 10 Jun 2015 18:50:41 -0400 Subject: [PATCH] Converted Array .select{ ... } to .select{{ ... }}. --- integration/array_lib_tests.rb | 2 +- integration/ctrl_struct_lib_tests.rb | 2 +- lib/fOOrth/library/array_library.rb | 22 ++++++---------------- lib/fOOrth/library/ctrl_struct_library.rb | 14 -------------- lib/fOOrth/library/stubs.rb | 13 +++++-------- 5 files changed, 13 insertions(+), 40 deletions(-) diff --git a/integration/array_lib_tests.rb b/integration/array_lib_tests.rb index 1cff814..b541949 100644 --- a/integration/array_lib_tests.rb +++ b/integration/array_lib_tests.rb @@ -84,7 +84,7 @@ class ArrayLibraryTester < Minitest::Test end def test_the_select - foorth_equal('[ 0 10 do i loop ] .select{ v 1 and 0= }', [[0,2,4,6,8]]) + foorth_equal('[ 0 10 do i loop ] .select{{ v 1 and 0= }}', [[0,2,4,6,8]]) end def test_simple_array_indexing diff --git a/integration/ctrl_struct_lib_tests.rb b/integration/ctrl_struct_lib_tests.rb index 3d7cc8f..515b1bb 100644 --- a/integration/ctrl_struct_lib_tests.rb +++ b/integration/ctrl_struct_lib_tests.rb @@ -74,7 +74,7 @@ class CtrlStructLibraryTester < Minitest::Test foorth_raises('4 .new{{ }}') foorth_raises('4 .each{{ }}') foorth_raises('4 .map{{ }}') - foorth_raises('4 .select{ }') + foorth_raises('4 .select{{ }}') end end diff --git a/lib/fOOrth/library/array_library.rb b/lib/fOOrth/library/array_library.rb index 8a3e83c..2ea5812 100644 --- a/lib/fOOrth/library/array_library.rb +++ b/lib/fOOrth/library/array_library.rb @@ -37,6 +37,12 @@ module XfOOrth vm.push(self.map { |val| block.call(vm, val, idx); idx += 1; vm.pop}) }) + # [array] .select{{ ... }} [selected_array] + Array.create_shared_method('.select{{', NosSpec, [], &lambda { |vm| + idx, block = 0, vm.pop + vm.push(self.select { |val| block.call(vm, val, idx); idx += 1; vm.pop}) + }) + # [] [ v1 v2 ... vn ] [[v1,v2,...vn]]; an array literal value VirtualMachine.create_shared_method('[', VmSpec, [:immediate], &lambda { |vm| vm.nest_mode('vm.squash; ', :array_literal) @@ -264,19 +270,3 @@ module XfOOrth }) end - -#* Runtime library support for fOOrth constructs. -class Array - - # Runtime support for the .select{ } construct. - def do_foorth_select(&block) - index = 0 - self.select do |value| - value = block.call(value, index) - index += 1 - value - end - end - - -end diff --git a/lib/fOOrth/library/ctrl_struct_library.rb b/lib/fOOrth/library/ctrl_struct_library.rb index 3cf1e7e..e531ee2 100644 --- a/lib/fOOrth/library/ctrl_struct_library.rb +++ b/lib/fOOrth/library/ctrl_struct_library.rb @@ -113,20 +113,6 @@ module XfOOrth }) }) - #The object oriented .select{ } construct. - VirtualMachine.create_shared_method('.select{', VmSpec, [:immediate], &lambda {|vm| - suspend_execute_mode('vm.push(vm.pop.do_foorth_select{|vloop, xloop| ', :select_block) - - context.create_local_method('v', LocalSpec, [:immediate], - &lambda {|vm| vm << "vm.push(vloop); "} ) - - context.create_local_method('x', LocalSpec, [:immediate], - &lambda {|vm| vm << "vm.push(xloop); "} ) - - context.create_local_method('}', LocalSpec, [:immediate], - &lambda {|vm| vm.resume_execute_mode('vm.pop}); ', [:select_block]) }) - }) - #The object oriented .with{ } construct. VirtualMachine.create_shared_method('.with{', VmSpec, [:immediate], &lambda {|vm| old_mode = context[:mode] diff --git a/lib/fOOrth/library/stubs.rb b/lib/fOOrth/library/stubs.rb index 38a405d..7dbb4a3 100644 --- a/lib/fOOrth/library/stubs.rb +++ b/lib/fOOrth/library/stubs.rb @@ -48,9 +48,11 @@ module XfOOrth Object.create_shared_method('@', TosSpec, [:stub]) Object.create_shared_method('!', TosSpec, [:stub]) - Object.create_shared_method('.each{{', NosSpec, [:stub]) - Object.create_shared_method('.new{{', NosSpec, [:stub]) - Object.create_shared_method('.map{{', NosSpec, [:stub]) + #Procedure literal stubs. + Object.create_shared_method('.each{{', NosSpec, [:stub]) + Object.create_shared_method('.new{{', NosSpec, [:stub]) + Object.create_shared_method('.map{{', NosSpec, [:stub]) + Object.create_shared_method('.select{{', NosSpec, [:stub]) #Define some "crossover" symbols. #SymbolMap.add_entry('.init', :foorth_new) -- aliased in core.rb @@ -66,11 +68,6 @@ end #* Runtime library support stubs. class Object - # Runtime stub for the .select{ } construct. - def do_foorth_select(&block) - error "F12: A #{self.foorth_name} does not support .select{ ... }." - end - # Runtime stub for the .open{ } construct. def do_foorth_open_block(_vm, &block) error "F12: A #{self.foorth_name} does not support .open{ ... }."