class XfOOrth::Context
A class for the management of global, hierarchical, and nested compile time contexts.
-
compiler/context.rb - Context constructor, tag support, and local defs.
-
compiler/context/locals.rb - Support local methods in context.
-
compiler/context/map_name.rb - The fOOrth language mapping of names in a context.
-
compiler/context/tags.rb - Support tags in context.
Get information about this compiler context.
Attributes
The previous context object that this one builds on. Set to nil if there is none.
Public Class Methods
Setup an instance of compiler context.
Parameters:
-
previous - The previous context object or nil if there is none.
-
data - A hash of context data.
# File lib/fOOrth/compiler/context.rb, line 23 def initialize(previous, data={}) @previous, @data = previous, data end
Public Instance Methods
Retrieve the data value currently in effect.
# File lib/fOOrth/compiler/context/tags.rb, line 10 def [](index) @data[index] || (previous && previous[index]) end
Set a data value.
# File lib/fOOrth/compiler/context/tags.rb, line 15 def []=(index,value) @data[index] = value end
Is the current nesting level what is expected?
Parameters
-
expected_depth - the expected nesting depth.
Notes
-
Raises an error (F12) on incorrect nesting.
# File lib/fOOrth/compiler/context.rb, line 37 def check_depth(expected_depth) if expected_depth - self.depth != 0 error "F12: Error, Invalid control/structure nesting." end end
Validate a current data value.
Parameters:
-
symbol - The symbol of the value to be tested.
-
expect - An array of valid values.
Note:
-
Throws a
XfOOrthError
if the value is not valid. -
To check for no value, use [nil] for expect.
-
Returns true to facilitate testing only.
# File lib/fOOrth/compiler/context/tags.rb, line 49 def check_set(symbol, expect) current = self[symbol] unless expect.include?(current) error "F10: Found a #{current.inspect}, expected #{expect}" end true end
Create a local method on this context.
Parameters:
-
name - The name of the method to create.
-
spec_class - The specification class to use.
-
options - An array of options.
-
block - A block to associate with the name.
Returns
-
The spec created for the shared method.
# File lib/fOOrth/compiler/context/locals.rb, line 17 def create_local_method(name, spec_class, options, &block) sym = SymbolMap.add_entry(name) self[sym] = spec_class.new(name, sym, options, &block) end
How many levels of nested context are there?
# File lib/fOOrth/compiler/context.rb, line 28 def depth 1 + (previous ? previous.depth : 0) end
Get the context with the specified type.
# File lib/fOOrth/compiler/context/tags.rb, line 30 def get_context_by_ctrl(ctrl_type) result = self while result return result if result[:ctrl] == ctrl_type result = result.previous end error "F92: Unable to locate a context for #{ctrl_type.inspect}" end
Get introspection info.
Endemic Code Smells
-
:reek:FeatureEnvy :reek:TooManyStatements
# File lib/fOOrth/library/introspection/context.rb, line 12 def get_info results = [["Level", depth]] @data.each do |key, value| results << ["", ""] if value.is_a?(AbstractWordSpec) results << ["Name", SymbolMap.unmap(key)] results << ["Mapping", key] results.concat(value.get_info) else results << ["Name", key] results << ["Value", value] end end if (prev = self.previous) results.concat([["", ""]]).concat(prev.get_info) end results end
Map a name to a specification.
Parameters:
-
name - The string to be mapped.
Returns:
-
The specification that corresponds to the name or nil if none found.
# File lib/fOOrth/compiler/context/map_name.rb, line 14 def map_with_defaults(name) if (@symbol = SymbolMap.map(@name = name)) do_map_name || case @name[0] when '.' TosSpec.new(@name, @symbol, [:temp]) when '~' SelfSpec.new(@name, @symbol, [:temp]) else spec_error end end end
Map a name to a specification.
Parameters:
-
name - The string to be mapped.
Returns:
-
The specification that corresponds to the name or nil if none found.
# File lib/fOOrth/compiler/context/map_name.rb, line 35 def map_without_defaults(name) if (@symbol = SymbolMap.map(@name = name)) do_map_name end end
Merge in a hash of tag data.
# File lib/fOOrth/compiler/context/tags.rb, line 25 def merge(new_data) @data.merge!(new_data) end
Signal that no receiver was found in this context. This is an internal error indication.
# File lib/fOOrth/compiler/context.rb, line 60 def no_target_error error("F90: No target found in context.") end
Remove a local method on this context.
Parameters:
-
The name of the method to remove.
# File lib/fOOrth/compiler/context/locals.rb, line 25 def remove_local_method(name) if (sym = SymbolMap.map(name)) @data.delete(sym) else error "F90: Unable to remove local method #{name}" end end
Get the current target.
# File lib/fOOrth/compiler/context.rb, line 44 def target self[:obj] || self[:cls] || self[:vm] || no_target_error end
Get the current target class.
# File lib/fOOrth/compiler/context.rb, line 54 def target_class self[:cls] || no_target_error end
Get the current target object.
# File lib/fOOrth/compiler/context.rb, line 49 def target_object self[:obj] || no_target_error end
Private Instance Methods
Do a search of the globals.
# File lib/fOOrth/compiler/context/map_name.rb, line 75 def do_global_map $FOORTH_GLOBALS[@symbol] end
Do a search of dictionaries based on the syntax of the name.
# File lib/fOOrth/compiler/context/map_name.rb, line 45 def do_map_name self[@symbol] || do_target_class_map || do_target_object_map || do_target_vm_map || do_object_class_map || do_global_map end
Do a search of the Object
class for the item.
# File lib/fOOrth/compiler/context/map_name.rb, line 55 def do_object_class_map Object.map_foorth_shared(@symbol) end
Do a search of the :cls tag if it is specified.
# File lib/fOOrth/compiler/context/map_name.rb, line 60 def do_target_class_map (tc = self[:cls]) && tc.map_foorth_shared(@symbol) end
Do a search of the :obj tag if it is specified.
# File lib/fOOrth/compiler/context/map_name.rb, line 65 def do_target_object_map (to = self[:obj]) && to.map_foorth_exclusive(@symbol) end
Do a search of the :vm tag if it is specified.
# File lib/fOOrth/compiler/context/map_name.rb, line 70 def do_target_vm_map (vm = self[:vm]) && vm.map_foorth_exclusive(@symbol) end
Error: Unable to find a specification.
# File lib/fOOrth/compiler/context/map_name.rb, line 80 def spec_error error "F11: ?#{@name}?" end