Class: Pione::Lang::Environment
- Inherits:
-
StructX
- Object
- StructX
- Pione::Lang::Environment
- Defined in:
- lib/pione/lang/environment.rb
Overview
Environment is a environment of language interpretation.
Instance Method Summary collapse
- #add_package(package_name, parent_ids = []) ⇒ Object
-
#dumpable ⇒ Object
Return a new environment that we can dump.
-
#entrance_rule(package_id = current_package_id) ⇒ RuleDeclaration
Return entrance rule.
-
#entrance_rule_name(package_id = current_package_id) ⇒ String
Return name of entrance rule.
-
#find_ancestor_ids(package_id) ⇒ Object
Find ancestor's IDs of the package ID.
- #find_package_id_by_package_name(package_name) ⇒ Object
-
#layer ⇒ Object
Create a new environment which tables are overlayed current tables.
-
#make_root_rule(main_param_set) ⇒ RuleDefinition
Make a root rule definition.
-
#merge_param_set(param_set, option = {}) ⇒ Object
Merge the parameter set as variables into this environment.
-
#package_get(ref) ⇒ Object
Get the definition of package.
-
#package_set(ref, val) ⇒ Object
Set the definition of package.
-
#rule_get(ref) ⇒ Object
Get the definition of rule expression.
- #rule_get!(ref) ⇒ Object
-
#rule_get_value(ref) ⇒ Object
Get the expression of the reference.
-
#rule_set(ref, val) ⇒ Object
Set the definition of rule expression.
-
#setup_new_package(package_name, parent_ids = []) ⇒ Object
Introduce new package in the environment.
-
#setup_package_id(ref) ⇒ Object
Set current package id to the reference if the package id is unknown.
-
#temp(temporary_info, &b) ⇒ Object
Make a temporary environment with the temporary informations and evaluate the block in it.
-
#variable_get(ref) ⇒ Object
Get the value of variable.
- #variable_get!(ref) ⇒ Object
-
#variable_set(ref, val) ⇒ Object
Set the value of variable.
- #variable_set!(ref, val) ⇒ Object
Instance Method Details
#add_package(package_name, parent_ids = []) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/pione/lang/environment.rb', line 345 def add_package(package_name, parent_ids=[]) # generate a new package id package_id = Util::PackageID.generate(self, package_name) # add package id table package_id_table[package_name] = package_id # make reference and definition ref = PackageExpr.new(name: package_name, package_id: package_id) definition = PackageDefinition.new(package_id: package_id, parent_ids: parent_ids) # set it to package table package_set(ref, definition) # return package id return package_id, definition end |
#dumpable ⇒ Object
Return a new environment that we can dump. This is because deleagatable tables use hashs with default proc, so it is not able to dump by using +Marshal+.
405 406 407 |
# File 'lib/pione/lang/environment.rb', line 405 def dumpable set(variable_table: variable_table.dumpable, rule_table: rule_table.dumpable) end |
#entrance_rule(package_id = current_package_id) ⇒ RuleDeclaration
Return entrance rule.
259 260 261 |
# File 'lib/pione/lang/environment.rb', line 259 def entrance_rule(package_id=current_package_id) rule_get!(Lang::RuleExpr.new(entrance_rule_name(package_id))) end |
#entrance_rule_name(package_id = current_package_id) ⇒ String
Return name of entrance rule.
240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/pione/lang/environment.rb', line 240 def entrance_rule_name(package_id=current_package_id) # find main rule name from annotations annotations = package_get(Lang::PackageExpr.new(package_id: package_id)).annotations annotations.each do |annotation| if annotation.annotation_type == "Entrance" return annotation.pieces.first.name end end # default name return "Main" end |
#find_ancestor_ids(package_id) ⇒ Object
Find ancestor's IDs of the package ID. The way of ancestors search is depth-first.
303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/pione/lang/environment.rb', line 303 def find_ancestor_ids(package_id) ancestor_ids = [] if package = package_get(PackageExpr.new(package_id: package_id)) ancestor_ids += package.parent_ids package.parent_ids.each do |parent_id| find_ancestor_ids(parent_id).each do |ancestor_id| if not(ancestor_ids.include?(ancestor_id)) ancestor_ids << ancestor_id end end end end return ancestor_ids end |
#find_package_id_by_package_name(package_name) ⇒ Object
298 299 300 |
# File 'lib/pione/lang/environment.rb', line 298 def find_package_id_by_package_name(package_name) package_id_table[package_name] end |
#layer ⇒ Object
Create a new environment which tables are overlayed current tables.
319 320 321 322 323 324 325 326 327 328 |
# File 'lib/pione/lang/environment.rb', line 319 def layer self.class.new( variable_table: VariableTable.new(variable_table), rule_table: RuleTable.new(rule_table), package_table: package_table, package_ids: package_ids, current_package_id: current_package_id, current_definition: current_definition ) end |
#make_root_rule(main_param_set) ⇒ RuleDefinition
Make a root rule definition.
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/pione/lang/environment.rb', line 373 def make_root_rule(main_param_set) # put variable of parameter set for main rule variable_set(Variable.new("MAIN_PARAM_SET"), ParameterSetSequence.of(main_param_set)) # make root rule Package::Document.parse(<<-PIONE % entrance_rule_name, current_package_id, nil, nil, "*System*").eval(self) Rule Root input '*'.all or null output '*'.all Flow rule %s.param($MAIN_PARAM_SET) End PIONE rule_get(RuleExpr.new("Root")) end |
#merge_param_set(param_set, option = {}) ⇒ Object
Merge the parameter set as variables into this environment. Rebinding variables raise error, but it is ignored when force flag is true.
336 337 338 339 340 341 342 343 |
# File 'lib/pione/lang/environment.rb', line 336 def merge_param_set(param_set, option={}) param_set.keys.each do |key| var = Variable.new(name: key, package_id: current_package_id) val = param_set[key] option[:force] ? variable_set!(var, val) : variable_set(var, val) end return self end |
#package_get(ref) ⇒ Object
Get the definition of package.
278 279 280 281 282 |
# File 'lib/pione/lang/environment.rb', line 278 def package_get(ref) raise ArgumentError.new(ref.inspect) unless ref.package_id package_table.get(ref) end |
#package_set(ref, val) ⇒ Object
Set the definition of package.
285 286 287 288 289 |
# File 'lib/pione/lang/environment.rb', line 285 def package_set(ref, val) raise ArgumentError.new(ref.inspect) unless ref.package_id package_table.set(ref, val) end |
#rule_get(ref) ⇒ Object
Get the definition of rule expression. We use current package id if it is implicit.
226 227 228 |
# File 'lib/pione/lang/environment.rb', line 226 def rule_get(ref) rule_table.get(self, setup_package_id(ref)) end |
#rule_get!(ref) ⇒ Object
230 231 232 |
# File 'lib/pione/lang/environment.rb', line 230 def rule_get!(ref) rule_table.get!(self, setup_package_id(ref)) end |
#rule_get_value(ref) ⇒ Object
Get the expression of the reference.
264 265 266 267 268 269 270 |
# File 'lib/pione/lang/environment.rb', line 264 def rule_get_value(ref) if val = rule_table.get_value(self, setup_package_id(ref)) return val else (raise UnboundError.new(ref)) end end |
#rule_set(ref, val) ⇒ Object
Set the definition of rule expression.
273 274 275 |
# File 'lib/pione/lang/environment.rb', line 273 def rule_set(ref, val) rule_table.set(setup_package_id(ref), val) end |
#setup_new_package(package_name, parent_ids = []) ⇒ Object
Introduce new package in the environment.
360 361 362 363 364 365 |
# File 'lib/pione/lang/environment.rb', line 360 def setup_new_package(package_name, parent_ids=[]) package_id, definition = add_package(package_name, parent_ids) # update current package id set(current_package_id: package_id, current_definition: definition) end |
#setup_package_id(ref) ⇒ Object
Set current package id to the reference if the package id is unknown.
390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/pione/lang/environment.rb', line 390 def setup_package_id(ref) return ref if ref.package_id # check current package id unless current_package_id raise EnvironmentError.new("we couldn't determine the package id: %s" % ref) end # create new reference with the package id ref.set(package_id: current_package_id) end |
#temp(temporary_info, &b) ⇒ Object
Make a temporary environment with the temporary informations and evaluate the block in it. This is for evaluating declarations.
293 294 295 296 |
# File 'lib/pione/lang/environment.rb', line 293 def temp(temporary_info, &b) set(temporary_info).tap {|x| yield(x)} return nil end |
#variable_get(ref) ⇒ Object
Get the value of variable. We use current package id if it is implicit.
207 208 209 |
# File 'lib/pione/lang/environment.rb', line 207 def variable_get(ref) variable_table.get(self, setup_package_id(ref)) end |
#variable_get!(ref) ⇒ Object
211 212 213 |
# File 'lib/pione/lang/environment.rb', line 211 def variable_get!(ref) variable_table.get!(self, setup_package_id(ref)) end |