rpl.rb/lib/core/filesystem.rb

35 lines
902 B
Ruby
Raw Normal View History

2021-12-15 13:33:52 +01:00
module Rpl
module Lang
module Core
module_function
# ( filename -- content ) read file and put content on stack as string
def fread( stack, dictionary )
2022-02-08 15:45:36 +01:00
stack, args = Rpl::Lang.stack_extract( stack, [%i[string]] )
2021-12-15 13:33:52 +01:00
path = File.absolute_path( args[0][:value] )
2021-12-15 16:36:11 +01:00
2021-12-15 13:33:52 +01:00
stack << { type: :string,
value: File.read( path ) }
2021-12-15 16:36:11 +01:00
[stack, dictionary]
end
# ( filename -- … ) read and run file
def feval( stack, dictionary )
2022-02-08 15:45:36 +01:00
Rpl::Lang.eval( stack, dictionary, 'fread eval' )
2021-12-15 16:36:11 +01:00
end
2021-12-16 11:02:42 +01:00
# ( content filename -- ) write content into filename
2021-12-15 16:36:11 +01:00
def fwrite( stack, dictionary )
2022-02-08 15:45:36 +01:00
stack, args = Rpl::Lang.stack_extract( stack, [%i[string], :any] )
2021-12-15 16:36:11 +01:00
File.write( File.absolute_path( args[0][:value] ),
2021-12-16 11:02:42 +01:00
args[1][:value] )
2021-12-15 13:33:52 +01:00
[stack, dictionary]
end
end
end
end