fOOrth 0.6.4 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/integration/compile_lib_tests.rb +41 -0
  3. data/irbt.rb +0 -2
  4. data/lib/fOOrth/compiler.rb +4 -8
  5. data/lib/fOOrth/compiler/cast.rb +39 -0
  6. data/lib/fOOrth/compiler/context/map_name.rb +12 -31
  7. data/lib/fOOrth/compiler/process.rb +5 -3
  8. data/lib/fOOrth/core/class.rb +1 -1
  9. data/lib/fOOrth/debug.rb +0 -2
  10. data/lib/fOOrth/debug/display_abort.rb +0 -6
  11. data/lib/fOOrth/library.rb +1 -0
  12. data/lib/fOOrth/library/array_library.rb +1 -47
  13. data/lib/fOOrth/library/command_library.rb +35 -60
  14. data/lib/fOOrth/library/compile_library.rb +27 -7
  15. data/lib/fOOrth/library/formatting/bullets.rb +121 -0
  16. data/lib/fOOrth/library/formatting/columns.rb +144 -0
  17. data/lib/fOOrth/library/hash_library.rb +2 -1
  18. data/lib/fOOrth/library/introspection/class.rb +84 -0
  19. data/lib/fOOrth/library/introspection/context.rb +36 -0
  20. data/lib/fOOrth/library/introspection/object.rb +69 -0
  21. data/lib/fOOrth/library/introspection/string.rb +28 -0
  22. data/lib/fOOrth/library/introspection/symbol_map.rb +19 -0
  23. data/lib/fOOrth/library/introspection/vm.rb +70 -0
  24. data/lib/fOOrth/library/introspection/word_specs.rb +18 -0
  25. data/lib/fOOrth/library/introspection_library.rb +78 -0
  26. data/lib/fOOrth/library/stack_library.rb +2 -4
  27. data/lib/fOOrth/library/stdio_library.rb +68 -0
  28. data/lib/fOOrth/library/stubs.rb +11 -7
  29. data/lib/fOOrth/library/vm_library.rb +1 -5
  30. data/lib/fOOrth/version.rb +1 -1
  31. data/tests/core_tests.rb +1 -1
  32. metadata +13 -4
  33. data/lib/fOOrth/debug/context_dump.rb +0 -31
  34. data/lib/fOOrth/debug/vm_dump.rb +0 -27
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cde7de9c337c4b22e08296e3b2a6fb2b57e5a112
4
- data.tar.gz: 6a3280aae89e3ac08f58de51411d9a73a2b321b8
3
+ metadata.gz: c3e8b4a5ccbb3c56347525737ec50f95a1016e19
4
+ data.tar.gz: 9bdb8b038c70895ddb82722ff0deeed11c3bdf47
5
5
  SHA512:
6
- metadata.gz: eedf9eb80534ec2e6057857b81c641c0059252604b28466d423e281125a0722877e70cd9a143396fdcf0b1fc512189327cc0906eaa382b8ff82313b4015c548d
7
- data.tar.gz: e21563be0a8a01a0949beed1b97da9250ce0dce5a60e87d8bef76a542b4552c8ef65ec92cd691e88f516c4a532667083d8a91d0febc1ecaa041b6369c4de1548
6
+ metadata.gz: 8fe86ae93e915c05e9702fd3996d96a258cd0c5e164292eb2c9a1776a576a2c2878fd59c640bdae12e0543689cf3854b2a24076e2c2b8534b6cc4e6180d56241
7
+ data.tar.gz: 04f4a12816c42a8b698a2f7c43055d7ba3ce3151dee06103f1d17826cc9182355f66240a3d8390b6939376dcd193abad00a4271507d7bcf2b5cac7b2f1fa0d18
@@ -195,4 +195,45 @@ class CompileLibraryTester < Minitest::Test
195
195
  foorth_equal('42 // foo bar etc 33', [42])
196
196
  end
197
197
 
198
+ def test_method_casting
199
+ foorth_equal(' asm"vm.push(vm.get_cast)" ', [nil] )
200
+ foorth_equal('\'. asm"vm.push(vm.get_cast); vm.clear_cast" ', [XfOOrth::TosSpec] )
201
+ foorth_equal('\'* asm"vm.push(vm.get_cast); vm.clear_cast" ', [XfOOrth::NosSpec] )
202
+ foorth_equal('\'~ asm"vm.push(vm.get_cast); vm.clear_cast" ', [XfOOrth::SelfSpec] )
203
+
204
+ foorth_raises('\'. ')
205
+ foorth_raises('\'* ')
206
+ foorth_raises('\'~ ')
207
+
208
+ foorth_raises('\'. \'. ')
209
+ foorth_raises('\'. \'* ')
210
+ foorth_raises('\'. \'~ ')
211
+
212
+ foorth_raises('\'* \'. ')
213
+ foorth_raises('\'* \'* ')
214
+ foorth_raises('\'* \'~ ')
215
+
216
+ foorth_raises('\'~ \'. ')
217
+ foorth_raises('\'~ \'* ')
218
+ foorth_raises('\'~ \'~ ')
219
+
220
+ foorth_equal('Object \'. .: .test ; asm"vm.push(vm.get_cast)" ', [nil] )
221
+
222
+ foorth_raises("'. : foo ; ")
223
+ foorth_raises("'. !: foo ; ")
224
+
225
+ foorth_raises("'* : foo ; ")
226
+ foorth_raises("'* !: foo ; ")
227
+
228
+ foorth_raises("'~ : foo ; ")
229
+ foorth_raises("'~ !: foo ; ")
230
+
231
+ foorth_equal("Integer .: .riff self swap - ; 10 5 .riff", [-5])
232
+ foorth_equal("Integer '* .: .diff self swap - ; 10 5 .diff", [5])
233
+
234
+ foorth_equal("Integer .: minus self swap - ; 10 5 minus", [5])
235
+ foorth_equal("Integer '. .: rinus self swap - ; 10 5 rinus", [-5])
236
+
237
+ end
238
+
198
239
  end
data/irbt.rb CHANGED
@@ -2,8 +2,6 @@
2
2
  # An IRB + fOOrth Test bed
3
3
 
4
4
  require 'irb'
5
- $force_alias_read_line_module = true
6
- require 'mini_readline'
7
5
 
8
6
  puts "Starting an IRB console with fOOrth loaded."
9
7
 
@@ -5,6 +5,7 @@ require_relative 'compiler/parser'
5
5
  require_relative 'compiler/token'
6
6
  require_relative 'compiler/modes'
7
7
  require_relative 'compiler/word_specs'
8
+ require_relative 'compiler/cast'
8
9
  require_relative 'compiler/context'
9
10
  require_relative 'compiler/process'
10
11
 
@@ -34,16 +35,11 @@ module XfOOrth
34
35
  #The level of comment nesting.
35
36
  attr_accessor :parens
36
37
 
37
- #Is a force compile in effect?
38
- attr_accessor :force
39
-
40
38
  #Return the compiler to a known state.
41
39
  def compiler_reset
42
- @buffer = nil
43
- @parser = nil
44
- @quotes = 0
45
- @parens = 0
46
- @force = false
40
+ @buffer = @parser = nil
41
+ @quotes = @parens = 0
42
+ clear_cast
47
43
  @context = Context.new(nil, vm: self, mode: :execute)
48
44
  self
49
45
  end
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+
3
+ #* compiler/cast.rb - Support for casting methods to alternate routings.
4
+ module XfOOrth
5
+
6
+ #* compiler/cast.rb - Support for casting methods to alternate routings.
7
+ class VirtualMachine
8
+
9
+ #Set the method cast.
10
+ def set_cast(spec)
11
+ error "F12: Multiple methods casts detected." if @cast
12
+ @cast = spec
13
+ end
14
+
15
+ #Clear the method cast
16
+ def clear_cast
17
+ @cast = nil
18
+ end
19
+
20
+ #Verify the method cast
21
+ def verify_cast(allowed)
22
+ if @cast && !allowed.include?(@cast)
23
+ error "F13: Cast of #{@cast.foorth_class_name} not allowed."
24
+ end
25
+ end
26
+
27
+ #Make sure there are no dangling casts.
28
+ def verify_casts_cleared
29
+ error "F12: Dangling method cast detected." if @cast
30
+ end
31
+
32
+ #Get the method cast and clear it.
33
+ def get_cast
34
+ @cast
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -11,9 +11,9 @@ module XfOOrth
11
11
  #* name - The string to be mapped.
12
12
  #<br>Returns:
13
13
  #* The specification that corresponds to the name or nil if none found.
14
- def map(name)
14
+ def map(name, allow_defaults=:allow)
15
15
  if (@symbol = SymbolMap.map(@name = name))
16
- do_map_name
16
+ do_map_name(allow_defaults)
17
17
  end
18
18
  end
19
19
 
@@ -21,42 +21,23 @@ module XfOOrth
21
21
  private
22
22
 
23
23
  #Do a search of dictionaries based on the syntax of the name.
24
- def do_map_name
25
- self[@symbol] ||
26
- case @name[0]
24
+ def do_map_name(allow_defaults)
25
+ self[@symbol] ||
26
+ do_target_class_map ||
27
+ do_target_object_map ||
28
+ do_target_vm_map ||
29
+ do_object_class_map ||
30
+ do_global_map ||
31
+ (allow_defaults && case @name[0]
27
32
  when '.'
28
- do_object_class_map ||
29
- do_target_vm_map ||
30
33
  TosSpec.new(@name, @symbol, [:temp])
31
34
 
32
35
  when '~'
33
- do_target_class_map ||
34
- do_target_object_map ||
35
- do_target_vm_map ||
36
36
  SelfSpec.new(@name, @symbol, [:temp])
37
37
 
38
- when '@'
39
- do_target_class_map ||
40
- do_target_object_map ||
41
- do_target_vm_map ||
42
- spec_error
43
-
44
- when '$'
45
- do_global_target_map ||
46
- do_target_vm_map ||
47
- spec_error
48
-
49
- when '#'
50
- do_target_vm_map ||
51
- spec_error
52
-
53
38
  else
54
- do_object_class_map ||
55
- do_target_vm_map ||
56
- do_global_target_map ||
57
39
  spec_error
58
- end
59
-
40
+ end)
60
41
  end
61
42
 
62
43
  #Do a search of the Object class for the item.
@@ -80,7 +61,7 @@ module XfOOrth
80
61
  end
81
62
 
82
63
  #Do a search of the globals.
83
- def do_global_target_map
64
+ def do_global_map
84
65
  $FOORTH_GLOBALS[@symbol]
85
66
  end
86
67
 
@@ -18,8 +18,11 @@ module XfOOrth
18
18
  #* source - A source object. Typically a Console, StringSource or FileSource.
19
19
  def process(source)
20
20
  save, @parser, start_depth = @parser, Parser.new(source), @context.depth
21
+
21
22
  due_process
22
23
  @context.check_depth(start_depth)
24
+ verify_casts_cleared
25
+
23
26
  @parser = save
24
27
  end
25
28
 
@@ -35,11 +38,10 @@ module XfOOrth
35
38
  dbg_puts token.to_s
36
39
  code = token.code
37
40
 
38
- if execute_mode? || ((token.has_tag?(:immediate)) && (!@force))
41
+ if execute_mode? || token.has_tag?(:immediate)
39
42
  @context.target.instance_exec(self, &eval("lambda {|vm| #{code} }"))
40
43
  else
41
- @buffer << code
42
- @force = false
44
+ self << code
43
45
  end
44
46
  end
45
47
 
@@ -7,7 +7,7 @@ class Class
7
7
  #<br>Decree!
8
8
  #* These are to be the only references to @_private_foorth_name!
9
9
  def foorth_name
10
- @_private_foorth_name ||= name.freeze
10
+ @_private_foorth_name ||= name.gsub(/.*::/, '').freeze
11
11
  end
12
12
 
13
13
  #Get the name of the class or a safe default.
data/lib/fOOrth/debug.rb CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  require_relative 'debug/display_abort'
4
4
  require_relative 'debug/dbg_puts'
5
- require_relative 'debug/context_dump'
6
- require_relative 'debug/vm_dump'
7
5
 
8
6
  #Set up the default debug conduit.
9
7
  $foorth_dbg = $stdout
@@ -18,12 +18,6 @@ module XfOOrth
18
18
  if debug
19
19
  puts "Data Stack Contents: #{data_stack.inspect}"
20
20
 
21
- if @context
22
- @context.debug_dump(self)
23
- else
24
- puts "Error: No context is available!"
25
- end
26
-
27
21
  puts "\nInternal Backtrace Dump:"
28
22
  puts
29
23
  puts exception.backtrace
@@ -25,6 +25,7 @@ require_relative 'library/bundle_library'
25
25
  require_relative 'library/sync_bundle_library'
26
26
  require_relative 'library/other_value_types_library'
27
27
  require_relative 'library/command_library'
28
+ require_relative 'library/introspection_library'
28
29
  require_relative 'library/in_stream_library'
29
30
  require_relative 'library/out_stream_library'
30
31
  require_relative 'library/queue_library'
@@ -470,7 +470,7 @@ module XfOOrth
470
470
  result = "[ "
471
471
 
472
472
  self.each do |value|
473
- if value.is_a?(String)
473
+ if value.is_a?(String) || value.nil?
474
474
  result << value.inspect + " "
475
475
  else
476
476
  value.to_foorth_s(vm)
@@ -532,50 +532,4 @@ module XfOOrth
532
532
  Array.create_shared_method('.keys', TosSpec, [],
533
533
  &lambda {|vm| vm.push((0...self.length).to_a) })
534
534
 
535
-
536
- $fcpl = 80 #fOOrth Character Per Line
537
- $flpp = 25 #fOOrth Lines Per Page
538
-
539
- # [ l 2 3 ... n ] .pp []; pretty print the array!
540
- Array.create_shared_method('.pp', TosSpec, [], &lambda {|vm|
541
- self.foorth_strmax(vm)
542
- width = vm.pop + 1
543
- cols = (width <= $fcpl) ? ($fcpl / width) : 1
544
- full_rows = ((cols * width) % $fcpl) == 0
545
- rows_left = (self.length + cols - 1) / cols
546
- pages = (rows_left + $flpp - 1) / $flpp
547
- page_capacity = $flpp * cols
548
-
549
- (0...pages).each do |page|
550
- offset = page * page_capacity
551
- rows_this_page = (rows_left >= $flpp) ? $flpp : rows_left
552
-
553
- (0...rows_this_page).each do |row|
554
- (0...cols).each do |col|
555
- self[offset + col*rows_this_page + row].to_foorth_s(vm)
556
-
557
- if cols > 1
558
- print vm.pop.ljust(width)
559
- else
560
- print vm.pop
561
- end
562
- end
563
-
564
- puts unless full_rows
565
- end
566
-
567
- rows_left -= $flpp
568
- puts "\n"
569
- end
570
- })
571
-
572
- #Show the page length.
573
- VirtualMachine.create_shared_method(')pl', MacroSpec,
574
- [:macro, 'puts "Page Length = #{$flpp}"; '])
575
-
576
-
577
- #Set/show the page length.
578
- VirtualMachine.create_shared_method(')set_pl', MacroSpec,
579
- [:macro, 'puts "New Page Length = #{$flpp = vm.pop.to_i}"; '])
580
-
581
535
  end
@@ -27,34 +27,6 @@ module XfOOrth
27
27
  VirtualMachine.create_shared_method(')noshow', MacroSpec,
28
28
  [:macro, "vm.show_stack = false; "])
29
29
 
30
- #Dump the context.
31
- VirtualMachine.create_shared_method(')context', VmSpec, [],
32
- &lambda {|vm| vm.context.debug_dump(vm) })
33
-
34
- #Dump the context right NOW!.
35
- VirtualMachine.create_shared_method(')context!', VmSpec, [:immediate],
36
- &lambda {|vm| vm.context.debug_dump(vm) })
37
-
38
- #Dump the virtual machine.
39
- VirtualMachine.create_shared_method(')vm', VmSpec, [],
40
- &lambda {|vm| vm.debug_dump })
41
-
42
- #Dump the virtual machine right NOW!
43
- VirtualMachine.create_shared_method(')vm!', VmSpec, [:immediate],
44
- &lambda {|vm| vm.debug_dump })
45
-
46
- #Map a symbol entry
47
- VirtualMachine.create_shared_method(')map"', VmSpec, [], &lambda {|vm|
48
- str = vm.pop.to_s
49
- puts "#{str} => #{(SymbolMap.map(str).to_s)}"
50
- })
51
-
52
- #Unmap a symbol entry
53
- VirtualMachine.create_shared_method(')unmap"', VmSpec, [], &lambda {|vm|
54
- str = vm.pop.to_s
55
- puts "#{str} <= #{(SymbolMap.unmap(str.to_sym).to_s)}"
56
- })
57
-
58
30
  #Start an Interactive RuBy session (IRB)
59
31
  VirtualMachine.create_shared_method(')irb', VmSpec, [], &lambda {|vm|
60
32
  require 'irb'
@@ -130,76 +102,79 @@ module XfOOrth
130
102
  VirtualMachine.create_shared_method(')entries', VmSpec, [], &lambda {|vm|
131
103
  entries = SymbolMap.forward_map.keys.sort
132
104
  puts 'Symbol Map Entries = '
133
- entries.foorth_pretty(vm)
105
+ entries.foorth_columns(vm)
134
106
  puts
135
107
  })
136
108
 
137
109
  #List the methods defined for this object.
138
110
  Object.create_shared_method(')methods', TosSpec, [], &lambda {|vm|
139
- if self.foorth_has_exclusive?
140
- puts 'Exclusive Methods = '
141
- self.foorth_exclusive.extract_method_names.sort.foorth_pretty(vm)
111
+ if foorth_has_exclusive?
112
+ puts '#{foorth_name} Exclusive Methods = '
113
+ foorth_exclusive.extract_method_names.sort.foorth_columns(vm)
142
114
  end
143
115
 
144
- puts "#{self.class.foorth_name} Shared Methods = "
145
- self.class.foorth_shared.extract_method_names.sort.foorth_pretty(vm)
116
+ my_class = self.class
117
+
118
+ puts "#{my_class.foorth_name} Shared Methods = "
119
+ my_class.foorth_shared.extract_method_names.sort.foorth_columns(vm)
146
120
  })
147
121
 
148
122
  #List the methods defined for this class.
149
123
  Class.create_shared_method(')methods', TosSpec, [], &lambda {|vm|
150
- if self.foorth_has_exclusive?
151
- puts "#{self.foorth_name} Class Methods = "
152
- self.foorth_exclusive.extract_method_names.sort.foorth_pretty(vm)
124
+ if foorth_has_exclusive?
125
+ puts "#{foorth_name} Class Methods = "
126
+ foorth_exclusive.extract_method_names.sort.foorth_columns(vm)
153
127
  end
154
128
 
155
- puts "#{self.foorth_name} Shared Methods = "
156
- self.foorth_shared.extract_method_names.sort.foorth_pretty(vm)
129
+ puts "#{foorth_name} Shared Methods = "
130
+ foorth_shared.extract_method_names.sort.foorth_columns(vm)
157
131
  })
158
132
 
159
133
  #List the stubs defined for this class.
160
134
  Class.create_shared_method(')stubs', TosSpec, [], &lambda {|vm|
161
- if self.foorth_has_exclusive?
162
- puts "#{self.foorth_name} Class Stubs = "
163
- self.foorth_exclusive.extract_method_names(:stubs).sort.foorth_pretty(vm)
135
+ if foorth_has_exclusive?
136
+ puts "#{foorth_name} Class Stubs = "
137
+ foorth_exclusive.extract_method_names(:stubs).sort.foorth_columns(vm)
164
138
  end
165
139
 
166
- puts "#{self.foorth_name} Shared Stubs = "
167
- self.foorth_shared.extract_method_names(:stubs).sort.foorth_pretty(vm)
140
+ puts "#{foorth_name} Shared Stubs = "
141
+ foorth_shared.extract_method_names(:stubs).sort.foorth_columns(vm)
168
142
  })
169
143
 
170
144
 
171
145
  #List the classes defined in fOOrth.
172
146
  VirtualMachine.create_shared_method(')classes', VmSpec, [], &lambda {|vm|
173
- $FOORTH_GLOBALS.values.
174
- select {|entry| entry.has_tag?(:class)}.
175
- collect {|spec| spec.new_class.foorth_name}.
176
- sort.
177
- foorth_pretty(vm)
147
+ $FOORTH_GLOBALS.values
148
+ .select {|entry| entry.has_tag?(:class)}
149
+ .collect {|spec| spec.new_class.foorth_name}
150
+ .sort
151
+ .foorth_columns(vm)
178
152
  })
179
153
 
180
154
  #List the globals defined in fOOrth.
181
155
  VirtualMachine.create_shared_method(')globals', VmSpec, [], &lambda {|vm|
182
- $FOORTH_GLOBALS.keys.
183
- select {|key| !($FOORTH_GLOBALS[key].has_tag?(:class))}.
184
- collect {|key| "#{XfOOrth::SymbolMap.unmap(key)} (#{key.to_s})"}.
185
- foorth_pretty(vm)
156
+ $FOORTH_GLOBALS.keys
157
+ .select {|key| !($FOORTH_GLOBALS[key].has_tag?(:class))}
158
+ .collect {|key| "#{XfOOrth::SymbolMap.unmap(key)}"}
159
+ .sort
160
+ .foorth_columns(vm)
186
161
  })
187
162
 
188
163
  #List the virtual machine methods
189
164
  #List the methods defined for this object.
190
165
  VirtualMachine.create_shared_method(')words', VmSpec, [], &lambda {|vm|
191
166
  if vm.foorth_has_exclusive?
192
- puts 'Exclusive Methods = '
193
- vm.foorth_exclusive.extract_method_names.sort.foorth_pretty(vm)
167
+ puts "#{foorth_name} Exclusive Methods = "
168
+ vm.foorth_exclusive.extract_method_names.sort.foorth_columns(vm)
194
169
  end
195
170
 
196
- puts "#{vm.class.foorth_name} Shared Methods = "
197
- vm.class.foorth_shared.extract_method_names.sort.foorth_pretty(vm)
171
+ my_class = vm.class
172
+
173
+ puts "#{my_class.foorth_name} Shared Methods = "
174
+ my_class.foorth_shared.extract_method_names.sort.foorth_columns(vm)
198
175
  })
199
176
 
200
177
  VirtualMachine.create_shared_method(')threads', VmSpec, [], &lambda {|vm|
201
- Thread.list.
202
- collect {|thrd| "#{thrd} vm = <#{thrd[:vm].name}>" }.
203
- foorth_pretty(vm)
178
+ puts Thread.list.map {|thread| "#{thread} vm = <#{thread[:vm].name}>"}
204
179
  })
205
180
  end