Module: Odin::Transform::Expr
- Defined in:
- lib/odin/transform/transform_expr.rb
Overview
Compiles an infix arithmetic formula string into a tree of existing verbs at parse time. No runtime evaluator: the result is an ordinary verb expression, so arithmetic runs through the deterministic verbs (add, subtract, multiply, divide, mod, negate, pow, and a whitelist of numeric functions). Variables resolve under an explicit bindings object passed as the second argument: in %expr “a + b” @.vars, the name a reads @.vars.a.
Precedence, high to low:
1. parentheses, function call
2. ^ power (right-associative)
3. unary - / + (looser than ^, so -2^2 = -(2^2) = -4; (-2)^2 = 4)
4. * / % (left-associative)
5. + - (left-associative)
Defined Under Namespace
Classes: ExprSyntaxError, Parser, Token
Constant Summary collapse
- BINARY_OP =
{ "+" => "add", "-" => "subtract", "*" => "multiply", "/" => "divide", "%" => "mod" }.freeze
- FUNCTIONS =
{ "abs" => { verb: "abs", min: 1, max: 1 }, "floor" => { verb: "floor", min: 1, max: 1 }, "ceil" => { verb: "ceil", min: 1, max: 1 }, "trunc" => { verb: "trunc", min: 1, max: 1 }, "sqrt" => { verb: "sqrt", min: 1, max: 1 }, "round" => { verb: "round", min: 1, max: 2 }, "pow" => { verb: "pow", min: 2, max: 2 }, "min" => { verb: "minOf", min: 1, max: Float::INFINITY }, "max" => { verb: "maxOf", min: 1, max: Float::INFINITY } }.freeze
Class Method Summary collapse
- .compile(formula, binding_path = nil) ⇒ Object
- .literal(text, is_float) ⇒ Object
- .tokenize(src) ⇒ Object
- .verb_node(verb, args) ⇒ Object
Class Method Details
.compile(formula, binding_path = nil) ⇒ Object
46 47 48 |
# File 'lib/odin/transform/transform_expr.rb', line 46 def compile(formula, binding_path = nil) Parser.new(tokenize(formula), binding_path).parse end |
.literal(text, is_float) ⇒ Object
98 99 100 101 |
# File 'lib/odin/transform/transform_expr.rb', line 98 def literal(text, is_float) value = is_float ? Types::DynValue.of_float(text.to_f) : Types::DynValue.of_integer(text.to_i) LiteralExpr.new(value) end |
.tokenize(src) ⇒ Object
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 |
# File 'lib/odin/transform/transform_expr.rb', line 50 def tokenize(src) tokens = [] i = 0 len = src.length while i < len c = src[i] if c =~ /\s/ i += 1 next end if c =~ /[0-9]/ j = i is_float = false j += 1 while j < len && src[j] =~ /[0-9]/ if src[j] == "." is_float = true j += 1 j += 1 while j < len && src[j] =~ /[0-9]/ end if src[j] == "e" || src[j] == "E" is_float = true j += 1 j += 1 if src[j] == "+" || src[j] == "-" j += 1 while j < len && src[j] =~ /[0-9]/ end tokens << Token.new(:num, src[i...j], is_float) i = j next end if c =~ /[A-Za-z_]/ j = i j += 1 while j < len && src[j] =~ /[A-Za-z0-9_.]/ tokens << Token.new(:ident, src[i...j], false) i = j next end case c when "(" then tokens << Token.new(:lparen, c, false) when ")" then tokens << Token.new(:rparen, c, false) when "," then tokens << Token.new(:comma, c, false) when "+", "-", "*", "/", "%", "^" then tokens << Token.new(:op, c, false) else raise ExprSyntaxError, "unexpected character '#{c}'" end i += 1 end tokens end |