implement FEVAL & FWRITE (wip)

This commit is contained in:
Gwenhael Le Moine 2021-12-15 16:36:11 +01:00
parent 5dc798f830
commit 2c9203f079
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
4 changed files with 56 additions and 4 deletions

View file

@ -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

View file

@ -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

View 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
View file

@ -0,0 +1,6 @@
1 2 +
« dup dup * * »
'trrr' sto
trrr