Commit af065a0c authored by Robert Griesemer's avatar Robert Griesemer

- make code in gosrc compile again, check in all pending changes

  (this code doesn't match the existing language at this point,
  but it's a large code base which compiles - will eventually go
  away)
- enable compilation of it again in run.bash

R=r
DELTA=1147  (534 added, 311 deleted, 302 changed)
OCL=22176
CL=22176
parent 8d21004b
......@@ -52,11 +52,11 @@ time make
make smoketest
) || exit $?
# (xcd ../usr/gri/gosrc
# make clean
# time make
# # make test
# ) || exit $?
(xcd ../usr/gri/gosrc
make clean
time make
# make test
) || exit $?
(xcd ../doc/progs
time run
......
......@@ -5,12 +5,26 @@
package AST
import Globals "globals"
import GlobalObject "object"
import Type "type"
import Universe "universe"
// ----------------------------------------------------------------------------
// Expressions
export const /* op */ (
LITERAL = iota;
OBJECT;
DEREF;
SELECT;
CALL;
TUPLE;
)
// ----------------------------------------------------------------------------
// Literals
export type Literal struct {
pos_ int;
......@@ -22,18 +36,13 @@ export type Literal struct {
}
func (x *Literal) pos() int {
return x.pos_;
}
func (x *Literal) typ() *Globals.Type {
return x.typ_;
}
func (x *Literal) op() int { return LITERAL; }
func (x *Literal) pos() int { return x.pos_; }
func (x *Literal) typ() *Globals.Type { return x.typ_; }
export func NewLiteral(pos int, typ *Globals.Type) *Literal {
x := new(*Literal);
x := new(Literal);
x.pos_ = pos;
x.typ_ = typ;
return x;
......@@ -43,6 +52,9 @@ export func NewLiteral(pos int, typ *Globals.Type) *Literal {
export var Bad, True, False, Nil *Literal;
// ----------------------------------------------------------------------------
// Objects
// NOTE We could use Globals.Object directly if we'd added a typ()
// method to its interface. However, this would require renaming the
// typ field everywhere... - Need to think about accessors again.
......@@ -52,24 +64,43 @@ export type Object struct {
}
func (x *Object) pos() int {
return x.pos_;
func (x *Object) op() int { return OBJECT; }
func (x *Object) pos() int { return x.pos_; }
func (x *Object) typ() *Globals.Type { return x.obj.typ; }
export func NewObject(pos int, obj* Globals.Object) *Object {
x := new(Object);
x.pos_ = pos;
x.obj = obj;
return x;
}
func (x *Object) typ() *Globals.Type {
return x.obj.typ;
// ----------------------------------------------------------------------------
// Derefs
// TODO model Deref as unary operation?
export type Deref struct {
ptr_ Globals.Expr;
}
export func NewObject(pos int, obj* Globals.Object) *Object {
x := new(*Object);
x.pos_ = pos;
x.obj = obj;
func (x *Deref) op() int { return DEREF; }
func (x *Deref) pos() int { return x.ptr_.pos(); }
func (x *Deref) typ() *Globals.Type { return x.ptr_.typ().elt; }
export func NewDeref(ptr Globals.Expr) *Deref {
x := new(Deref);
x.ptr_ = ptr;
return x;
}
// ----------------------------------------------------------------------------
// Selectors
// TODO model Selector as binary operation?
export type Selector struct {
pos_ int;
......@@ -77,39 +108,87 @@ export type Selector struct {
}
func (x *Selector) pos() int {
return x.pos_;
func (x *Selector) op() int { return SELECT; }
func (x *Selector) pos() int { return x.pos_; }
func (x *Selector) typ() *Globals.Type { return x.typ_; }
export func NewSelector(pos int, typ *Globals.Type) *Selector {
x := new(Selector);
x.pos_ = pos;
x.typ_ = typ;
return x;
}
func (x *Selector) typ() *Globals.Type {
return x.typ_;
// ----------------------------------------------------------------------------
// Calls
export type Call struct {
recv, callee Globals.Expr;
args *Globals.List;
}
export func NewSelector(pos int, typ *Globals.Type) *Selector {
x := new(*Selector);
x.pos_ = pos;
x.typ_ = typ;
func (x *Call) op() int { return CALL; }
func (x *Call) pos() int { return 0; }
func (x *Call) typ() *Globals.Type { return nil; }
export func NewCall(args *Globals.List) *Call {
x := new(Call);
x.args = args;
return x;
}
// ----------------------------------------------------------------------------
// Binary expressions
export type BinaryExpr struct {
op_ int;
pos_ int;
typ_ *Globals.Type;
op int;
x, y Globals.Expr;
}
func (x *BinaryExpr) pos() int {
return x.pos_;
func (x *BinaryExpr) op() int { return x.op_; }
func (x *BinaryExpr) pos() int { return x.pos_; }
func (x *BinaryExpr) typ() *Globals.Type { return x.typ_; }
// ----------------------------------------------------------------------------
// Tuples
export type Tuple struct {
typ_ *Globals.Type;
list *Globals.List;
}
func (x *BinaryExpr) typ() *Globals.Type {
return x.typ_;
func (x *Tuple) op() int { return TUPLE; }
func (x *Tuple) pos() int { return x.list.first.expr.pos(); }
func (x *Tuple) typ() *Globals.Type { return x.typ_; }
export func NewTuple(list *Globals.List) *Tuple {
// make corresponding tuple type
scope := Globals.NewScope(nil);
for p := list.first; p != nil; p = p.next {
x := p.expr;
obj := Globals.NewObject(x.pos(), GlobalObject.FIELD, "");
obj.typ = x.typ();
scope.Add(obj);
}
typ := Globals.NewType(Type.TUPLE);
typ.scope = scope;
// create the tuple
x := new(Tuple);
x.typ_ = typ;
x.list = list;
return x;
}
......
......@@ -19,6 +19,54 @@ import Printer "printer"
import Verifier "verifier"
// Compute (line, column) information for a given source position.
func LineCol(src string, pos int) (line, col int) {
line = 1;
lpos := 0;
if pos > len(src) {
pos = len(src);
}
for i := 0; i < pos; i++ {
if src[i] == '\n' {
line++;
lpos = i;
}
}
return line, pos - lpos;
}
export func Error(comp *Globals.Compilation, pos int, msg string) {
const errdist = 10;
delta := pos - comp.errpos; // may be negative!
if delta < 0 {
delta = -delta;
}
if delta > errdist || comp.nerrors == 0 /* always report first error */ {
print(comp.src_file);
if pos >= 0 {
// print position
line, col := LineCol(comp.src, pos);
if Platform.USER == "gri" {
print(":", line, ":", col);
} else {
print(":", line);
}
}
print(": ", msg, "\n");
comp.nerrors++;
comp.errpos = pos;
}
if comp.nerrors >= 10 {
sys.exit(1);
}
}
func ReadImport(comp* Globals.Compilation, filename string, update bool) (data string, ok bool) {
if filename == "" {
panic("illegal package file name");
......@@ -83,34 +131,38 @@ export func Export(comp *Globals.Compilation, pkg_file string) {
export func Compile(comp *Globals.Compilation, src_file string) {
// TODO This is incorrect: When compiling with the -r flag, we are
// calling this function recursively w/o setting up a new comp - this
// is broken and leads to an assertion error (more then one package
// upon parsing of the package header).
src, ok := Platform.ReadSourceFile(src_file);
if !ok {
print("cannot open ", src_file, "\n");
return;
}
comp.src_file = src_file;
comp.src = src;
if comp.flags.verbosity > 0 {
print(src_file, "\n");
}
scanner := new(*Scanner.Scanner);
scanner := new(Scanner.Scanner);
scanner.Open(src_file, src);
var tstream chan *Scanner.Token;
if comp.flags.token_chan {
tstream = new(chan *Scanner.Token, 100);
tstream = make(chan *Scanner.Token, 100);
go scanner.Server(tstream);
}
parser := new(*Parser.Parser);
parser := new(Parser.Parser);
parser.Open(comp, scanner, tstream);
parser.ParseProgram();
if parser.S.nerrors > 0 {
return;
}
if !comp.flags.ast {
if parser.scanner.nerrors > 0 {
return;
}
......
......@@ -174,6 +174,9 @@ func (E *Exporter) WriteType(typ *Globals.Type) {
}
switch typ.form {
case Type.VOID:
// for now until we have enough of the front-end working.
case Type.FORWARD:
// corresponding package must be forward-declared too
if typ.obj == nil || E.comp.pkg_list[typ.obj.pnolev].key != "" {
......@@ -181,25 +184,29 @@ func (E *Exporter) WriteType(typ *Globals.Type) {
}
case Type.ALIAS, Type.MAP:
E.WriteType(typ.aux);
E.WriteType(typ.key);
E.WriteType(typ.elt);
case Type.TUPLE:
E.WriteType(typ.elt);
case Type.ARRAY:
E.WriteInt(typ.len_);
E.WriteInt(typ.len);
E.WriteType(typ.elt);
case Type.CHANNEL:
E.WriteInt(typ.flags);
E.WriteInt(typ.aux);
E.WriteType(typ.elt);
case Type.FUNCTION:
E.WriteInt(typ.flags);
case Type.FUNCTION, Type.METHOD:
E.WriteInt(typ.len);
E.WriteType(typ.elt);
E.WriteScope(typ.scope);
case Type.STRUCT, Type.INTERFACE:
E.WriteScope(typ.scope);
case Type.POINTER, Type.REFERENCE:
case Type.POINTER:
E.WriteType(typ.elt);
default:
......@@ -266,7 +273,7 @@ func (E *Exporter) Export(comp* Globals.Compilation) string {
i++;
}
}
E.type_ref = Universe.types.len_;
E.type_ref = Universe.types.len;
// export package 0
pkg := comp.pkg_list[0];
......
// 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 Expr
import Globals "globals"
import Universe "universe"
import Object "object"
import Type "type"
import AST "ast"
// TODO the following shortcuts below don't work due to 6g/6l bugs
//type Compilation Globals.Compilation
//type Expr Globals.Expr
func Error(comp *Globals.Compilation, pos int, msg string) {
comp.env.Error(comp, pos, msg);
}
export func Deref(comp *Globals.Compilation, x Globals.Expr) Globals.Expr {
switch typ := x.typ(); typ.form {
case Type.BAD:
// ignore
case Type.POINTER:
x = AST.NewDeref(x);
default:
Error(comp, x.pos(), `"*" not applicable (typ.form = ` + Type.FormStr(typ.form) + `)`);
x = AST.Bad;
}
return x;
}
export func Select(comp *Globals.Compilation, x Globals.Expr, pos int, selector string) Globals.Expr {
if x.typ().form == Type.POINTER {
x = Deref(comp, x);
}
switch typ := x.typ(); typ.form {
case Type.BAD:
// ignore
case Type.STRUCT, Type.INTERFACE:
obj := typ.scope.Lookup(selector);
if obj != nil {
x = AST.NewSelector(x.pos(), obj.typ);
} else {
Error(comp, pos, `no field/method "` + selector + `"`);
x = AST.Bad;
}
default:
Error(comp, pos, `"." not applicable (typ.form = ` + Type.FormStr(typ.form) + `)`);
x = AST.Bad;
}
return x;
}
export func AssertType(comp *Globals.Compilation, x Globals.Expr, pos int, typ *Globals.Type) Globals.Expr {
return AST.Bad;
}
export func Index(comp *Globals.Compilation, x, i Globals.Expr) Globals.Expr {
if x.typ().form == Type.POINTER {
x = Deref(comp, x);
}
switch typ := x.typ(); typ.form {
case Type.BAD:
// ignore
case Type.STRING, Type.ARRAY:
x = AST.Bad;
case Type.MAP:
if Type.Equal(typ.key, i.typ()) {
// x = AST.NewSubscript(x, i1);
x = AST.Bad;
} else {
Error(comp, x.pos(), "map key type mismatch");
x = AST.Bad;
}
default:
Error(comp, x.pos(), `"[]" not applicable (typ.form = ` + Type.FormStr(typ.form) + `)`);
x = AST.Bad;
}
return x;
}
export func Slice(comp *Globals.Compilation, x, i, j Globals.Expr) Globals.Expr {
if x.typ().form == Type.POINTER {
x = Deref(comp, x);
}
switch typ := x.typ(); typ.form {
case Type.BAD:
// ignore
break;
case Type.STRING, Type.ARRAY:
x = AST.Bad;
case Type.MAP:
if Type.Equal(typ.key, i.typ()) {
// x = AST.NewSubscript(x, i1);
x = AST.Bad;
} else {
Error(comp, x.pos(), "map key type mismatch");
x = AST.Bad;
}
default:
Error(comp, x.pos(), `"[:]" not applicable (typ.form = ` + Type.FormStr(typ.form) + `)`);
x = AST.Bad;
}
return x;
}
export func Call(comp *Globals.Compilation, x Globals.Expr, args *Globals.List) Globals.Expr {
if x.typ().form == Type.POINTER {
x = Deref(comp, x);
}
if x.op() == AST.OBJECT && x.(*AST.Object).obj.kind == Object.BUILTIN {
panic("builtin call - UNIMPLEMENTED");
}
typ := x.typ();
if typ.form == Type.FUNCTION || typ.form == Type.METHOD {
// TODO check args against parameters
}
return AST.Bad;
}
export func UnaryExpr(comp *Globals.Compilation, x Globals.Expr) Globals.Expr {
return AST.Bad;
}
export func BinaryExpr(comp *Globals.Compilation, x, y Globals.Expr) Globals.Expr {
e := new(AST.BinaryExpr);
e.typ_ = x.typ(); // TODO fix this
//e.op = P.tok; // TODO should we use tokens or separate operator constants?
e.x = x;
e.y = y;
return e;
}
......@@ -18,6 +18,11 @@ type Scope struct
type Elem struct
type Compilation struct
// Object represents a language object, such as a constant, variable, type,
// etc. (kind). An objects is (pre-)declared at a particular position in the
// source code (pos), has a name (ident), a type (typ), and a package number
// or nesting level (pnolev).
export type Object struct {
exported bool;
pos int; // source position (< 0 if unknown position)
......@@ -31,12 +36,12 @@ export type Object struct {
export type Type struct {
ref int; // for exporting only: >= 0 means already exported
form int;
flags int; // channels, functions
size int; // in bytes
len_ int; // array length, no. of parameters (w/o recv)
len int; // array length, no. of function/method parameters (w/o recv)
aux int; // channel info
obj *Object; // primary type object or NULL
aux *Type; // alias base type or map key
elt *Type; // aliases, arrays, maps, channels, pointers
key *Type; // alias base type or map key
elt *Type; // aliased type, array, map, channel or pointer element type, function result type, tuple function type
scope *Scope; // forwards, structs, interfaces, functions
}
......@@ -51,7 +56,7 @@ export type Package struct {
export type List struct {
len_ int;
len int;
first, last *Elem;
};
......@@ -70,17 +75,12 @@ export type Flags struct {
print_interface bool;
verbosity uint;
sixg bool;
scan bool;
parse bool;
ast bool;
deps bool;
token_chan bool;
}
export type Environment struct {
Error *(comp *Compilation); // TODO complete this
Error *(comp *Compilation, pos int, msg string);
Import *(comp *Compilation, pkg_file string) *Package;
Export *(comp *Compilation, pkg_file string);
Compile *(comp *Compilation, src_file string);
......@@ -91,7 +91,15 @@ export type Compilation struct {
// environment
flags *Flags;
env *Environment;
// TODO rethink the need for this here
src_file string;
src string;
// Error handling
nerrors int; // number of errors reported
errpos int; // last error position
// TODO use open arrays eventually
pkg_list [256] *Package; // pkg_list[0] is the current package
pkg_ref int;
......@@ -99,6 +107,7 @@ export type Compilation struct {
export type Expr interface {
op() int; // node operation
pos() int; // source position
typ() *Type;
// ... more to come here
......@@ -113,7 +122,7 @@ export type Stat interface {
// TODO This is hideous! We need to have a decent way to do lists.
// Ideally open arrays that allow '+'.
type Elem struct {
export type Elem struct {
next *Elem;
val int;
str string;
......@@ -129,7 +138,7 @@ type Elem struct {
export var Universe_void_t *Type // initialized by Universe to Universe.void_t
export func NewObject(pos, kind int, ident string) *Object {
obj := new(*Object);
obj := new(Object);
obj.exported = false;
obj.pos = pos;
obj.kind = kind;
......@@ -141,7 +150,7 @@ export func NewObject(pos, kind int, ident string) *Object {
export func NewType(form int) *Type {
typ := new(*Type);
typ := new(Type);
typ.ref = -1; // not yet exported
typ.form = form;
return typ;
......@@ -149,7 +158,7 @@ export func NewType(form int) *Type {
export func NewPackage(file_name string, obj *Object, scope *Scope) *Package {
pkg := new(*Package);
pkg := new(Package);
pkg.ref = -1; // not yet exported
pkg.file_name = file_name;
pkg.key = "<the package key>"; // empty key means package forward declaration
......@@ -160,12 +169,12 @@ export func NewPackage(file_name string, obj *Object, scope *Scope) *Package {
export func NewList() *List {
return new(*List);
return new(List);
}
export func NewScope(parent *Scope) *Scope {
scope := new(*Scope);
scope := new(Scope);
scope.parent = parent;
scope.entries = NewList();
return scope;
......@@ -176,7 +185,7 @@ export func NewScope(parent *Scope) *Scope {
// Object methods
func (obj *Object) Copy() *Object {
copy := new(*Object);
copy := new(Object);
copy.exported = obj.exported;
copy.pos = obj.pos;
copy.kind = obj.kind;
......@@ -191,7 +200,7 @@ func (obj *Object) Copy() *Object {
// List methods
func (L *List) at(i int) *Elem {
if i < 0 || L.len_ <= i {
if i < 0 || L.len <= i {
panic("index out of bounds");
}
......@@ -205,13 +214,13 @@ func (L *List) at(i int) *Elem {
func (L *List) Clear() {
L.len_, L.first, L.last = 0, nil, nil;
L.len, L.first, L.last = 0, nil, nil;
}
func (L *List) Add() *Elem {
L.len_++;
e := new(*Elem);
L.len++;
e := new(Elem);
if L.first == nil {
L.first = e;
} else {
......@@ -242,6 +251,11 @@ func (L *List) TypAt(i int) *Type {
}
func (L *List) ExprAt(i int) Expr {
return L.at(i).expr;
}
func (L *List) AddInt(val int) {
L.Add().val = val;
}
......@@ -280,18 +294,23 @@ func (scope *Scope) Lookup(ident string) *Object {
}
func (scope *Scope) Add(obj* Object) {
scope.entries.AddObj(obj);
}
func (scope *Scope) Insert(obj *Object) {
if scope.Lookup(obj.ident) != nil {
panic("obj already inserted");
}
scope.entries.AddObj(obj);
scope.Add(obj);
}
func (scope *Scope) InsertImport(obj *Object) *Object {
p := scope.Lookup(obj.ident);
if p == nil {
scope.Insert(obj);
scope.Add(obj);
p = obj;
}
return p;
......
......@@ -20,10 +20,6 @@ func PrintHelp() {
" -p print package interface\n" +
" -v [0 .. 3] verbosity level\n" +
" -6g 6g compatibility mode\n" +
" -scan scan only, print tokens\n" +
" -parse parse only, print productions\n" +
" -ast analyse only, print ast\n" +
" -deps print package dependencies\n" +
" -token_chan use token channel to scan and parse in parallel\n"
);
}
......@@ -49,7 +45,7 @@ func main() {
}
// collect flags and files
flags := new(*Globals.Flags);
flags := new(Globals.Flags);
files := Globals.NewList();
for arg != "" {
switch arg {
......@@ -69,13 +65,6 @@ func main() {
continue;
}
case "-6g": flags.sixg = true;
case "-scan": flags.scan = true;
print("note: -scan flag ignored at the moment\n");
case "-parse": flags.parse = true;
print("note: -parse flag ignored at the moment\n");
case "-ast": flags.ast = true;
case "-deps": flags.deps = true;
print("note: -deps flag ignored at the moment\n");
case "-token_chan": flags.token_chan = true;
default: files.AddStr(arg);
}
......@@ -83,7 +72,8 @@ func main() {
}
// setup environment
env := new(*Globals.Environment);
env := new(Globals.Environment);
env.Error = &Compilation.Error;
env.Import = &Compilation.Import;
env.Export = &Compilation.Export;
env.Compile = &Compilation.Compile;
......@@ -91,7 +81,7 @@ func main() {
// compile files
for p := files.first; p != nil; p = p.next {
// setup compilation
comp := new(*Globals.Compilation);
comp := new(Globals.Compilation);
comp.flags = flags;
comp.env = env;
......
......@@ -200,24 +200,32 @@ func (I *Importer) ReadType() *Globals.Type {
I.type_ref++;
switch (typ.form) {
case Type.VOID:
// for now until we have enough of the front-end working
// change the form to BAD to avoid error messages
typ.form = Type.BAD;
case Type.FORWARD:
typ.scope = Globals.NewScope(nil);
break;
case Type.TUPLE:
typ.elt = I.ReadType();
case Type.ALIAS, Type.MAP:
typ.aux = I.ReadType();
typ.key = I.ReadType();
typ.elt = I.ReadType();
case Type.ARRAY:
typ.len_ = I.ReadInt();
typ.len = I.ReadInt();
typ.elt = I.ReadType();
case Type.CHANNEL:
typ.flags = I.ReadInt();
typ.aux = I.ReadInt();
typ.elt = I.ReadType();
case Type.FUNCTION:
typ.flags = I.ReadInt();
case Type.FUNCTION, Type.METHOD:
typ.len = I.ReadInt();
typ.elt = I.ReadType();
typ.scope = Globals.NewScope(nil);
I.ReadScope(typ.scope, false);
......@@ -225,7 +233,7 @@ func (I *Importer) ReadType() *Globals.Type {
typ.scope = Globals.NewScope(nil);
I.ReadScope(typ.scope, false);
case Type.POINTER, Type.REFERENCE:
case Type.POINTER:
typ.elt = I.ReadType();
default:
......
......@@ -9,7 +9,7 @@ import Globals "globals"
export const /* kind */ (
BAD = iota; // error handling
CONST; TYPE; VAR; FIELD; FUNC; PACKAGE; LABEL;
CONST; TYPE; VAR; FIELD; FUNC; BUILTIN; PACKAGE; LABEL;
END; // end of scope (import/export only)
)
......@@ -27,6 +27,7 @@ export func KindStr(kind int) string {
case VAR: return "VAR";
case FIELD: return "FIELD";
case FUNC: return "FUNC";
case BUILTIN: return "BUILTIN";
case PACKAGE: return "PACKAGE";
case LABEL: return "LABEL";
case END: return "END";
......
This diff is collapsed.
......@@ -40,7 +40,7 @@ func IsAnonymous(name string) bool {
func (P *Printer) PrintSigRange(typ *Globals.Type, a, b int) {
scope := typ.scope;
if a + 1 == b && IsAnonymous(scope.entries.ObjAt(a).ident) {
P.PrintType(scope.entries.TypAt(a)); // result type only
P.PrintType(scope.entries.ObjAt(a).typ); // result type only
} else {
print("(");
for i := a; i < b; i++ {
......@@ -57,16 +57,16 @@ func (P *Printer) PrintSigRange(typ *Globals.Type, a, b int) {
func (P *Printer) PrintSignature(typ *Globals.Type, fun *Globals.Object) {
if typ.form != Type.FUNCTION {
panic("typ.form != Type.FUNCTION");
}
p0 := 0;
if typ.flags & Type.RECV != 0 {
if typ.form == Type.METHOD {
p0 = 1;
} else {
if typ.form != Type.FUNCTION {
panic("not a function or method");
}
}
r0 := p0 + typ.len_;
l0 := typ.scope.entries.len_;
r0 := p0 + typ.len;
l0 := typ.scope.entries.len;
if P.level == 0 {
print("func ");
......@@ -105,7 +105,7 @@ func (P *Printer) PrintScope(scope *Globals.Scope, delta int) {
// determine the number of scope entries to print
var n int;
if P.print_all {
n = scope.entries.len_;
n = scope.entries.len;
} else {
n = 0;
for p := scope.entries.first; p != nil; p = p.next {
......@@ -130,12 +130,12 @@ func (P *Printer) PrintScope(scope *Globals.Scope, delta int) {
}
}
func (P *Printer) PrintObjectStruct(obj *Globals.Object) {
switch obj.kind {
case Object.BAD:
print("bad ");
P.PrintObject(obj);
print(" /* bad */");
case Object.CONST:
print("const ");
......@@ -149,10 +149,11 @@ func (P *Printer) PrintObjectStruct(obj *Globals.Object) {
print(" ");
P.PrintTypeStruct(obj.typ);
case Object.VAR, Object.FIELD:
if P.level == 0 {
print("var ");
}
case Object.VAR:
print("var ");
fallthrough;
case Object.FIELD:
P.PrintObject(obj);
print(" ");
P.PrintType(obj.typ);
......@@ -160,6 +161,10 @@ func (P *Printer) PrintObjectStruct(obj *Globals.Object) {
case Object.FUNC:
P.PrintSignature(obj.typ, obj);
case Object.BUILTIN:
P.PrintObject(obj);
print(" /* builtin */");
case Object.PACKAGE:
print("package ");
P.PrintObject(obj);
......@@ -197,11 +202,14 @@ func (P *Printer) PrintTypeStruct(typ *Globals.Type) {
case Type.VOID:
print("void");
case Type.BAD:
print("<bad type>");
case Type.FORWARD:
print("<forward type>");
case Type.BAD:
print("<bad type>");
case Type.TUPLE:
print("<tuple type>");
case Type.NIL, Type.BOOL, Type.UINT, Type.INT, Type.FLOAT, Type.STRING, Type.ANY:
if typ.obj == nil {
......@@ -211,9 +219,9 @@ func (P *Printer) PrintTypeStruct(typ *Globals.Type) {
case Type.ALIAS:
P.PrintType(typ.elt);
if typ.aux != typ.elt {
if typ.key != typ.elt {
print(" /* ");
P.PrintType(typ.aux);
P.PrintType(typ.key);
print(" */");
}
......@@ -233,12 +241,12 @@ func (P *Printer) PrintTypeStruct(typ *Globals.Type) {
case Type.MAP:
print("map [");
P.PrintType(typ.aux);
P.PrintType(typ.key);
print("] ");
P.PrintType(typ.elt);
case Type.CHANNEL:
switch typ.flags {
switch typ.aux {
case Type.SEND: print("chan <- ");
case Type.RECV: print("<- chan ");
case Type.SEND + Type.RECV: print("chan ");
......@@ -253,10 +261,6 @@ func (P *Printer) PrintTypeStruct(typ *Globals.Type) {
print("*");
P.PrintType(typ.elt);
case Type.REFERENCE:
print("&");
P.PrintType(typ.elt);
default:
panic("UNREACHABLE");
......
......@@ -86,7 +86,6 @@ export const (
ELSE;
EXPORT;
FALLTHROUGH;
FALSE;
FOR;
FUNC;
GO;
......@@ -94,17 +93,13 @@ export const (
IF;
IMPORT;
INTERFACE;
IOTA;
MAP;
NEW;
NIL;
PACKAGE;
RANGE;
RETURN;
SELECT;
STRUCT;
SWITCH;
TRUE;
TYPE;
VAR;
KEYWORDS_END;
......@@ -191,7 +186,6 @@ export func TokenName(tok int) string {
case ELSE: return "else";
case EXPORT: return "export";
case FALLTHROUGH: return "fallthrough";
case FALSE: return "false";
case FOR: return "for";
case FUNC: return "func";
case GO: return "go";
......@@ -199,17 +193,13 @@ export func TokenName(tok int) string {
case IF: return "if";
case IMPORT: return "import";
case INTERFACE: return "interface";
case IOTA: return "iota";
case MAP: return "map";
case NEW: return "new";
case NIL: return "nil";
case PACKAGE: return "package";
case RANGE: return "range";
case RETURN: return "return";
case SELECT: return "select";
case STRUCT: return "struct";
case SWITCH: return "switch";
case TRUE: return "true";
case TYPE: return "type";
case VAR: return "var";
}
......@@ -219,7 +209,7 @@ export func TokenName(tok int) string {
func init() {
Keywords = new(map [string] int);
Keywords = make(map [string] int);
for i := KEYWORDS_BEG; i <= KEYWORDS_END; i++ {
Keywords[TokenName(i)] = i;
......@@ -578,14 +568,14 @@ func (S *Scanner) ScanDigits(n int, base int) {
}
func (S *Scanner) ScanEscape() string {
func (S *Scanner) ScanEscape(quote int) string {
// TODO: fix this routine
ch := S.ch;
pos := S.chpos;
S.Next();
switch (ch) {
case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', '\'', '"':
case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\':
return string(ch);
case '0', '1', '2', '3', '4', '5', '6', '7':
......@@ -605,9 +595,13 @@ func (S *Scanner) ScanEscape() string {
return ""; // TODO fix this
default:
// check for quote outside the switch for better generated code (eventually)
if ch == quote {
return string(quote);
}
S.Error(pos, "illegal char escape");
}
return ""; // TODO fix this
}
......@@ -619,7 +613,7 @@ func (S *Scanner) ScanChar() string {
ch := S.ch;
S.Next();
if ch == '\\' {
S.ScanEscape();
S.ScanEscape('\'');
}
S.Expect('\'');
......@@ -639,7 +633,7 @@ func (S *Scanner) ScanString() string {
break;
}
if ch == '\\' {
S.ScanEscape();
S.ScanEscape('"');
}
}
......@@ -781,7 +775,7 @@ export type Token struct {
func (S *Scanner) Server(c chan *Token) {
for {
t := new(*Token);
t := new(Token);
t.tok, t.pos, t.val = S.Scan();
c <- t;
if t.tok == EOF {
......
......@@ -8,7 +8,7 @@ import Scanner "scanner"
func Scan1(filename, src string) {
S := new(*Scanner.Scanner);
S := new(Scanner.Scanner);
S.Open(filename, src);
for {
tok, pos, val := S.Scan();
......@@ -25,9 +25,9 @@ func Scan1(filename, src string) {
func Scan2(filename, src string) {
S := new(*Scanner.Scanner);
S := new(Scanner.Scanner);
S.Open(filename, src);
c := new(chan *Scanner.Token, 32);
c := make(chan *Scanner.Token, 32);
go S.Server(c);
for {
var t *Scanner.Token;
......
......@@ -10,8 +10,12 @@ import Object "object"
export const /* form */ (
// internal types
// VOID types are used when we don't have a type.
VOID = iota;
// We should never see one of these.
UNDEF = iota;
// VOID types are used when we don't have a type. Never exported.
// (exported type forms must be > 0)
VOID;
// BAD types are compatible with any type and don't cause further errors.
// They are introduced only as a result of an error in the source code. A
......@@ -22,6 +26,10 @@ export const /* form */ (
// be used as element types of pointer types and must be resolved before
// their internals are accessible.
FORWARD;
// TUPLE types represent multi-valued result types of functions and
// methods.
TUPLE;
// The type of nil.
NIL;
......@@ -33,13 +41,13 @@ export const /* form */ (
ANY;
// composite types
ALIAS; ARRAY; STRUCT; INTERFACE; MAP; CHANNEL; FUNCTION; POINTER; REFERENCE;
ALIAS; ARRAY; STRUCT; INTERFACE; MAP; CHANNEL; FUNCTION; METHOD; POINTER;
)
export const /* flag */ (
SEND = 1 << iota; // chan>
RECV; // chan< or method
export const /* Type.aux */ (
SEND = 1; // chan>
RECV = 2; // chan<
)
......@@ -53,6 +61,7 @@ export func FormStr(form int) string {
case VOID: return "VOID";
case BAD: return "BAD";
case FORWARD: return "FORWARD";
case TUPLE: return "TUPLE";
case NIL: return "NIL";
case BOOL: return "BOOL";
case UINT: return "UINT";
......@@ -67,8 +76,8 @@ export func FormStr(form int) string {
case MAP: return "MAP";
case CHANNEL: return "CHANNEL";
case FUNCTION: return "FUNCTION";
case METHOD: return "METHOD";
case POINTER: return "POINTER";
case REFERENCE: return "REFERENCE";
}
return "<unknown Type form>";
}
......@@ -102,25 +111,24 @@ func Equal0(x, y *Globals.Type) bool {
case ARRAY:
return
x.len_ == y.len_ &&
x.len == y.len &&
Equal(x.elt, y.elt);
case MAP:
return
Equal(x.aux, y.aux) &&
Equal(x.key, y.key) &&
Equal(x.elt, y.elt);
case CHANNEL:
return
x.flags == y.flags &&
x.aux == y.aux &&
Equal(x.elt, y.elt);
case FUNCTION:
case FUNCTION, METHOD:
{ xp := x.scope.entries;
yp := x.scope.entries;
if x.flags != y.flags && // function or method
x.len_ != y.len_ && // number of parameters
xp.len_ != yp.len_ // recv + parameters + results
if x.len != y.len && // number of parameters
xp.len != yp.len // recv + parameters + results
{
return false;
}
......@@ -163,8 +171,12 @@ func Equal0(x, y *Globals.Type) bool {
panic("UNIMPLEMENTED");
return false;
case POINTER, REFERENCE:
case POINTER:
return Equal(x.elt, y.elt);
case TUPLE:
panic("UNIMPLEMENTED");
return false;
}
panic("UNREACHABLE");
......
......@@ -48,7 +48,9 @@ export var (
ptrint_t *Globals.Type;
true_,
false_ *Globals.Object;
false_,
iota_,
nil_ *Globals.Object;
)
......@@ -72,17 +74,17 @@ func DeclType(form int, ident string, size int) *Globals.Type {
func DeclAlias(ident string, typ *Globals.Type) *Globals.Type {
alias := Globals.NewType(Type.ALIAS);
alias.aux = typ;
alias.key = typ;
alias.elt = typ;
return DeclObj(Object.TYPE, ident, alias).typ;
}
func Register(typ *Globals.Type) *Globals.Type {
if types.len_ < 0 {
panic("types.len_ < 0");
if types.len < 0 {
panic("types.len < 0");
}
typ.ref = types.len_;
typ.ref = types.len;
types.AddTyp(typ);
return typ;
}
......@@ -96,7 +98,7 @@ func init() {
void_t = Globals.NewType(Type.VOID);
Globals.Universe_void_t = void_t;
bad_t = Globals.NewType(Type.BAD);
nil_t = DeclType(Type.NIL, "nil", 8);
nil_t = Globals.NewType(Type.NIL);
// Basic types
bool_t = Register(DeclType(Type.BOOL, "bool", 1));
......@@ -130,9 +132,14 @@ func init() {
// Predeclared constants
true_ = DeclObj(Object.CONST, "true", bool_t);
false_ = DeclObj(Object.CONST, "false", bool_t);
iota_ = DeclObj(Object.CONST, "iota", int_t);
nil_ = DeclObj(Object.CONST, "nil", nil_t);
// Builtin functions
DeclObj(Object.FUNC, "len", Globals.NewType(Type.FUNCTION)); // incomplete
DeclObj(Object.BUILTIN, "len", void_t);
DeclObj(Object.BUILTIN, "new", void_t);
DeclObj(Object.BUILTIN, "panic", void_t);
DeclObj(Object.BUILTIN, "print", void_t);
// scope.Print();
}
......@@ -46,11 +46,15 @@ func (V *Verifier) VerifyType(typ *Globals.Type) {
switch typ.form {
case Type.VOID:
case Type.BAD:
break; // TODO for now - remove eventually
case Type.FORWARD:
if typ.scope == nil {
Error("forward types must have a scope");
}
case Type.TUPLE:
break;
case Type.NIL:
break;
......@@ -82,8 +86,6 @@ func (V *Verifier) VerifyType(typ *Globals.Type) {
break;
case Type.POINTER:
break;
case Type.REFERENCE:
break;
default:
Error("illegal type form " + Type.FormStr(typ.form));
}
......@@ -139,12 +141,12 @@ func (V *Verifier) VerifyPackage(pkg *Globals.Package, pno int) {
func (V *Verifier) Verify(comp *Globals.Compilation) {
// initialize Verifier
V.comp = comp;
V.objs = new(map[*Globals.Object] bool);
V.typs = new(map[*Globals.Type] bool);
V.pkgs = new(map[*Globals.Package] bool);
V.objs = make(map[*Globals.Object] bool);
V.typs = make(map[*Globals.Type] bool);
V.pkgs = make(map[*Globals.Package] bool);
// verify all packages
filenames := new(map[string] bool);
filenames := make(map[string] bool);
for i := 0; i < comp.pkg_ref; i++ {
pkg := comp.pkg_list[i];
// each pkg filename must appear only once
......@@ -158,6 +160,6 @@ func (V *Verifier) Verify(comp *Globals.Compilation) {
export func Verify(comp *Globals.Compilation) {
V := new(*Verifier);
V := new(Verifier);
V.Verify(comp);
}
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