Commit 81d7c518 authored by Robert Griesemer's avatar Robert Griesemer

First cut at a Go pretty printer:

- code scavenged from Go-in-Go front-end (will merge back)
- using "symbol-table" free parsing to build AST
- no printing yet

R=r
OCL=15504
CL=15504
parent a67258f3
# 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.
G=6g
L=6l
pretty: pretty.6
$(L) -o pretty pretty.6
test: all
pretty *.go
# pretty $(GOROOT)/test/fixedbugs/*.go # some files legally don't compile
pretty $(GOROOT)/test/sieve.go
pretty $(GOROOT)/src/pkg/*.go
pretty $(GOROOT)/src/lib/flag.go
pretty $(GOROOT)/src/lib/fmt.go
pretty $(GOROOT)/src/lib/rand.go
pretty $(GOROOT)/src/lib/math/*.go
pretty $(GOROOT)/src/lib/container/*.go
pretty $(GOROOT)/src/syscall/*.go
pretty base.go decls.go
pretty -token_chan base.go decls.go
echo "PASSED"
install: pretty
cp pretty $(HOME)/bin/pretty
clean:
rm -f pretty *.6 *~
pretty.6: parser.6 printer.6 platform.6 scanner.6
parser.6: ast.6 scanner.6 utils.6
scanner.6: utils.6 platform.6
%.6: %.go
$(G) $(F) $<
// 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.
package AST;
export type Expr interface {
pos() int;
print();
}
export type Stat interface {
pos() int;
print();
}
// ---------------------------------------------------------------------
// Concrete nodes
export type Ident struct {
pos_ int;
val_ string;
}
func (p *Ident) pos() int {
return p.pos_;
}
func (p *Ident) print() {
print("x"); // TODO fix this
}
// TODO: complete this
This diff is collapsed.
// 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.
package Platform
import Utils "utils"
// ----------------------------------------------------------------------------
// Environment
export var
GOARCH,
GOOS,
GOROOT,
USER string;
func GetEnv(key string) string {
n := len(key);
for i := 0; i < sys.envc(); i++ {
v := sys.envv(i);
if v[0 : n] == key {
return v[n + 1 : len(v)]; // +1: trim "="
}
}
return "";
}
func init() {
GOARCH = GetEnv("GOARCH");
GOOS = GetEnv("GOOS");
GOROOT = GetEnv("GOROOT");
USER = GetEnv("USER");
}
// ----------------------------------------------------------------------------
// I/O
export const (
MAGIC_obj_file = "@gri-go.7@v0"; // make it clear thar it cannot be a source file
src_file_ext = ".go";
obj_file_ext = ".7";
)
export func ReadObjectFile(filename string) (data string, ok bool) {
data, ok = sys.readfile(filename + obj_file_ext);
magic := MAGIC_obj_file; // TODO remove once len(constant) works
if ok && len(data) >= len(magic) && data[0 : len(magic)] == magic {
return data, ok;
}
return "", false;
}
export func ReadSourceFile(name string) (data string, ok bool) {
name = Utils.TrimExt(name, src_file_ext) + src_file_ext;
data, ok = sys.readfile(name);
return data, ok;
}
export func WriteObjectFile(name string, data string) bool {
name = Utils.TrimExt(Utils.BaseName(name), src_file_ext) + obj_file_ext;
return sys.writefile(name, data);
}
// 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.
package main
import Flag "flag"
import Platform "platform"
import Scanner "scanner"
import AST "ast" // should not be needed
import Parser "parser"
import Printer "printer"
var (
verbose = Flag.Bool("v", false, nil, "verbose mode");
sixg = Flag.Bool("6g", false, nil, "6g compatibility mode");
tokenchan = Flag.Bool("token_chan", false, nil, "use token channel for scanner-parser connection");
)
func main() {
Flag.Parse();
// process files
for i := 0; i < Flag.NArg(); i++ {
src_file := Flag.Arg(i);
src, ok := Platform.ReadSourceFile(src_file);
if !ok {
print("cannot open ", src_file, "\n");
return;
}
scanner := new(Scanner.Scanner);
scanner.Open(src_file, src);
var tstream *<-chan *Scanner.Token;
if tokenchan.BVal() {
tstream = scanner.TokenStream();
}
parser := new(Parser.Parser);
parser.Open(verbose.BVal(), scanner, tstream);
parser.ParseProgram();
}
}
// 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.
package Printer;
// TODO Fill in the code to print the AST
This diff is collapsed.
// 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.
package Utils
export func BaseName(s string) string {
// TODO this is not correct for non-ASCII strings!
i := len(s) - 1;
for i >= 0 && s[i] != '/' {
if s[i] > 128 {
panic("non-ASCII string");
}
i--;
}
return s[i + 1 : len(s)];
}
export func Contains(s, sub string, pos int) bool {
end := pos + len(sub);
return pos >= 0 && end <= len(s) && s[pos : end] == sub;
}
export func TrimExt(s, ext string) string {
i := len(s) - len(ext);
if i >= 0 && s[i : len(s)] == ext {
s = s[0 : i];
}
return s;
}
export func IntToString(x, base int) string {
x0 := x;
if x < 0 {
x = -x;
if x < 0 {
panic("smallest int not handled");
}
} else if x == 0 {
return "0";
}
// x > 0
hex := "0123456789ABCDEF";
var buf [32] byte;
i := len(buf);
for x > 0 {
i--;
buf[i] = hex[x % base];
x /= base;
}
if x0 < 0 {
i--;
buf[i] = '-';
}
return string(buf)[i : len(buf)];
}
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