Commit 4b0b7d8d authored by Robert Griesemer's avatar Robert Griesemer

- import and export code, bug fixes

- almost back to where I was in C++, but now all in Go

R=r
OCL=13627
CL=13627
parent ebec9917
// 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.
// Base for the decls.go tests.
package base
type Node struct {
left, right *Node;
val bool
}
export Node
......@@ -4,6 +4,7 @@
package Compilation
import Utils "utils"
import Globals "globals"
import Object "object"
import Type "type"
......@@ -14,28 +15,6 @@ import Parser "parser"
import Export "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)];
}
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";
}
export Compile
func Compile(file_name string, verbose int) {
src, ok := sys.readfile(file_name);
......@@ -47,11 +26,6 @@ func Compile(file_name string, verbose int) {
Universe.Init(); // TODO eventually this should be only needed once
comp := Globals.NewCompilation();
pkg := Globals.NewPackage(file_name);
comp.Insert(pkg);
if comp.npkgs != 1 {
panic "should have exactly one package now";
}
scanner := new(Scanner.Scanner);
scanner.Open(file_name, src);
......@@ -66,8 +40,6 @@ func Compile(file_name string, verbose int) {
}
// export
/*
exp := new(Export.Exporter);
exp.Export(comp, FixExt(BaseName(file_name)));
*/
exp.Export(comp, Utils.FixExt(Utils.BaseName(file_name)));
}
// 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.
// Tests.
package decls
// import "base" // this fails
import base "base"
import base2 "base"
const c0 int = 0
const c1 float = 1.
const (
c2 byte = 2;
c3 int = 3;
c4 float = 4.;
)
type Node0 base.Node
type Node1 *base2.Node
type T0 byte
type T1 T0
type (
T2 [10]T0;
T3 map [string] int;
T4 struct {
f1, f2, f3 int;
f4 [] float;
};
T5 *T4;
)
type F0 func ()
type F1 func (a int)
type F2 func (a, b int, c float)
type F3 func () bool
type F4 func (a int) (z T5, ok bool)
type F5 func (a, b int, c float) (z T5, ok bool)
type F6 func (a int, b float) bool
type F7 func (a int, b float, c, d *bool) bool
type (
M0 func (p T5) . ();
M1 func (p T5) . (a int);
M2 func (p T5) . (a, b int, c float);
M3 func (p T5) . () bool;
M4 func (p T5) . (a int) (z T5, ok bool);
M5 func (p T5) . (a, b int, c float) (z T5, ok bool);
)
type T6 chan int
type T7 chan<- T6
type T8 chan-< T6
type T9 struct {
p *T9;
q [] map [int] *T9;
f *func(x, y *T9) *T9;
}
type T10;
type T11 struct {
p *T10;
}
type T10 struct {
p *T11;
}
type T12 struct {
p *T12
}
type I0 interface {}
type I1 interface {
Do0(q *I0);
Do1(p *I1) bool;
}
type I2 interface {
M0();
M1(a int);
M2(a, b int, c float);
M3() bool;
M4(a int) (z T5, ok bool);
M5(a, b int, c float) (z T5, ok bool);
}
var v0 int
var v1 float = c1
var (
v2 T2;
v3 struct {
f1, f2, f3 M0;
}
)
func f0() {}
func f1(a int) {}
func f2(a, b int, c float) {}
func f3() bool {}
func f4(a int) (z T5, ok bool) {}
func f5(a, b int, c float) (z T5, ok bool) {}
func (p *T4) m0() {}
func (p *T4) m1(a int) {}
func (p *T4) m2(a, b int, c float) {}
func (p *T4) m3() bool {}
func (p *T4) m4(a int) (z T5, ok bool) {}
func (p *T4) m5(a, b int, c float) (z T5, ok bool) {
L: var x = a;
}
export c0, c1, v2, v3
export T0, T1, T4, T4, T4, M0, M5, I2, f0, f1, Node0, Node1
......@@ -38,29 +38,30 @@ func (E *Exporter) WriteByte(x byte) {
func (E *Exporter) WriteInt(x int) {
/*
if E.debug {
print " #", x;
}
*/
x0 := x;
for x < -64 || x >= 64 {
E.WriteByte(byte(x & 127));
x = int(uint(x >> 7)); // arithmetic shift
}
// -64 <= x && x < 64
E.WriteByte(byte(x + 192));
/*
if E.debug {
print " #", x0;
}
*/
}
func (E *Exporter) WriteString(s string) {
if E.debug {
print ` "`, s, `"`;
}
n := len(s);
E.WriteInt(n);
for i := 0; i < n; i++ {
E.WriteByte(s[i]);
}
if E.debug {
print ` "`, s, `"`;
}
}
......@@ -68,14 +69,15 @@ func (E *Exporter) WriteObjTag(tag int) {
if tag < 0 {
panic "tag < 0";
}
E.WriteInt(tag);
if E.debug {
print "\nObj: ", tag; // obj kind
}
E.WriteInt(tag);
}
func (E *Exporter) WriteTypeTag(tag int) {
E.WriteInt(tag);
if E.debug {
if tag > 0 {
print "\nTyp ", E.type_ref, ": ", tag; // type form
......@@ -83,11 +85,11 @@ func (E *Exporter) WriteTypeTag(tag int) {
print " [Typ ", -tag, "]"; // type ref
}
}
E.WriteInt(tag);
}
func (E *Exporter) WritePackageTag(tag int) {
E.WriteInt(tag);
if E.debug {
if tag > 0 {
print "\nPkg ", E.pkg_ref, ": ", tag; // package no
......@@ -95,7 +97,6 @@ func (E *Exporter) WritePackageTag(tag int) {
print " [Pkg ", -tag, "]"; // package ref
}
}
E.WriteInt(tag);
}
......@@ -119,6 +120,7 @@ func (E *Exporter) WriteScope(scope *Globals.Scope) {
n++;
}
}
E.WriteInt(n);
// export the objects, if any
if n > 0 {
......@@ -149,7 +151,7 @@ func (E *Exporter) WriteObject(obj *Globals.Object) {
E.WriteObjTag(obj.kind);
E.WriteString(obj.ident);
E.WriteType(obj.typ);
//E.WritePackage(E.comp.pkgs[obj.pnolev]);
E.WritePackage(E.comp.pkgs[obj.pnolev]);
switch obj.kind {
case Object.CONST:
......@@ -249,16 +251,16 @@ func (E *Exporter) WritePackage(pkg *Globals.Package) {
func (E *Exporter) Export(comp* Globals.Compilation, file_name string) {
if E.debug {
print "exporting to ", file_name;
}
E.comp = comp;
E.debug = true;
E.debug = false;
E.pos = 0;
E.pkg_ref = 0;
E.type_ref = 0;
if E.debug {
print "exporting to ", file_name, "\n";
}
// Predeclared types are "pre-exported".
// TODO run the loop below only in debug mode
{ i := 0;
......
......@@ -280,6 +280,7 @@ func (C *Compilation) Insert(pkg *Package) {
if C.Lookup(pkg.file_name) != nil {
panic "package already inserted";
}
pkg.obj.pnolev = C.npkgs;
C.pkgs[C.npkgs] = pkg;
C.npkgs++;
}
......
......@@ -10,6 +10,7 @@ import Type "type"
import Universe "universe"
export Importer // really only want to export Import()
type Importer struct {
comp *Globals.Compilation;
debug bool;
......@@ -257,6 +258,7 @@ func (I *Importer) ReadPackage() *Globals.Package {
if pkg == nil {
// new package
pkg = Globals.NewPackage(file_name);
pkg.obj = Globals.NewObject(-1, Object.PACKAGE, ident);
pkg.scope = Globals.NewScope(nil);
pkg = I.comp.InsertImport(pkg);
......@@ -271,24 +273,25 @@ func (I *Importer) ReadPackage() *Globals.Package {
}
func (I *Importer) Import(comp* Globals.Compilation, file_name string) {
func (I *Importer) Import(comp* Globals.Compilation, file_name string) *Globals.Package {
I.comp = comp;
I.debug = false;
I.buf = "";
I.pos = 0;
I.npkgs = 0;
I.ntypes = 0;
if I.debug {
print "importing from ", file_name;
print "importing from ", file_name, "\n";
}
buf, ok := sys.readfile(file_name);
if !ok {
panic "import failed";
return nil;
}
I.comp = comp;
I.debug = true;
I.buf = buf;
I.pos = 0;
I.npkgs = 0;
I.ntypes = 0;
// Predeclared types are "pre-exported".
// Predeclared types are "pre-imported".
for p := Universe.types.first; p != nil; p = p.next {
if p.typ.ref != I.ntypes {
panic "incorrect ref for predeclared type";
......@@ -311,4 +314,6 @@ func (I *Importer) Import(comp* Globals.Compilation, file_name string) {
if I.debug {
print "\n(", I.pos, " bytes)\n";
}
return pkg;
}
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 BaseName
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 FixExt
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";
}
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