2008-10-20 16:03:40 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2008-10-14 19:14:01 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
package AST
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2008-11-19 17:49:50 -07:00
|
|
|
import (
|
2009-02-13 16:07:56 -07:00
|
|
|
"vector";
|
2009-03-03 19:25:07 -07:00
|
|
|
"token";
|
2009-02-06 12:10:25 -07:00
|
|
|
SymbolTable "symboltable";
|
2008-11-19 17:49:50 -07:00
|
|
|
)
|
2008-10-14 19:14:01 -06:00
|
|
|
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type (
|
2009-01-23 10:44:01 -07:00
|
|
|
Block struct;
|
2009-02-03 18:44:01 -07:00
|
|
|
Expr interface;
|
2009-02-27 16:40:17 -07:00
|
|
|
Decl interface;
|
2008-10-14 19:14:01 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-01-23 10:44:01 -07:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Support
|
|
|
|
|
|
|
|
func assert(pred bool) {
|
|
|
|
if !pred {
|
|
|
|
panic("assertion failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-23 14:50:14 -07:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// All nodes have a source position and a token.
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
Pos int; // source position (< 0 => unknown position)
|
|
|
|
Tok int; // identifying token
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-16 13:16:50 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-02-27 16:40:17 -07:00
|
|
|
// Expressions
|
2009-01-09 17:28:09 -07:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
const /* channel mode */ (
|
2008-10-16 13:16:50 -06:00
|
|
|
FULL = iota;
|
|
|
|
SEND;
|
|
|
|
RECV;
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-02-03 18:44:01 -07:00
|
|
|
type (
|
2009-02-04 19:28:41 -07:00
|
|
|
ExprVisitor interface;
|
2009-02-27 16:40:17 -07:00
|
|
|
Signature struct;
|
2009-02-03 18:44:01 -07:00
|
|
|
|
|
|
|
Expr interface {
|
|
|
|
Pos() int;
|
2009-02-04 19:28:41 -07:00
|
|
|
Visit(v ExprVisitor);
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
2009-02-27 16:40:17 -07:00
|
|
|
|
2009-02-03 18:44:01 -07:00
|
|
|
BadExpr struct {
|
|
|
|
Pos_ int;
|
|
|
|
};
|
|
|
|
|
|
|
|
Ident struct {
|
|
|
|
Pos_ int;
|
2009-02-06 12:10:25 -07:00
|
|
|
Obj *SymbolTable.Object;
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
BinaryExpr struct {
|
2009-02-27 16:40:17 -07:00
|
|
|
Pos_ int;
|
|
|
|
Tok int;
|
2009-02-03 18:44:01 -07:00
|
|
|
X, Y Expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
UnaryExpr struct {
|
2009-02-27 16:40:17 -07:00
|
|
|
Pos_ int;
|
|
|
|
Tok int;
|
2009-02-03 18:44:01 -07:00
|
|
|
X Expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
BasicLit struct {
|
2009-02-27 16:40:17 -07:00
|
|
|
Pos_ int;
|
|
|
|
Tok int;
|
2009-02-03 18:44:01 -07:00
|
|
|
Val string
|
|
|
|
};
|
|
|
|
|
|
|
|
FunctionLit struct {
|
|
|
|
Pos_ int; // position of "func"
|
2009-02-27 16:40:17 -07:00
|
|
|
Typ *Signature;
|
2009-02-03 18:44:01 -07:00
|
|
|
Body *Block;
|
|
|
|
};
|
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
Group struct {
|
|
|
|
Pos_ int; // position of "("
|
|
|
|
X Expr;
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Selector struct {
|
|
|
|
Pos_ int; // position of "."
|
|
|
|
X Expr;
|
|
|
|
Sel *Ident;
|
|
|
|
};
|
|
|
|
|
|
|
|
TypeGuard struct {
|
|
|
|
Pos_ int; // position of "."
|
|
|
|
X Expr;
|
2009-02-27 16:40:17 -07:00
|
|
|
Typ Expr;
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Index struct {
|
|
|
|
Pos_ int; // position of "["
|
|
|
|
X, I Expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
Call struct {
|
2009-03-03 17:00:06 -07:00
|
|
|
Pos_ int; // position of "(" or "{"
|
|
|
|
Tok int;
|
2009-02-03 18:44:01 -07:00
|
|
|
F, Args Expr
|
|
|
|
};
|
2009-02-27 16:40:17 -07:00
|
|
|
|
|
|
|
// Type literals are treated like expressions.
|
|
|
|
Ellipsis struct { // neither a type nor an expression
|
|
|
|
Pos_ int;
|
|
|
|
};
|
|
|
|
|
|
|
|
ArrayType struct {
|
|
|
|
Pos_ int; // position of "["
|
|
|
|
Len Expr;
|
|
|
|
Elt Expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
Field struct {
|
|
|
|
Idents []*Ident;
|
|
|
|
Typ Expr;
|
|
|
|
Tag Expr; // nil = no tag
|
|
|
|
};
|
|
|
|
|
|
|
|
StructType struct {
|
|
|
|
Pos_ int; // position of "struct"
|
|
|
|
Fields []*Field;
|
|
|
|
End int; // position of "}", End == 0 if forward declaration
|
|
|
|
};
|
|
|
|
|
|
|
|
PointerType struct {
|
|
|
|
Pos_ int; // position of "*"
|
|
|
|
Base Expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
Signature struct {
|
|
|
|
Params []*Field;
|
|
|
|
Result []*Field;
|
|
|
|
};
|
|
|
|
|
|
|
|
FunctionType struct {
|
|
|
|
Pos_ int; // position of "func"
|
|
|
|
Sig *Signature;
|
|
|
|
};
|
|
|
|
|
|
|
|
InterfaceType struct {
|
|
|
|
Pos_ int; // position of "interface"
|
|
|
|
Methods []*Field;
|
|
|
|
End int; // position of "}", End == 0 if forward declaration
|
|
|
|
};
|
|
|
|
|
|
|
|
SliceType struct {
|
|
|
|
Pos_ int; // position of "["
|
|
|
|
};
|
|
|
|
|
|
|
|
MapType struct {
|
|
|
|
Pos_ int; // position of "map"
|
|
|
|
Key Expr;
|
|
|
|
Val Expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
ChannelType struct {
|
|
|
|
Pos_ int; // position of "chan" or "<-"
|
|
|
|
Mode int;
|
|
|
|
Val Expr;
|
|
|
|
};
|
2009-02-03 18:44:01 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-02-04 19:28:41 -07:00
|
|
|
type ExprVisitor interface {
|
2009-02-03 18:44:01 -07:00
|
|
|
DoBadExpr(x *BadExpr);
|
|
|
|
DoIdent(x *Ident);
|
|
|
|
DoBinaryExpr(x *BinaryExpr);
|
|
|
|
DoUnaryExpr(x *UnaryExpr);
|
|
|
|
DoBasicLit(x *BasicLit);
|
|
|
|
DoFunctionLit(x *FunctionLit);
|
2009-02-27 16:40:17 -07:00
|
|
|
DoGroup(x *Group);
|
2009-02-03 18:44:01 -07:00
|
|
|
DoSelector(x *Selector);
|
|
|
|
DoTypeGuard(x *TypeGuard);
|
|
|
|
DoIndex(x *Index);
|
|
|
|
DoCall(x *Call);
|
2009-02-27 16:40:17 -07:00
|
|
|
|
|
|
|
DoEllipsis(x *Ellipsis);
|
|
|
|
DoArrayType(x *ArrayType);
|
|
|
|
DoStructType(x *StructType);
|
|
|
|
DoPointerType(x *PointerType);
|
|
|
|
DoFunctionType(x *FunctionType);
|
|
|
|
DoInterfaceType(x *InterfaceType);
|
|
|
|
DoSliceType(x *SliceType);
|
|
|
|
DoMapType(x *MapType);
|
|
|
|
DoChannelType(x *ChannelType);
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
// TODO replace these with an embedded field
|
2009-02-03 18:44:01 -07:00
|
|
|
func (x *BadExpr) Pos() int { return x.Pos_; }
|
|
|
|
func (x *Ident) Pos() int { return x.Pos_; }
|
|
|
|
func (x *BinaryExpr) Pos() int { return x.Pos_; }
|
|
|
|
func (x *UnaryExpr) Pos() int { return x.Pos_; }
|
|
|
|
func (x *BasicLit) Pos() int { return x.Pos_; }
|
|
|
|
func (x *FunctionLit) Pos() int { return x.Pos_; }
|
2009-02-27 16:40:17 -07:00
|
|
|
func (x *Group) Pos() int { return x.Pos_; }
|
2009-02-03 18:44:01 -07:00
|
|
|
func (x *Selector) Pos() int { return x.Pos_; }
|
|
|
|
func (x *TypeGuard) Pos() int { return x.Pos_; }
|
|
|
|
func (x *Index) Pos() int { return x.Pos_; }
|
|
|
|
func (x *Call) Pos() int { return x.Pos_; }
|
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
func (x *Ellipsis) Pos() int { return x.Pos_; }
|
|
|
|
func (x *ArrayType) Pos() int { return x.Pos_; }
|
|
|
|
func (x *StructType) Pos() int { return x.Pos_; }
|
|
|
|
func (x *PointerType) Pos() int { return x.Pos_; }
|
|
|
|
func (x *FunctionType) Pos() int { return x.Pos_; }
|
|
|
|
func (x *InterfaceType) Pos() int { return x.Pos_; }
|
|
|
|
func (x *SliceType) Pos() int { return x.Pos_; }
|
|
|
|
func (x *MapType) Pos() int { return x.Pos_; }
|
|
|
|
func (x *ChannelType) Pos() int { return x.Pos_; }
|
|
|
|
|
2009-02-03 18:44:01 -07:00
|
|
|
|
2009-02-04 19:28:41 -07:00
|
|
|
func (x *BadExpr) Visit(v ExprVisitor) { v.DoBadExpr(x); }
|
|
|
|
func (x *Ident) Visit(v ExprVisitor) { v.DoIdent(x); }
|
|
|
|
func (x *BinaryExpr) Visit(v ExprVisitor) { v.DoBinaryExpr(x); }
|
|
|
|
func (x *UnaryExpr) Visit(v ExprVisitor) { v.DoUnaryExpr(x); }
|
|
|
|
func (x *BasicLit) Visit(v ExprVisitor) { v.DoBasicLit(x); }
|
|
|
|
func (x *FunctionLit) Visit(v ExprVisitor) { v.DoFunctionLit(x); }
|
2009-02-27 16:40:17 -07:00
|
|
|
func (x *Group) Visit(v ExprVisitor) { v.DoGroup(x); }
|
2009-02-04 19:28:41 -07:00
|
|
|
func (x *Selector) Visit(v ExprVisitor) { v.DoSelector(x); }
|
|
|
|
func (x *TypeGuard) Visit(v ExprVisitor) { v.DoTypeGuard(x); }
|
|
|
|
func (x *Index) Visit(v ExprVisitor) { v.DoIndex(x); }
|
|
|
|
func (x *Call) Visit(v ExprVisitor) { v.DoCall(x); }
|
2009-02-03 18:44:01 -07:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
func (x *Ellipsis) Visit(v ExprVisitor) { v.DoEllipsis(x); }
|
|
|
|
func (x *ArrayType) Visit(v ExprVisitor) { v.DoArrayType(x); }
|
|
|
|
func (x *StructType) Visit(v ExprVisitor) { v.DoStructType(x); }
|
|
|
|
func (x *PointerType) Visit(v ExprVisitor) { v.DoPointerType(x); }
|
|
|
|
func (x *FunctionType) Visit(v ExprVisitor) { v.DoFunctionType(x); }
|
|
|
|
func (x *InterfaceType) Visit(v ExprVisitor) { v.DoInterfaceType(x); }
|
|
|
|
func (x *SliceType) Visit(v ExprVisitor) { v.DoSliceType(x); }
|
|
|
|
func (x *MapType) Visit(v ExprVisitor) { v.DoMapType(x); }
|
|
|
|
func (x *ChannelType) Visit(v ExprVisitor) { v.DoChannelType(x); }
|
|
|
|
|
2009-02-03 18:44:01 -07:00
|
|
|
|
|
|
|
|
|
|
|
// Length of a comma-separated expression list.
|
|
|
|
func ExprLen(x Expr) int {
|
|
|
|
if x == nil {
|
|
|
|
return 0;
|
2009-01-23 14:50:14 -07:00
|
|
|
}
|
2009-02-03 18:44:01 -07:00
|
|
|
n := 1;
|
|
|
|
for {
|
2009-03-03 19:25:07 -07:00
|
|
|
if p, ok := x.(*BinaryExpr); ok && p.Tok == token.COMMA {
|
2009-02-03 18:44:01 -07:00
|
|
|
n++;
|
|
|
|
x = p.Y;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
2009-01-23 14:50:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-03 18:44:01 -07:00
|
|
|
func ExprAt(x Expr, i int) Expr {
|
|
|
|
for j := 0; j < i; j++ {
|
2009-03-03 19:25:07 -07:00
|
|
|
assert(x.(*BinaryExpr).Tok == token.COMMA);
|
2009-02-03 18:44:01 -07:00
|
|
|
x = x.(*BinaryExpr).Y;
|
|
|
|
}
|
2009-03-03 19:25:07 -07:00
|
|
|
if t, is_binary := x.(*BinaryExpr); is_binary && t.Tok == token.COMMA {
|
2009-02-03 18:44:01 -07:00
|
|
|
x = t.X;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-05 12:05:02 -07:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Blocks
|
|
|
|
//
|
|
|
|
// Syntactic constructs of the form:
|
|
|
|
//
|
|
|
|
// "{" StatementList "}"
|
|
|
|
// ":" StatementList
|
|
|
|
|
|
|
|
type Block struct {
|
|
|
|
Node;
|
2009-02-13 16:07:56 -07:00
|
|
|
List *vector.Vector;
|
2009-02-05 12:05:02 -07:00
|
|
|
End int; // position of closing "}" if present
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewBlock(pos, tok int) *Block {
|
2009-03-03 19:25:07 -07:00
|
|
|
assert(tok == token.LBRACE || tok == token.COLON);
|
2009-02-05 12:05:02 -07:00
|
|
|
b := new(Block);
|
2009-02-13 16:07:56 -07:00
|
|
|
b.Pos, b.Tok, b.List = pos, tok, vector.New(0);
|
2009-02-05 12:05:02 -07:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Statements
|
|
|
|
|
2009-02-04 19:28:41 -07:00
|
|
|
type (
|
|
|
|
StatVisitor interface;
|
|
|
|
|
|
|
|
Stat interface {
|
|
|
|
Visit(v StatVisitor);
|
|
|
|
};
|
|
|
|
|
|
|
|
BadStat struct {
|
|
|
|
Pos int;
|
|
|
|
};
|
|
|
|
|
|
|
|
LabelDecl struct {
|
|
|
|
Pos int; // position of ":"
|
|
|
|
Label *Ident;
|
|
|
|
};
|
|
|
|
|
|
|
|
DeclarationStat struct {
|
2009-02-27 16:40:17 -07:00
|
|
|
Decl Decl;
|
2009-02-04 19:28:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
ExpressionStat struct {
|
|
|
|
Pos int; // position of Tok
|
|
|
|
Tok int; // INC, DEC, RETURN, GO, DEFER
|
|
|
|
Expr Expr;
|
|
|
|
};
|
2009-02-05 12:05:02 -07:00
|
|
|
|
|
|
|
CompositeStat struct {
|
|
|
|
Body *Block;
|
|
|
|
};
|
|
|
|
|
2009-02-04 19:28:41 -07:00
|
|
|
IfStat struct {
|
|
|
|
Pos int; // position of "if"
|
|
|
|
Init Stat;
|
|
|
|
Cond Expr;
|
|
|
|
Body *Block;
|
|
|
|
Else Stat;
|
|
|
|
};
|
|
|
|
|
|
|
|
ForStat struct {
|
|
|
|
Pos int; // position of "for"
|
|
|
|
Init Stat;
|
|
|
|
Cond Expr;
|
|
|
|
Post Stat;
|
|
|
|
Body *Block;
|
|
|
|
};
|
2009-02-05 12:05:02 -07:00
|
|
|
|
|
|
|
CaseClause struct {
|
|
|
|
Pos int; // position for "case" or "default"
|
|
|
|
Expr Expr; // nil means default case
|
|
|
|
Body *Block;
|
|
|
|
};
|
|
|
|
|
2009-02-04 19:28:41 -07:00
|
|
|
SwitchStat struct {
|
|
|
|
Pos int; // position of "switch"
|
|
|
|
Init Stat;
|
|
|
|
Tag Expr;
|
|
|
|
Body *Block;
|
|
|
|
};
|
|
|
|
|
|
|
|
SelectStat struct {
|
|
|
|
Pos int; // position of "select"
|
|
|
|
Body *Block;
|
|
|
|
};
|
|
|
|
|
|
|
|
ControlFlowStat struct {
|
|
|
|
Pos int; // position of Tok
|
|
|
|
Tok int; // BREAK, CONTINUE, GOTO, FALLTHROUGH
|
|
|
|
Label *Ident; // if any, or nil
|
|
|
|
};
|
2009-02-12 17:06:21 -07:00
|
|
|
|
|
|
|
EmptyStat struct {
|
|
|
|
Pos int; // position of ";"
|
|
|
|
};
|
2009-02-04 19:28:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type StatVisitor interface {
|
|
|
|
DoBadStat(s *BadStat);
|
|
|
|
DoLabelDecl(s *LabelDecl);
|
|
|
|
DoDeclarationStat(s *DeclarationStat);
|
|
|
|
DoExpressionStat(s *ExpressionStat);
|
2009-02-05 12:05:02 -07:00
|
|
|
DoCompositeStat(s *CompositeStat);
|
2009-02-04 19:28:41 -07:00
|
|
|
DoIfStat(s *IfStat);
|
|
|
|
DoForStat(s *ForStat);
|
2009-02-05 12:05:02 -07:00
|
|
|
DoCaseClause(s *CaseClause);
|
2009-02-04 19:28:41 -07:00
|
|
|
DoSwitchStat(s *SwitchStat);
|
|
|
|
DoSelectStat(s *SelectStat);
|
|
|
|
DoControlFlowStat(s *ControlFlowStat);
|
2009-02-12 17:06:21 -07:00
|
|
|
DoEmptyStat(s *EmptyStat);
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *BadStat) Visit(v StatVisitor) { v.DoBadStat(s); }
|
|
|
|
func (s *LabelDecl) Visit(v StatVisitor) { v.DoLabelDecl(s); }
|
|
|
|
func (s *DeclarationStat) Visit(v StatVisitor) { v.DoDeclarationStat(s); }
|
|
|
|
func (s *ExpressionStat) Visit(v StatVisitor) { v.DoExpressionStat(s); }
|
2009-02-05 12:05:02 -07:00
|
|
|
func (s *CompositeStat) Visit(v StatVisitor) { v.DoCompositeStat(s); }
|
2009-02-04 19:28:41 -07:00
|
|
|
func (s *IfStat) Visit(v StatVisitor) { v.DoIfStat(s); }
|
|
|
|
func (s *ForStat) Visit(v StatVisitor) { v.DoForStat(s); }
|
2009-02-05 12:05:02 -07:00
|
|
|
func (s *CaseClause) Visit(v StatVisitor) { v.DoCaseClause(s); }
|
2009-02-04 19:28:41 -07:00
|
|
|
func (s *SwitchStat) Visit(v StatVisitor) { v.DoSwitchStat(s); }
|
|
|
|
func (s *SelectStat) Visit(v StatVisitor) { v.DoSelectStat(s); }
|
|
|
|
func (s *ControlFlowStat) Visit(v StatVisitor) { v.DoControlFlowStat(s); }
|
2009-02-12 17:06:21 -07:00
|
|
|
func (s *EmptyStat) Visit(v StatVisitor) { v.DoEmptyStat(s); }
|
2009-02-04 19:28:41 -07:00
|
|
|
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Declarations
|
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
type (
|
|
|
|
DeclVisitor interface;
|
|
|
|
|
|
|
|
Decl interface {
|
|
|
|
Visit(v DeclVisitor);
|
|
|
|
};
|
|
|
|
|
|
|
|
BadDecl struct {
|
|
|
|
Pos int;
|
|
|
|
};
|
|
|
|
|
|
|
|
ImportDecl struct {
|
|
|
|
Pos int; // if > 0: position of "import"
|
|
|
|
Ident *Ident;
|
|
|
|
Path Expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
ConstDecl struct {
|
|
|
|
Pos int; // if > 0: position of "const"
|
|
|
|
Idents []*Ident;
|
|
|
|
Typ Expr;
|
|
|
|
Vals Expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
TypeDecl struct {
|
|
|
|
Pos int; // if > 0: position of "type"
|
|
|
|
Ident *Ident;
|
|
|
|
Typ Expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
VarDecl struct {
|
|
|
|
Pos int; // if > 0: position of "var"
|
|
|
|
Idents []*Ident;
|
|
|
|
Typ Expr;
|
|
|
|
Vals Expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
FuncDecl struct {
|
|
|
|
Pos_ int; // position of "func"
|
|
|
|
Recv *Field;
|
|
|
|
Ident *Ident;
|
|
|
|
Sig *Signature;
|
|
|
|
Body *Block;
|
|
|
|
};
|
|
|
|
|
|
|
|
DeclList struct {
|
|
|
|
Pos int; // position of Tok
|
|
|
|
Tok int;
|
|
|
|
List []Decl;
|
|
|
|
End int;
|
|
|
|
};
|
|
|
|
)
|
2008-10-14 19:14:01 -06:00
|
|
|
|
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
type DeclVisitor interface {
|
|
|
|
DoBadDecl(d *BadDecl);
|
|
|
|
DoImportDecl(d *ImportDecl);
|
|
|
|
DoConstDecl(d *ConstDecl);
|
|
|
|
DoTypeDecl(d *TypeDecl);
|
|
|
|
DoVarDecl(d *VarDecl);
|
|
|
|
DoFuncDecl(d *FuncDecl);
|
|
|
|
DoDeclList(d *DeclList);
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
//func (d *Decl) Visit(v DeclVisitor) { v.DoDecl(d); }
|
|
|
|
func (d *BadDecl) Visit(v DeclVisitor) { v.DoBadDecl(d); }
|
|
|
|
func (d *ImportDecl) Visit(v DeclVisitor) { v.DoImportDecl(d); }
|
|
|
|
func (d *ConstDecl) Visit(v DeclVisitor) { v.DoConstDecl(d); }
|
|
|
|
func (d *TypeDecl) Visit(v DeclVisitor) { v.DoTypeDecl(d); }
|
|
|
|
func (d *VarDecl) Visit(v DeclVisitor) { v.DoVarDecl(d); }
|
|
|
|
func (d *FuncDecl) Visit(v DeclVisitor) { v.DoFuncDecl(d); }
|
|
|
|
func (d *DeclList) Visit(v DeclVisitor) { v.DoDeclList(d); }
|
2008-10-17 17:19:31 -06:00
|
|
|
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Program
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Comment struct {
|
2009-01-15 18:16:41 -07:00
|
|
|
Pos int;
|
|
|
|
Text string;
|
2008-10-17 00:30:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func NewComment(pos int, text string) *Comment {
|
2009-01-06 16:01:04 -07:00
|
|
|
c := new(Comment);
|
2009-01-15 18:16:41 -07:00
|
|
|
c.Pos, c.Text = pos, text;
|
2008-10-17 00:30:42 -06:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Program struct {
|
2009-03-03 19:25:07 -07:00
|
|
|
Pos int; // tok is token.PACKAGE
|
2009-02-03 18:44:01 -07:00
|
|
|
Ident Expr;
|
2009-02-27 16:40:17 -07:00
|
|
|
Decls []Decl;
|
2009-02-13 16:07:56 -07:00
|
|
|
Comments *vector.Vector;
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func NewProgram(pos int) *Program {
|
2009-01-06 16:01:04 -07:00
|
|
|
p := new(Program);
|
2009-01-15 18:16:41 -07:00
|
|
|
p.Pos = pos;
|
2008-10-14 19:14:01 -06:00
|
|
|
return p;
|
|
|
|
}
|