Class: Succubus::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/succubus/generator.rb

Overview

Class which handles running through a Grammar and producing a Result string.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grammar, seed = nil) ⇒ Generator

Create a new Succubus::Generator, ready to run through the supplied Succubus::Grammar, optionally with the given random seed.

Parameters:



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/succubus/generator.rb', line 25

def initialize(grammar, seed=nil)
  @rules = grammar.rules

  @seed = seed
  unless @seed
    srand()
    @seed = rand(0xffffffff)
  end
  srand(@seed)

  @errors = []
end

Class Method Details

.run(grammar, rule, seed = nil) ⇒ Object

Run through a given Succubus::Grammar, starting at a given rule. Optionally use a given random seed.

Parameters:

  • grammar (Grammar)

    the Succubus::Grammar to generate a string from

  • rule (Symbol)

    the name of the rule to start generation from

  • seed (Integer) (defaults to: nil)

    optional random seed. Defaults to nil, which will make the Succubus::Generator use a random seed



15
16
17
# File 'lib/succubus/generator.rb', line 15

def self.run(grammar, rule, seed=nil)
  new(grammar, seed).run(rule)
end

Instance Method Details

#run(rule) ⇒ Result

Produce a random Result string from the Generator’s Succubus::Grammar

Parameters:

  • rule (Symbol)

    the name of the rule to start generation from

Returns:



42
43
44
45
46
47
# File 'lib/succubus/generator.rb', line 42

def run(rule)
  @result = Result.new(@seed, invoke(rule))
  @result.set_errors(@errors)

  return @result
end