blob: e4720c5c82aa50495bb0bfe269ca5af9816a2199 [file] [log] [blame]
Robert Griesemer63ed2b72008-10-24 14:08:01 -07001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package Compilation
6
Robert Griesemereba73552008-10-26 21:32:30 -07007import OS "os"
8import Platform "platform"
Robert Griesemer63ed2b72008-10-24 14:08:01 -07009import Scanner "scanner"
10import Parser "parser"
11import AST "ast"
12
13
Robert Griesemereba73552008-10-26 21:32:30 -070014func assert(b bool) {
15 if !b {
16 panic("assertion failed");
17 }
18}
19
Robert Griesemer63ed2b72008-10-24 14:08:01 -070020
21export type Flags struct {
22 verbose bool;
23 sixg bool;
24 deps bool;
25 columns bool;
26 testmode bool;
27 tokenchan bool;
28}
29
30
Robert Griesemereba73552008-10-26 21:32:30 -070031export func Compile(src_file string, flags *Flags) (*AST.Program, int) {
32 src, ok := Platform.ReadSourceFile(src_file);
33 if !ok {
34 print("cannot open ", src_file, "\n");
35 return nil, 1;
36 }
Robert Griesemer63ed2b72008-10-24 14:08:01 -070037
Robert Griesemer63ed2b72008-10-24 14:08:01 -070038 var scanner Scanner.Scanner;
39 scanner.Open(src_file, src, flags.columns, flags.testmode);
40
41 var tstream *<-chan *Scanner.Token;
42 if flags.tokenchan {
43 tstream = scanner.TokenStream();
44 }
45
46 var parser Parser.Parser;
Robert Griesemereba73552008-10-26 21:32:30 -070047 parser.Open(flags.verbose, flags.sixg, flags.deps, &scanner, tstream);
Robert Griesemer63ed2b72008-10-24 14:08:01 -070048
Robert Griesemereba73552008-10-26 21:32:30 -070049 prog := parser.ParseProgram();
50 return prog, scanner.nerrors;
51}
52
53
54func FileExists(name string) bool {
55 fd, err := OS.Open(name, OS.O_RDONLY, 0);
56 if err == nil {
57 fd.Close();
58 return true;
59 }
60 return false;
61}
62
63
64func AddDeps(globalset *map [string] bool, wset *AST.List, src_file string, flags *Flags) {
65 dummy, found := globalset[src_file];
66 if !found {
67 globalset[src_file] = true;
68
69 prog, nerrors := Compile(src_file, flags);
70 if nerrors > 0 {
71 return;
72 }
73
74 nimports := prog.decls.len();
75 if nimports > 0 {
76 print(src_file, ".6:\t");
77
78 localset := new(map [string] bool);
79 for i := 0; i < nimports; i++ {
80 decl := prog.decls.at(i).(*AST.Decl);
81 assert(decl.tok == Scanner.IMPORT && decl.val.tok == Scanner.STRING);
82 src := decl.val.s;
83 src = src[1 : len(src) - 1]; // strip "'s
84
85 // ignore files when they are seen a 2nd time
86 dummy, found := localset[src];
87 if !found {
88 localset[src] = true;
89 if FileExists(src + ".go") {
90 wset.Add(src);
91 print(" ", src, ".6");
92 } else if
93 FileExists(Platform.GOROOT + "/pkg/" + src + ".6") ||
94 FileExists(Platform.GOROOT + "/pkg/" + src + ".a") {
95
96 } else {
97 // TODO should collect these and print later
98 //print("missing file: ", src, "\n");
99 }
100 }
101 }
102 print("\n\n");
103 }
104 }
105}
106
107
108export func ComputeDeps(src_file string, flags *Flags) {
109 globalset := new(map [string] bool);
110 wset := AST.NewList();
111 wset.Add(src_file);
112 for wset.len() > 0 {
113 AddDeps(globalset, wset, wset.Pop().(string), flags);
114 }
Robert Griesemer63ed2b72008-10-24 14:08:01 -0700115}