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"
|
2008-10-14 19:14:01 -06:00
|
|
|
import Node "node"
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-09-22 19:26:12 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
export type Printer struct {
|
2008-09-30 19:50:29 -06:00
|
|
|
level int; // true scope level
|
|
|
|
indent int; // indentation level
|
|
|
|
semi bool; // pending ";"
|
2008-10-16 11:16:59 -06:00
|
|
|
newl int; // pending "\n"'s
|
2008-09-22 19:26:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-15 18:06:28 -06:00
|
|
|
func (P *Printer) String(pos int, s string) {
|
2008-09-30 19:50:29 -06:00
|
|
|
if P.semi && P.level > 0 { // no semicolons at level 0
|
|
|
|
print(";");
|
2008-09-23 19:34:17 -06:00
|
|
|
}
|
2008-10-16 11:16:59 -06:00
|
|
|
|
|
|
|
if P.newl > 0 {
|
|
|
|
for i := P.newl; i > 0; i-- {
|
|
|
|
print("\n");
|
|
|
|
}
|
2008-09-30 19:50:29 -06:00
|
|
|
for i := P.indent; i > 0; i-- {
|
|
|
|
print("\t");
|
|
|
|
}
|
2008-09-23 19:34:17 -06:00
|
|
|
}
|
2008-10-16 11:16:59 -06:00
|
|
|
|
2008-09-30 19:50:29 -06:00
|
|
|
print(s);
|
2008-10-16 11:16:59 -06:00
|
|
|
|
|
|
|
P.semi, P.newl = false, 0;
|
2008-09-23 19:34:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-15 18:06:28 -06:00
|
|
|
func (P *Printer) Blank() {
|
|
|
|
P.String(0, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) Token(pos int, tok int) {
|
|
|
|
P.String(pos, Scanner.TokenString(tok));
|
2008-10-15 12:48:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-30 19:50:29 -06:00
|
|
|
func (P *Printer) OpenScope(paren string) {
|
2008-10-16 11:16:59 -06:00
|
|
|
P.semi, P.newl = false, 0;
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(0, paren);
|
2008-09-30 19:50:29 -06:00
|
|
|
P.level++;
|
|
|
|
P.indent++;
|
2008-10-16 11:16:59 -06:00
|
|
|
P.newl = 1;
|
2008-09-30 19:50:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) CloseScope(paren string) {
|
|
|
|
P.indent--;
|
2008-09-30 20:31:27 -06:00
|
|
|
P.semi = false;
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(0, paren);
|
2008-09-30 20:31:27 -06:00
|
|
|
P.level--;
|
2008-10-16 11:16:59 -06:00
|
|
|
P.semi, P.newl = false, 1;
|
2008-09-22 19:26:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 19:34:17 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Types
|
|
|
|
|
2008-10-15 18:06:28 -06:00
|
|
|
func (P *Printer) Type(t *Node.Type)
|
2008-10-14 19:14:01 -06:00
|
|
|
func (P *Printer) Expr(x *Node.Expr)
|
2008-09-24 16:50:28 -06:00
|
|
|
|
2008-10-16 13:16:50 -06:00
|
|
|
func (P *Printer) Parameters(pos int, list *Node.List) {
|
|
|
|
P.String(pos, "(");
|
2008-10-15 18:06:28 -06:00
|
|
|
var prev int;
|
|
|
|
for i, n := 0, list.len(); i < n; i++ {
|
|
|
|
x := list.at(i).(*Node.Expr);
|
|
|
|
if i > 0 {
|
|
|
|
if prev == x.tok || prev == Scanner.TYPE {
|
|
|
|
P.String(0, ", ");
|
|
|
|
} else {
|
|
|
|
P.Blank();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
P.Expr(x);
|
|
|
|
prev = x.tok;
|
|
|
|
}
|
2008-10-16 13:16:50 -06:00
|
|
|
P.String(0, ")");
|
2008-10-15 18:06:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) Fields(list *Node.List) {
|
2008-10-16 15:25:23 -06:00
|
|
|
P.OpenScope(" {");
|
2008-10-15 18:06:28 -06:00
|
|
|
var prev int;
|
|
|
|
for i, n := 0, list.len(); i < n; i++ {
|
|
|
|
x := list.at(i).(*Node.Expr);
|
|
|
|
if i > 0 {
|
|
|
|
if prev == Scanner.TYPE {
|
|
|
|
P.String(0, ";");
|
2008-10-16 11:16:59 -06:00
|
|
|
P.newl = 1;
|
2008-10-15 18:06:28 -06:00
|
|
|
} else if prev == x.tok {
|
|
|
|
P.String(0, ", ");
|
|
|
|
} else {
|
|
|
|
P.Blank();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
P.Expr(x);
|
|
|
|
prev = x.tok;
|
|
|
|
}
|
2008-10-16 11:16:59 -06:00
|
|
|
P.newl = 1;
|
2008-10-16 15:25:23 -06:00
|
|
|
P.CloseScope("}");
|
2008-10-15 18:06:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
func (P *Printer) Type(t *Node.Type) {
|
2008-10-15 18:06:28 -06:00
|
|
|
if t == nil { // TODO remove this check
|
|
|
|
P.String(0, "<nil type>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
switch t.tok {
|
|
|
|
case Scanner.IDENT:
|
|
|
|
P.Expr(t.expr);
|
2008-09-24 16:50:28 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
case Scanner.LBRACK:
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(t.pos, "[");
|
2008-10-14 19:14:01 -06:00
|
|
|
if t.expr != nil {
|
|
|
|
P.Expr(t.expr);
|
|
|
|
}
|
2008-10-16 13:16:50 -06:00
|
|
|
P.String(0, "]");
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Type(t.elt);
|
|
|
|
|
2008-10-16 15:25:23 -06:00
|
|
|
case Scanner.STRUCT, Scanner.INTERFACE:
|
|
|
|
P.Token(t.pos, t.tok);
|
2008-10-14 19:14:01 -06:00
|
|
|
if t.list != nil {
|
2008-10-16 15:25:23 -06:00
|
|
|
P.Blank();
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Fields(t.list);
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
2008-09-24 16:50:28 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
case Scanner.MAP:
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(t.pos, "[");
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Type(t.key);
|
2008-10-16 13:16:50 -06:00
|
|
|
P.String(0, "]");
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Type(t.elt);
|
|
|
|
|
|
|
|
case Scanner.CHAN:
|
2008-10-15 18:06:28 -06:00
|
|
|
var m string;
|
2008-10-14 19:14:01 -06:00
|
|
|
switch t.mode {
|
2008-10-15 18:06:28 -06:00
|
|
|
case Node.FULL: m = "chan ";
|
|
|
|
case Node.RECV: m = "<-chan ";
|
|
|
|
case Node.SEND: m = "chan <- ";
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(t.pos, m);
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Type(t.elt);
|
|
|
|
|
|
|
|
case Scanner.MUL:
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(t.pos, "*");
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Type(t.elt);
|
2008-09-24 16:50:28 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
case Scanner.LPAREN:
|
2008-10-16 13:16:50 -06:00
|
|
|
P.Parameters(t.pos, t.list);
|
2008-10-15 18:06:28 -06:00
|
|
|
if t.elt != nil {
|
2008-10-16 13:16:50 -06:00
|
|
|
P.Blank();
|
|
|
|
P.Parameters(0, t.elt.list);
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
2008-09-24 16:50:28 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
default:
|
|
|
|
panic("UNREACHABLE");
|
2008-09-25 16:14:26 -06:00
|
|
|
}
|
2008-09-24 16:50:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
2008-10-14 19:14:01 -06:00
|
|
|
// Expressions
|
2008-09-24 16:50:28 -06:00
|
|
|
|
2008-10-15 12:48:18 -06:00
|
|
|
func (P *Printer) Expr1(x *Node.Expr, prec1 int) {
|
2008-10-14 19:14:01 -06:00
|
|
|
if x == nil {
|
|
|
|
return; // empty expression list
|
2008-09-24 16:50:28 -06:00
|
|
|
}
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
switch x.tok {
|
2008-10-15 18:06:28 -06:00
|
|
|
case Scanner.TYPE:
|
2008-10-16 15:25:23 -06:00
|
|
|
// type expr
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Type(x.t);
|
|
|
|
|
2008-10-15 12:48:18 -06:00
|
|
|
case Scanner.IDENT, Scanner.INT, Scanner.STRING, Scanner.FLOAT:
|
2008-10-16 15:25:23 -06:00
|
|
|
// literal
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(x.pos, x.s);
|
2008-10-15 12:48:18 -06:00
|
|
|
|
|
|
|
case Scanner.COMMA:
|
2008-10-16 15:25:23 -06:00
|
|
|
// list
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Expr1(x.x, 0);
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(x.pos, ", ");
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Expr1(x.y, 0);
|
|
|
|
|
|
|
|
case Scanner.PERIOD:
|
2008-10-16 15:25:23 -06:00
|
|
|
// selector or type guard
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Expr1(x.x, 8); // 8 == highest precedence
|
|
|
|
P.String(x.pos, ".");
|
2008-10-16 15:25:23 -06:00
|
|
|
if x.y != nil {
|
|
|
|
P.Expr1(x.y, 8);
|
|
|
|
} else {
|
|
|
|
P.String(0, "(");
|
|
|
|
P.Type(x.t);
|
|
|
|
P.String(0, ")");
|
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
|
|
|
|
case Scanner.LBRACK:
|
2008-10-16 15:25:23 -06:00
|
|
|
// index
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Expr1(x.x, 8);
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(x.pos, "[");
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Expr1(x.y, 0);
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(0, "]");
|
2008-10-15 12:48:18 -06:00
|
|
|
|
|
|
|
case Scanner.LPAREN:
|
2008-10-16 15:25:23 -06:00
|
|
|
// call
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Expr1(x.x, 8);
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(x.pos, "(");
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Expr1(x.y, 0);
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(0, ")");
|
2008-10-16 13:16:50 -06:00
|
|
|
|
|
|
|
case Scanner.LBRACE:
|
2008-10-16 15:25:23 -06:00
|
|
|
// composite
|
2008-10-16 13:16:50 -06:00
|
|
|
P.Expr1(x.x, 8);
|
|
|
|
P.String(x.pos, "{");
|
|
|
|
P.Expr1(x.y, 0);
|
|
|
|
P.String(0, "}");
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2008-09-24 16:50:28 -06:00
|
|
|
default:
|
2008-10-16 15:25:23 -06:00
|
|
|
// unary and binary expressions
|
2008-10-14 19:14:01 -06:00
|
|
|
if x.x == nil {
|
|
|
|
// unary expression
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Token(x.pos, x.tok);
|
|
|
|
P.Expr1(x.y, 7); // 7 == unary operator precedence
|
2008-10-14 19:14:01 -06:00
|
|
|
} else {
|
|
|
|
// binary expression: print ()'s if necessary
|
2008-10-15 12:48:18 -06:00
|
|
|
prec := Scanner.Precedence(x.tok);
|
|
|
|
if prec < prec1 {
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(0, "(");
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Expr1(x.x, prec);
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Blank();
|
|
|
|
P.Token(x.pos, x.tok);
|
|
|
|
P.Blank();
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Expr1(x.y, prec);
|
|
|
|
if prec < prec1 {
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(0, ")");
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
2008-09-24 16:50:28 -06:00
|
|
|
}
|
2008-09-25 16:14:26 -06:00
|
|
|
}
|
2008-09-25 18:20:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-15 12:48:18 -06:00
|
|
|
func (P *Printer) Expr(x *Node.Expr) {
|
|
|
|
P.Expr1(x, 0);
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-15 12:48:18 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Statements
|
2008-10-14 19:14:01 -06:00
|
|
|
|
|
|
|
func (P *Printer) Stat(s *Node.Stat)
|
|
|
|
|
|
|
|
func (P *Printer) StatementList(list *Node.List) {
|
|
|
|
for i, n := 0, list.len(); i < n; i++ {
|
|
|
|
P.Stat(list.at(i).(*Node.Stat));
|
2008-10-16 11:16:59 -06:00
|
|
|
P.newl = 1;
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-15 12:48:18 -06:00
|
|
|
func (P *Printer) Block(list *Node.List, indent bool) {
|
2008-10-14 19:14:01 -06:00
|
|
|
P.OpenScope("{");
|
2008-10-15 12:48:18 -06:00
|
|
|
if !indent {
|
|
|
|
P.indent--;
|
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
P.StatementList(list);
|
2008-10-15 12:48:18 -06:00
|
|
|
if !indent {
|
|
|
|
P.indent++;
|
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
P.CloseScope("}");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Printer) ControlClause(s *Node.Stat) {
|
2008-10-16 15:25:23 -06:00
|
|
|
has_post := s.tok == Scanner.FOR && s.post != nil; // post also used by "if"
|
|
|
|
if s.init == nil && !has_post {
|
|
|
|
// no semicolons required
|
|
|
|
if s.expr != nil {
|
|
|
|
P.Blank();
|
|
|
|
P.Expr(s.expr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// all semicolons required
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Blank();
|
2008-10-16 15:25:23 -06:00
|
|
|
if s.init != nil {
|
|
|
|
P.Stat(s.init);
|
|
|
|
}
|
2008-09-30 19:50:29 -06:00
|
|
|
P.semi = true;
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Blank();
|
2008-10-16 15:25:23 -06:00
|
|
|
if s.expr != nil {
|
|
|
|
P.Expr(s.expr);
|
|
|
|
}
|
|
|
|
if has_post {
|
|
|
|
P.semi = true;
|
|
|
|
P.Blank();
|
|
|
|
P.Stat(s.post);
|
|
|
|
P.semi = false
|
|
|
|
}
|
2008-09-25 12:50:34 -06:00
|
|
|
}
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Blank();
|
2008-09-25 12:50:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-15 18:06:28 -06:00
|
|
|
func (P *Printer) Declaration(d *Node.Decl, parenthesized bool);
|
2008-10-14 19:14:01 -06:00
|
|
|
|
|
|
|
func (P *Printer) Stat(s *Node.Stat) {
|
|
|
|
if s == nil { // TODO remove this check
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(0, "<nil stat>");
|
2008-10-14 19:14:01 -06:00
|
|
|
return;
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
2008-10-15 12:48:18 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
switch s.tok {
|
|
|
|
case 0: // TODO use a real token const
|
2008-10-16 11:16:59 -06:00
|
|
|
// expression statement
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Expr(s.expr);
|
|
|
|
P.semi = true;
|
2008-09-23 17:40:12 -06:00
|
|
|
|
2008-10-16 11:16:59 -06:00
|
|
|
case Scanner.COLON:
|
|
|
|
// label declaration
|
|
|
|
P.indent--;
|
|
|
|
P.Expr(s.expr);
|
|
|
|
P.Token(s.pos, s.tok);
|
|
|
|
P.indent++;
|
|
|
|
P.semi = false;
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
case Scanner.CONST, Scanner.TYPE, Scanner.VAR:
|
2008-10-16 11:16:59 -06:00
|
|
|
// declaration
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Declaration(s.decl, false);
|
2008-09-23 17:40:12 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
case Scanner.DEFINE, Scanner.ASSIGN, Scanner.ADD_ASSIGN,
|
|
|
|
Scanner.SUB_ASSIGN, Scanner.MUL_ASSIGN, Scanner.QUO_ASSIGN,
|
|
|
|
Scanner.REM_ASSIGN, Scanner.AND_ASSIGN, Scanner.OR_ASSIGN,
|
|
|
|
Scanner.XOR_ASSIGN, Scanner.SHL_ASSIGN, Scanner.SHR_ASSIGN:
|
2008-10-16 11:16:59 -06:00
|
|
|
// assignment
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Expr(s.lhs);
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Blank();
|
|
|
|
P.Token(s.pos, s.tok);
|
|
|
|
P.Blank();
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Expr(s.expr);
|
|
|
|
P.semi = true;
|
2008-09-23 17:40:12 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
case Scanner.INC, Scanner.DEC:
|
|
|
|
P.Expr(s.expr);
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Token(s.pos, s.tok);
|
2008-10-14 19:14:01 -06:00
|
|
|
P.semi = true;
|
2008-09-23 17:40:12 -06:00
|
|
|
|
2008-10-15 12:48:18 -06:00
|
|
|
case Scanner.LBRACE:
|
2008-10-16 11:16:59 -06:00
|
|
|
// block
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Block(s.block, true);
|
|
|
|
|
|
|
|
case Scanner.IF:
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(s.pos, "if");
|
2008-10-14 19:14:01 -06:00
|
|
|
P.ControlClause(s);
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Block(s.block, true);
|
|
|
|
if s.post != nil {
|
2008-10-16 11:16:59 -06:00
|
|
|
P.newl = 0;
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(0, " else ");
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Stat(s.post);
|
|
|
|
}
|
|
|
|
|
|
|
|
case Scanner.FOR:
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(s.pos, "for");
|
2008-10-15 12:48:18 -06:00
|
|
|
P.ControlClause(s);
|
|
|
|
P.Block(s.block, true);
|
|
|
|
|
|
|
|
case Scanner.SWITCH, Scanner.SELECT:
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Token(s.pos, s.tok);
|
2008-10-15 12:48:18 -06:00
|
|
|
P.ControlClause(s);
|
|
|
|
P.Block(s.block, false);
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
case Scanner.CASE, Scanner.DEFAULT:
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Token(s.pos, s.tok);
|
2008-10-14 19:14:01 -06:00
|
|
|
if s.expr != nil {
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Blank();
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Expr(s.expr);
|
|
|
|
}
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(0, ":");
|
2008-10-16 15:25:23 -06:00
|
|
|
P.indent++;
|
|
|
|
P.newl = 1;
|
2008-10-14 19:14:01 -06:00
|
|
|
P.StatementList(s.block);
|
2008-10-16 15:25:23 -06:00
|
|
|
P.indent--;
|
|
|
|
P.newl = 1;
|
2008-10-15 12:48:18 -06:00
|
|
|
|
2008-10-16 11:16:59 -06:00
|
|
|
case Scanner.GO, Scanner.RETURN, Scanner.FALLTHROUGH, Scanner.BREAK, Scanner.CONTINUE, Scanner.GOTO:
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Token(s.pos, s.tok);
|
2008-10-16 11:16:59 -06:00
|
|
|
if s.expr != nil {
|
|
|
|
P.Blank();
|
|
|
|
P.Expr(s.expr);
|
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
P.semi = true;
|
|
|
|
|
|
|
|
default:
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(s.pos, "<stat>");
|
2008-10-14 19:14:01 -06:00
|
|
|
P.semi = true;
|
2008-09-24 16:50:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Declarations
|
|
|
|
|
|
|
|
|
2008-10-15 18:06:28 -06:00
|
|
|
func (P *Printer) Declaration(d *Node.Decl, parenthesized bool) {
|
|
|
|
if d == nil { // TODO remove this check
|
|
|
|
P.String(0, "<nil decl>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !parenthesized {
|
2008-10-14 19:14:01 -06:00
|
|
|
if d.exported {
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(0, "export ");
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Token(d.pos, d.tok);
|
|
|
|
P.Blank();
|
2008-09-25 12:50:34 -06:00
|
|
|
}
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
if d.ident == nil {
|
2008-10-15 18:06:28 -06:00
|
|
|
P.OpenScope("(");
|
|
|
|
for i := 0; i < d.list.len(); i++ {
|
|
|
|
P.Declaration(d.list.at(i).(*Node.Decl), true);
|
2008-10-16 11:16:59 -06:00
|
|
|
P.semi, P.newl = true, 1;
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
2008-10-15 18:06:28 -06:00
|
|
|
P.CloseScope(")");
|
2008-09-25 12:50:34 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
} else {
|
2008-10-16 13:16:50 -06:00
|
|
|
if d.tok == Scanner.FUNC && d.typ.key != nil {
|
|
|
|
P.Parameters(0, d.typ.key.list);
|
|
|
|
P.Blank();
|
|
|
|
}
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Expr(d.ident);
|
2008-10-16 11:16:59 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
if d.typ != nil {
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Blank();
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Type(d.typ);
|
|
|
|
}
|
2008-10-16 11:16:59 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
if d.val != nil {
|
2008-10-15 18:06:28 -06:00
|
|
|
if d.tok == Scanner.IMPORT {
|
|
|
|
P.Blank();
|
|
|
|
} else {
|
|
|
|
P.String(0, " = ");
|
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Expr(d.val);
|
|
|
|
}
|
2008-10-16 11:16:59 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
if d.list != nil {
|
|
|
|
if d.tok != Scanner.FUNC {
|
|
|
|
panic("must be a func declaration");
|
|
|
|
}
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Blank();
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Block(d.list, true);
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
2008-10-16 13:16:50 -06:00
|
|
|
|
|
|
|
if d.tok != Scanner.TYPE {
|
|
|
|
P.semi = true;
|
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
2008-10-16 11:16:59 -06:00
|
|
|
|
|
|
|
P.newl = 1;
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2008-10-16 11:16:59 -06:00
|
|
|
// extra newline after a function declaration
|
|
|
|
if d.tok == Scanner.FUNC {
|
|
|
|
P.newl++;
|
|
|
|
}
|
|
|
|
|
2008-10-15 12:48:18 -06:00
|
|
|
// extra newline at the top level
|
2008-10-14 19:14:01 -06:00
|
|
|
if P.level == 0 {
|
2008-10-16 11:16:59 -06:00
|
|
|
P.newl++;
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
2008-09-25 16:14:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Program
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
func (P *Printer) Program(p *Node.Program) {
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(p.pos, "package ");
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Expr(p.ident);
|
2008-10-16 11:16:59 -06:00
|
|
|
P.newl = 2;
|
2008-10-14 19:14:01 -06:00
|
|
|
for i := 0; i < p.decls.len(); i++ {
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Declaration(p.decls.at(i), false);
|
2008-09-22 19:26:12 -06:00
|
|
|
}
|
2008-10-16 11:16:59 -06:00
|
|
|
P.newl = 1;
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(0, ""); // flush
|
2008-09-22 19:26:12 -06:00
|
|
|
}
|