File: ast.go

package info (click to toggle)
golang-github-go-restruct-restruct 1.2.0-alpha-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 344 kB
  • sloc: makefile: 2
file content (247 lines) | stat: -rw-r--r-- 3,962 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package expr

import (
	"fmt"
	"strconv"
)

type node interface {
	source() string
}

type unaryop int

const (
	unaryplus unaryop = iota
	unarynegate
	unarynot
	unarybitnot
	unaryderef
	unaryref
)

type binaryop int

const (
	binarylogicalor binaryop = iota
	binarylogicaland
	binaryequal
	binarynotequal
	binarylesser
	binarylesserequal
	binarygreater
	binarygreaterequal
	binaryadd
	binarysub
	binaryor
	binaryxor
	binarymul
	binarydiv
	binaryrem
	binarylsh
	binaryrsh
	binaryand
	binaryandnot
	binarymember
	binarycall
	binarysubscript
	binarygroup
)

// Identifier node.
type identnode struct {
	pos   int
	ident string
}

func newidentnode(t token) identnode {
	return identnode{t.pos, t.sval}
}

func (n identnode) source() string {
	return n.ident
}

// Integer literal node.
type intnode struct {
	pos  int
	uval uint64
	ival int64
	sign bool
}

func newintnode(t token) intnode {
	return intnode{pos: t.pos, uval: t.uval, ival: t.ival, sign: t.sign}
}

func (n intnode) source() string {
	if n.sign {
		return strconv.FormatInt(n.ival, 10)
	}
	return strconv.FormatUint(n.uval, 10)
}

// Float literal node.
type floatnode struct {
	pos  int
	fval float64
}

func newfloatnode(t token) floatnode {
	return floatnode{pos: t.pos, fval: t.fval}
}

func (n floatnode) source() string {
	return strconv.FormatFloat(n.fval, 'f', -1, 64)
}

// Bool literal node.
type boolnode struct {
	pos int
	val bool
}

func newboolnode(t token) boolnode {
	return boolnode{t.pos, t.bval}
}

func (n boolnode) source() string {
	if n.val {
		return "true"
	}
	return "false"
}

// String literal node.
type strnode struct {
	pos int
	val string
}

func newstrnode(t token) strnode {
	return strnode{t.pos, t.sval}
}

func (n strnode) source() string {
	return fmt.Sprintf("%q", n.val)
}

// Rune literal node.
type runenode struct {
	pos int
	val rune
}

func newrunenode(t token) runenode {
	return runenode{t.pos, rune(t.ival)}
}

func (n runenode) source() string {
	return fmt.Sprintf("%q", n.val)
}

// Nil node.
type nilnode struct {
	pos int
}

func newnilnode(t token) nilnode {
	return nilnode{t.pos}
}

func (nilnode) source() string {
	return "nil"
}

// Unary expression node.
type unaryexpr struct {
	op unaryop
	n  node
}

func (n unaryexpr) source() string {
	operand := n.n.source()
	switch n.op {
	case unaryplus:
		return "+" + operand
	case unarynegate:
		return "-" + operand
	case unarynot:
		return "!" + operand
	case unarybitnot:
		return "^" + operand
	case unaryderef:
		return "*" + operand
	case unaryref:
		return "&" + operand
	}
	panic("invalid unary expr?")
}

// Binary expression node.
type binaryexpr struct {
	op   binaryop
	a, b node
}

func (n binaryexpr) source() string {
	a, b := n.a.source(), n.b.source()
	switch n.op {
	case binarylogicalor:
		return a + " || " + b
	case binarylogicaland:
		return a + " && " + b
	case binaryequal:
		return a + " == " + b
	case binarynotequal:
		return a + " != " + b
	case binarylesser:
		return a + " < " + b
	case binarylesserequal:
		return a + " <= " + b
	case binarygreater:
		return a + " > " + b
	case binarygreaterequal:
		return a + " >= " + b
	case binaryadd:
		return a + " + " + b
	case binarysub:
		return a + " - " + b
	case binaryor:
		return a + " | " + b
	case binaryxor:
		return a + " ^ " + b
	case binarymul:
		return a + " * " + b
	case binarydiv:
		return a + " / " + b
	case binaryrem:
		return a + " % " + b
	case binarylsh:
		return a + " << " + b
	case binaryrsh:
		return a + " >> " + b
	case binaryand:
		return a + " & " + b
	case binaryandnot:
		return a + " &^ " + b
	case binarymember:
		return a + "." + b
	case binarycall:
		return a + "(" + b + ")"
	case binarysubscript:
		return a + "[" + b + "]"
	case binarygroup:
		return a + ", " + b
	}
	panic("invalid binary expr?")
}

// Ternary expression node.
type ternaryexpr struct {
	a, b, c node
}

func (n ternaryexpr) source() string {
	return n.a.source() + " ? " + n.b.source() + " : " + n.c.source()
}