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 (
|
2009-02-13 16:07:56 -07:00
|
|
|
"vector";
|
2009-01-14 16:19:34 -07:00
|
|
|
"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";
|
2009-03-11 13:52:11 -06:00
|
|
|
"scanner";
|
2009-01-14 16:19:34 -07:00
|
|
|
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;
|
|
|
|
Deps bool;
|
|
|
|
Columns 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;
|
2009-03-02 21:27:09 -07:00
|
|
|
src []byte;
|
2008-11-24 19:24:21 -07:00
|
|
|
columns bool;
|
2009-03-11 13:52:11 -06:00
|
|
|
errline int;
|
|
|
|
nerrors int;
|
2008-11-24 19:24:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-02 21:27:09 -07:00
|
|
|
func (h *errorHandler) Init(filename string, src []byte, columns bool) {
|
2008-11-24 19:24:21 -07:00
|
|
|
h.filename = filename;
|
|
|
|
h.src = src;
|
|
|
|
h.columns = columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
/*
|
2008-11-24 19:24:21 -07:00
|
|
|
// 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-03-02 21:27:09 -07:00
|
|
|
return line, utf8.RuneCount(src[lpos : pos]);
|
2008-11-24 19:24:21 -07:00
|
|
|
}
|
2009-03-11 13:52:11 -06:00
|
|
|
*/
|
2008-11-24 19:24:21 -07:00
|
|
|
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
func (h *errorHandler) ErrorMsg(loc scanner.Location, msg string) {
|
|
|
|
fmt.Printf("%s:%d:", h.filename, loc.Line);
|
|
|
|
if h.columns {
|
|
|
|
fmt.Printf("%d:", loc.Col);
|
2008-11-24 19:24:21 -07:00
|
|
|
}
|
2009-03-11 13:52:11 -06:00
|
|
|
fmt.Printf(" %s\n", msg);
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
h.errline = loc.Line;
|
2008-11-24 19:24:21 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
h.nerrors++;
|
2008-11-24 19:24:21 -07:00
|
|
|
if h.nerrors >= 10 {
|
2009-03-03 17:00:06 -07:00
|
|
|
sys.Exit(1);
|
2008-11-24 19:24:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
func (h *errorHandler) Error(loc scanner.Location, msg string) {
|
|
|
|
// only report errors that are on a new line
|
2008-11-24 19:24:21 -07:00
|
|
|
// in the hope to avoid most follow-up errors
|
2009-03-11 13:52:11 -06:00
|
|
|
if loc.Line != h.errline {
|
|
|
|
h.ErrorMsg(loc, msg);
|
2008-12-19 04:05:37 -07:00
|
|
|
}
|
2008-11-24 19:24:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
var scanner scanner.Scanner;
|
2009-03-02 21:27:09 -07:00
|
|
|
scanner.Init(src, &err, true);
|
2008-10-24 15:08:01 -06:00
|
|
|
|
|
|
|
var parser Parser.Parser;
|
2009-03-10 17:31:19 -06:00
|
|
|
parser.Init(&scanner, &err, flags.Verbose);
|
2008-10-26 22:32:30 -06:00
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
prog := parser.Parse(Parser.ParseEntirePackage);
|
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-03-11 13:51:10 -06:00
|
|
|
dir, err := os.Stat(name);
|
2009-02-06 12:10:25 -07:00
|
|
|
return err == nil;
|
|
|
|
}
|
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
/*
|
|
|
|
func printDep(localset map [string] bool, wset *vector.Vector, decl AST.Decl2) {
|
2009-02-06 12:10:25 -07:00
|
|
|
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-02-27 16:40:17 -07:00
|
|
|
*/
|
2008-10-26 22:32:30 -06:00
|
|
|
|
|
|
|
|
2009-02-13 16:07:56 -07:00
|
|
|
func addDeps(globalset map [string] bool, wset *vector.Vector, 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-02-27 16:40:17 -07:00
|
|
|
nimports := len(prog.Decls);
|
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-02-27 16:40:17 -07:00
|
|
|
decl := prog.Decls[i];
|
|
|
|
panic();
|
|
|
|
/*
|
2009-03-11 13:52:11 -06:00
|
|
|
assert(decl.Tok == scanner.IMPORT);
|
2009-02-06 12:10:25 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2009-02-27 16:40:17 -07:00
|
|
|
*/
|
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-03-05 18:15:36 -07:00
|
|
|
panic("dependency printing currently disabled");
|
2009-01-06 16:01:04 -07:00
|
|
|
globalset := make(map [string] bool);
|
2009-02-13 16:07:56 -07:00
|
|
|
wset := vector.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
|
|
|
}
|