-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.go
More file actions
143 lines (134 loc) · 2.89 KB
/
Copy pathast.go
File metadata and controls
143 lines (134 loc) · 2.89 KB
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
49
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"fmt"
"github.com/timtadh/lexmachine"
"strings"
)
type Node struct {
Name string
Token *lexmachine.Token
Children []*Node
Parent *Node
Context map[string]*AbstractLocalVariable
}
const (
newLine = "\n"
emptySpace = " "
middleItem = "├── "
continueItem = "│ "
lastItem = "└── "
)
func NewNode(name string, token *lexmachine.Token) *Node {
return &Node{
Name: name,
Token: token,
Context: make(map[string]*AbstractLocalVariable),
}
}
// Add a child at the end of the children list to a node.
func (n *Node) AddKid(kid *Node) *Node {
n.Children = append(n.Children, kid)
if kid != nil {
kid.Parent = n
}
return n
}
// Add a child at the start of the children list to a Node
func (n *Node) PrependKid(kid *Node) *Node {
kids := append(make([]*Node, 0, cap(n.Children)+1), kid)
n.Children = append(kids, n.Children...)
if kid != nil {
kid.Parent = n
}
return n
}
func (n *Node) AddParent(parent *Node) *Node {
n.Parent = parent
return n
}
// String makes the AST human readable
func (n *Node) String() string {
if n == nil {
return ""
}
parts := make([]string, 0, len(n.Children))
parts = append(parts, n.Name)
if n.Token != nil && string(n.Token.Lexeme) != n.Name {
parts = append(parts, fmt.Sprintf("%q", string(n.Token.Lexeme)))
}
for _, k := range n.Children {
parts = append(parts, k.String())
}
if len(parts) > 1 {
return fmt.Sprintf("%v", strings.Join(parts, " "))
}
return strings.Join(parts, "")
//p := printer{}
//return p.Print(*n)
}
//
//type printer struct {
//}
//type PrintableNode interface {
// Print(Node) string
//}
//
//func (n *Node) Text() string {
// if n.Token == nil {
// return n.Name
// }
// return string(n.Token.Lexeme)
//}
//func (n *Node) Items() []*Node {
// return n.Children
//}
//
//func (p *printer) printText(text string, spaces []bool, last bool) string {
// var result string
// for _, space := range spaces {
// if space {
// result += emptySpace
// } else {
// result += continueItem
// }
// }
//
// indicator := middleItem
// if last {
// indicator = lastItem
// }
//
// var out string
// lines := strings.Split(text, "\n")
// for i := range lines {
// text := lines[i]
// if i == 0 {
// out += result + indicator + text + newLine
// continue
// }
// if last {
// indicator = emptySpace
// } else {
// indicator = continueItem
// }
// out += result + indicator + text + newLine
// }
//
// return out
//}
//
//func (p *printer) printItems(t []*Node, spaces []bool) string {
// var result string
// for i, f := range t {
// last := i == len(t)-1
// result += p.printText(f.Text(), spaces, last)
// if len(f.Items()) > 0 {
// spacesChild := append(spaces, last)
// result += p.printItems(f.Items(), spacesChild)
// }
// }
// return result
//}
//func (p *printer) Print(t Node) string {
// return t.Text() + newLine + p.printItems(t.Items(), []bool{})
//}