Module: XfOOrth

Defined in:
lib/fOOrth.rb,
lib/fOOrth/core.rb,
lib/fOOrth/main.rb,
lib/fOOrth/debug.rb,
lib/fOOrth/library.rb,
lib/fOOrth/version.rb,
lib/fOOrth/compiler.rb,
lib/fOOrth/initialize.rb,
lib/fOOrth/symbol_map.rb,
lib/fOOrth/interpreter.rb,
lib/fOOrth/compiler/cast.rb,
lib/fOOrth/library/stubs.rb,
lib/fOOrth/compiler/modes.rb,
lib/fOOrth/compiler/token.rb,
lib/fOOrth/debug/dbg_puts.rb,
lib/fOOrth/compiler/parser.rb,
lib/fOOrth/compiler/source.rb,
lib/fOOrth/compiler/context.rb,
lib/fOOrth/compiler/process.rb,
lib/fOOrth/interpreter/squash.rb,
lib/fOOrth/library/vm_library.rb,
lib/fOOrth/compiler/word_specs.rb,
lib/fOOrth/debug/display_abort.rb,
lib/fOOrth/interpreter/do_loop.rb,
lib/fOOrth/compiler/parser/skip.rb,
lib/fOOrth/core/virtual_machine.rb,
lib/fOOrth/library/hash_library.rb,
lib/fOOrth/library/time_library.rb,
lib/fOOrth/compiler/context/tags.rb,
lib/fOOrth/compiler/modes/nested.rb,
lib/fOOrth/library/alias_library.rb,
lib/fOOrth/library/array_library.rb,
lib/fOOrth/library/class_library.rb,
lib/fOOrth/library/clone_library.rb,
lib/fOOrth/library/duration/make.rb,
lib/fOOrth/library/fiber_library.rb,
lib/fOOrth/library/mutex_library.rb,
lib/fOOrth/library/queue_library.rb,
lib/fOOrth/library/stack_library.rb,
lib/fOOrth/library/stdio_library.rb,
lib/fOOrth/library/stubs_library.rb,
lib/fOOrth/compiler/modes/delayed.rb,
lib/fOOrth/compiler/modes/suspend.rb,
lib/fOOrth/compiler/parser/normal.rb,
lib/fOOrth/interpreter/data_stack.rb,
lib/fOOrth/library/bundle_library.rb,
lib/fOOrth/library/object_library.rb,
lib/fOOrth/library/string_library.rb,
lib/fOOrth/library/thread_library.rb,
lib/fOOrth/compiler/context/locals.rb,
lib/fOOrth/compiler/modes/compiled.rb,
lib/fOOrth/compiler/modes/deferred.rb,
lib/fOOrth/compiler/parser/special.rb,
lib/fOOrth/compiler/process/string.rb,
lib/fOOrth/compiler/source/console.rb,
lib/fOOrth/interpreter/add_to_hash.rb,
lib/fOOrth/library/command_library.rb,
lib/fOOrth/library/compile_library.rb,
lib/fOOrth/library/complex_library.rb,
lib/fOOrth/library/integer_library.rb,
lib/fOOrth/library/numeric_library.rb,
lib/fOOrth/monkey_patch/exceptions.rb,
lib/fOOrth/library/data_ref_library.rb,
lib/fOOrth/library/duration_library.rb,
lib/fOOrth/library/introspection/vm.rb,
lib/fOOrth/library/rational_library.rb,
lib/fOOrth/compiler/context/map_name.rb,
lib/fOOrth/compiler/process/generate.rb,
lib/fOOrth/library/in_stream_library.rb,
lib/fOOrth/library/procedure_library.rb,
lib/fOOrth/compiler/parser/get_string.rb,
lib/fOOrth/compiler/process/get_token.rb,
lib/fOOrth/compiler/process/procedure.rb,
lib/fOOrth/compiler/source/read_point.rb,
lib/fOOrth/library/duration/formatter.rb,
lib/fOOrth/library/duration/intervals.rb,
lib/fOOrth/library/formatting/bullets.rb,
lib/fOOrth/library/formatting/columns.rb,
lib/fOOrth/library/out_stream_library.rb,
lib/fOOrth/compiler/source/file_source.rb,
lib/fOOrth/library/ctrl_struct_library.rb,
lib/fOOrth/library/duration/arithmetic.rb,
lib/fOOrth/library/sync_bundle_library.rb,
lib/fOOrth/compiler/source/string_source.rb,
lib/fOOrth/library/introspection/context.rb,
lib/fOOrth/library/introspection_library.rb,
lib/fOOrth/library/introspection/symbol_map.rb,
lib/fOOrth/library/introspection/word_specs.rb,
lib/fOOrth/library/other_value_types_library.rb

Overview

  • library/other_value_types_library.rb - Support for true, false, and nil in

the fOOrth library.

Defined Under Namespace

Modules: ReadPoint, SymbolMap Classes: AbstractSource, AbstractWordSpec, BulletPoints, ClassSpec, ColumnizedPage, Console, Context, Duration, FileSource, ForceExit, GlobalVarSpec, InstanceVarSpec, LocalSpec, MacroSpec, NosSpec, Parser, SelfSpec, SilentExit, StringSource, ThreadVarSpec, Token, TosSpec, VirtualMachine, VmSpec, XfOOrthError, XfOOrth_Bundle, XfOOrth_Fiber, XfOOrth_InStream, XfOOrth_OutStream, XfOOrth_SyncBundle

Constant Summary collapse

TimeFormat =

The user facing format used for date/time display.

'%Y-%m-%d at %I:%M%P'
VERSION =

The version string for fOOrth.

"0.6.10"
DC_VAR =

The procedure used for dot colon instance vars

lambda {|vm|
  var_name = vm.parser.get_word()

  unless /^@[a-z][a-z0-9_]*$/ =~ var_name
    error "F10: Invalid var name #{var_name}"
  end

  var_symbol = XfOOrth::SymbolMap.add_entry(var_name)
  vm << "#{'@'+(var_symbol.to_s)} = [vm.pop]; "

  vm.context.target_class.create_shared_method(var_name, InstanceVarSpec, [])
}
DC_VAL =

The procedure used for dot colon instance vals

lambda {|vm|
  val_name = vm.parser.get_word()

  unless /^@[a-z][a-z0-9_]*$/ =~ val_name
    error "F10: Invalid val name #{val_name}"
  end

  val_symbol = XfOOrth::SymbolMap.add_entry(val_name)
  vm << "#{'@'+(val_symbol.to_s)} = vm.pop; "

  vm.context.target_class.create_shared_method(val_name, InstanceVarSpec, [])
}
DCC_VAR =

The procedure used for dot colon colon instance vars

lambda { |vm|
  var_name = vm.parser.get_word()

  unless /^@[a-z][a-z0-9_]*$/ =~ var_name
    error "F10: Invalid var name #{var_name}"
  end

  var_symbol = XfOOrth::SymbolMap.add_entry(var_name)
  vm << "#{'@'+(var_symbol.to_s)} = [vm.pop]; "

  vm.context.target_object.create_exclusive_method(var_name, InstanceVarSpec, [])
}
DCC_VAL =

The procedure used for dot colon colon instance vals

lambda {|vm|
  val_name = vm.parser.get_word()

  unless /^@[a-z][a-z0-9_]*$/ =~ val_name
    error "F10: Invalid val name #{val_name}"
  end

  val_symbol = XfOOrth::SymbolMap.add_entry(val_name)
  vm << "#{'@'+(val_symbol.to_s)} = vm.pop; "

  vm.context.target_object.create_exclusive_method(val_name, InstanceVarSpec, [])
}
DegreesPerRadian =

The number of degrees in one radian.

180.0/Math::PI
Local_Var_Action =

The lambda used to define local variables. fOOrth language definition is:

n

var: lv [], lv = [n]

lambda {|vm|
  name   = vm.parser.get_word()
  error "F10: Invalid var name #{name}" unless /^[a-z][a-z0-9_]*$/ =~ name
  symbol = XfOOrth::SymbolMap.add_entry(name)
  vm << "#{symbol} = [vm.pop]; "

  #Add a local defn for the local variable.
  vm.context.create_local_method(name, LocalSpec, [:immediate],
    &lambda {|vm| vm << "vm.push(#{symbol}); "} )
}
Local_Val_Action =

The lambda used to define local values. fOOrth language definition is:

n

val: lv [], lv = n

lambda {|vm|
  name   = vm.parser.get_word()
  error "F10: Invalid val name #{name}" unless /^[a-z][a-z0-9_]*$/ =~ name
  symbol = XfOOrth::SymbolMap.add_entry(name)
  vm << "#{symbol} = vm.pop; "

  #Add a local defn for the local variable.
  vm.context.create_local_method(name, LocalSpec, [:immediate],
    &lambda {|vm| vm << "vm.push(#{symbol}); "} )
}

Class Method Summary collapse

Class Method Details

.add_common_compiler_locals(vm, ctrl) ⇒ Object

Set up the common local defns.
Parameters:

  • vm - The current virtual machine instance.

  • ctrl - A list of valid start controls.


Endemic Code Smells

  • :reek:TooManyStatements



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/fOOrth/library/compile_library.rb', line 193

def self.add_common_compiler_locals(vm, ctrl)
  context = vm.context

  #Support for local data.
  context.create_local_method('var:', LocalSpec, [:immediate], &Local_Var_Action)
  context.create_local_method('val:', LocalSpec, [:immediate], &Local_Val_Action)

  #Support for super methods.
  context.create_local_method('super', LocalSpec, [:immediate],
    &lambda {|vm| vm << 'super(vm); ' })

  #The standard end-compile adapter word: ';' semi-colon.
  context.create_local_method(';', LocalSpec, [:immediate], &lambda {|vm|
    vm.clear_cast
    vm.end_compile_mode([ctrl])
  })
end

.add_dot_colon_colon_locals(context) ⇒ Object

Add locals specific to a dot colon colon methods.



180
181
182
183
# File 'lib/fOOrth/library/compile_library.rb', line 180

def self.add_dot_colon_colon_locals(context)
  context.create_local_method('var@:', LocalSpec, [:immediate], &DCC_VAR)
  context.create_local_method('val@:', LocalSpec, [:immediate], &DCC_VAL)
end

.add_dot_colon_locals(context) ⇒ Object

Add locals specific to a dot colon methods.



122
123
124
125
# File 'lib/fOOrth/library/compile_library.rb', line 122

def self.add_dot_colon_locals(context)
  context.create_local_method('var@:', LocalSpec, [:immediate], &DC_VAR)
  context.create_local_method('val@:', LocalSpec, [:immediate], &DC_VAL)
end

.announcementsObject

Display the start-up messages for the interactive session.



57
58
59
60
61
# File 'lib/fOOrth/main.rb', line 57

def self.announcements
  puts "Welcome to fOOrth: fO(bject)O(riented)rth."
  puts "\nfOOrth Reference Implementation Version: #{XfOOrth.version}"
  puts "\nSession began on: #{Time.now.strftime(TimeFormat)}"
end

.mainObject

The starting point for an interactive fOOrth programming session. This method only returns when the session is closed.
Returns:

  • The virtual machine used to run the session.


To launch a fOOrth interactive session, simply use: XfOOrth::main
Endemic Code Smells

  • :reek:TooManyStatements



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
# File 'lib/fOOrth/main.rb', line 19

def self.main
  vm = VirtualMachine.vm('Console')
  running = false

  loop do
    begin
      running ||= start_up(vm)
      vm.process_console

    rescue Interrupt, ForceExit, SilentExit, MiniReadlineEOI => err
      puts "\n#{err.foorth_message}"
      break

    rescue StandardError => err
      vm.display_abort(err)

    rescue Exception => err
      puts "\n#{err.class.to_s.gsub(/.*::/, '')} detected: #{err}"
      puts err.backtrace
      break
    end

    break unless running
  end

  vm
end

.name_to_type(name, vm) ⇒ Object

Determine the type of method being created. This only applies to non-vm methods as vm methods are all of type VmSpec.
Parameters *name - The name of the method to be created. <Returns>

  • The class of the spec to be used for this method.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/fOOrth/library/compile_library.rb', line 217

def self.name_to_type(name, vm)
  normal_spec = case name[0]
  when '.'
    TosSpec

  when '~'
    SelfSpec

  when /[0-9A-Z$@#]/
    error "F10: Invalid name for a method: #{name}"

  else
    NosSpec
  end

  vm.get_cast || normal_spec
end

.process_command_line_optionsObject

Process the command line arguments. A string is returned containing fOOrth commands to be executed after the dictionary is loaded.
Returns

  • A string of fOOrth commands to be executed after the dictionary is loaded.


Endemic Code Smells

  • :reek:TooManyStatements



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fOOrth/main.rb', line 69

def self.process_command_line_options
  begin

    defer, found = "", false

    opts = GetoptLong.new(
      [ "--help",  "-h", "-?", GetoptLong::NO_ARGUMENT ],
      [ "--load",  "-l",       GetoptLong::REQUIRED_ARGUMENT ],
      [ "--debug", "-d",       GetoptLong::NO_ARGUMENT ],
      [ "--show",  "-s",       GetoptLong::NO_ARGUMENT ],
      [ "--quit",  "-q",       GetoptLong::NO_ARGUMENT ],
      [ "--words", "-w",       GetoptLong::NO_ARGUMENT ])

    # Translate the parsed options into fOOrth.
    opts.each do |opt, arg|

      unless found
        puts
        found = true
      end

      case opt
      when "--debug"
        defer << ")debug "
      when "--show"
        defer << ")show "
      when "--load"
        defer << ")load#{arg.inspect}"
      when "--quit"
        defer << ")quit "
      when "--words"
        defer << ")words "
      else
        fail SilentExit
      end
    end

    puts if found

  rescue Exception
    puts
    puts "fOOrth available options:"
    puts
    puts "--help  -h  -?          Display this message and exit."
    puts "--load  -l <filename>   Load the specified fOOrth source file."
    puts "--debug -d              Default to debug ON."
    puts "--quit  -q              Quit after processing the command line."
    puts "--show  -s              Default to show results ON."
    puts "--words -w              List the current vocabulary."
    puts
    raise SilentExit
  end

  defer
end

.start_up(vm) ⇒ Object

Perform one time start-up actions.



48
49
50
51
52
53
54
# File 'lib/fOOrth/main.rb', line 48

def self.start_up(vm)
  announcements
  vm.debug = false
  vm.show_stack = false
  vm.process_string(process_command_line_options)
  true
end

.validate_string_method(type, target, name) ⇒ Object

Check for the case where a string method is created on another class.
Parameters *type - The class of the method to be created. *target - The object that is to receive this method. *name - The name of the method to be created.
Endemic Code Smells

  • :reek:ControlParameter – false positive



260
261
262
263
264
# File 'lib/fOOrth/library/compile_library.rb', line 260

def self.validate_string_method(type, target, name)
  if type == TosSpec && name[-1] == '"' && target != String
    error "F13: Creating a string method #{name} on a #{target.foorth_name}"
  end
end

.validate_type(vm, type, name) ⇒ Object

Compare the new method’s spec against the specs of other methods of the same name. If no specs exist, create one on Object if the new spec is not a virtual machine spec.
Parameters *vm - The current virtual machine. *type - The class of the method to be created. *name - The name of the method to be created.



242
243
244
245
246
247
248
249
250
251
# File 'lib/fOOrth/library/compile_library.rb', line 242

def self.validate_type(vm, type, name)
  if (spec = vm.context.map_without_defaults(name))
    if spec.class != type
      error "F90: Spec type mismatch #{spec.foorth_name} vs #{type.foorth_name}"
    end
  else
    Object.create_shared_method(name, type, [:stub]) unless type == VmSpec
  end

end

.versionObject

The version of this module.
Returns

  • A version string; <major>.<minor>.<step>



36
37
38
# File 'lib/fOOrth.rb', line 36

def self.version
  VERSION
end