Commit 5d571cc6 authored by Robert Griesemer's avatar Robert Griesemer

snapshot:

- ast statements now use interfaces
- deleted old (now unused) code

R=r
OCL=24422
CL=24422
parent 1595a194
...@@ -18,7 +18,6 @@ type ( ...@@ -18,7 +18,6 @@ type (
Block struct; Block struct;
Expr interface; Expr interface;
StatImpl struct;
Decl struct; Decl struct;
) )
...@@ -323,29 +322,6 @@ func (typ* Type) String() string { ...@@ -323,29 +322,6 @@ func (typ* Type) String() string {
var BadType = NewType(0, Scanner.ILLEGAL); var BadType = NewType(0, Scanner.ILLEGAL);
// ----------------------------------------------------------------------------
// Blocks
//
// Syntactic constructs of the form:
//
// "{" StatementList "}"
// ":" StatementList
type Block struct {
Node;
List *array.Array;
End int; // position of closing "}" if present
}
func NewBlock(pos, tok int) *Block {
assert(tok == Scanner.LBRACE || tok == Scanner.COLON);
b := new(Block);
b.Pos, b.Tok, b.List = pos, tok, array.New(0);
return b;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Expressions // Expressions
...@@ -515,6 +491,29 @@ func (t *Type) Nfields() int { ...@@ -515,6 +491,29 @@ func (t *Type) Nfields() int {
} }
// ----------------------------------------------------------------------------
// Blocks
//
// Syntactic constructs of the form:
//
// "{" StatementList "}"
// ":" StatementList
type Block struct {
Node;
List *array.Array;
End int; // position of closing "}" if present
}
func NewBlock(pos, tok int) *Block {
assert(tok == Scanner.LBRACE || tok == Scanner.COLON);
b := new(Block);
b.Pos, b.Tok, b.List = pos, tok, array.New(0);
return b;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Statements // Statements
...@@ -543,7 +542,11 @@ type ( ...@@ -543,7 +542,11 @@ type (
Tok int; // INC, DEC, RETURN, GO, DEFER Tok int; // INC, DEC, RETURN, GO, DEFER
Expr Expr; Expr Expr;
}; };
CompositeStat struct {
Body *Block;
};
IfStat struct { IfStat struct {
Pos int; // position of "if" Pos int; // position of "if"
Init Stat; Init Stat;
...@@ -559,7 +562,13 @@ type ( ...@@ -559,7 +562,13 @@ type (
Post Stat; Post Stat;
Body *Block; Body *Block;
}; };
CaseClause struct {
Pos int; // position for "case" or "default"
Expr Expr; // nil means default case
Body *Block;
};
SwitchStat struct { SwitchStat struct {
Pos int; // position of "switch" Pos int; // position of "switch"
Init Stat; Init Stat;
...@@ -585,8 +594,10 @@ type StatVisitor interface { ...@@ -585,8 +594,10 @@ type StatVisitor interface {
DoLabelDecl(s *LabelDecl); DoLabelDecl(s *LabelDecl);
DoDeclarationStat(s *DeclarationStat); DoDeclarationStat(s *DeclarationStat);
DoExpressionStat(s *ExpressionStat); DoExpressionStat(s *ExpressionStat);
DoCompositeStat(s *CompositeStat);
DoIfStat(s *IfStat); DoIfStat(s *IfStat);
DoForStat(s *ForStat); DoForStat(s *ForStat);
DoCaseClause(s *CaseClause);
DoSwitchStat(s *SwitchStat); DoSwitchStat(s *SwitchStat);
DoSelectStat(s *SelectStat); DoSelectStat(s *SelectStat);
DoControlFlowStat(s *ControlFlowStat); DoControlFlowStat(s *ControlFlowStat);
...@@ -597,35 +608,15 @@ func (s *BadStat) Visit(v StatVisitor) { v.DoBadStat(s); } ...@@ -597,35 +608,15 @@ func (s *BadStat) Visit(v StatVisitor) { v.DoBadStat(s); }
func (s *LabelDecl) Visit(v StatVisitor) { v.DoLabelDecl(s); } func (s *LabelDecl) Visit(v StatVisitor) { v.DoLabelDecl(s); }
func (s *DeclarationStat) Visit(v StatVisitor) { v.DoDeclarationStat(s); } func (s *DeclarationStat) Visit(v StatVisitor) { v.DoDeclarationStat(s); }
func (s *ExpressionStat) Visit(v StatVisitor) { v.DoExpressionStat(s); } func (s *ExpressionStat) Visit(v StatVisitor) { v.DoExpressionStat(s); }
func (s *CompositeStat) Visit(v StatVisitor) { v.DoCompositeStat(s); }
func (s *IfStat) Visit(v StatVisitor) { v.DoIfStat(s); } func (s *IfStat) Visit(v StatVisitor) { v.DoIfStat(s); }
func (s *ForStat) Visit(v StatVisitor) { v.DoForStat(s); } func (s *ForStat) Visit(v StatVisitor) { v.DoForStat(s); }
func (s *CaseClause) Visit(v StatVisitor) { v.DoCaseClause(s); }
func (s *SwitchStat) Visit(v StatVisitor) { v.DoSwitchStat(s); } func (s *SwitchStat) Visit(v StatVisitor) { v.DoSwitchStat(s); }
func (s *SelectStat) Visit(v StatVisitor) { v.DoSelectStat(s); } func (s *SelectStat) Visit(v StatVisitor) { v.DoSelectStat(s); }
func (s *ControlFlowStat) Visit(v StatVisitor) { v.DoControlFlowStat(s); } func (s *ControlFlowStat) Visit(v StatVisitor) { v.DoControlFlowStat(s); }
// ----------------------------------------------------------------------------
// Old style statements
type StatImpl struct {
Node;
Init, Post *StatImpl;
Expr Expr;
Body *Block; // composite statement body
Decl *Decl; // declaration statement
}
func NewStat(pos, tok int) *StatImpl {
s := new(StatImpl);
s.Pos, s.Tok = pos, tok;
return s;
}
var OldBadStat = NewStat(0, Scanner.ILLEGAL);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Declarations // Declarations
......
This diff is collapsed.
...@@ -727,20 +727,10 @@ func (P *Printer) Stat(s AST.Stat) { ...@@ -727,20 +727,10 @@ func (P *Printer) Stat(s AST.Stat) {
} }
func (P *Printer) StatImpl(s *AST.StatImpl)
func (P *Printer) StatementList(list *array.Array) { func (P *Printer) StatementList(list *array.Array) {
for i, n := 0, list.Len(); i < n; i++ { for i, n := 0, list.Len(); i < n; i++ {
P.newlines = 1; // for first entry P.newlines = 1; // for first entry
list.At(i).(AST.Stat).Visit(P);
if s, is_StatImpl := list.At(i).(*AST.StatImpl); is_StatImpl {
P.StatImpl(s);
} else if s, is_Stat := list.At(i).(AST.Stat); is_Stat {
s.Visit(P);
} else {
panic();
}
P.newlines = 1; P.newlines = 1;
P.state = inside_list; P.state = inside_list;
} }
...@@ -769,120 +759,8 @@ func (P *Printer) Block(b *AST.Block, indent bool) { ...@@ -769,120 +759,8 @@ func (P *Printer) Block(b *AST.Block, indent bool) {
} }
func (P *Printer) OldControlClause(s *AST.StatImpl) {
has_post := s.Tok == Scanner.FOR && s.Post != nil; // post also used by "if"
P.separator = blank;
if s.Init == nil && !has_post {
// no semicolons required
if s.Expr != nil {
P.Expr(s.Expr);
}
} else {
// all semicolons required
// (they are not separators, print them explicitly)
if s.Init != nil {
P.StatImpl(s.Init);
P.separator = none;
}
P.String(0, ";");
P.separator = blank;
if s.Expr != nil {
P.Expr(s.Expr);
P.separator = none;
}
if s.Tok == Scanner.FOR {
P.String(0, ";");
P.separator = blank;
if has_post {
P.StatImpl(s.Post);
}
}
}
P.separator = blank;
}
func (P *Printer) Declaration(d *AST.Decl, parenthesized bool); func (P *Printer) Declaration(d *AST.Decl, parenthesized bool);
func (P *Printer) StatImpl(s *AST.StatImpl) {
switch s.Tok {
case Scanner.EXPRSTAT:
// expression statement
P.Expr(s.Expr);
P.separator = semicolon;
case Scanner.COLON:
// label declaration
P.indentation--;
P.Expr(s.Expr);
P.Token(s.Pos, s.Tok);
P.indentation++;
P.separator = none;
case Scanner.CONST, Scanner.TYPE, Scanner.VAR:
// declaration
P.Declaration(s.Decl, false);
case Scanner.INC, Scanner.DEC:
P.Expr(s.Expr);
P.Token(s.Pos, s.Tok);
P.separator = semicolon;
case Scanner.LBRACE:
// block
P.Block(s.Body, true);
case Scanner.IF:
P.String(s.Pos, "if");
P.OldControlClause(s);
P.Block(s.Body, true);
if s.Post != nil {
P.separator = blank;
P.String(0, "else");
P.separator = blank;
P.StatImpl(s.Post);
}
case Scanner.FOR:
P.String(s.Pos, "for");
P.OldControlClause(s);
P.Block(s.Body, true);
case Scanner.SWITCH, Scanner.SELECT:
P.Token(s.Pos, s.Tok);
P.OldControlClause(s);
P.Block(s.Body, false);
case Scanner.CASE, Scanner.DEFAULT:
P.Token(s.Pos, s.Tok);
if s.Expr != nil {
P.separator = blank;
P.Expr(s.Expr);
}
// TODO: try to use P.Block instead
// P.Block(s.Body, true);
P.String(s.Body.Pos, ":");
P.indentation++;
P.StatementList(s.Body.List);
P.indentation--;
P.newlines = 1;
case
Scanner.GO, Scanner.DEFER, Scanner.RETURN, Scanner.FALLTHROUGH,
Scanner.BREAK, Scanner.CONTINUE, Scanner.GOTO:
P.Token(s.Pos, s.Tok);
if s.Expr != nil {
P.separator = blank;
P.Expr(s.Expr);
}
P.separator = semicolon;
default:
P.Error(s.Pos, s.Tok, "stat");
}
}
func (P *Printer) DoBadStat(s *AST.BadStat) { func (P *Printer) DoBadStat(s *AST.BadStat) {
panic(); panic();
...@@ -890,7 +768,11 @@ func (P *Printer) DoBadStat(s *AST.BadStat) { ...@@ -890,7 +768,11 @@ func (P *Printer) DoBadStat(s *AST.BadStat) {
func (P *Printer) DoLabelDecl(s *AST.LabelDecl) { func (P *Printer) DoLabelDecl(s *AST.LabelDecl) {
panic(); P.indentation--;
P.Expr(s.Label);
P.String(s.Pos, ":");
P.indentation++;
P.separator = none;
} }
...@@ -920,6 +802,11 @@ func (P *Printer) DoExpressionStat(s *AST.ExpressionStat) { ...@@ -920,6 +802,11 @@ func (P *Printer) DoExpressionStat(s *AST.ExpressionStat) {
} }
func (P *Printer) DoCompositeStat(s *AST.CompositeStat) {
P.Block(s.Body, true);
}
func (P *Printer) ControlClause(isForStat bool, init AST.Stat, expr AST.Expr, post AST.Stat) { func (P *Printer) ControlClause(isForStat bool, init AST.Stat, expr AST.Expr, post AST.Stat) {
P.separator = blank; P.separator = blank;
if init == nil && post == nil { if init == nil && post == nil {
...@@ -972,6 +859,24 @@ func (P *Printer) DoForStat(s *AST.ForStat) { ...@@ -972,6 +859,24 @@ func (P *Printer) DoForStat(s *AST.ForStat) {
} }
func (P *Printer) DoCaseClause(s *AST.CaseClause) {
if s.Expr != nil {
P.String(s.Pos, "case");
P.separator = blank;
P.Expr(s.Expr);
} else {
P.String(s.Pos, "default");
}
// TODO: try to use P.Block instead
// P.Block(s.Body, true);
P.String(s.Body.Pos, ":");
P.indentation++;
P.StatementList(s.Body.List);
P.indentation--;
P.newlines = 1;
}
func (P *Printer) DoSwitchStat(s *AST.SwitchStat) { func (P *Printer) DoSwitchStat(s *AST.SwitchStat) {
P.String(s.Pos, "switch"); P.String(s.Pos, "switch");
P.ControlClause(false, s.Init, s.Tag, nil); P.ControlClause(false, s.Init, s.Tag, nil);
...@@ -980,7 +885,9 @@ func (P *Printer) DoSwitchStat(s *AST.SwitchStat) { ...@@ -980,7 +885,9 @@ func (P *Printer) DoSwitchStat(s *AST.SwitchStat) {
func (P *Printer) DoSelectStat(s *AST.SelectStat) { func (P *Printer) DoSelectStat(s *AST.SelectStat) {
panic(); P.String(s.Pos, "select");
P.separator = blank;
P.Block(s.Body, false);
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#!/bin/bash #!/bin/bash
CMD="./pretty"
TMP1=test_tmp1.go TMP1=test_tmp1.go
TMP2=test_tmp2.go TMP2=test_tmp2.go
TMP3=test_tmp3.go TMP3=test_tmp3.go
...@@ -66,7 +67,7 @@ cleanup() { ...@@ -66,7 +67,7 @@ cleanup() {
silent() { silent() {
cleanup cleanup
./pretty -s $1 > $TMP1 $CMD -s $1 > $TMP1
if [ $? != 0 ]; then if [ $? != 0 ]; then
cat $TMP1 cat $TMP1
echo "Error (silent mode test): test.sh $1" echo "Error (silent mode test): test.sh $1"
...@@ -77,9 +78,9 @@ silent() { ...@@ -77,9 +78,9 @@ silent() {
idempotent() { idempotent() {
cleanup cleanup
./pretty $1 > $TMP1 $CMD $1 > $TMP1
./pretty $TMP1 > $TMP2 $CMD $TMP1 > $TMP2
./pretty $TMP2 > $TMP3 $CMD $TMP2 > $TMP3
cmp -s $TMP2 $TMP3 cmp -s $TMP2 $TMP3
if [ $? != 0 ]; then if [ $? != 0 ]; then
diff $TMP2 $TMP3 diff $TMP2 $TMP3
...@@ -91,7 +92,7 @@ idempotent() { ...@@ -91,7 +92,7 @@ idempotent() {
valid() { valid() {
cleanup cleanup
./pretty $1 > $TMP1 $CMD $1 > $TMP1
6g -o /dev/null $TMP1 6g -o /dev/null $TMP1
if [ $? != 0 ]; then if [ $? != 0 ]; then
echo "Error (validity test): test.sh $1" echo "Error (validity test): test.sh $1"
...@@ -128,10 +129,10 @@ runtests() { ...@@ -128,10 +129,10 @@ runtests() {
# run selftest1 always # run selftest1 always
./pretty -t selftest1.go > $TMP1 $CMD -t selftest1.go > $TMP1
if [ $? != 0 ]; then if [ $? != 0 ]; then
cat $TMP1 cat $TMP1
echo "Error (selftest1): pretty -t selftest1.go" echo "Error (selftest1): $CMD -t selftest1.go"
exit 1 exit 1
fi fi
count selftest1.go count selftest1.go
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment