2008-09-18 17:58:37 -06:00
|
|
|
// 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.
|
|
|
|
|
2008-09-22 19:26:12 -06:00
|
|
|
package Printer
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-09-22 19:26:12 -06:00
|
|
|
import Scanner "scanner"
|
|
|
|
import AST "ast"
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-09-22 19:26:12 -06:00
|
|
|
|
2008-09-25 12:50:34 -06:00
|
|
|
// Printer implements AST.Visitor
|
|
|
|
type Printer struct {
|
2008-09-23 17:40:12 -06:00
|
|
|
indent int;
|
2008-09-22 19:26:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 19:34:17 -06:00
|
|
|
func (P *Printer) NewLine(delta int) {
|
|
|
|
P.indent += delta;
|
|
|
|
if P.indent < 0 {
|
|
|
|
panic("negative indent");
|
|
|
|
}
|
|
|
|
print("\n");
|
|
|
|
for i := P.indent; i > 0; i-- {
|
|
|
|
print("\t");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
func (P *Printer) String(s string) {
|
2008-09-22 19:26:12 -06:00
|
|
|
print(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
func (P *Printer) Print(x AST.Node) {
|
|
|
|
x.Visit(P);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 19:34:17 -06:00
|
|
|
func (P *Printer) PrintList(p *AST.List) {
|
2008-09-24 16:50:28 -06:00
|
|
|
for i := 0; i < p.len(); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
P.String(", ");
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
2008-09-24 16:50:28 -06:00
|
|
|
P.Print(p.at(i));
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Basics
|
|
|
|
|
|
|
|
func (P *Printer) DoNil(x *AST.Nil) {
|
2008-09-25 12:50:34 -06:00
|
|
|
P.String("<NIL>");
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoIdent(x *AST.Ident) {
|
|
|
|
P.String(x.val);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 19:34:17 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Types
|
|
|
|
|
|
|
|
func (P *Printer) DoFunctionType(x *AST.FunctionType) {
|
|
|
|
/*
|
|
|
|
if x.recv != nil {
|
|
|
|
P.DoVarDeclList(x.recv);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
P.String("(");
|
|
|
|
P.PrintList(x.params);
|
|
|
|
P.String(") ");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-24 16:50:28 -06:00
|
|
|
func (P *Printer) DoArrayType(x *AST.ArrayType) {
|
|
|
|
P.String("[");
|
|
|
|
P.Print(x.len_);
|
|
|
|
P.String("] ");
|
|
|
|
P.Print(x.elt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoStructType(x *AST.StructType) {
|
|
|
|
P.String("struct {");
|
|
|
|
if x.fields.len() > 0 {
|
|
|
|
P.NewLine(1);
|
|
|
|
for i := 0; i < x.fields.len(); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
P.NewLine(0);
|
|
|
|
}
|
|
|
|
P.Print(x.fields.at(i));
|
|
|
|
P.String(";");
|
|
|
|
}
|
|
|
|
P.NewLine(-1);
|
|
|
|
}
|
|
|
|
P.String("}");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoMapType(x *AST.MapType) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoChannelType(x *AST.ChannelType) {
|
|
|
|
P.String("chan ");
|
|
|
|
P.Print(x.elt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoInterfaceType(x *AST.InterfaceType) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoPointerType(x *AST.PointerType) {
|
|
|
|
P.String("*");
|
|
|
|
P.Print(x.base);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Declarations
|
|
|
|
|
|
|
|
func (P *Printer) DoBlock(x *AST.Block);
|
|
|
|
|
2008-09-23 19:34:17 -06:00
|
|
|
|
2008-09-24 16:50:28 -06:00
|
|
|
func (P *Printer) DoConstDecl(x *AST.ConstDecl) {
|
|
|
|
P.Print(x.ident);
|
|
|
|
P.String(" ");
|
|
|
|
P.Print(x.typ);
|
|
|
|
P.String(" = ");
|
|
|
|
P.Print(x.val);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoTypeDecl(x *AST.TypeDecl) {
|
|
|
|
P.Print(x.ident);
|
|
|
|
P.String(" ");
|
|
|
|
P.Print(x.typ);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoVarDecl(x *AST.VarDecl) {
|
|
|
|
P.PrintList(x.idents);
|
|
|
|
P.String(" ");
|
|
|
|
P.Print(x.typ);
|
|
|
|
if x.vals != nil {
|
|
|
|
P.String(" = ");
|
|
|
|
P.PrintList(x.vals);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoVarDeclList(x *AST.VarDeclList) {
|
|
|
|
P.PrintList(x.idents);
|
|
|
|
P.String(" ");
|
|
|
|
P.Print(x.typ);
|
|
|
|
}
|
2008-09-23 19:34:17 -06:00
|
|
|
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
func (P *Printer) DoFuncDecl(x *AST.FuncDecl) {
|
|
|
|
P.String("func ");
|
2008-09-23 19:34:17 -06:00
|
|
|
if x.typ.recv != nil {
|
|
|
|
P.String("(");
|
2008-09-25 12:50:34 -06:00
|
|
|
P.DoVarDeclList(x.typ.recv);
|
2008-09-23 19:34:17 -06:00
|
|
|
P.String(") ");
|
|
|
|
}
|
2008-09-23 17:40:12 -06:00
|
|
|
P.DoIdent(x.ident);
|
2008-09-23 19:34:17 -06:00
|
|
|
P.DoFunctionType(x.typ);
|
2008-09-23 17:40:12 -06:00
|
|
|
if x.body != nil {
|
|
|
|
P.DoBlock(x.body);
|
|
|
|
} else {
|
2008-09-23 19:34:17 -06:00
|
|
|
P.String(";");
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
2008-09-23 19:34:17 -06:00
|
|
|
P.NewLine(0);
|
|
|
|
P.NewLine(0);
|
|
|
|
P.NewLine(0);
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-24 16:50:28 -06:00
|
|
|
func (P *Printer) DoDeclaration(x *AST.Declaration) {
|
|
|
|
P.String(Scanner.TokenName(x.tok));
|
|
|
|
P.String(" ");
|
|
|
|
switch x.decls.len() {
|
|
|
|
case 0:
|
|
|
|
P.String("()");
|
|
|
|
case 1:
|
|
|
|
P.Print(x.decls.at(0));
|
|
|
|
default:
|
|
|
|
P.String("(");
|
|
|
|
P.NewLine(1);
|
|
|
|
for i := 0; i < x.decls.len(); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
P.NewLine(0);
|
|
|
|
}
|
2008-09-25 12:50:34 -06:00
|
|
|
//print("*** i = ", i, "\n");
|
2008-09-24 16:50:28 -06:00
|
|
|
P.Print(x.decls.at(i));
|
|
|
|
}
|
|
|
|
P.NewLine(-1);
|
|
|
|
P.String(")");
|
|
|
|
}
|
|
|
|
P.NewLine(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Expressions
|
|
|
|
|
|
|
|
func (P *Printer) DoBinary(x *AST.Binary) {
|
|
|
|
print("(");
|
|
|
|
P.Print(x.x);
|
|
|
|
P.String(" " + Scanner.TokenName(x.tok) + " ");
|
|
|
|
P.Print(x.y);
|
|
|
|
print(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoUnary(x *AST.Unary) {
|
|
|
|
P.String(Scanner.TokenName(x.tok));
|
|
|
|
P.Print(x.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoLiteral(x *AST.Literal) {
|
|
|
|
P.String(x.val);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoPair(x *AST.Pair) {
|
|
|
|
P.Print(x.x);
|
|
|
|
P.String(" : ");
|
|
|
|
P.Print(x.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoIndex(x *AST.Index) {
|
|
|
|
P.Print(x.x);
|
|
|
|
P.String("[");
|
|
|
|
P.Print(x.index);
|
|
|
|
P.String("]");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoCall(x *AST.Call) {
|
|
|
|
P.Print(x.fun);
|
|
|
|
P.String("(");
|
2008-09-23 19:34:17 -06:00
|
|
|
P.PrintList(x.args);
|
2008-09-23 17:40:12 -06:00
|
|
|
P.String(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoSelector(x *AST.Selector) {
|
|
|
|
P.Print(x.x);
|
|
|
|
P.String(".");
|
|
|
|
P.String(x.field);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Statements
|
|
|
|
|
|
|
|
func (P *Printer) DoBlock(x *AST.Block) {
|
|
|
|
if x == nil || x.stats == nil {
|
2008-09-23 19:34:17 -06:00
|
|
|
P.NewLine(0);
|
2008-09-22 19:26:12 -06:00
|
|
|
return;
|
|
|
|
}
|
2008-09-23 17:40:12 -06:00
|
|
|
|
2008-09-23 19:34:17 -06:00
|
|
|
P.String("{");
|
|
|
|
P.NewLine(1);
|
2008-09-23 17:40:12 -06:00
|
|
|
for i := 0; i < x.stats.len(); i++ {
|
2008-09-23 19:34:17 -06:00
|
|
|
if i > 0 {
|
|
|
|
P.NewLine(0);
|
|
|
|
}
|
2008-09-23 17:40:12 -06:00
|
|
|
P.Print(x.stats.at(i));
|
|
|
|
}
|
2008-09-23 19:34:17 -06:00
|
|
|
P.NewLine(-1);
|
|
|
|
P.String("}");
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoExprStat(x *AST.ExprStat) {
|
|
|
|
P.Print(x.expr);
|
2008-09-23 19:34:17 -06:00
|
|
|
P.String(";");
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoAssignment(x *AST.Assignment) {
|
2008-09-23 19:34:17 -06:00
|
|
|
P.PrintList(x.lhs);
|
2008-09-23 17:40:12 -06:00
|
|
|
P.String(" " + Scanner.TokenName(x.tok) + " ");
|
2008-09-23 19:34:17 -06:00
|
|
|
P.PrintList(x.rhs);
|
|
|
|
P.String(";");
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-25 12:50:34 -06:00
|
|
|
func (P *Printer) PrintControlClause(x *AST.ControlClause) {
|
|
|
|
if x.has_init {
|
|
|
|
P.String(" ");
|
|
|
|
P.Print(x.init);
|
|
|
|
P.String(";");
|
|
|
|
}
|
|
|
|
if x.has_expr {
|
|
|
|
P.String(" ");
|
|
|
|
P.Print(x.expr);
|
|
|
|
}
|
|
|
|
if x.has_post {
|
|
|
|
P.String("; ");
|
|
|
|
P.Print(x.post);
|
|
|
|
}
|
|
|
|
P.String(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 19:34:17 -06:00
|
|
|
func (P *Printer) DoIfStat(x *AST.IfStat) {
|
2008-09-25 12:50:34 -06:00
|
|
|
P.String("if");
|
|
|
|
P.PrintControlClause(x.ctrl);
|
2008-09-23 17:40:12 -06:00
|
|
|
P.DoBlock(x.then);
|
2008-09-25 12:50:34 -06:00
|
|
|
if x.has_else {
|
2008-09-23 19:34:17 -06:00
|
|
|
P.String(" else ");
|
2008-09-25 12:50:34 -06:00
|
|
|
P.Print(x.else_);
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 19:34:17 -06:00
|
|
|
func (P *Printer) DoForStat(x *AST.ForStat) {
|
2008-09-25 12:50:34 -06:00
|
|
|
P.String("for");
|
|
|
|
P.PrintControlClause(x.ctrl);
|
2008-09-23 19:34:17 -06:00
|
|
|
P.DoBlock(x.body);
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-24 16:50:28 -06:00
|
|
|
/*
|
|
|
|
func AnalyzeCase(x *AST.SwitchStat) bool {
|
|
|
|
for i := 0; i < x.cases.len(); i++ {
|
|
|
|
clause := x.cases.at(i).(AST.CaseClause);
|
|
|
|
if clause.stats.len() > 1 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoCaseClause(x *AST.CaseClause) {
|
|
|
|
if x.exprs != nil {
|
|
|
|
P.String("case ");
|
|
|
|
P.PrintList(x.exprs);
|
|
|
|
P.String(":");
|
|
|
|
} else {
|
|
|
|
P.String("default:");
|
|
|
|
}
|
|
|
|
|
|
|
|
n := x.stats.len();
|
|
|
|
m := n;
|
|
|
|
if x.falls {
|
|
|
|
m++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if m == 0 {
|
|
|
|
P.NewLine(0);
|
|
|
|
} else {
|
|
|
|
P.NewLine(1);
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if i > 0 {
|
|
|
|
P.NewLine(0);
|
|
|
|
}
|
|
|
|
P.Print(x.stats.at(i));
|
|
|
|
}
|
|
|
|
if x.falls {
|
|
|
|
if n > 0 {
|
|
|
|
P.NewLine(0);
|
|
|
|
}
|
|
|
|
P.String("fallthrough;");
|
|
|
|
}
|
|
|
|
P.NewLine(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) DoSwitchStat(x *AST.SwitchStat) {
|
2008-09-25 12:50:34 -06:00
|
|
|
P.String("switch");
|
|
|
|
P.PrintControlClause(x.ctrl);
|
2008-09-24 16:50:28 -06:00
|
|
|
P.String("{");
|
|
|
|
P.NewLine(0);
|
|
|
|
for i := 0; i < x.cases.len(); i++ {
|
|
|
|
P.Print(x.cases.at(i));
|
|
|
|
}
|
|
|
|
P.NewLine(0);
|
|
|
|
P.String("}");
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-24 16:50:28 -06:00
|
|
|
func (P *Printer) DoReturnStat(x *AST.ReturnStat) {
|
2008-09-23 17:40:12 -06:00
|
|
|
P.String("return ");
|
2008-09-23 19:34:17 -06:00
|
|
|
P.PrintList(x.res);
|
|
|
|
P.String(";");
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-24 16:50:28 -06:00
|
|
|
func (P *Printer) DoIncDecStat(x *AST.IncDecStat) {
|
|
|
|
P.Print(x.expr);
|
|
|
|
P.String(Scanner.TokenName(x.tok));
|
|
|
|
P.String(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-25 12:50:34 -06:00
|
|
|
func (P *Printer) DoControlFlowStat(x *AST.ControlFlowStat) {
|
|
|
|
P.String(Scanner.TokenName(x.tok));
|
|
|
|
if x.label != nil {
|
|
|
|
P.String(" ");
|
|
|
|
P.Print(x.label);
|
|
|
|
}
|
|
|
|
P.String(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Program
|
|
|
|
|
|
|
|
func (P *Printer) DoProgram(x *AST.Program) {
|
|
|
|
P.String("package ");
|
|
|
|
P.DoIdent(x.ident);
|
2008-09-23 19:34:17 -06:00
|
|
|
P.NewLine(0);
|
2008-09-23 17:40:12 -06:00
|
|
|
for i := 0; i < x.decls.len(); i++ {
|
|
|
|
P.Print(x.decls.at(i));
|
2008-09-22 19:26:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Driver
|
|
|
|
|
|
|
|
export func Print(x AST.Node) {
|
2008-09-22 19:26:12 -06:00
|
|
|
var P Printer;
|
2008-09-23 17:40:12 -06:00
|
|
|
(&P).Print(x);
|
2008-09-22 19:26:12 -06:00
|
|
|
print("\n");
|
|
|
|
}
|
2008-09-23 17:40:12 -06:00
|
|
|
|