Class: Kalc::Interpreter
- Inherits:
-
Object
- Object
- Kalc::Interpreter
- Defined in:
- lib/kalc/interpreter.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
Returns the value of attribute env.
Instance Method Summary collapse
-
#initialize ⇒ Interpreter
constructor
A new instance of Interpreter.
- #load_stdlib(grammar, transform) ⇒ Object
- #run(ast) ⇒ Object
Constructor Details
#initialize ⇒ Interpreter
Returns a new instance of Interpreter.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 106 107 108 109 110 111 112 113 114 115 116 117 118 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/kalc/interpreter.rb', line 9 def initialize @env = Environment.new do |env| env.add_function(:IF, lambda { |cxt, cond, if_true, if_false| cond.eval(cxt) ? if_true.eval(cxt) : if_false.eval(cxt) }) env.add_function(:OR, lambda { |cxt, *args| retval = false args.each do |arg| if arg.eval(cxt) retval = true break end end retval }) env.add_function(:NOT, lambda { |cxt, val| !val.eval(cxt) }) env.add_function(:AND, lambda { |cxt, *args| retval = true args.each do |arg| unless arg.eval(cxt) retval = false break end end retval }) env.add_function(:RAND, lambda { |cxt, val| rand(val.eval(cxt)) }) env.add_function(:SYSTEM, lambda { |cxt, val| throw "Nope. I don't think so!" }) # IS? env.add_function(:ISLOGICAL, lambda { |cxt, val| newval = val.eval(cxt) newval == true || newval == false }) env.add_function(:ISNONTEXT, lambda { |cxt, val| !val.eval(cxt).is_a? String }) env.add_function(:ISNUMBER, lambda { |cxt, val| val.eval(cxt).is_a? Numeric }) env.add_function(:ISTEXT, lambda { |cxt, val| val.eval(cxt).is_a? String }) # Math env.add_function(:ABS, lambda { |cxt, val| val.eval(cxt).abs }) env.add_function(:DEGREES, lambda { |cxt, val| val.eval(cxt) * (180.0 / Math::PI) }) env.add_function(:PRODUCT, lambda { |cxt, *args| args.map { |a| a.eval(cxt) }.inject(:*) }) env.add_function(:RADIANS, lambda { |cxt, val| val.eval(cxt) * (Math::PI / 180.0) }) env.add_function(:ROUND, lambda { |cxt, num, digits| num.eval(cxt).round(digits.eval(cxt).to_i) }) env.add_function(:SUM, lambda { |cxt, *args| args.map { |a| a.eval(cxt) }.inject(:+) }) env.add_function(:TRUNC, lambda { |cxt, val| Integer(val.eval(cxt)) }) env.add_function(:LN, lambda { |cxt, val| Math.log(val.eval(cxt)) }) env.add_function(:MAX, lambda { |cxt, first, *args| (args<<first).map { |a| a.eval(cxt) }.max }) env.add_function(:MIN, lambda { |cxt, first, *args| (args<<first).map { |a| a.eval(cxt) }.min }) math_funs = %w(acos acosh asin asinh atan atanh cbrt cos cosh erf erfc exp gamma lgamma log log2 log10 sin sinh sqrt tan tanh) math_funs.each do |math_fun| env.add_function(math_fun.upcase.to_sym, lambda { |cxt, val| Math.send(math_fun.to_sym, val.eval(cxt)) }) end # Strings string_funs = %w(chomp chop chr clear count downcase hex inspect intern to_sym length size lstrip succ next oct ord reverse rstrip strip swapcase to_c to_f to_i to_r upcase) string_funs.each do |str_fun| env.add_function(str_fun.upcase.to_sym, lambda { |cxt, val| String.new(val.eval(cxt)).send(str_fun.to_sym) }) end env.add_function(:CHAR, lambda { |cxt, val| Integer(val.eval(cxt)).chr }) env.add_function(:CLEAN, lambda { |cxt, val| val.eval(cxt).gsub(/\P{ASCII}/, '') }) env.add_function(:CODE, lambda { |cxt, val| val.eval(cxt).ord }) env.add_function(:CONCATENATE, lambda { |cxt, *args| args.map { |a| a.eval(cxt) }.join }) env.add_function(:DOLLAR, lambda { |cxt, val, decimal_places| "%.#{Integer(decimal_places.eval(cxt))}f" % BigDecimal.new(val.eval(cxt)) }) env.add_function(:EXACT, lambda { |cxt, string1, string2| string1.eval(cxt) == string2.eval(cxt) }) env.add_function(:FIND, lambda { |cxt, string1, string2, starting_pos| start = Integer(starting_pos.eval(cxt)) - 1 string1.eval(cxt)[start..-1].index(string2.eval(cxt)) + 1 }) env.add_function(:FIXED, lambda { |cxt, val, decimal_places, no_commas| output = "%.#{Integer(decimal_places.eval(cxt))}f" % BigDecimal.new(val.eval(cxt)) output = output.to_s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse if !no_commas.eval(cxt) output }) env.add_function(:LEFT, lambda { |cxt, val, number_of_characters| num = Integer(number_of_characters.eval(cxt)) - 1 val.eval(cxt)[0..num] }) env.add_function(:LEN, lambda { |cxt, val| val.eval(cxt).length }) env.add_function(:LOWER, lambda { |cxt, val| val.eval(cxt).downcase }) env.add_function(:MID, lambda { |cxt, val, start_position, number_of_characters| start = Integer(start_position.eval(cxt)) - 1 chars = Integer(number_of_characters.eval(cxt)) - 1 val.eval(cxt)[start..chars + start] }) env.add_function(:PROPER, lambda { |cxt, val| val.eval(cxt).split(' ').map { |c| c.capitalize }.join(' ') }) env.add_function(:REPLACE, lambda { |cxt, val, start_position, number_of_chars, new_text| start = Integer(start_position.eval(cxt)) - 1 chars = Integer(number_of_chars.eval(cxt)) - 1 output = val.eval(cxt) output[start..chars + start] = new_text.eval(cxt) output }) env.add_function(:REPT, lambda { |cxt, val, number_of_times| val.eval(cxt) * Integer(number_of_times.eval(cxt)) }) env.add_function(:RIGHT, lambda { |cxt, val, number_of_characters| num = -Integer(number_of_characters.eval(cxt)) val.eval(cxt)[num..-1] }) env.add_function(:SEARCH, lambda { |cxt, string1, string2, start_position| start = Integer(start_position.eval(cxt)) - 1 string2.eval(cxt).downcase[start..-1].index(string1.eval(cxt).downcase) + 1 }) env.add_function(:SUBSTITUTE, lambda { |cxt, val, old_text, new_text| val.eval(cxt).gsub(old_text.eval(cxt), new_text.eval(cxt)) }) env.add_function(:TRIM, lambda { |cxt, val| val.eval(cxt).strip }) env.add_function(:UPPER, lambda { |cxt, val| val.eval(cxt).upcase }) env.add_function(:VALUE, lambda { |cxt, val| val.eval(cxt).to_f }) # Regular expressions env.add_function(:REGEXP_MATCH, lambda { |cxt, val, regex| r = regex.eval(cxt) /#{r}/.match(val.eval(cxt)) }) env.add_function(:REGEXP_REPLACE, lambda { |cxt, val, regex, to_replace| r = regex.eval(cxt) val.eval(cxt).gsub(/#{r}/, to_replace.eval(cxt)) }) # Debug env.add_function(:P, lambda { |cxt, *output| p output }) env.add_function(:PP, lambda { |cxt, *output| pp output }) env.add_function(:PUTS, lambda { |cxt, output| puts output.eval(cxt) }) env.add_function(:FLOOR, lambda { |cxt, val| val.eval(cxt).floor }) env.add_function(:CEILING, lambda { |cxt, val| val.eval(cxt).ceil }) end end |
Instance Attribute Details
#env ⇒ Object
Returns the value of attribute env.
7 8 9 |
# File 'lib/kalc/interpreter.rb', line 7 def env @env end |
Instance Method Details
#load_stdlib(grammar, transform) ⇒ Object
268 269 270 271 272 273 274 |
# File 'lib/kalc/interpreter.rb', line 268 def load_stdlib(grammar, transform) stdlib = "#{File.dirname(__FILE__)}/stdlib.kalc" input = File.read(stdlib) g = grammar.parse(input) ast = transform.apply(g) run(ast) end |
#run(ast) ⇒ Object
276 277 278 |
# File 'lib/kalc/interpreter.rb', line 276 def run(ast) ast.eval(@env) end |