Class: CTioga2::Commands::Interpreter
- Inherits:
-
Object
- Object
- CTioga2::Commands::Interpreter
- Defined in:
- lib/ctioga2/commands/interpreter.rb
Overview
The core class interpreting all the commands and executing them. It holds a hash class variable containing all the Command objects defined so far.
Constant Summary collapse
- @@commands =
All commands defined so far.
{}
- @@groups =
All command groups defined so far.
{}
- @@types =
All types defined so fat
{}
Instance Attribute Summary collapse
-
#command_line_parser ⇒ Object
readonly
The Parsers::CommandLineParser object used to…
-
#context ⇒ Object
The current context.
-
#doc ⇒ Object
readonly
The Documentation::Doc object that can interact with documentation.
-
#file_parser ⇒ Object
readonly
The Parsers::FileParser object used to…
-
#instructions ⇒ Object
The list of Instruction that were run so far.
-
#plotmaker_target ⇒ Object
The PlotMaker object that will receive the commands of the Interpreter.
-
#variables ⇒ Object
A Variables object holding the …
Class Method Summary collapse
-
.command(cmd) ⇒ Object
Returns the command given by its name cmd, or nil if none was found.
-
.commands ⇒ Object
Returns the commands hash.
-
.delete_command(cmd) ⇒ Object
Deletes a command whose name is given.
-
.group(id) ⇒ Object
Returns the groups given by its id, or nil if none was found.
-
.groups ⇒ Object
Returns the groups hash.
-
.register_command(command) ⇒ Object
Registers a given command.
-
.register_group(group) ⇒ Object
Registers a given group.
-
.register_type(type) ⇒ Object
Registers a given type.
-
.type(name) ⇒ Object
Returns the named CommandType.
-
.types ⇒ Object
Returns all registered CommandType objects.
Instance Method Summary collapse
-
#add_instruction(instruction) ⇒ Object
Adds the given instruction to the list of Instruction that were run so far.
-
#call_function(name, args) ⇒ Object
Calls the given function and returns the result.
-
#command_names ⇒ Object
Returns the list of all know command names.
-
#get_command(symbol) ⇒ Object
Returns a Command object corresponding to the given symbol, or raises an UnknownCommand exception.
-
#initialize(target) ⇒ Interpreter
constructor
Creates an Interpreter with target as the PlotMaker target object.
-
#run_command(command, arguments, options = nil) ⇒ Object
Runs command with the given arguments and options, converting them as necessary.
-
#run_command_file(file) ⇒ Object
Parses and runs the given file.
-
#run_command_line(args) ⇒ Object
Parses and run the given command-line, sending the commands to the #plotmaker_target.
-
#run_commands(string) ⇒ Object
Parses and runs the given string.
Constructor Details
#initialize(target) ⇒ Interpreter
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.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# 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 |
Instance Attribute Details
#command_line_parser ⇒ Object (readonly)
The Parsers::CommandLineParser object used to… parse the command-line. (surprising, isn’t it ??)
157 158 159 |
# File 'lib/ctioga2/commands/interpreter.rb', line 157 def command_line_parser @command_line_parser end |
#context ⇒ Object
The current context
166 167 168 |
# File 'lib/ctioga2/commands/interpreter.rb', line 166 def context @context end |
#doc ⇒ Object (readonly)
The Documentation::Doc object that can interact with documentation
160 161 162 |
# File 'lib/ctioga2/commands/interpreter.rb', line 160 def doc @doc end |
#file_parser ⇒ Object (readonly)
The Parsers::FileParser object used to… parse files ?
163 164 165 |
# File 'lib/ctioga2/commands/interpreter.rb', line 163 def file_parser @file_parser end |
#instructions ⇒ Object
The list of Instruction that were run so far
169 170 171 |
# File 'lib/ctioga2/commands/interpreter.rb', line 169 def instructions @instructions end |
#plotmaker_target ⇒ Object
The PlotMaker object that will receive the commands of the Interpreter.
153 154 155 |
# File 'lib/ctioga2/commands/interpreter.rb', line 153 def plotmaker_target @plotmaker_target end |
#variables ⇒ Object
A Variables object holding the … variables ! (I’m sure you guessed it !)
149 150 151 |
# File 'lib/ctioga2/commands/interpreter.rb', line 149 def variables @variables end |
Class Method Details
.command(cmd) ⇒ Object
Returns the command given by its name cmd, or nil if none was found.
128 129 130 |
# File 'lib/ctioga2/commands/interpreter.rb', line 128 def self.command(cmd) return @@commands[cmd] end |
.commands ⇒ Object
Returns the commands hash
138 139 140 |
# File 'lib/ctioga2/commands/interpreter.rb', line 138 def self.commands return @@commands end |
.delete_command(cmd) ⇒ Object
Deletes a command whose name is given
123 124 125 |
# File 'lib/ctioga2/commands/interpreter.rb', line 123 def self.delete_command(cmd) @@commands.delete(cmd) end |
.group(id) ⇒ Object
Returns the groups given by its id, or nil if none was found.
133 134 135 |
# File 'lib/ctioga2/commands/interpreter.rb', line 133 def self.group(id) return @@groups[id] end |
.groups ⇒ Object
Returns the groups hash
143 144 145 |
# File 'lib/ctioga2/commands/interpreter.rb', line 143 def self.groups return @@groups end |
.register_command(command) ⇒ Object
Registers a given command. This is called automatically from Command.new, so you should not have to do it yourself.
71 72 73 74 75 76 77 78 79 80 81 |
# 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) ⇒ Object
Registers a given group. This is called automatically from CommandGroup.new, so you should not have to do it yourself.
85 86 87 88 89 90 91 92 93 94 95 |
# 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) ⇒ Object
Registers a given type. This is called automatically from CommandType.new, so you should not have to do it yourself.
99 100 101 102 103 104 105 106 107 108 109 |
# 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) ⇒ Object
Returns the named CommandType
113 114 115 |
# File 'lib/ctioga2/commands/interpreter.rb', line 113 def self.type(name) return @@types[name] end |
.types ⇒ Object
Returns all registered CommandType objects
118 119 120 |
# File 'lib/ctioga2/commands/interpreter.rb', line 118 def self.types return @@types end |
Instance Method Details
#add_instruction(instruction) ⇒ Object
Adds the given instruction to the list of Instruction that were run so far.
202 203 204 |
# File 'lib/ctioga2/commands/interpreter.rb', line 202 def add_instruction(instruction) @instructions << instruction end |
#call_function(name, args) ⇒ Object
Calls the given function and returns the result
208 209 210 211 212 213 214 |
# 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.(args, self) end |
#command_names ⇒ Object
Returns the list of all know command names
258 259 260 |
# File 'lib/ctioga2/commands/interpreter.rb', line 258 def command_names return @@commands.keys end |
#get_command(symbol) ⇒ Object
Returns a Command object corresponding to the given symbol, or raises an UnknownCommand exception.
249 250 251 252 253 254 255 |
# 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) ⇒ Object
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.
269 270 271 272 273 274 275 276 277 278 |
# File 'lib/ctioga2/commands/interpreter.rb', line 269 def run_command(command, arguments, = nil) converted_args = command.convert_arguments(arguments) if = command.() else = nil end command.run_command(@plotmaker_target, converted_args, ) end |
#run_command_file(file) ⇒ Object
Parses and runs the given file. Sets PlotMaker#figure_name to the base name of the given file if no figure name was specified.
228 229 230 231 232 233 234 235 236 237 238 239 240 |
# 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) ⇒ Object
Parses and run the given command-line, sending the commands to the #plotmaker_target.
219 220 221 222 223 |
# 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) ⇒ Object
Parses and runs the given string.
243 244 245 |
# File 'lib/ctioga2/commands/interpreter.rb', line 243 def run_commands(string) @file_parser.run_commands(string, self) end |