Class: Fbe::Award::BTerm

Inherits:
Factbase::Term
  • Object
show all
Defined in:
lib/fbe/award.rb

Overview

A term for bill.

Instance Method Summary collapse

Constructor Details

#initialize(_fb, operator, operands) ⇒ BTerm

Returns a new instance of BTerm.



62
63
64
65
66
# File 'lib/fbe/award.rb', line 62

def initialize(_fb, operator, operands)
  super(nil, nil, nil)
  @op = operator
  @operands = operands
end

Instance Method Details

#abstract?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/fbe/award.rb', line 76

def abstract?
  false
end

#bill_to(bill) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fbe/award.rb', line 80

def bill_to(bill)
  case @op
  when :award
    @operands.each do |o|
      o.bill_to(bill)
    rescue StandardError => e
      raise "Failure in #{o}: #{e.message}"
    end
  when :aka
    @operands[0..-2].each do |o|
      o.bill_to(bill)
    rescue StandardError => e
      raise "Failure in #{o}: #{e.message}"
    end
  when :let, :set
    bill.set(@operands[0], to_val(@operands[1], bill))
  when :give
    text = @operands[1]
    text = '' if text.nil?
    bill.line(to_val(@operands[0], bill), text)
  when :explain, :in
    # nothing, just ignore
  else
    raise "Unknown term '#{@op}'"
  end
end

#calc(bill) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fbe/award.rb', line 119

def calc(bill)
  case @op
  when :total
    bill.points
  when :if
    to_val(@operands[0], bill) ? to_val(@operands[1], bill) : to_val(@operands[2], bill)
  when :and
    @operands.all? { |o| to_val(o, bill) }
  when :or
    @operands.any? { |o| to_val(o, bill) }
  when :not
    !to_val(@operands[0], bill)
  when :eq
    to_val(@operands[0], bill) == to_val(@operands[1], bill)
  when :lt
    to_val(@operands[0], bill) < to_val(@operands[1], bill)
  when :lte
    to_val(@operands[0], bill) <= to_val(@operands[1], bill)
  when :gt
    to_val(@operands[0], bill) > to_val(@operands[1], bill)
  when :gte
    to_val(@operands[0], bill) >= to_val(@operands[1], bill)
  when :div
    to_val(@operands[0], bill) / to_val(@operands[1], bill)
  when :times
    to_val(@operands[0], bill) * to_val(@operands[1], bill)
  when :plus
    to_val(@operands[0], bill) + to_val(@operands[1], bill)
  when :minus
    to_val(@operands[0], bill) - to_val(@operands[1], bill)
  when :max
    [to_val(@operands[0], bill), to_val(@operands[1], bill)].max
  when :min
    [to_val(@operands[0], bill), to_val(@operands[1], bill)].min
  when :between
    v = to_val(@operands[0], bill)
    a = to_val(@operands[1], bill)
    b = to_val(@operands[2], bill)
    min, max = [a, b].minmax
    return 0 if (!v.negative? && v < min) || (!v.positive? && v > max)
    v.clamp(min, max)
  else
    raise "Unknown term '#{@op}'"
  end
end

#static?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/fbe/award.rb', line 72

def static?
  true
end

#to_sObject



68
69
70
# File 'lib/fbe/award.rb', line 68

def to_s
  "(#{@op} #{@operands.join(' ')})"
end

#to_val(any, bill) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fbe/award.rb', line 107

def to_val(any, bill)
  if any.is_a?(BTerm)
    any.calc(bill)
  elsif any.is_a?(Symbol)
    v = bill.vars[any]
    raise "Unknown name '#{any}' among [#{bill.vars.keys.join(', ')}]" if v.nil?
    v
  else
    any
  end
end