rpl.rb/lib/core/filesystem.rb

38 lines
983 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 )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[string]] )
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 )
stack << { value: 'fread eval',
2021-12-15 16:36:11 +01:00
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
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 )
2021-12-16 11:02:42 +01:00
stack, args = Rpl::Lang::Core.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