2008-10-24 15:08:01 -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.
|
|
|
|
|
|
|
|
package Compilation
|
|
|
|
|
2009-01-14 16:19:34 -07:00
|
|
|
import (
|
|
|
|
"array";
|
|
|
|
"utf8";
|
2009-02-06 12:10:25 -07:00
|
|
|
"fmt";
|
|
|
|
"os";
|
2009-02-06 16:26:30 -07:00
|
|
|
Utils "utils";
|
2009-01-14 16:19:34 -07:00
|
|
|
Platform "platform";
|
|
|
|
Scanner "scanner";
|
|
|
|
Parser "parser";
|
|
|
|
AST "ast";
|
|
|
|
TypeChecker "typechecker";
|
|
|
|
)
|
2008-10-24 15:08:01 -06:00
|
|
|
|
|
|
|
|
2008-10-26 22:32:30 -06:00
|
|
|
func assert(b bool) {
|
|
|
|
if !b {
|
|
|
|
panic("assertion failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-24 15:08:01 -06:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Flags struct {
|
2009-01-16 16:31:34 -07:00
|
|
|
Verbose bool;
|
|
|
|
Sixg bool;
|
|
|
|
Deps bool;
|
|
|
|
Columns bool;
|
|
|
|
Testmode bool;
|
2008-10-24 15:08:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-15 18:16:41 -07:00
|
|
|
type errorHandler struct {
|
2008-11-24 19:24:21 -07:00
|
|
|
filename string;
|
|
|
|
src string;
|
|
|
|
nerrors int;
|
|
|
|
nwarnings int;
|
|
|
|
errpos int;
|
|
|
|
columns bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-15 18:16:41 -07:00
|
|
|
func (h *errorHandler) Init(filename, src string, columns bool) {
|
2008-11-24 19:24:21 -07:00
|
|
|
h.filename = filename;
|
|
|
|
h.src = src;
|
|
|
|
h.nerrors = 0;
|
|
|
|
h.nwarnings = 0;
|
|
|
|
h.errpos = 0;
|
|
|
|
h.columns = columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Compute (line, column) information for a given source position.
|
2009-01-15 18:16:41 -07:00
|
|
|
func (h *errorHandler) LineCol(pos int) (line, col int) {
|
2008-11-24 19:24:21 -07:00
|
|
|
line = 1;
|
|
|
|
lpos := 0;
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-11-24 19:24:21 -07:00
|
|
|
src := h.src;
|
|
|
|
if pos > len(src) {
|
|
|
|
pos = len(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < pos; i++ {
|
|
|
|
if src[i] == '\n' {
|
|
|
|
line++;
|
|
|
|
lpos = i;
|
|
|
|
}
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-01-14 16:19:34 -07:00
|
|
|
return line, utf8.RuneCountInString(src, lpos, pos - lpos);
|
2008-11-24 19:24:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-15 18:16:41 -07:00
|
|
|
func (h *errorHandler) ErrorMsg(pos int, msg string) {
|
2008-11-24 19:24:21 -07:00
|
|
|
print(h.filename, ":");
|
|
|
|
if pos >= 0 {
|
|
|
|
// print position
|
|
|
|
line, col := h.LineCol(pos);
|
|
|
|
print(line, ":");
|
|
|
|
if h.columns {
|
|
|
|
print(col, ":");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print(" ", msg, "\n");
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-11-24 19:24:21 -07:00
|
|
|
h.nerrors++;
|
|
|
|
h.errpos = pos;
|
|
|
|
|
|
|
|
if h.nerrors >= 10 {
|
2009-01-15 15:19:35 -07:00
|
|
|
// TODO enable when done with name convention
|
2009-01-16 15:58:14 -07:00
|
|
|
//sys.Exit(1);
|
2008-11-24 19:24:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-15 18:16:41 -07:00
|
|
|
func (h *errorHandler) Error(pos int, msg string) {
|
2008-11-24 19:24:21 -07:00
|
|
|
// only report errors that are sufficiently far away from the previous error
|
|
|
|
// in the hope to avoid most follow-up errors
|
|
|
|
const errdist = 20;
|
|
|
|
delta := pos - h.errpos; // may be negative!
|
|
|
|
if delta < 0 {
|
|
|
|
delta = -delta;
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-11-24 19:24:21 -07:00
|
|
|
if delta > errdist || h.nerrors == 0 /* always report first error */ {
|
|
|
|
h.ErrorMsg(pos, msg);
|
2008-12-19 04:05:37 -07:00
|
|
|
}
|
2008-11-24 19:24:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-15 18:16:41 -07:00
|
|
|
func (h *errorHandler) Warning(pos int, msg string) {
|
2008-11-24 19:24:21 -07:00
|
|
|
panic("UNIMPLEMENTED");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func Compile(src_file string, flags *Flags) (*AST.Program, int) {
|
2008-10-26 22:32:30 -06:00
|
|
|
src, ok := Platform.ReadSourceFile(src_file);
|
|
|
|
if !ok {
|
|
|
|
print("cannot open ", src_file, "\n");
|
|
|
|
return nil, 1;
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-01-15 18:16:41 -07:00
|
|
|
var err errorHandler;
|
2009-01-16 16:31:34 -07:00
|
|
|
err.Init(src_file, src, flags.Columns);
|
2008-10-24 15:08:01 -06:00
|
|
|
|
|
|
|
var scanner Scanner.Scanner;
|
2009-01-16 16:31:34 -07:00
|
|
|
scanner.Init(&err, src, true, flags.Testmode);
|
2008-10-24 15:08:01 -06:00
|
|
|
|
|
|
|
var parser Parser.Parser;
|
2009-02-06 12:10:25 -07:00
|
|
|
parser.Open(flags.Verbose, flags.Sixg, flags.Deps, &scanner);
|
2008-10-26 22:32:30 -06:00
|
|
|
|
|
|
|
prog := parser.ParseProgram();
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-12-16 19:02:22 -07:00
|
|
|
if err.nerrors == 0 {
|
2009-01-06 15:54:18 -07:00
|
|
|
TypeChecker.CheckProgram(&err, prog);
|
2008-12-16 19:02:22 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-11-24 19:24:21 -07:00
|
|
|
return prog, err.nerrors;
|
2008-10-26 22:32:30 -06:00
|
|
|
}
|
|
|
|
|
2008-10-24 15:08:01 -06:00
|
|
|
|
2009-01-15 18:16:41 -07:00
|
|
|
func fileExists(name string) bool {
|
2009-02-06 12:10:25 -07:00
|
|
|
fd, err := os.Open(name, os.O_RDONLY, 0);
|
|
|
|
defer fd.Close();
|
|
|
|
return err == nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func printDep(localset map [string] bool, wset *array.Array, decl *AST.Decl) {
|
|
|
|
src := decl.Val.(*AST.BasicLit).Val;
|
|
|
|
src = src[1 : len(src) - 1]; // strip "'s
|
|
|
|
|
|
|
|
// ignore files when they are seen a 2nd time
|
|
|
|
dummy, found := localset[src];
|
|
|
|
if !found {
|
|
|
|
localset[src] = true;
|
|
|
|
if fileExists(src + ".go") {
|
|
|
|
wset.Push(src);
|
|
|
|
fmt.Printf(" %s.6", src);
|
|
|
|
} else if
|
|
|
|
fileExists(Platform.GOROOT + "/pkg/" + src + ".6") ||
|
|
|
|
fileExists(Platform.GOROOT + "/pkg/" + src + ".a") {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// TODO should collect these and print later
|
|
|
|
//print("missing file: ", src, "\n");
|
|
|
|
}
|
2008-10-26 22:32:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-15 18:16:41 -07:00
|
|
|
func addDeps(globalset map [string] bool, wset *array.Array, src_file string, flags *Flags) {
|
2008-10-26 22:32:30 -06:00
|
|
|
dummy, found := globalset[src_file];
|
|
|
|
if !found {
|
|
|
|
globalset[src_file] = true;
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-10-26 22:32:30 -06:00
|
|
|
prog, nerrors := Compile(src_file, flags);
|
|
|
|
if nerrors > 0 {
|
|
|
|
return;
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-01-15 18:16:41 -07:00
|
|
|
nimports := prog.Decls.Len();
|
2008-10-26 22:32:30 -06:00
|
|
|
if nimports > 0 {
|
2009-02-06 12:10:25 -07:00
|
|
|
fmt.Printf("%s.6:\t", src_file);
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-01-06 16:01:04 -07:00
|
|
|
localset := make(map [string] bool);
|
2008-10-26 22:32:30 -06:00
|
|
|
for i := 0; i < nimports; i++ {
|
2009-01-15 18:16:41 -07:00
|
|
|
decl := prog.Decls.At(i).(*AST.Decl);
|
2009-02-06 12:10:25 -07:00
|
|
|
assert(decl.Tok == Scanner.IMPORT);
|
|
|
|
if decl.List == nil {
|
|
|
|
printDep(localset, wset, decl);
|
|
|
|
} else {
|
|
|
|
for j := 0; j < decl.List.Len(); j++ {
|
|
|
|
printDep(localset, wset, decl.List.At(j).(*AST.Decl));
|
2008-10-26 22:32:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print("\n\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func ComputeDeps(src_file string, flags *Flags) {
|
2009-01-06 16:01:04 -07:00
|
|
|
globalset := make(map [string] bool);
|
2008-11-19 17:49:50 -07:00
|
|
|
wset := array.New(0);
|
2009-02-06 16:26:30 -07:00
|
|
|
wset.Push(Utils.TrimExt(src_file, ".go"));
|
2008-11-19 17:49:50 -07:00
|
|
|
for wset.Len() > 0 {
|
2009-01-15 18:16:41 -07:00
|
|
|
addDeps(globalset, wset, wset.Pop().(string), flags);
|
2008-10-26 22:32:30 -06:00
|
|
|
}
|
2008-10-24 15:08:01 -06:00
|
|
|
}
|