Module: Dogfood::DSL::Evaluator
- Defined in:
- lib/dogfood/dsl/evaluator.rb
Overview
Evaluates minimal $... expressions. Safe: it is a tiny hand-rolled recursive-descent parser, NOT eval. See SPEC.md 4.4 for the grammar.
Defined Under Namespace
Classes: Parser
Constant Summary collapse
- TOKEN =
/\$\{([^}]*)\}/
Class Method Summary collapse
-
.eval(value, bindings:, state:, rng:, self_object: nil) ⇒ Object
Evaluates a value that may contain $... tokens.
-
.evaluate_expression(expr, bindings:, state:, rng:, self_object: nil) ⇒ Object
Parses and evaluates a single expression (without $... delimiters).
- .stringify(value) ⇒ Object
Class Method Details
.eval(value, bindings:, state:, rng:, self_object: nil) ⇒ Object
Evaluates a value that may contain $... tokens. Returns the raw bound value if the entire input is a single $...; otherwise interpolates tokens into the surrounding string.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dogfood/dsl/evaluator.rb', line 13 def self.eval(value, bindings:, state:, rng:, self_object: nil) if value.is_a?(String) && (m = TOKEN.match(value)) && m[0] == value return evaluate_expression(m[1], bindings: bindings, state: state, rng: rng, self_object: self_object) end case value when String value.gsub(TOKEN) do result = evaluate_expression(Regexp.last_match(1), bindings: bindings, state: state, rng: rng, self_object: self_object) stringify(result) end when Hash value.each_with_object({}) do |(k, v), acc| acc[eval(k, bindings: bindings, state: state, rng: rng, self_object: self_object)] = eval(v, bindings: bindings, state: state, rng: rng, self_object: self_object) end when Array value.map { |v| eval(v, bindings: bindings, state: state, rng: rng, self_object: self_object) } else value end end |
.evaluate_expression(expr, bindings:, state:, rng:, self_object: nil) ⇒ Object
Parses and evaluates a single expression (without $... delimiters).
37 38 39 |
# File 'lib/dogfood/dsl/evaluator.rb', line 37 def self.evaluate_expression(expr, bindings:, state:, rng:, self_object: nil) Parser.new(expr, bindings: bindings, state: state, rng: rng, self_object: self_object).parse end |
.stringify(value) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/dogfood/dsl/evaluator.rb', line 41 def self.stringify(value) case value when nil then "null" when true then "true" when false then "false" else value.to_s end end |