diff --git a/language.rb b/language.rb index f93d8c5..52fea6a 100644 --- a/language.rb +++ b/language.rb @@ -218,8 +218,8 @@ module Rpl # FILESYSTEM @dictionary.add( 'fread', proc { |stack, dictionary| Rpl::Lang::Core.fread( stack, dictionary ) } ) - # @dictionary.add( 'fload', proc { |stack, dictionary| Rpl::Lang::Core.__todo( stack, dictionary ) } ) # ( filename -- … ) « FREAD EVAL » - # @dictionary.add( 'fwrite', proc { |stack, dictionary| Rpl::Lang::Core.__todo( stack, dictionary ) } ) # ( content filename mode -- ) write content into filename using mode (w, a, …) + @dictionary.add( 'feval', proc { |stack, dictionary| Rpl::Lang::Core.feval( stack, dictionary ) } ) + @dictionary.add( 'fwrite', proc { |stack, dictionary| Rpl::Lang::Core.fwrite( stack, dictionary ) } ) # GRAPHICS end diff --git a/lib/core/filesystem.rb b/lib/core/filesystem.rb index b714e4f..347dc99 100644 --- a/lib/core/filesystem.rb +++ b/lib/core/filesystem.rb @@ -8,9 +8,28 @@ module Rpl stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[string]] ) path = File.absolute_path( args[0][:value][1..-2] ) - p path + stack << { type: :string, - value: File.read( path ) } + value: "\"#{File.read( path )}\"" } + + [stack, dictionary] + end + + # ( filename -- … ) read and run file + def feval( stack, dictionary ) + stack << { value: '« fread eval »', + type: :program } + + Rpl::Lang::Core.eval( stack, dictionary ) + end + + # ( content filename mode -- ) write content into filename using mode (w, a, …) + def fwrite( stack, dictionary ) + stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[string], %i[string], :any] ) + + path = args[1][:value][1..-2] + puts "File.open( #{path}, mode: #{args[0][:value][1..-2]} ) { |file| file.write( #{args[2][:value]} ) }" + File.open( path ) { |file| file.write( args[0][:value] ) } [stack, dictionary] end diff --git a/spec/language_filesystem_spec.rb b/spec/language_filesystem_spec.rb new file mode 100644 index 0000000..272fc11 --- /dev/null +++ b/spec/language_filesystem_spec.rb @@ -0,0 +1,27 @@ +# coding: utf-8 +# frozen_string_literal: true + +require 'test/unit' + +require_relative '../language' + +class TestLanguageTimeDate < Test::Unit::TestCase + def test_fread + lang = Rpl::Language.new + lang.run '"spec/test.rpl" fread' + + assert_equal [{ value: "\"1 2 + + +« dup dup * * » +'trrr' sto + +trrr +\"", type: :string }], + lang.stack + + lang.run 'eval vars' + assert_equal [{ value: 27, base: 10, type: :numeric }, + { value: ["'trrr'"], type: :list }], + lang.stack + end +end diff --git a/spec/test.rpl b/spec/test.rpl new file mode 100644 index 0000000..e4ecb57 --- /dev/null +++ b/spec/test.rpl @@ -0,0 +1,6 @@ +1 2 + + +« dup dup * * » +'trrr' sto + +trrr