Class: Kumi::Core::ExpressionRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/kumi/core/expression_renderer.rb

Overview

Renders a Syntax AST expression back into compact, readable algebra —the form a human (or an LLM) reads to understand what a declaration computes, without the source file. Deterministic and total over the node set; an unknown node degrades to its class name rather than raising.

adult      := input.age >= 18
line       := input.items[].item.qty * input.items[].item.price
subtotal   := sum(line)
tier       := cascade { (adult & wealthy) => "premium";
                        adult => "standard"; else => "none" }

Constant Summary collapse

INFIX =

Inverse of the parser’s operator sugar, so ‘:multiply` prints as `*`.

{
  add: "+", subtract: "-", multiply: "*", divide: "/",
  modulo: "%", power: "**",
  "<": "<", "<=": "<=", ">": ">", ">=": ">=",
  "==": "==", "!=": "!=", and: "&", or: "|"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.render(node) ⇒ Object



24
25
26
# File 'lib/kumi/core/expression_renderer.rb', line 24

def self.render(node)
  new.render(node)
end

Instance Method Details

#render(node) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kumi/core/expression_renderer.rb', line 28

def render(node)
  case node
  when Kumi::Syntax::Literal              then render_literal(node.value)
  when Kumi::Syntax::InputReference       then "input.#{node.name}"
  when Kumi::Syntax::InputElementReference then render_input_element(node.path)
  when Kumi::Syntax::DeclarationReference  then node.name.to_s
  when Kumi::Syntax::CallExpression        then render_call(node)
  when Kumi::Syntax::CascadeExpression     then render_cascade(node)
  when Kumi::Syntax::ArrayExpression       then "[#{node.elements.map { |e| render(e) }.join(', ')}]"
  when Kumi::Syntax::HashExpression        then render_hash(node)
  else
    node.class.name&.split("::")&.last || node.inspect
  end
end