rpl.rb/lib/rpl/words/filesystem.rb

40 lines
1.3 KiB
Ruby
Raw Normal View History

2022-02-10 14:50:59 +01:00
# frozen_string_literal: true
2021-12-15 13:33:52 +01:00
module RplLang
module Words
module FileSystem
2022-02-26 18:53:39 +01:00
include Types
def populate_dictionary
super
2021-12-15 13:33:52 +01:00
@dictionary.add_word( ['fread'],
'Filesystem',
'( filename -- content ) read file and put content on stack as string',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplString]] )
2021-12-15 16:36:11 +01:00
2022-02-26 18:53:39 +01:00
path = File.absolute_path( args[0].value )
2021-12-15 16:36:11 +01:00
2022-02-26 18:53:39 +01:00
@stack << RplString.new( "\"#{File.read( path )}\"" )
end )
2022-02-11 16:15:01 +01:00
@dictionary.add_word( ['feval'],
'Filesystem',
'( filename -- … ) read and run file',
2022-02-26 18:53:39 +01:00
RplProgram.new( '« fread eval »' ) )
2022-02-11 16:15:01 +01:00
@dictionary.add_word( ['fwrite'],
'Filesystem',
'( content filename -- ) write content into filename',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplString], :any] )
2021-12-15 16:36:11 +01:00
2022-02-26 18:53:39 +01:00
File.write( File.absolute_path( args[0].value ),
args[1].value )
end )
end
2021-12-15 13:33:52 +01:00
end
end
end