summaryrefslogtreecommitdiff
path: root/usr/gri/pretty/ast.go
blob: d566361bf2e0af35a3dcb88500ad052a9174bf76 (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package AST

import Scanner "scanner"


type (
	Any interface {};
	Type struct;
	Expr struct;
	Stat struct;
	Decl struct;
)


// ----------------------------------------------------------------------------
// Lists
//
// If p is a list and p == nil, then p.len() == 0.
// Thus, empty lists can be represented by nil.

export type List struct {
	a *[] Any;
}


func (p *List) Init() {
	p.a = new([] Any, 10) [0 : 0];
}


func (p *List) len() int {
	if p == nil { return 0; }
	return len(p.a);
}


func (p *List) at(i int) Any {
	return p.a[i];
}


func (p *List) set(i int, x Any) {
	p.a[i] = x;
}


func (p *List) Add(x Any) {
	a := p.a;
	n := len(a);

	if n == cap(a) {
		b := new([] Any, 2*n);
		for i := 0; i < n; i++ {
			b[i] = a[i];
		}
		a = b;
	}

	a = a[0 : n + 1];
	a[n] = x;
	p.a = a;
}


func (p *List) Pop() Any {
	a := p.a;
	n := len(a);
	
	var x Any;
	if n > 0 {
		x = a[n - 1];
		a = a[0 : n - 1];
		p.a = a;
	} else {
		panic("pop from empty list");
	}
	
	return x;
}


func (p *List) Clear() {
	p.a = p.a[0 : 0];
}


export func NewList() *List {
	p := new(List);
	p.Init();
	return p;
}


// ----------------------------------------------------------------------------
// All nodes have a source position and and token.

export type Node struct {
	pos, tok int;
}


// ----------------------------------------------------------------------------
// Expressions

export type Expr struct {
	Node;
	x, y *Expr;  // binary (x, y) and unary (y) expressions
	// TODO find a more space efficient way to hold these
	s string;  // identifiers and literals
	t *Type;  // type expressions, function literal types
	block *List;  // stats for function literals
}


func (x *Expr) len() int {
	if x == nil {
		return 0;
	}
	n := 1;
	for ; x.tok == Scanner.COMMA; x = x.y {
		n++;
	}
	return n;
}


export func NewExpr(pos, tok int, x, y *Expr) *Expr {
	if x != nil && x.tok == Scanner.TYPE || y != nil && y.tok == Scanner.TYPE {
		panic("no type expression allowed");
	}
	e := new(Expr);
	e.pos, e.tok, e.x, e.y = pos, tok, x, y;
	return e;
}


export func NewLit(pos, tok int, s string) *Expr {
	e := new(Expr);
	e.pos, e.tok, e.s = pos, tok, s;
	return e;
}


export var BadExpr = NewExpr(0, Scanner.ILLEGAL, nil, nil);


// ----------------------------------------------------------------------------
// Types

export const /* channel mode */ (
	FULL = iota;
	SEND;
	RECV;
)


export type Type struct {
	Node;
	expr *Expr;  // type name, array length
	mode int;  // channel mode
	key *Type;  // receiver type, map key
	elt *Type;  // array element, map or channel value, or pointer base type, result type
	list *List;  // struct fields, interface methods, function parameters
}


func (t *Type) nfields() int {
	nx, nt := 0, 0;
	for i, n := 0, t.list.len(); i < n; i++ {
		if t.list.at(i).(*Expr).tok == Scanner.TYPE {
			nt++;
		} else {
			nx++;
		}
	}
	if nx == 0 {
		return nt;
	}
	return nx;
}


export func NewType(pos, tok int) *Type {
	t := new(Type);
	t.pos, t.tok = pos, tok;
	return t;
}


// requires complete Type type
export func NewTypeExpr(t *Type) *Expr {
	e := new(Expr);
	e.pos, e.tok, e.t = t.pos, Scanner.TYPE, t;
	return e;
}


export var BadType = NewType(0, Scanner.ILLEGAL);


// ----------------------------------------------------------------------------
// Statements

export type Stat struct {
	Node;
	init, post *Stat;
	expr *Expr;
	block *List;
	decl *Decl;
}


export func NewStat(pos, tok int) *Stat {
	s := new(Stat);
	s.pos, s.tok = pos, tok;
	return s;
}


export var BadStat = NewStat(0, Scanner.ILLEGAL);


// ----------------------------------------------------------------------------
// Declarations

export type Decl struct {
	Node;
	exported bool;
	ident *Expr;  // nil for ()-style declarations
	typ *Type;
	val *Expr;
	// list of *Decl for ()-style declarations
	// list of *Stat for func declarations (or nil for forward decl)
	list *List;
}


export func NewDecl(pos, tok int, exported bool) *Decl {
	d := new(Decl);
	d.pos, d.tok, d.exported = pos, tok, exported;
	return d;
}


export var BadDecl = NewDecl(0, Scanner.ILLEGAL, false);


// ----------------------------------------------------------------------------
// Program

export type Comment struct {
	pos int;
	text string;
}


export func NewComment(pos int, text string) *Comment {
	c := new(Comment);
	c.pos, c.text = pos, text;
	return c;
}


export type Program struct {
	pos int;  // tok is Scanner.PACKAGE
	ident *Expr;
	decls *List;
	comments *List;
}


export func NewProgram(pos int) *Program {
	p := new(Program);
	p.pos = pos;
	return p;
}