module ConlangWordGenerator

Base classes for operators.

Class for a set of primitive symbols and their weights.

Constants

VERSION

Public Class Methods

load_definitions(bindings) click to toggle source

Interpreter

# File lib/conlang/operators.rb, line 117
  def self.load_definitions(bindings)
  statements = ""

  bindings.each_key do |k|
    statements += "#{k} = bindings['#{k}']\n"
  end

  statements
end
maybeOp(*args) click to toggle source
# File lib/conlang/operators.rb, line 104
def self.maybeOp(*args)
  if args.length == 1
    Maybe.new(50, args[0]) 
  elsif args.length == 2
    Maybe.new(args[0].to_i, args[1])
  else
    raise LangSyntaxError, "Invalid Maybe() operator arguments."
  end
end
orOp(*args) click to toggle source

Functions for operators

# File lib/conlang/operators.rb, line 94
def self.orOp(*args)
  if args.length == 2
    Or.new(50, args[0], args[1])
  elsif args.length == 3
    Or.new(args[0].to_i, args[1], args[2])
  else
    raise LangSyntaxError, "Invalid Or() operator arguments."
  end
end
run_expression(expr, bindings) click to toggle source
# File lib/conlang/operators.rb, line 127
def self.run_expression(expr, bindings)
  # Modify method's names to avoid
  # errors on Ruby syntax.
  expr.gsub!(/\b[Oo]r\b/, 'orOp')
  expr.gsub!(/\b[Mm]aybe\b/, 'maybeOp')

  expr =  load_definitions(bindings) + expr

  # Evaluate all generated assigments
  # and then the grammatical expression.
  begin
    eval(expr)
  rescue
    raise LangSyntaxError, "Invalid operators or bindings " +
                           "in grammatical expression."
  end
end