implement FEVAL & FWRITE (wip)
This commit is contained in:
parent
5dc798f830
commit
2c9203f079
4 changed files with 56 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
27
spec/language_filesystem_spec.rb
Normal file
27
spec/language_filesystem_spec.rb
Normal file
|
@ -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
|
6
spec/test.rpl
Normal file
6
spec/test.rpl
Normal file
|
@ -0,0 +1,6 @@
|
|||
1 2 +
|
||||
|
||||
« dup dup * * »
|
||||
'trrr' sto
|
||||
|
||||
trrr
|
Loading…
Reference in a new issue