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-10-20 17:44:03 -06:00
|
|
|
import Strings "strings"
|
2008-09-22 19:26:12 -06:00
|
|
|
import Scanner "scanner"
|
2008-10-20 16:03:40 -06:00
|
|
|
import AST "ast"
|
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-10-20 17:44:03 -06:00
|
|
|
pos int; // actual output position
|
|
|
|
|
2008-10-17 00:30:42 -06:00
|
|
|
// formatting control
|
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-10-17 00:30:42 -06:00
|
|
|
|
|
|
|
// comments
|
2008-10-20 16:03:40 -06:00
|
|
|
clist *AST.List;
|
2008-10-17 00:30:42 -06:00
|
|
|
cindex int;
|
|
|
|
cpos int;
|
2008-09-22 19:26:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 17:44:03 -06:00
|
|
|
// Bottleneck interface - all output goes through here.
|
|
|
|
func (P *Printer) print(s string) {
|
|
|
|
print(s);
|
|
|
|
// TODO do we need the code below?
|
|
|
|
// P.pos += Strings.utflen(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-20 17:44:03 -06:00
|
|
|
|
2008-10-17 00:30:42 -06:00
|
|
|
/*
|
|
|
|
for pos > P.cpos {
|
|
|
|
// we have a comment
|
2008-10-20 16:03:40 -06:00
|
|
|
c := P.clist.at(P.cindex).(*AST.Comment);
|
2008-10-17 00:30:42 -06:00
|
|
|
if c.text[1] == '/' {
|
|
|
|
print(" " + c.text);
|
|
|
|
if P.newl <= 0 {
|
|
|
|
P.newl = 1; // line comments must have a newline
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print(c.text);
|
|
|
|
}
|
|
|
|
P.cindex++;
|
|
|
|
if P.cindex < P.clist.len() {
|
2008-10-20 16:03:40 -06:00
|
|
|
P.cpos = P.clist.at(P.cindex).(*AST.Comment).pos;
|
2008-10-17 00:30:42 -06:00
|
|
|
} else {
|
|
|
|
P.cpos = 1000000000; // infinite
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2008-10-20 17:44:03 -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 20:24:33 -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-10-17 17:19:31 -06:00
|
|
|
func (P *Printer) Error(pos int, tok int, msg string) {
|
|
|
|
P.String(0, "<");
|
|
|
|
P.Token(pos, tok);
|
|
|
|
P.String(0, " " + msg + ">");
|
|
|
|
}
|
|
|
|
|
2008-09-22 19:26:12 -06:00
|
|
|
|
2008-09-23 19:34:17 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Types
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Printer) Type(t *AST.Type)
|
|
|
|
func (P *Printer) Expr(x *AST.Expr)
|
2008-09-24 16:50:28 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Printer) Parameters(pos int, list *AST.List) {
|
2008-10-16 13:16:50 -06:00
|
|
|
P.String(pos, "(");
|
2008-10-15 18:06:28 -06:00
|
|
|
var prev int;
|
|
|
|
for i, n := 0, list.len(); i < n; i++ {
|
2008-10-20 16:03:40 -06:00
|
|
|
x := list.at(i).(*AST.Expr);
|
2008-10-15 18:06:28 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Printer) Fields(list *AST.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++ {
|
2008-10-20 16:03:40 -06:00
|
|
|
x := list.at(i).(*AST.Expr);
|
2008-10-15 18:06:28 -06:00
|
|
|
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-20 16:03:40 -06:00
|
|
|
func (P *Printer) Type(t *AST.Type) {
|
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-16 20:24:33 -06:00
|
|
|
P.String(t.pos, "map [");
|
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-20 16:03:40 -06:00
|
|
|
case AST.FULL: m = "chan ";
|
|
|
|
case AST.RECV: m = "<-chan ";
|
|
|
|
case AST.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:
|
2008-10-17 17:19:31 -06:00
|
|
|
P.Error(t.pos, t.tok, "type");
|
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-20 16:03:40 -06:00
|
|
|
func (P *Printer) Block(list *AST.List, indent bool);
|
2008-10-17 17:19:31 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Printer) Expr1(x *AST.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
|
|
|
|
2008-10-17 17:19:31 -06:00
|
|
|
case Scanner.FUNC:
|
|
|
|
// function literal
|
|
|
|
P.String(x.pos, "func");
|
|
|
|
P.Type(x.t);
|
|
|
|
P.Block(x.block, true);
|
|
|
|
P.newl = 0;
|
|
|
|
|
2008-10-15 12:48:18 -06:00
|
|
|
case Scanner.COMMA:
|
2008-10-16 15:25:23 -06:00
|
|
|
// list
|
2008-10-20 17:44:03 -06:00
|
|
|
// (don't use binary expression printing because of different spacing)
|
|
|
|
P.Expr1(x.x, Scanner.LowestPrec);
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(x.pos, ", ");
|
2008-10-20 17:44:03 -06:00
|
|
|
P.Expr1(x.y, Scanner.LowestPrec);
|
2008-10-15 12:48:18 -06:00
|
|
|
|
|
|
|
case Scanner.PERIOD:
|
2008-10-16 15:25:23 -06:00
|
|
|
// selector or type guard
|
2008-10-20 11:01:34 -06:00
|
|
|
P.Expr1(x.x, Scanner.HighestPrec);
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(x.pos, ".");
|
2008-10-16 15:25:23 -06:00
|
|
|
if x.y != nil {
|
2008-10-20 11:01:34 -06:00
|
|
|
P.Expr1(x.y, Scanner.HighestPrec);
|
2008-10-16 15:25:23 -06:00
|
|
|
} 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-20 11:01:34 -06:00
|
|
|
P.Expr1(x.x, Scanner.HighestPrec);
|
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-20 11:01:34 -06:00
|
|
|
P.Expr1(x.x, Scanner.HighestPrec);
|
2008-10-15 18:06:28 -06:00
|
|
|
P.String(x.pos, "(");
|
2008-10-20 17:44:03 -06:00
|
|
|
P.Expr1(x.y, Scanner.LowestPrec);
|
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-17 00:30:42 -06:00
|
|
|
P.Type(x.t);
|
2008-10-16 13:16:50 -06:00
|
|
|
P.String(x.pos, "{");
|
2008-10-20 17:44:03 -06:00
|
|
|
P.Expr1(x.y, Scanner.LowestPrec);
|
2008-10-16 13:16:50 -06:00
|
|
|
P.String(0, "}");
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2008-09-24 16:50:28 -06:00
|
|
|
default:
|
2008-10-20 17:44:03 -06:00
|
|
|
// unary and binary expressions including ":" for pairs
|
|
|
|
prec := Scanner.UnaryPrec;
|
|
|
|
if x.x != nil {
|
|
|
|
prec = Scanner.Precedence(x.tok);
|
|
|
|
}
|
|
|
|
if prec < prec1 {
|
|
|
|
P.String(0, "(");
|
|
|
|
}
|
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);
|
2008-10-14 19:14:01 -06:00
|
|
|
} else {
|
2008-10-20 17:44:03 -06:00
|
|
|
// binary expression
|
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-20 17:44:03 -06:00
|
|
|
}
|
|
|
|
P.Expr1(x.y, prec);
|
|
|
|
if prec < prec1 {
|
|
|
|
P.String(0, ")");
|
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-20 16:03:40 -06:00
|
|
|
func (P *Printer) Expr(x *AST.Expr) {
|
2008-10-20 11:01:34 -06:00
|
|
|
P.Expr1(x, Scanner.LowestPrec);
|
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
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Printer) Stat(s *AST.Stat)
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Printer) StatementList(list *AST.List) {
|
2008-10-14 19:14:01 -06:00
|
|
|
for i, n := 0, list.len(); i < n; i++ {
|
2008-10-20 16:03:40 -06:00
|
|
|
P.Stat(list.at(i).(*AST.Stat));
|
2008-10-16 11:16:59 -06:00
|
|
|
P.newl = 1;
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Printer) Block(list *AST.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("}");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Printer) ControlClause(s *AST.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);
|
|
|
|
}
|
2008-10-16 20:24:33 -06:00
|
|
|
if s.tok == Scanner.FOR {
|
2008-10-16 15:25:23 -06:00
|
|
|
P.semi = true;
|
2008-10-16 20:24:33 -06:00
|
|
|
if has_post {
|
|
|
|
P.Blank();
|
|
|
|
P.Stat(s.post);
|
|
|
|
P.semi = false
|
|
|
|
}
|
2008-10-16 15:25:23 -06:00
|
|
|
}
|
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-20 16:03:40 -06:00
|
|
|
func (P *Printer) Declaration(d *AST.Decl, parenthesized bool);
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Printer) Stat(s *AST.Stat) {
|
2008-10-14 19:14:01 -06:00
|
|
|
switch s.tok {
|
2008-10-17 17:19:31 -06:00
|
|
|
case Scanner.EXPRSTAT:
|
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.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-17 17:19:31 -06:00
|
|
|
P.Error(s.pos, s.tok, "stat");
|
2008-09-24 16:50:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Declarations
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Printer) Declaration(d *AST.Decl, parenthesized bool) {
|
2008-10-15 18:06:28 -06:00
|
|
|
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-16 20:24:33 -06:00
|
|
|
if d.tok != Scanner.FUNC && d.list != nil {
|
2008-10-15 18:06:28 -06:00
|
|
|
P.OpenScope("(");
|
|
|
|
for i := 0; i < d.list.len(); i++ {
|
2008-10-20 16:03:40 -06:00
|
|
|
P.Declaration(d.list.at(i).(*AST.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-20 16:03:40 -06:00
|
|
|
func (P *Printer) Program(p *AST.Program) {
|
2008-10-17 00:30:42 -06:00
|
|
|
// TODO should initialize all fields?
|
|
|
|
P.clist = p.comments;
|
|
|
|
P.cindex = 0;
|
|
|
|
if p.comments.len() > 0 {
|
2008-10-20 16:03:40 -06:00
|
|
|
P.cpos = p.comments.at(0).(*AST.Comment).pos;
|
2008-10-17 00:30:42 -06:00
|
|
|
} else {
|
|
|
|
P.cpos = 1000000000; // infinite
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|