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

- use stringtorune library function for faster rune scanning

- converted 2 right-recursive parsing functions into iterative versions
- renamed node.go -> ast.go (clearer)

R=r
OCL=17496
CL=17498
parent ef40d943
...@@ -19,11 +19,11 @@ clean: ...@@ -19,11 +19,11 @@ clean:
pretty.6: parser.6 printer.6 platform.6 scanner.6 pretty.6: parser.6 printer.6 platform.6 scanner.6
printer.6: node.6 scanner.6 printer.6: ast.6 scanner.6
parser.6: scanner.6 utils.6 printer.6 node.6 parser.6: scanner.6 utils.6 printer.6 ast.6
node.6: scanner.6 ast.6: scanner.6
scanner.6: utils.6 platform.6 scanner.6: utils.6 platform.6
......
...@@ -9,7 +9,7 @@ GO = /home/iant/go/bin/gccgo ...@@ -9,7 +9,7 @@ GO = /home/iant/go/bin/gccgo
LDFLAGS = -Wl,-R,/home/iant/go/lib LDFLAGS = -Wl,-R,/home/iant/go/lib
PRETTY_OBJS = \ PRETTY_OBJS = \
node.o \ ast.o \
pretty.o \ pretty.o \
parser.o \ parser.o \
platform.o \ platform.o \
...@@ -33,11 +33,11 @@ clean: ...@@ -33,11 +33,11 @@ clean:
pretty.o: parser.o printer.o platform.o scanner.o flag.o pretty.o: parser.o printer.o platform.o scanner.o flag.o
parser.o: node.o scanner.o utils.o printer.o parser.o: ast.o scanner.o utils.o printer.o
scanner.o: utils.o platform.o scanner.o: utils.o platform.o
node.o: scanner.o ast.o: scanner.o
flag.o: fmt.o flag.o: fmt.o
$(GO) -O2 -c -g $(GOROOT)/src/lib/flag.go $(GO) -O2 -c -g $(GOROOT)/src/lib/flag.go
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package Node package AST
import Scanner "scanner" import Scanner "scanner"
......
This diff is collapsed.
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
package Printer package Printer
import Scanner "scanner" import Scanner "scanner"
import Node "node" import AST "ast"
export type Printer struct { export type Printer struct {
...@@ -16,7 +16,7 @@ export type Printer struct { ...@@ -16,7 +16,7 @@ export type Printer struct {
newl int; // pending "\n"'s newl int; // pending "\n"'s
// comments // comments
clist *Node.List; clist *AST.List;
cindex int; cindex int;
cpos int; cpos int;
} }
...@@ -30,7 +30,7 @@ func (P *Printer) String(pos int, s string) { ...@@ -30,7 +30,7 @@ func (P *Printer) String(pos int, s string) {
/* /*
for pos > P.cpos { for pos > P.cpos {
// we have a comment // we have a comment
c := P.clist.at(P.cindex).(*Node.Comment); c := P.clist.at(P.cindex).(*AST.Comment);
if c.text[1] == '/' { if c.text[1] == '/' {
print(" " + c.text); print(" " + c.text);
if P.newl <= 0 { if P.newl <= 0 {
...@@ -41,7 +41,7 @@ func (P *Printer) String(pos int, s string) { ...@@ -41,7 +41,7 @@ func (P *Printer) String(pos int, s string) {
} }
P.cindex++; P.cindex++;
if P.cindex < P.clist.len() { if P.cindex < P.clist.len() {
P.cpos = P.clist.at(P.cindex).(*Node.Comment).pos; P.cpos = P.clist.at(P.cindex).(*AST.Comment).pos;
} else { } else {
P.cpos = 1000000000; // infinite P.cpos = 1000000000; // infinite
} }
...@@ -100,14 +100,14 @@ func (P *Printer) Error(pos int, tok int, msg string) { ...@@ -100,14 +100,14 @@ func (P *Printer) Error(pos int, tok int, msg string) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Types // Types
func (P *Printer) Type(t *Node.Type) func (P *Printer) Type(t *AST.Type)
func (P *Printer) Expr(x *Node.Expr) func (P *Printer) Expr(x *AST.Expr)
func (P *Printer) Parameters(pos int, list *Node.List) { func (P *Printer) Parameters(pos int, list *AST.List) {
P.String(pos, "("); P.String(pos, "(");
var prev int; var prev int;
for i, n := 0, list.len(); i < n; i++ { for i, n := 0, list.len(); i < n; i++ {
x := list.at(i).(*Node.Expr); x := list.at(i).(*AST.Expr);
if i > 0 { if i > 0 {
if prev == x.tok || prev == Scanner.TYPE { if prev == x.tok || prev == Scanner.TYPE {
P.String(0, ", "); P.String(0, ", ");
...@@ -122,11 +122,11 @@ func (P *Printer) Parameters(pos int, list *Node.List) { ...@@ -122,11 +122,11 @@ func (P *Printer) Parameters(pos int, list *Node.List) {
} }
func (P *Printer) Fields(list *Node.List) { func (P *Printer) Fields(list *AST.List) {
P.OpenScope(" {"); P.OpenScope(" {");
var prev int; var prev int;
for i, n := 0, list.len(); i < n; i++ { for i, n := 0, list.len(); i < n; i++ {
x := list.at(i).(*Node.Expr); x := list.at(i).(*AST.Expr);
if i > 0 { if i > 0 {
if prev == Scanner.TYPE { if prev == Scanner.TYPE {
P.String(0, ";"); P.String(0, ";");
...@@ -145,7 +145,7 @@ func (P *Printer) Fields(list *Node.List) { ...@@ -145,7 +145,7 @@ func (P *Printer) Fields(list *Node.List) {
} }
func (P *Printer) Type(t *Node.Type) { func (P *Printer) Type(t *AST.Type) {
switch t.tok { switch t.tok {
case Scanner.IDENT: case Scanner.IDENT:
P.Expr(t.expr); P.Expr(t.expr);
...@@ -174,9 +174,9 @@ func (P *Printer) Type(t *Node.Type) { ...@@ -174,9 +174,9 @@ func (P *Printer) Type(t *Node.Type) {
case Scanner.CHAN: case Scanner.CHAN:
var m string; var m string;
switch t.mode { switch t.mode {
case Node.FULL: m = "chan "; case AST.FULL: m = "chan ";
case Node.RECV: m = "<-chan "; case AST.RECV: m = "<-chan ";
case Node.SEND: m = "chan <- "; case AST.SEND: m = "chan <- ";
} }
P.String(t.pos, m); P.String(t.pos, m);
P.Type(t.elt); P.Type(t.elt);
...@@ -201,9 +201,9 @@ func (P *Printer) Type(t *Node.Type) { ...@@ -201,9 +201,9 @@ func (P *Printer) Type(t *Node.Type) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Expressions // Expressions
func (P *Printer) Block(list *Node.List, indent bool); func (P *Printer) Block(list *AST.List, indent bool);
func (P *Printer) Expr1(x *Node.Expr, prec1 int) { func (P *Printer) Expr1(x *AST.Expr, prec1 int) {
if x == nil { if x == nil {
return; // empty expression list return; // empty expression list
} }
...@@ -288,7 +288,7 @@ func (P *Printer) Expr1(x *Node.Expr, prec1 int) { ...@@ -288,7 +288,7 @@ func (P *Printer) Expr1(x *Node.Expr, prec1 int) {
} }
func (P *Printer) Expr(x *Node.Expr) { func (P *Printer) Expr(x *AST.Expr) {
P.Expr1(x, Scanner.LowestPrec); P.Expr1(x, Scanner.LowestPrec);
} }
...@@ -296,17 +296,17 @@ func (P *Printer) Expr(x *Node.Expr) { ...@@ -296,17 +296,17 @@ func (P *Printer) Expr(x *Node.Expr) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Statements // Statements
func (P *Printer) Stat(s *Node.Stat) func (P *Printer) Stat(s *AST.Stat)
func (P *Printer) StatementList(list *Node.List) { func (P *Printer) StatementList(list *AST.List) {
for i, n := 0, list.len(); i < n; i++ { for i, n := 0, list.len(); i < n; i++ {
P.Stat(list.at(i).(*Node.Stat)); P.Stat(list.at(i).(*AST.Stat));
P.newl = 1; P.newl = 1;
} }
} }
func (P *Printer) Block(list *Node.List, indent bool) { func (P *Printer) Block(list *AST.List, indent bool) {
P.OpenScope("{"); P.OpenScope("{");
if !indent { if !indent {
P.indent--; P.indent--;
...@@ -319,7 +319,7 @@ func (P *Printer) Block(list *Node.List, indent bool) { ...@@ -319,7 +319,7 @@ func (P *Printer) Block(list *Node.List, indent bool) {
} }
func (P *Printer) ControlClause(s *Node.Stat) { func (P *Printer) ControlClause(s *AST.Stat) {
has_post := s.tok == Scanner.FOR && s.post != nil; // post also used by "if" has_post := s.tok == Scanner.FOR && s.post != nil; // post also used by "if"
if s.init == nil && !has_post { if s.init == nil && !has_post {
// no semicolons required // no semicolons required
...@@ -351,9 +351,9 @@ func (P *Printer) ControlClause(s *Node.Stat) { ...@@ -351,9 +351,9 @@ func (P *Printer) ControlClause(s *Node.Stat) {
} }
func (P *Printer) Declaration(d *Node.Decl, parenthesized bool); func (P *Printer) Declaration(d *AST.Decl, parenthesized bool);
func (P *Printer) Stat(s *Node.Stat) { func (P *Printer) Stat(s *AST.Stat) {
switch s.tok { switch s.tok {
case Scanner.EXPRSTAT: case Scanner.EXPRSTAT:
// expression statement // expression statement
...@@ -432,7 +432,7 @@ func (P *Printer) Stat(s *Node.Stat) { ...@@ -432,7 +432,7 @@ func (P *Printer) Stat(s *Node.Stat) {
// Declarations // Declarations
func (P *Printer) Declaration(d *Node.Decl, parenthesized bool) { func (P *Printer) Declaration(d *AST.Decl, parenthesized bool) {
if !parenthesized { if !parenthesized {
if d.exported { if d.exported {
P.String(0, "export "); P.String(0, "export ");
...@@ -444,7 +444,7 @@ func (P *Printer) Declaration(d *Node.Decl, parenthesized bool) { ...@@ -444,7 +444,7 @@ func (P *Printer) Declaration(d *Node.Decl, parenthesized bool) {
if d.tok != Scanner.FUNC && d.list != nil { if d.tok != Scanner.FUNC && d.list != nil {
P.OpenScope("("); P.OpenScope("(");
for i := 0; i < d.list.len(); i++ { for i := 0; i < d.list.len(); i++ {
P.Declaration(d.list.at(i).(*Node.Decl), true); P.Declaration(d.list.at(i).(*AST.Decl), true);
P.semi, P.newl = true, 1; P.semi, P.newl = true, 1;
} }
P.CloseScope(")"); P.CloseScope(")");
...@@ -501,12 +501,12 @@ func (P *Printer) Declaration(d *Node.Decl, parenthesized bool) { ...@@ -501,12 +501,12 @@ func (P *Printer) Declaration(d *Node.Decl, parenthesized bool) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Program // Program
func (P *Printer) Program(p *Node.Program) { func (P *Printer) Program(p *AST.Program) {
// TODO should initialize all fields? // TODO should initialize all fields?
P.clist = p.comments; P.clist = p.comments;
P.cindex = 0; P.cindex = 0;
if p.comments.len() > 0 { if p.comments.len() > 0 {
P.cpos = p.comments.at(0).(*Node.Comment).pos; P.cpos = p.comments.at(0).(*AST.Comment).pos;
} else { } else {
P.cpos = 1000000000; // infinite P.cpos = 1000000000; // infinite
} }
......
...@@ -295,6 +295,21 @@ export type Scanner struct { ...@@ -295,6 +295,21 @@ export type Scanner struct {
// Read the next Unicode char into S.ch. // Read the next Unicode char into S.ch.
// S.ch < 0 means end-of-file. // S.ch < 0 means end-of-file.
func (S *Scanner) Next() { func (S *Scanner) Next() {
if S.pos < len(S.src) {
// assume ascii
r, w := int(S.src[S.pos]), 1;
if r > 0x80 {
// wasn't ascii
r, w = sys.stringtorune(S.src, S.pos);
}
S.ch = r;
S.chpos = S.pos;
S.pos += w;
} else {
S.ch = -1; // eof
S.chpos = len(S.src);
}
/*
const ( const (
Bit1 = 7; Bit1 = 7;
Bitx = 6; Bitx = 6;
...@@ -389,6 +404,7 @@ bad: ...@@ -389,6 +404,7 @@ bad:
S.chpos = S.pos; S.chpos = S.pos;
S.pos += 1; S.pos += 1;
return; return;
*/
} }
......
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