rpl.rb/lib/rpl/words/general.rb

113 lines
4.1 KiB
Ruby
Raw Normal View History

2021-12-07 16:09:17 +01:00
# frozen_string_literal: true
2022-02-28 11:41:56 +01:00
require 'tempfile'
module RplLang
module Words
module General
2022-02-26 18:53:39 +01:00
include Types
def populate_dictionary
super
@dictionary.add_word( ['nop'],
'General',
'( -- ) no operation',
proc {} )
2022-02-09 16:35:54 +01:00
@dictionary.add_word( ['help'],
'General',
'( w -- s ) pop help string of the given word',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplName]] )
2022-02-09 16:35:54 +01:00
2022-02-26 18:53:39 +01:00
word = @dictionary.words[ args[0].value ]
2022-02-28 11:40:47 +01:00
@stack << Types.new_object( RplString, "\"#{args[0].value}: #{word.nil? ? 'not a core word' : word[:help]}\"" )
end )
2022-02-23 11:33:43 +01:00
@dictionary.add_word( ['words'],
'REPL',
'DEBUG',
proc do
@dictionary.words
.to_a
.group_by { |word| word.last[:category] }
.each do |cat, words|
puts cat
2022-02-28 11:41:39 +01:00
puts " #{words.map(&:first).join(', ')}"
2022-02-23 11:33:43 +01:00
end
end )
@dictionary.add_word( ['quit'],
'General',
'( -- ) Stop and quit interpreter',
proc {} )
@dictionary.add_word( ['version'],
'General',
'( -- n ) Pop the interpreter\'s version number',
proc do
2022-02-28 11:40:47 +01:00
@stack << Types.new_object( RplString, "\"#{@version}\"" )
end )
2022-02-09 16:35:54 +01:00
@dictionary.add_word( ['uname'],
'General',
'( -- s ) Pop the interpreter\'s complete indentification string',
proc do
2022-02-28 11:40:47 +01:00
@stack << Types.new_object( RplString, "\"Rpl Interpreter version #{@version}\"" )
end )
2022-02-09 16:38:09 +01:00
@dictionary.add_word( ['history'],
'REPL',
'',
proc {} )
2022-02-09 16:38:09 +01:00
2022-02-28 11:41:56 +01:00
@dictionary.add_word( ['edit'],
'General',
'( -- s ) Pop the interpreter\'s complete indentification string',
proc do
args = stack_extract( [:any] )
value = args[0].to_s
tempfile = Tempfile.new('rpl')
begin
tempfile.write( value )
tempfile.rewind
`$EDITOR #{tempfile.path}`
edited_value = tempfile.read
ensure
tempfile.close
tempfile.unlink
end
@stack << Types.new_object( args[0].class, edited_value )
end )
@dictionary.add_word( ['.s'],
'REPL',
'DEBUG',
proc { pp @stack } )
2022-02-23 11:33:43 +01:00
@dictionary.add_word( ['.d'],
'REPL',
'DEBUG',
proc { pp @dictionary } )
@dictionary.add_word( ['.v'],
'REPL',
'DEBUG',
proc { pp @dictionary.vars } )
@dictionary.add_word( ['.lv'],
'REPL',
'DEBUG',
proc { pp @dictionary.local_vars_layers } )
end
end
end
end