1
0
mirror of https://github.com/golang/go synced 2024-11-23 00:20:12 -07:00
go/usr/gri/gosrc/compilation.go
2008-07-17 14:53:13 -07:00

115 lines
2.1 KiB
Go

// 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
import Globals "globals"
import Object "object"
import Type "type"
import Universe "universe"
import Scanner "scanner"
import Parser "parser"
import Export "export"
export Compilation
type Compilation struct {
src_name string;
pkg *Globals.Object;
imports [256] *Globals.Package; // TODO need open arrays
nimports int;
}
func (C *Compilation) Lookup(file_name string) *Globals.Package {
for i := 0; i < C.nimports; i++ {
pkg := C.imports[i];
if pkg.file_name == file_name {
return pkg;
}
}
return nil;
}
func (C *Compilation) Insert(pkg *Globals.Package) {
if C.Lookup(pkg.file_name) != nil {
panic "package already inserted";
}
pkg.pno = C.nimports;
C.imports[C.nimports] = pkg;
C.nimports++;
}
func (C *Compilation) InsertImport(pkg *Globals.Package) *Globals.Package {
p := C.Lookup(pkg.file_name);
if (p == nil) {
// no primary package found
C.Insert(pkg);
p = pkg;
}
return p;
}
func BaseName(s string) string {
// TODO this is not correct for non-ASCII strings!
i := len(s);
for i >= 0 && s[i] != '/' {
if s[i] > 128 {
panic "non-ASCII string"
}
i--;
}
return s[i + 1 : len(s)];
}
func FixExt(s string) string {
i := len(s) - 3; // 3 == len(".go");
if s[i : len(s)] == ".go" {
s = s[0 : i];
}
return s + ".7"
}
func (C *Compilation) Import(pkg_name string) (pno int) {
panic "UNIMPLEMENTED";
}
func (C *Compilation) Export() {
file_name := FixExt(BaseName(C.src_name)); // strip src dir
Export.Export(file_name/*, C */);
}
export Compile
func Compile(src_name string, verbose int) {
comp := new(Compilation);
comp.src_name = src_name;
comp.pkg = nil;
comp.nimports = 0;
src, ok := sys.readfile(src_name);
if !ok {
print "cannot open ", src_name, "\n"
return;
}
Universe.Init();
S := new(Scanner.Scanner);
S.Open(src_name, src);
P := new(Parser.Parser);
P.Open(S, verbose);
print "parsing ", src_name, "\n";
P.ParseProgram();
//comp.Export();
}