2022-02-10 14:50:59 +01:00
|
|
|
# frozen_string_literal: true
|
2021-12-15 13:33:52 +01:00
|
|
|
|
2022-02-11 15:46:47 +01:00
|
|
|
module RplLang
|
2022-02-25 15:43:48 +01:00
|
|
|
module Words
|
2022-02-11 15:46:47 +01:00
|
|
|
module FileSystem
|
2022-02-26 18:53:39 +01:00
|
|
|
include Types
|
|
|
|
|
2022-02-11 15:46:47 +01:00
|
|
|
def populate_dictionary
|
|
|
|
super
|
2021-12-15 13:33:52 +01:00
|
|
|
|
2022-08-31 09:57:10 +02:00
|
|
|
category = 'Filesystem'
|
|
|
|
|
2022-02-11 15:46:47 +01:00
|
|
|
@dictionary.add_word( ['fread'],
|
2022-08-31 09:57:10 +02:00
|
|
|
category,
|
2022-02-11 15:46:47 +01:00
|
|
|
'( 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-10-04 14:57:13 +02:00
|
|
|
path = File.expand_path( args[0].value )
|
2021-12-15 16:36:11 +01:00
|
|
|
|
2022-02-28 11:40:47 +01:00
|
|
|
@stack << Types.new_object( RplString, "\"#{File.read( path )}\"" )
|
2022-02-11 15:46:47 +01:00
|
|
|
end )
|
2022-02-11 16:15:01 +01:00
|
|
|
|
2022-02-11 15:46:47 +01:00
|
|
|
@dictionary.add_word( ['feval'],
|
2022-08-31 09:57:10 +02:00
|
|
|
category,
|
2022-02-11 15:46:47 +01:00
|
|
|
'( filename -- … ) read and run file',
|
2022-02-28 11:40:47 +01:00
|
|
|
Types.new_object( RplProgram, '« fread eval »' ) )
|
2022-02-11 16:15:01 +01:00
|
|
|
|
2022-02-11 15:46:47 +01:00
|
|
|
@dictionary.add_word( ['fwrite'],
|
2022-08-31 09:57:10 +02:00
|
|
|
category,
|
2022-02-11 15:46:47 +01:00
|
|
|
'( 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-10-04 14:57:13 +02:00
|
|
|
File.write( File.expand_path( args[0].value ),
|
2022-02-26 18:53:39 +01:00
|
|
|
args[1].value )
|
2022-02-11 15:46:47 +01:00
|
|
|
end )
|
|
|
|
end
|
2021-12-15 13:33:52 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|