class CTioga2::Commands::Interpreter

The core class interpreting all the commands and executing them. It holds a hash class variable containing all the Command objects defined so far.

Attributes

command_line_parser[R]

The Parsers::CommandLineParser object used to… parse the command-line. (surprising, isn't it ??)

context[RW]

The current context

doc[R]

The Documentation::Doc object that can interact with documentation

file_parser[R]

The Parsers::FileParser object used to… parse files ?

instructions[RW]

The list of Instruction that were run so far

plotmaker_target[RW]

The PlotMaker object that will receive the commands of the Interpreter.

variables[RW]

A Variables object holding the … variables ! (I'm sure you guessed it !)

Public Class Methods

command(cmd) click to toggle source

Returns the command given by its name cmd, or nil if none was found.

# File lib/ctioga2/commands/interpreter.rb, line 128
def self.command(cmd)
  return @@commands[cmd]
end
commands() click to toggle source

Returns the commands hash

# File lib/ctioga2/commands/interpreter.rb, line 138
def self.commands
  return @@commands
end
delete_command(cmd) click to toggle source

Deletes a command whose name is given

# File lib/ctioga2/commands/interpreter.rb, line 123
def self.delete_command(cmd)
  @@commands.delete(cmd)
end
group(id) click to toggle source

Returns the groups given by its id, or nil if none was found.

# File lib/ctioga2/commands/interpreter.rb, line 133
def self.group(id)
  return @@groups[id]
end
groups() click to toggle source

Returns the groups hash

# File lib/ctioga2/commands/interpreter.rb, line 143
def self.groups
  return @@groups
end
new(target) click to toggle source

Creates an Interpreter with target as the PlotMaker target object.

As far as command-line and help is concerned, it takes a snapshot of the current commands known to the system, so please instantiate it last.

todo probably this behavior is not really desired. Easy to fix.

# File lib/ctioga2/commands/interpreter.rb, line 180
def initialize(target)
  @plotmaker_target = target
  @command_line_parser = 
    Parsers::CommandLineParser.new(@@commands.values, 
                                   CTioga2::PlotMaker::PlotCommand)

  @doc = Documentation::Doc.new()
  @variables = Variables.new

  # We import the variables from the environment, just like a in
  # a Makefile
  for k, v in ENV
    @variables.define_variable(k, v)
  end

  @file_parser = Parsers::FileParser.new
  @context = ParsingContext.new
  @instructions = []
end
register_command(command) click to toggle source

Registers a given command. This is called automatically from Command.new, so you should not have to do it yourself.

# File lib/ctioga2/commands/interpreter.rb, line 71
def self.register_command(command)
  if self.command(command.name)
    raise DoubleDefinition, "Command '#{command.name}' already defined"
  else
    if command.name =~ NameValidationRE
      @@commands[command.name] = command
    else
      raise InvalidName, "Name '#{command.name}' is invalid"
    end
  end
end
register_group(group) click to toggle source

Registers a given group. This is called automatically from CommandGroup.new, so you should not have to do it yourself.

# File lib/ctioga2/commands/interpreter.rb, line 85
def self.register_group(group)
  if self.group(group.id)
    raise DoubleDefinition, "Group '#{group.id}' already defined"
  else
    if group.id =~ NameValidationRE
      @@groups[group.id] = group
    else
      raise InvalidName, "Name '#{group.id}' is invalid"
    end
  end
end
register_type(type) click to toggle source

Registers a given type. This is called automatically from CommandType.new, so you should not have to do it yourself.

# File lib/ctioga2/commands/interpreter.rb, line 99
def self.register_type(type)
  if self.type(type.name)
    raise DoubleDefinition, "Type '#{type.name}' already defined"
  else
    if type.name =~ NameValidationRE
      @@types[type.name] = type
    else
      raise InvalidName, "Name '#{type.name}' is invalid"
    end
  end
end
type(name) click to toggle source

Returns the named CommandType

# File lib/ctioga2/commands/interpreter.rb, line 113
def self.type(name)
  return @@types[name]
end
types() click to toggle source

Returns all registered CommandType objects

# File lib/ctioga2/commands/interpreter.rb, line 118
def self.types
  return @@types
end

Public Instance Methods

add_instruction(instruction) click to toggle source

Adds the given instruction to the list of Instruction that were run so far.

# File lib/ctioga2/commands/interpreter.rb, line 202
def add_instruction(instruction)
  @instructions << instruction
end
call_function(name, args) click to toggle source

Calls the given function and returns the result

# File lib/ctioga2/commands/interpreter.rb, line 208
def call_function(name, args)
  func = Function.named_function(name)
  if ! func
    raise "Unkown function #{name}"
  end
  return func.expand(args, self)
end
command_names() click to toggle source

Returns the list of all know command names

# File lib/ctioga2/commands/interpreter.rb, line 258
def command_names
  return @@commands.keys
end
get_command(symbol) click to toggle source

Returns a Command object corresponding to the given symbol, or raises an UnknownCommand exception.

# File lib/ctioga2/commands/interpreter.rb, line 249
def get_command(symbol)
  if @@commands.key? symbol
    return @@commands[symbol]
  else
    raise UnknownCommand, "Unknown command: #{symbol}"
  end
end
run_command(command, arguments, options = nil) click to toggle source

Runs command with the given arguments and options, converting them as necessary. All the commands ran from this interpreter should be ran from here.

command can be either a String or a Command

Later, it could be a good idea to add a spying mechanism here.

# File lib/ctioga2/commands/interpreter.rb, line 269
def run_command(command, arguments, options = nil)
  converted_args = command.convert_arguments(arguments)
  if options
    converted_options = command.convert_options(options)
  else
    converted_options = nil
  end
  command.run_command(@plotmaker_target, converted_args,
                      converted_options)
end
run_command_file(file) click to toggle source

Parses and runs the given file. Sets PlotMaker#figure_name to the base name of the given file if no figure name was specified.

# File lib/ctioga2/commands/interpreter.rb, line 228
def run_command_file(file)
  if ! @plotmaker_target.figure_name
    @plotmaker_target.figure_name = file.gsub(/\.[^.]+$/,'').
      gsub(/%/, '%%')
  end

  dir = File::dirname(file)
  base = File::basename(file)

  Utils::chdir(dir) do
    @file_parser.run_command_file(base, self)
  end
end
run_command_line(args) click to toggle source

Parses and run the given command-line, sending the commands to the plotmaker_target.

# File lib/ctioga2/commands/interpreter.rb, line 219
def run_command_line(args)
  @command_line_parser.parse_command_line(args, self) do |arg|
    puts "Non-optional argument: #{arg.first}"
  end
end
run_commands(string) click to toggle source

Parses and runs the given string.

# File lib/ctioga2/commands/interpreter.rb, line 243
def run_commands(string)
  @file_parser.run_commands(string, self)
end