Module: Jade::AST::PrettyPrinter

Extended by:
PrettyPrinter
Included in:
PrettyPrinter
Defined in:
lib/jade/ast/pretty_printer.rb

Instance Method Summary collapse

Instance Method Details



6
7
8
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
# File 'lib/jade/ast/pretty_printer.rb', line 6

def print(node, indent = 0)
  prefix = '  ' * indent

  case node
  in AST::VariableReference(name:)
    prefix + "Var(#{name})"

  in AST::Assign(pattern:, expression:)
    prefix + "VarBound(#{pattern} = " + print(expression, indent) + ")"

  in AST::Literal(value:)
    case value
    in Integer | TrueClass | FalseClass
      value.to_s

    in String
      "\"#{value}\""
    end
  in AST::FunctionCall(callee:, args:)
    case callee
    in AST::VariableReference(name:)
      if is_infix?(name)
        operator = name.delete_prefix('(').delete_suffix(')')

        return prefix + "(#{print(args[0])} #{operator} #{print(args[1])})"
      end
    else
    end

    args
      .map { print(it) }.join(', ')
      .then { "(#{it})"}
      .then { prefix + print(callee, indent) + it }
  end
end