Module: RplLang::Words::General

Includes:
Types
Included in:
Rpl
Defined in:
lib/rpl/words/general.rb

Instance Method Summary collapse

Methods included from Types

new_object

Instance Method Details

#populate_dictionaryObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rpl/words/general.rb', line 10

def populate_dictionary
  super

  @dictionary.add_word( ['nop'],
                        'General',
                        '( -- ) no operation',
                        proc {} )

  @dictionary.add_word( ['help'],
                        'General',
                        '( w -- s ) pop help string of the given word',
                        proc do
                          args = stack_extract( [[RplName]] )

                          word = @dictionary.words[ args[0].value ]

                          @stack << Types.new_object( RplString, "\"#{args[0].value}: #{word.nil? ? 'not a core word' : word[:help]}\"" )
                        end )

  @dictionary.add_word( ['words'],
                        'REPL',
                        'DEBUG',
                        proc do
                          @dictionary.words
                                     .to_a
                                     .group_by { |word| word.last[:category] }
                                     .each do |cat, words|
                            puts cat
                            puts "    #{words.map(&:first).join(', ')}"
                          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
                          @stack << Types.new_object( RplString, "\"#{@version}\"" )
                        end )

  @dictionary.add_word( ['uname'],
                        'General',
                        '( -- s ) Pop the interpreter\'s complete indentification string',
                        proc do
                          @stack << Types.new_object( RplString, "\"Rpl Interpreter version #{@version}\"" )
                        end )

  @dictionary.add_word( ['history'],
                        'REPL',
                        '',
                        proc {} )

  @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 } )

  @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