-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
48 lines (39 loc) · 875 Bytes
/
dump.go
1
2
3
4
5
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
41
42
43
44
45
46
47
48
/*
* Copyright (c) 2023, Dana Burkart <dana.burkart@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
package ast
import (
"reflect"
"strings"
)
type Dumper struct {
Output string
indent int
}
func (d *Dumper) Visit(node ASTNode) Visitor {
if node == nil {
d.indent -= 1
return nil
}
level := strings.Repeat(" ", d.indent)
value := node.Value()
switch t := node.(type) {
case *TopicSelectorNode:
value = "in " + t.Topic.Lexeme
case *DataFunctionNode:
var args string
for _, a := range t.Arguments {
args += a.Value() + ", "
}
value = "name(" + node.Value() + ") args(" + args[:len(args)-2] + ")"
case *TupleElementNode:
value = t.Identifier.Value() + "[" + t.Subscript.Value() + "]"
}
t := reflect.TypeOf(node)
output := level + t.Elem().Name() + "[" + value + "]" + "\n"
d.Output += output
d.indent += 1
return d
}