Module: ConlangWordGenerator

Defined in:
lib/conlang/operators.rb,
lib/conlang/version.rb,
lib/conlang/symbolset.rb

Overview

Class for a set of primitive symbols and their weights.

Defined Under Namespace

Classes: Append, Maybe, Or, SymbolSet

Constant Summary collapse

VERSION =
'0.3.1'

Class Method Summary collapse

Class Method Details

.load_definitions(bindings) ⇒ Object

Interpreter



117
118
119
120
121
122
123
124
125
# 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) ⇒ Object



104
105
106
107
108
109
110
111
112
# 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) ⇒ Object

Functions for operators



94
95
96
97
98
99
100
101
102
# 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) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# 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