mirror of
https://github.com/golang/go.git
synced 2025-05-14 20:04:39 +00:00
- 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
This commit is contained in:
parent
8d21004b41
commit
af065a0c77
10
src/run.bash
10
src/run.bash
@ -52,11 +52,11 @@ time make
|
|||||||
make smoketest
|
make smoketest
|
||||||
) || exit $?
|
) || exit $?
|
||||||
|
|
||||||
# (xcd ../usr/gri/gosrc
|
(xcd ../usr/gri/gosrc
|
||||||
# make clean
|
make clean
|
||||||
# time make
|
time make
|
||||||
# # make test
|
# make test
|
||||||
# ) || exit $?
|
) || exit $?
|
||||||
|
|
||||||
(xcd ../doc/progs
|
(xcd ../doc/progs
|
||||||
time run
|
time run
|
||||||
|
@ -5,12 +5,26 @@
|
|||||||
package AST
|
package AST
|
||||||
|
|
||||||
import Globals "globals"
|
import Globals "globals"
|
||||||
|
import GlobalObject "object"
|
||||||
|
import Type "type"
|
||||||
import Universe "universe"
|
import Universe "universe"
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Expressions
|
// Expressions
|
||||||
|
|
||||||
|
export const /* op */ (
|
||||||
|
LITERAL = iota;
|
||||||
|
OBJECT;
|
||||||
|
DEREF;
|
||||||
|
SELECT;
|
||||||
|
CALL;
|
||||||
|
TUPLE;
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Literals
|
||||||
|
|
||||||
export type Literal struct {
|
export type Literal struct {
|
||||||
pos_ int;
|
pos_ int;
|
||||||
@ -22,18 +36,13 @@ export type Literal struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (x *Literal) pos() int {
|
func (x *Literal) op() int { return LITERAL; }
|
||||||
return x.pos_;
|
func (x *Literal) pos() int { return x.pos_; }
|
||||||
}
|
func (x *Literal) typ() *Globals.Type { return x.typ_; }
|
||||||
|
|
||||||
|
|
||||||
func (x *Literal) typ() *Globals.Type {
|
|
||||||
return x.typ_;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export func NewLiteral(pos int, typ *Globals.Type) *Literal {
|
export func NewLiteral(pos int, typ *Globals.Type) *Literal {
|
||||||
x := new(*Literal);
|
x := new(Literal);
|
||||||
x.pos_ = pos;
|
x.pos_ = pos;
|
||||||
x.typ_ = typ;
|
x.typ_ = typ;
|
||||||
return x;
|
return x;
|
||||||
@ -43,6 +52,9 @@ export func NewLiteral(pos int, typ *Globals.Type) *Literal {
|
|||||||
export var Bad, True, False, Nil *Literal;
|
export var Bad, True, False, Nil *Literal;
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Objects
|
||||||
|
|
||||||
// NOTE We could use Globals.Object directly if we'd added a typ()
|
// NOTE We could use Globals.Object directly if we'd added a typ()
|
||||||
// method to its interface. However, this would require renaming the
|
// method to its interface. However, this would require renaming the
|
||||||
// typ field everywhere... - Need to think about accessors again.
|
// typ field everywhere... - Need to think about accessors again.
|
||||||
@ -52,24 +64,43 @@ export type Object struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (x *Object) pos() int {
|
func (x *Object) op() int { return OBJECT; }
|
||||||
return x.pos_;
|
func (x *Object) pos() int { return x.pos_; }
|
||||||
}
|
func (x *Object) typ() *Globals.Type { return x.obj.typ; }
|
||||||
|
|
||||||
|
|
||||||
func (x *Object) typ() *Globals.Type {
|
|
||||||
return x.obj.typ;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export func NewObject(pos int, obj* Globals.Object) *Object {
|
export func NewObject(pos int, obj* Globals.Object) *Object {
|
||||||
x := new(*Object);
|
x := new(Object);
|
||||||
x.pos_ = pos;
|
x.pos_ = pos;
|
||||||
x.obj = obj;
|
x.obj = obj;
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Derefs
|
||||||
|
|
||||||
|
// TODO model Deref as unary operation?
|
||||||
|
export type Deref struct {
|
||||||
|
ptr_ Globals.Expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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?
|
// TODO model Selector as binary operation?
|
||||||
export type Selector struct {
|
export type Selector struct {
|
||||||
pos_ int;
|
pos_ int;
|
||||||
@ -77,39 +108,87 @@ export type Selector struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (x *Selector) pos() int {
|
func (x *Selector) op() int { return SELECT; }
|
||||||
return x.pos_;
|
func (x *Selector) pos() int { return x.pos_; }
|
||||||
}
|
func (x *Selector) typ() *Globals.Type { return x.typ_; }
|
||||||
|
|
||||||
|
|
||||||
func (x *Selector) typ() *Globals.Type {
|
|
||||||
return x.typ_;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export func NewSelector(pos int, typ *Globals.Type) *Selector {
|
export func NewSelector(pos int, typ *Globals.Type) *Selector {
|
||||||
x := new(*Selector);
|
x := new(Selector);
|
||||||
x.pos_ = pos;
|
x.pos_ = pos;
|
||||||
x.typ_ = typ;
|
x.typ_ = typ;
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Calls
|
||||||
|
|
||||||
|
export type Call struct {
|
||||||
|
recv, callee Globals.Expr;
|
||||||
|
args *Globals.List;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 {
|
export type BinaryExpr struct {
|
||||||
|
op_ int;
|
||||||
pos_ int;
|
pos_ int;
|
||||||
typ_ *Globals.Type;
|
typ_ *Globals.Type;
|
||||||
op int;
|
|
||||||
x, y Globals.Expr;
|
x, y Globals.Expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (x *BinaryExpr) pos() int {
|
func (x *BinaryExpr) op() int { return x.op_; }
|
||||||
return x.pos_;
|
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 {
|
func (x *Tuple) op() int { return TUPLE; }
|
||||||
return x.typ_;
|
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"
|
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) {
|
func ReadImport(comp* Globals.Compilation, filename string, update bool) (data string, ok bool) {
|
||||||
if filename == "" {
|
if filename == "" {
|
||||||
panic("illegal package file name");
|
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) {
|
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);
|
src, ok := Platform.ReadSourceFile(src_file);
|
||||||
if !ok {
|
if !ok {
|
||||||
print("cannot open ", src_file, "\n");
|
print("cannot open ", src_file, "\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
comp.src_file = src_file;
|
||||||
|
comp.src = src;
|
||||||
|
|
||||||
if comp.flags.verbosity > 0 {
|
if comp.flags.verbosity > 0 {
|
||||||
print(src_file, "\n");
|
print(src_file, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
scanner := new(*Scanner.Scanner);
|
scanner := new(Scanner.Scanner);
|
||||||
scanner.Open(src_file, src);
|
scanner.Open(src_file, src);
|
||||||
|
|
||||||
var tstream chan *Scanner.Token;
|
var tstream chan *Scanner.Token;
|
||||||
if comp.flags.token_chan {
|
if comp.flags.token_chan {
|
||||||
tstream = new(chan *Scanner.Token, 100);
|
tstream = make(chan *Scanner.Token, 100);
|
||||||
go scanner.Server(tstream);
|
go scanner.Server(tstream);
|
||||||
}
|
}
|
||||||
|
|
||||||
parser := new(*Parser.Parser);
|
parser := new(Parser.Parser);
|
||||||
parser.Open(comp, scanner, tstream);
|
parser.Open(comp, scanner, tstream);
|
||||||
|
|
||||||
parser.ParseProgram();
|
parser.ParseProgram();
|
||||||
if parser.S.nerrors > 0 {
|
if parser.scanner.nerrors > 0 {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if !comp.flags.ast {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,6 +174,9 @@ func (E *Exporter) WriteType(typ *Globals.Type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch typ.form {
|
switch typ.form {
|
||||||
|
case Type.VOID:
|
||||||
|
// for now until we have enough of the front-end working.
|
||||||
|
|
||||||
case Type.FORWARD:
|
case Type.FORWARD:
|
||||||
// corresponding package must be forward-declared too
|
// corresponding package must be forward-declared too
|
||||||
if typ.obj == nil || E.comp.pkg_list[typ.obj.pnolev].key != "" {
|
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:
|
case Type.ALIAS, Type.MAP:
|
||||||
E.WriteType(typ.aux);
|
E.WriteType(typ.key);
|
||||||
|
E.WriteType(typ.elt);
|
||||||
|
|
||||||
|
case Type.TUPLE:
|
||||||
E.WriteType(typ.elt);
|
E.WriteType(typ.elt);
|
||||||
|
|
||||||
case Type.ARRAY:
|
case Type.ARRAY:
|
||||||
E.WriteInt(typ.len_);
|
E.WriteInt(typ.len);
|
||||||
E.WriteType(typ.elt);
|
E.WriteType(typ.elt);
|
||||||
|
|
||||||
case Type.CHANNEL:
|
case Type.CHANNEL:
|
||||||
E.WriteInt(typ.flags);
|
E.WriteInt(typ.aux);
|
||||||
E.WriteType(typ.elt);
|
E.WriteType(typ.elt);
|
||||||
|
|
||||||
case Type.FUNCTION:
|
case Type.FUNCTION, Type.METHOD:
|
||||||
E.WriteInt(typ.flags);
|
E.WriteInt(typ.len);
|
||||||
|
E.WriteType(typ.elt);
|
||||||
E.WriteScope(typ.scope);
|
E.WriteScope(typ.scope);
|
||||||
|
|
||||||
case Type.STRUCT, Type.INTERFACE:
|
case Type.STRUCT, Type.INTERFACE:
|
||||||
E.WriteScope(typ.scope);
|
E.WriteScope(typ.scope);
|
||||||
|
|
||||||
case Type.POINTER, Type.REFERENCE:
|
case Type.POINTER:
|
||||||
E.WriteType(typ.elt);
|
E.WriteType(typ.elt);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -266,7 +273,7 @@ func (E *Exporter) Export(comp* Globals.Compilation) string {
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
E.type_ref = Universe.types.len_;
|
E.type_ref = Universe.types.len;
|
||||||
|
|
||||||
// export package 0
|
// export package 0
|
||||||
pkg := comp.pkg_list[0];
|
pkg := comp.pkg_list[0];
|
||||||
|
164
usr/gri/gosrc/expr.go
Executable file
164
usr/gri/gosrc/expr.go
Executable file
@ -0,0 +1,164 @@
|
|||||||
|
// 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 Elem struct
|
||||||
type Compilation 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 {
|
export type Object struct {
|
||||||
exported bool;
|
exported bool;
|
||||||
pos int; // source position (< 0 if unknown position)
|
pos int; // source position (< 0 if unknown position)
|
||||||
@ -31,12 +36,12 @@ export type Object struct {
|
|||||||
export type Type struct {
|
export type Type struct {
|
||||||
ref int; // for exporting only: >= 0 means already exported
|
ref int; // for exporting only: >= 0 means already exported
|
||||||
form int;
|
form int;
|
||||||
flags int; // channels, functions
|
|
||||||
size int; // in bytes
|
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
|
obj *Object; // primary type object or NULL
|
||||||
aux *Type; // alias base type or map key
|
key *Type; // alias base type or map key
|
||||||
elt *Type; // aliases, arrays, maps, channels, pointers
|
elt *Type; // aliased type, array, map, channel or pointer element type, function result type, tuple function type
|
||||||
scope *Scope; // forwards, structs, interfaces, functions
|
scope *Scope; // forwards, structs, interfaces, functions
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +56,7 @@ export type Package struct {
|
|||||||
|
|
||||||
|
|
||||||
export type List struct {
|
export type List struct {
|
||||||
len_ int;
|
len int;
|
||||||
first, last *Elem;
|
first, last *Elem;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -70,17 +75,12 @@ export type Flags struct {
|
|||||||
print_interface bool;
|
print_interface bool;
|
||||||
verbosity uint;
|
verbosity uint;
|
||||||
sixg bool;
|
sixg bool;
|
||||||
|
|
||||||
scan bool;
|
|
||||||
parse bool;
|
|
||||||
ast bool;
|
|
||||||
deps bool;
|
|
||||||
token_chan bool;
|
token_chan bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export type Environment struct {
|
export type Environment struct {
|
||||||
Error *(comp *Compilation); // TODO complete this
|
Error *(comp *Compilation, pos int, msg string);
|
||||||
Import *(comp *Compilation, pkg_file string) *Package;
|
Import *(comp *Compilation, pkg_file string) *Package;
|
||||||
Export *(comp *Compilation, pkg_file string);
|
Export *(comp *Compilation, pkg_file string);
|
||||||
Compile *(comp *Compilation, src_file string);
|
Compile *(comp *Compilation, src_file string);
|
||||||
@ -92,6 +92,14 @@ export type Compilation struct {
|
|||||||
flags *Flags;
|
flags *Flags;
|
||||||
env *Environment;
|
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
|
// TODO use open arrays eventually
|
||||||
pkg_list [256] *Package; // pkg_list[0] is the current package
|
pkg_list [256] *Package; // pkg_list[0] is the current package
|
||||||
pkg_ref int;
|
pkg_ref int;
|
||||||
@ -99,6 +107,7 @@ export type Compilation struct {
|
|||||||
|
|
||||||
|
|
||||||
export type Expr interface {
|
export type Expr interface {
|
||||||
|
op() int; // node operation
|
||||||
pos() int; // source position
|
pos() int; // source position
|
||||||
typ() *Type;
|
typ() *Type;
|
||||||
// ... more to come here
|
// ... 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.
|
// TODO This is hideous! We need to have a decent way to do lists.
|
||||||
// Ideally open arrays that allow '+'.
|
// Ideally open arrays that allow '+'.
|
||||||
|
|
||||||
type Elem struct {
|
export type Elem struct {
|
||||||
next *Elem;
|
next *Elem;
|
||||||
val int;
|
val int;
|
||||||
str string;
|
str string;
|
||||||
@ -129,7 +138,7 @@ type Elem struct {
|
|||||||
export var Universe_void_t *Type // initialized by Universe to Universe.void_t
|
export var Universe_void_t *Type // initialized by Universe to Universe.void_t
|
||||||
|
|
||||||
export func NewObject(pos, kind int, ident string) *Object {
|
export func NewObject(pos, kind int, ident string) *Object {
|
||||||
obj := new(*Object);
|
obj := new(Object);
|
||||||
obj.exported = false;
|
obj.exported = false;
|
||||||
obj.pos = pos;
|
obj.pos = pos;
|
||||||
obj.kind = kind;
|
obj.kind = kind;
|
||||||
@ -141,7 +150,7 @@ export func NewObject(pos, kind int, ident string) *Object {
|
|||||||
|
|
||||||
|
|
||||||
export func NewType(form int) *Type {
|
export func NewType(form int) *Type {
|
||||||
typ := new(*Type);
|
typ := new(Type);
|
||||||
typ.ref = -1; // not yet exported
|
typ.ref = -1; // not yet exported
|
||||||
typ.form = form;
|
typ.form = form;
|
||||||
return typ;
|
return typ;
|
||||||
@ -149,7 +158,7 @@ export func NewType(form int) *Type {
|
|||||||
|
|
||||||
|
|
||||||
export func NewPackage(file_name string, obj *Object, scope *Scope) *Package {
|
export func NewPackage(file_name string, obj *Object, scope *Scope) *Package {
|
||||||
pkg := new(*Package);
|
pkg := new(Package);
|
||||||
pkg.ref = -1; // not yet exported
|
pkg.ref = -1; // not yet exported
|
||||||
pkg.file_name = file_name;
|
pkg.file_name = file_name;
|
||||||
pkg.key = "<the package key>"; // empty key means package forward declaration
|
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 {
|
export func NewList() *List {
|
||||||
return new(*List);
|
return new(List);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export func NewScope(parent *Scope) *Scope {
|
export func NewScope(parent *Scope) *Scope {
|
||||||
scope := new(*Scope);
|
scope := new(Scope);
|
||||||
scope.parent = parent;
|
scope.parent = parent;
|
||||||
scope.entries = NewList();
|
scope.entries = NewList();
|
||||||
return scope;
|
return scope;
|
||||||
@ -176,7 +185,7 @@ export func NewScope(parent *Scope) *Scope {
|
|||||||
// Object methods
|
// Object methods
|
||||||
|
|
||||||
func (obj *Object) Copy() *Object {
|
func (obj *Object) Copy() *Object {
|
||||||
copy := new(*Object);
|
copy := new(Object);
|
||||||
copy.exported = obj.exported;
|
copy.exported = obj.exported;
|
||||||
copy.pos = obj.pos;
|
copy.pos = obj.pos;
|
||||||
copy.kind = obj.kind;
|
copy.kind = obj.kind;
|
||||||
@ -191,7 +200,7 @@ func (obj *Object) Copy() *Object {
|
|||||||
// List methods
|
// List methods
|
||||||
|
|
||||||
func (L *List) at(i int) *Elem {
|
func (L *List) at(i int) *Elem {
|
||||||
if i < 0 || L.len_ <= i {
|
if i < 0 || L.len <= i {
|
||||||
panic("index out of bounds");
|
panic("index out of bounds");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,13 +214,13 @@ func (L *List) at(i int) *Elem {
|
|||||||
|
|
||||||
|
|
||||||
func (L *List) Clear() {
|
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 {
|
func (L *List) Add() *Elem {
|
||||||
L.len_++;
|
L.len++;
|
||||||
e := new(*Elem);
|
e := new(Elem);
|
||||||
if L.first == nil {
|
if L.first == nil {
|
||||||
L.first = e;
|
L.first = e;
|
||||||
} else {
|
} 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) {
|
func (L *List) AddInt(val int) {
|
||||||
L.Add().val = val;
|
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) {
|
func (scope *Scope) Insert(obj *Object) {
|
||||||
if scope.Lookup(obj.ident) != nil {
|
if scope.Lookup(obj.ident) != nil {
|
||||||
panic("obj already inserted");
|
panic("obj already inserted");
|
||||||
}
|
}
|
||||||
scope.entries.AddObj(obj);
|
scope.Add(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (scope *Scope) InsertImport(obj *Object) *Object {
|
func (scope *Scope) InsertImport(obj *Object) *Object {
|
||||||
p := scope.Lookup(obj.ident);
|
p := scope.Lookup(obj.ident);
|
||||||
if p == nil {
|
if p == nil {
|
||||||
scope.Insert(obj);
|
scope.Add(obj);
|
||||||
p = obj;
|
p = obj;
|
||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
|
@ -20,10 +20,6 @@ func PrintHelp() {
|
|||||||
" -p print package interface\n" +
|
" -p print package interface\n" +
|
||||||
" -v [0 .. 3] verbosity level\n" +
|
" -v [0 .. 3] verbosity level\n" +
|
||||||
" -6g 6g compatibility mode\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"
|
" -token_chan use token channel to scan and parse in parallel\n"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -49,7 +45,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// collect flags and files
|
// collect flags and files
|
||||||
flags := new(*Globals.Flags);
|
flags := new(Globals.Flags);
|
||||||
files := Globals.NewList();
|
files := Globals.NewList();
|
||||||
for arg != "" {
|
for arg != "" {
|
||||||
switch arg {
|
switch arg {
|
||||||
@ -69,13 +65,6 @@ func main() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case "-6g": flags.sixg = true;
|
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;
|
case "-token_chan": flags.token_chan = true;
|
||||||
default: files.AddStr(arg);
|
default: files.AddStr(arg);
|
||||||
}
|
}
|
||||||
@ -83,7 +72,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// setup environment
|
// setup environment
|
||||||
env := new(*Globals.Environment);
|
env := new(Globals.Environment);
|
||||||
|
env.Error = &Compilation.Error;
|
||||||
env.Import = &Compilation.Import;
|
env.Import = &Compilation.Import;
|
||||||
env.Export = &Compilation.Export;
|
env.Export = &Compilation.Export;
|
||||||
env.Compile = &Compilation.Compile;
|
env.Compile = &Compilation.Compile;
|
||||||
@ -91,7 +81,7 @@ func main() {
|
|||||||
// compile files
|
// compile files
|
||||||
for p := files.first; p != nil; p = p.next {
|
for p := files.first; p != nil; p = p.next {
|
||||||
// setup compilation
|
// setup compilation
|
||||||
comp := new(*Globals.Compilation);
|
comp := new(Globals.Compilation);
|
||||||
comp.flags = flags;
|
comp.flags = flags;
|
||||||
comp.env = env;
|
comp.env = env;
|
||||||
|
|
||||||
|
@ -200,24 +200,32 @@ func (I *Importer) ReadType() *Globals.Type {
|
|||||||
I.type_ref++;
|
I.type_ref++;
|
||||||
|
|
||||||
switch (typ.form) {
|
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:
|
case Type.FORWARD:
|
||||||
typ.scope = Globals.NewScope(nil);
|
typ.scope = Globals.NewScope(nil);
|
||||||
break;
|
|
||||||
|
case Type.TUPLE:
|
||||||
|
typ.elt = I.ReadType();
|
||||||
|
|
||||||
case Type.ALIAS, Type.MAP:
|
case Type.ALIAS, Type.MAP:
|
||||||
typ.aux = I.ReadType();
|
typ.key = I.ReadType();
|
||||||
typ.elt = I.ReadType();
|
typ.elt = I.ReadType();
|
||||||
|
|
||||||
case Type.ARRAY:
|
case Type.ARRAY:
|
||||||
typ.len_ = I.ReadInt();
|
typ.len = I.ReadInt();
|
||||||
typ.elt = I.ReadType();
|
typ.elt = I.ReadType();
|
||||||
|
|
||||||
case Type.CHANNEL:
|
case Type.CHANNEL:
|
||||||
typ.flags = I.ReadInt();
|
typ.aux = I.ReadInt();
|
||||||
typ.elt = I.ReadType();
|
typ.elt = I.ReadType();
|
||||||
|
|
||||||
case Type.FUNCTION:
|
case Type.FUNCTION, Type.METHOD:
|
||||||
typ.flags = I.ReadInt();
|
typ.len = I.ReadInt();
|
||||||
|
typ.elt = I.ReadType();
|
||||||
typ.scope = Globals.NewScope(nil);
|
typ.scope = Globals.NewScope(nil);
|
||||||
I.ReadScope(typ.scope, false);
|
I.ReadScope(typ.scope, false);
|
||||||
|
|
||||||
@ -225,7 +233,7 @@ func (I *Importer) ReadType() *Globals.Type {
|
|||||||
typ.scope = Globals.NewScope(nil);
|
typ.scope = Globals.NewScope(nil);
|
||||||
I.ReadScope(typ.scope, false);
|
I.ReadScope(typ.scope, false);
|
||||||
|
|
||||||
case Type.POINTER, Type.REFERENCE:
|
case Type.POINTER:
|
||||||
typ.elt = I.ReadType();
|
typ.elt = I.ReadType();
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -9,7 +9,7 @@ import Globals "globals"
|
|||||||
|
|
||||||
export const /* kind */ (
|
export const /* kind */ (
|
||||||
BAD = iota; // error handling
|
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)
|
END; // end of scope (import/export only)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -27,6 +27,7 @@ export func KindStr(kind int) string {
|
|||||||
case VAR: return "VAR";
|
case VAR: return "VAR";
|
||||||
case FIELD: return "FIELD";
|
case FIELD: return "FIELD";
|
||||||
case FUNC: return "FUNC";
|
case FUNC: return "FUNC";
|
||||||
|
case BUILTIN: return "BUILTIN";
|
||||||
case PACKAGE: return "PACKAGE";
|
case PACKAGE: return "PACKAGE";
|
||||||
case LABEL: return "LABEL";
|
case LABEL: return "LABEL";
|
||||||
case END: return "END";
|
case END: return "END";
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -40,7 +40,7 @@ func IsAnonymous(name string) bool {
|
|||||||
func (P *Printer) PrintSigRange(typ *Globals.Type, a, b int) {
|
func (P *Printer) PrintSigRange(typ *Globals.Type, a, b int) {
|
||||||
scope := typ.scope;
|
scope := typ.scope;
|
||||||
if a + 1 == b && IsAnonymous(scope.entries.ObjAt(a).ident) {
|
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 {
|
} else {
|
||||||
print("(");
|
print("(");
|
||||||
for i := a; i < b; i++ {
|
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) {
|
func (P *Printer) PrintSignature(typ *Globals.Type, fun *Globals.Object) {
|
||||||
if typ.form != Type.FUNCTION {
|
|
||||||
panic("typ.form != Type.FUNCTION");
|
|
||||||
}
|
|
||||||
|
|
||||||
p0 := 0;
|
p0 := 0;
|
||||||
if typ.flags & Type.RECV != 0 {
|
if typ.form == Type.METHOD {
|
||||||
p0 = 1;
|
p0 = 1;
|
||||||
|
} else {
|
||||||
|
if typ.form != Type.FUNCTION {
|
||||||
|
panic("not a function or method");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
r0 := p0 + typ.len_;
|
r0 := p0 + typ.len;
|
||||||
l0 := typ.scope.entries.len_;
|
l0 := typ.scope.entries.len;
|
||||||
|
|
||||||
if P.level == 0 {
|
if P.level == 0 {
|
||||||
print("func ");
|
print("func ");
|
||||||
@ -105,7 +105,7 @@ func (P *Printer) PrintScope(scope *Globals.Scope, delta int) {
|
|||||||
// determine the number of scope entries to print
|
// determine the number of scope entries to print
|
||||||
var n int;
|
var n int;
|
||||||
if P.print_all {
|
if P.print_all {
|
||||||
n = scope.entries.len_;
|
n = scope.entries.len;
|
||||||
} else {
|
} else {
|
||||||
n = 0;
|
n = 0;
|
||||||
for p := scope.entries.first; p != nil; p = p.next {
|
for p := scope.entries.first; p != nil; p = p.next {
|
||||||
@ -134,8 +134,8 @@ func (P *Printer) PrintScope(scope *Globals.Scope, delta int) {
|
|||||||
func (P *Printer) PrintObjectStruct(obj *Globals.Object) {
|
func (P *Printer) PrintObjectStruct(obj *Globals.Object) {
|
||||||
switch obj.kind {
|
switch obj.kind {
|
||||||
case Object.BAD:
|
case Object.BAD:
|
||||||
print("bad ");
|
|
||||||
P.PrintObject(obj);
|
P.PrintObject(obj);
|
||||||
|
print(" /* bad */");
|
||||||
|
|
||||||
case Object.CONST:
|
case Object.CONST:
|
||||||
print("const ");
|
print("const ");
|
||||||
@ -149,10 +149,11 @@ func (P *Printer) PrintObjectStruct(obj *Globals.Object) {
|
|||||||
print(" ");
|
print(" ");
|
||||||
P.PrintTypeStruct(obj.typ);
|
P.PrintTypeStruct(obj.typ);
|
||||||
|
|
||||||
case Object.VAR, Object.FIELD:
|
case Object.VAR:
|
||||||
if P.level == 0 {
|
print("var ");
|
||||||
print("var ");
|
fallthrough;
|
||||||
}
|
|
||||||
|
case Object.FIELD:
|
||||||
P.PrintObject(obj);
|
P.PrintObject(obj);
|
||||||
print(" ");
|
print(" ");
|
||||||
P.PrintType(obj.typ);
|
P.PrintType(obj.typ);
|
||||||
@ -160,6 +161,10 @@ func (P *Printer) PrintObjectStruct(obj *Globals.Object) {
|
|||||||
case Object.FUNC:
|
case Object.FUNC:
|
||||||
P.PrintSignature(obj.typ, obj);
|
P.PrintSignature(obj.typ, obj);
|
||||||
|
|
||||||
|
case Object.BUILTIN:
|
||||||
|
P.PrintObject(obj);
|
||||||
|
print(" /* builtin */");
|
||||||
|
|
||||||
case Object.PACKAGE:
|
case Object.PACKAGE:
|
||||||
print("package ");
|
print("package ");
|
||||||
P.PrintObject(obj);
|
P.PrintObject(obj);
|
||||||
@ -197,11 +202,14 @@ func (P *Printer) PrintTypeStruct(typ *Globals.Type) {
|
|||||||
case Type.VOID:
|
case Type.VOID:
|
||||||
print("void");
|
print("void");
|
||||||
|
|
||||||
|
case Type.BAD:
|
||||||
|
print("<bad type>");
|
||||||
|
|
||||||
case Type.FORWARD:
|
case Type.FORWARD:
|
||||||
print("<forward type>");
|
print("<forward type>");
|
||||||
|
|
||||||
case Type.BAD:
|
case Type.TUPLE:
|
||||||
print("<bad type>");
|
print("<tuple type>");
|
||||||
|
|
||||||
case Type.NIL, Type.BOOL, Type.UINT, Type.INT, Type.FLOAT, Type.STRING, Type.ANY:
|
case Type.NIL, Type.BOOL, Type.UINT, Type.INT, Type.FLOAT, Type.STRING, Type.ANY:
|
||||||
if typ.obj == nil {
|
if typ.obj == nil {
|
||||||
@ -211,9 +219,9 @@ func (P *Printer) PrintTypeStruct(typ *Globals.Type) {
|
|||||||
|
|
||||||
case Type.ALIAS:
|
case Type.ALIAS:
|
||||||
P.PrintType(typ.elt);
|
P.PrintType(typ.elt);
|
||||||
if typ.aux != typ.elt {
|
if typ.key != typ.elt {
|
||||||
print(" /* ");
|
print(" /* ");
|
||||||
P.PrintType(typ.aux);
|
P.PrintType(typ.key);
|
||||||
print(" */");
|
print(" */");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,12 +241,12 @@ func (P *Printer) PrintTypeStruct(typ *Globals.Type) {
|
|||||||
|
|
||||||
case Type.MAP:
|
case Type.MAP:
|
||||||
print("map [");
|
print("map [");
|
||||||
P.PrintType(typ.aux);
|
P.PrintType(typ.key);
|
||||||
print("] ");
|
print("] ");
|
||||||
P.PrintType(typ.elt);
|
P.PrintType(typ.elt);
|
||||||
|
|
||||||
case Type.CHANNEL:
|
case Type.CHANNEL:
|
||||||
switch typ.flags {
|
switch typ.aux {
|
||||||
case Type.SEND: print("chan <- ");
|
case Type.SEND: print("chan <- ");
|
||||||
case Type.RECV: print("<- chan ");
|
case Type.RECV: print("<- chan ");
|
||||||
case Type.SEND + Type.RECV: print("chan ");
|
case Type.SEND + Type.RECV: print("chan ");
|
||||||
@ -253,10 +261,6 @@ func (P *Printer) PrintTypeStruct(typ *Globals.Type) {
|
|||||||
print("*");
|
print("*");
|
||||||
P.PrintType(typ.elt);
|
P.PrintType(typ.elt);
|
||||||
|
|
||||||
case Type.REFERENCE:
|
|
||||||
print("&");
|
|
||||||
P.PrintType(typ.elt);
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
panic("UNREACHABLE");
|
panic("UNREACHABLE");
|
||||||
|
|
||||||
|
@ -86,7 +86,6 @@ export const (
|
|||||||
ELSE;
|
ELSE;
|
||||||
EXPORT;
|
EXPORT;
|
||||||
FALLTHROUGH;
|
FALLTHROUGH;
|
||||||
FALSE;
|
|
||||||
FOR;
|
FOR;
|
||||||
FUNC;
|
FUNC;
|
||||||
GO;
|
GO;
|
||||||
@ -94,17 +93,13 @@ export const (
|
|||||||
IF;
|
IF;
|
||||||
IMPORT;
|
IMPORT;
|
||||||
INTERFACE;
|
INTERFACE;
|
||||||
IOTA;
|
|
||||||
MAP;
|
MAP;
|
||||||
NEW;
|
|
||||||
NIL;
|
|
||||||
PACKAGE;
|
PACKAGE;
|
||||||
RANGE;
|
RANGE;
|
||||||
RETURN;
|
RETURN;
|
||||||
SELECT;
|
SELECT;
|
||||||
STRUCT;
|
STRUCT;
|
||||||
SWITCH;
|
SWITCH;
|
||||||
TRUE;
|
|
||||||
TYPE;
|
TYPE;
|
||||||
VAR;
|
VAR;
|
||||||
KEYWORDS_END;
|
KEYWORDS_END;
|
||||||
@ -191,7 +186,6 @@ export func TokenName(tok int) string {
|
|||||||
case ELSE: return "else";
|
case ELSE: return "else";
|
||||||
case EXPORT: return "export";
|
case EXPORT: return "export";
|
||||||
case FALLTHROUGH: return "fallthrough";
|
case FALLTHROUGH: return "fallthrough";
|
||||||
case FALSE: return "false";
|
|
||||||
case FOR: return "for";
|
case FOR: return "for";
|
||||||
case FUNC: return "func";
|
case FUNC: return "func";
|
||||||
case GO: return "go";
|
case GO: return "go";
|
||||||
@ -199,17 +193,13 @@ export func TokenName(tok int) string {
|
|||||||
case IF: return "if";
|
case IF: return "if";
|
||||||
case IMPORT: return "import";
|
case IMPORT: return "import";
|
||||||
case INTERFACE: return "interface";
|
case INTERFACE: return "interface";
|
||||||
case IOTA: return "iota";
|
|
||||||
case MAP: return "map";
|
case MAP: return "map";
|
||||||
case NEW: return "new";
|
|
||||||
case NIL: return "nil";
|
|
||||||
case PACKAGE: return "package";
|
case PACKAGE: return "package";
|
||||||
case RANGE: return "range";
|
case RANGE: return "range";
|
||||||
case RETURN: return "return";
|
case RETURN: return "return";
|
||||||
case SELECT: return "select";
|
case SELECT: return "select";
|
||||||
case STRUCT: return "struct";
|
case STRUCT: return "struct";
|
||||||
case SWITCH: return "switch";
|
case SWITCH: return "switch";
|
||||||
case TRUE: return "true";
|
|
||||||
case TYPE: return "type";
|
case TYPE: return "type";
|
||||||
case VAR: return "var";
|
case VAR: return "var";
|
||||||
}
|
}
|
||||||
@ -219,7 +209,7 @@ export func TokenName(tok int) string {
|
|||||||
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Keywords = new(map [string] int);
|
Keywords = make(map [string] int);
|
||||||
|
|
||||||
for i := KEYWORDS_BEG; i <= KEYWORDS_END; i++ {
|
for i := KEYWORDS_BEG; i <= KEYWORDS_END; i++ {
|
||||||
Keywords[TokenName(i)] = 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
|
// TODO: fix this routine
|
||||||
|
|
||||||
ch := S.ch;
|
ch := S.ch;
|
||||||
pos := S.chpos;
|
pos := S.chpos;
|
||||||
S.Next();
|
S.Next();
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', '\'', '"':
|
case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\':
|
||||||
return string(ch);
|
return string(ch);
|
||||||
|
|
||||||
case '0', '1', '2', '3', '4', '5', '6', '7':
|
case '0', '1', '2', '3', '4', '5', '6', '7':
|
||||||
@ -605,6 +595,10 @@ func (S *Scanner) ScanEscape() string {
|
|||||||
return ""; // TODO fix this
|
return ""; // TODO fix this
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
// check for quote outside the switch for better generated code (eventually)
|
||||||
|
if ch == quote {
|
||||||
|
return string(quote);
|
||||||
|
}
|
||||||
S.Error(pos, "illegal char escape");
|
S.Error(pos, "illegal char escape");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -619,7 +613,7 @@ func (S *Scanner) ScanChar() string {
|
|||||||
ch := S.ch;
|
ch := S.ch;
|
||||||
S.Next();
|
S.Next();
|
||||||
if ch == '\\' {
|
if ch == '\\' {
|
||||||
S.ScanEscape();
|
S.ScanEscape('\'');
|
||||||
}
|
}
|
||||||
|
|
||||||
S.Expect('\'');
|
S.Expect('\'');
|
||||||
@ -639,7 +633,7 @@ func (S *Scanner) ScanString() string {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ch == '\\' {
|
if ch == '\\' {
|
||||||
S.ScanEscape();
|
S.ScanEscape('"');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -781,7 +775,7 @@ export type Token struct {
|
|||||||
|
|
||||||
func (S *Scanner) Server(c chan *Token) {
|
func (S *Scanner) Server(c chan *Token) {
|
||||||
for {
|
for {
|
||||||
t := new(*Token);
|
t := new(Token);
|
||||||
t.tok, t.pos, t.val = S.Scan();
|
t.tok, t.pos, t.val = S.Scan();
|
||||||
c <- t;
|
c <- t;
|
||||||
if t.tok == EOF {
|
if t.tok == EOF {
|
||||||
|
@ -8,7 +8,7 @@ import Scanner "scanner"
|
|||||||
|
|
||||||
|
|
||||||
func Scan1(filename, src string) {
|
func Scan1(filename, src string) {
|
||||||
S := new(*Scanner.Scanner);
|
S := new(Scanner.Scanner);
|
||||||
S.Open(filename, src);
|
S.Open(filename, src);
|
||||||
for {
|
for {
|
||||||
tok, pos, val := S.Scan();
|
tok, pos, val := S.Scan();
|
||||||
@ -25,9 +25,9 @@ func Scan1(filename, src string) {
|
|||||||
|
|
||||||
|
|
||||||
func Scan2(filename, src string) {
|
func Scan2(filename, src string) {
|
||||||
S := new(*Scanner.Scanner);
|
S := new(Scanner.Scanner);
|
||||||
S.Open(filename, src);
|
S.Open(filename, src);
|
||||||
c := new(chan *Scanner.Token, 32);
|
c := make(chan *Scanner.Token, 32);
|
||||||
go S.Server(c);
|
go S.Server(c);
|
||||||
for {
|
for {
|
||||||
var t *Scanner.Token;
|
var t *Scanner.Token;
|
||||||
|
@ -10,8 +10,12 @@ import Object "object"
|
|||||||
|
|
||||||
export const /* form */ (
|
export const /* form */ (
|
||||||
// internal types
|
// internal types
|
||||||
// VOID types are used when we don't have a type.
|
// We should never see one of these.
|
||||||
VOID = iota;
|
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.
|
// 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
|
// They are introduced only as a result of an error in the source code. A
|
||||||
@ -23,6 +27,10 @@ export const /* form */ (
|
|||||||
// their internals are accessible.
|
// their internals are accessible.
|
||||||
FORWARD;
|
FORWARD;
|
||||||
|
|
||||||
|
// TUPLE types represent multi-valued result types of functions and
|
||||||
|
// methods.
|
||||||
|
TUPLE;
|
||||||
|
|
||||||
// The type of nil.
|
// The type of nil.
|
||||||
NIL;
|
NIL;
|
||||||
|
|
||||||
@ -33,13 +41,13 @@ export const /* form */ (
|
|||||||
ANY;
|
ANY;
|
||||||
|
|
||||||
// composite types
|
// composite types
|
||||||
ALIAS; ARRAY; STRUCT; INTERFACE; MAP; CHANNEL; FUNCTION; POINTER; REFERENCE;
|
ALIAS; ARRAY; STRUCT; INTERFACE; MAP; CHANNEL; FUNCTION; METHOD; POINTER;
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
export const /* flag */ (
|
export const /* Type.aux */ (
|
||||||
SEND = 1 << iota; // chan>
|
SEND = 1; // chan>
|
||||||
RECV; // chan< or method
|
RECV = 2; // chan<
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -53,6 +61,7 @@ export func FormStr(form int) string {
|
|||||||
case VOID: return "VOID";
|
case VOID: return "VOID";
|
||||||
case BAD: return "BAD";
|
case BAD: return "BAD";
|
||||||
case FORWARD: return "FORWARD";
|
case FORWARD: return "FORWARD";
|
||||||
|
case TUPLE: return "TUPLE";
|
||||||
case NIL: return "NIL";
|
case NIL: return "NIL";
|
||||||
case BOOL: return "BOOL";
|
case BOOL: return "BOOL";
|
||||||
case UINT: return "UINT";
|
case UINT: return "UINT";
|
||||||
@ -67,8 +76,8 @@ export func FormStr(form int) string {
|
|||||||
case MAP: return "MAP";
|
case MAP: return "MAP";
|
||||||
case CHANNEL: return "CHANNEL";
|
case CHANNEL: return "CHANNEL";
|
||||||
case FUNCTION: return "FUNCTION";
|
case FUNCTION: return "FUNCTION";
|
||||||
|
case METHOD: return "METHOD";
|
||||||
case POINTER: return "POINTER";
|
case POINTER: return "POINTER";
|
||||||
case REFERENCE: return "REFERENCE";
|
|
||||||
}
|
}
|
||||||
return "<unknown Type form>";
|
return "<unknown Type form>";
|
||||||
}
|
}
|
||||||
@ -102,25 +111,24 @@ func Equal0(x, y *Globals.Type) bool {
|
|||||||
|
|
||||||
case ARRAY:
|
case ARRAY:
|
||||||
return
|
return
|
||||||
x.len_ == y.len_ &&
|
x.len == y.len &&
|
||||||
Equal(x.elt, y.elt);
|
Equal(x.elt, y.elt);
|
||||||
|
|
||||||
case MAP:
|
case MAP:
|
||||||
return
|
return
|
||||||
Equal(x.aux, y.aux) &&
|
Equal(x.key, y.key) &&
|
||||||
Equal(x.elt, y.elt);
|
Equal(x.elt, y.elt);
|
||||||
|
|
||||||
case CHANNEL:
|
case CHANNEL:
|
||||||
return
|
return
|
||||||
x.flags == y.flags &&
|
x.aux == y.aux &&
|
||||||
Equal(x.elt, y.elt);
|
Equal(x.elt, y.elt);
|
||||||
|
|
||||||
case FUNCTION:
|
case FUNCTION, METHOD:
|
||||||
{ xp := x.scope.entries;
|
{ xp := x.scope.entries;
|
||||||
yp := x.scope.entries;
|
yp := x.scope.entries;
|
||||||
if x.flags != y.flags && // function or method
|
if x.len != y.len && // number of parameters
|
||||||
x.len_ != y.len_ && // number of parameters
|
xp.len != yp.len // recv + parameters + results
|
||||||
xp.len_ != yp.len_ // recv + parameters + results
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -163,8 +171,12 @@ func Equal0(x, y *Globals.Type) bool {
|
|||||||
panic("UNIMPLEMENTED");
|
panic("UNIMPLEMENTED");
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
case POINTER, REFERENCE:
|
case POINTER:
|
||||||
return Equal(x.elt, y.elt);
|
return Equal(x.elt, y.elt);
|
||||||
|
|
||||||
|
case TUPLE:
|
||||||
|
panic("UNIMPLEMENTED");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
panic("UNREACHABLE");
|
panic("UNREACHABLE");
|
||||||
|
@ -48,7 +48,9 @@ export var (
|
|||||||
ptrint_t *Globals.Type;
|
ptrint_t *Globals.Type;
|
||||||
|
|
||||||
true_,
|
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 {
|
func DeclAlias(ident string, typ *Globals.Type) *Globals.Type {
|
||||||
alias := Globals.NewType(Type.ALIAS);
|
alias := Globals.NewType(Type.ALIAS);
|
||||||
alias.aux = typ;
|
alias.key = typ;
|
||||||
alias.elt = typ;
|
alias.elt = typ;
|
||||||
return DeclObj(Object.TYPE, ident, alias).typ;
|
return DeclObj(Object.TYPE, ident, alias).typ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func Register(typ *Globals.Type) *Globals.Type {
|
func Register(typ *Globals.Type) *Globals.Type {
|
||||||
if types.len_ < 0 {
|
if types.len < 0 {
|
||||||
panic("types.len_ < 0");
|
panic("types.len < 0");
|
||||||
}
|
}
|
||||||
typ.ref = types.len_;
|
typ.ref = types.len;
|
||||||
types.AddTyp(typ);
|
types.AddTyp(typ);
|
||||||
return typ;
|
return typ;
|
||||||
}
|
}
|
||||||
@ -96,7 +98,7 @@ func init() {
|
|||||||
void_t = Globals.NewType(Type.VOID);
|
void_t = Globals.NewType(Type.VOID);
|
||||||
Globals.Universe_void_t = void_t;
|
Globals.Universe_void_t = void_t;
|
||||||
bad_t = Globals.NewType(Type.BAD);
|
bad_t = Globals.NewType(Type.BAD);
|
||||||
nil_t = DeclType(Type.NIL, "nil", 8);
|
nil_t = Globals.NewType(Type.NIL);
|
||||||
|
|
||||||
// Basic types
|
// Basic types
|
||||||
bool_t = Register(DeclType(Type.BOOL, "bool", 1));
|
bool_t = Register(DeclType(Type.BOOL, "bool", 1));
|
||||||
@ -130,9 +132,14 @@ func init() {
|
|||||||
// Predeclared constants
|
// Predeclared constants
|
||||||
true_ = DeclObj(Object.CONST, "true", bool_t);
|
true_ = DeclObj(Object.CONST, "true", bool_t);
|
||||||
false_ = DeclObj(Object.CONST, "false", 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
|
// 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();
|
// scope.Print();
|
||||||
}
|
}
|
||||||
|
@ -46,11 +46,15 @@ func (V *Verifier) VerifyType(typ *Globals.Type) {
|
|||||||
|
|
||||||
switch typ.form {
|
switch typ.form {
|
||||||
case Type.VOID:
|
case Type.VOID:
|
||||||
|
case Type.BAD:
|
||||||
break; // TODO for now - remove eventually
|
break; // TODO for now - remove eventually
|
||||||
|
|
||||||
case Type.FORWARD:
|
case Type.FORWARD:
|
||||||
if typ.scope == nil {
|
if typ.scope == nil {
|
||||||
Error("forward types must have a scope");
|
Error("forward types must have a scope");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case Type.TUPLE:
|
||||||
break;
|
break;
|
||||||
case Type.NIL:
|
case Type.NIL:
|
||||||
break;
|
break;
|
||||||
@ -82,8 +86,6 @@ func (V *Verifier) VerifyType(typ *Globals.Type) {
|
|||||||
break;
|
break;
|
||||||
case Type.POINTER:
|
case Type.POINTER:
|
||||||
break;
|
break;
|
||||||
case Type.REFERENCE:
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
Error("illegal type form " + Type.FormStr(typ.form));
|
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) {
|
func (V *Verifier) Verify(comp *Globals.Compilation) {
|
||||||
// initialize Verifier
|
// initialize Verifier
|
||||||
V.comp = comp;
|
V.comp = comp;
|
||||||
V.objs = new(map[*Globals.Object] bool);
|
V.objs = make(map[*Globals.Object] bool);
|
||||||
V.typs = new(map[*Globals.Type] bool);
|
V.typs = make(map[*Globals.Type] bool);
|
||||||
V.pkgs = new(map[*Globals.Package] bool);
|
V.pkgs = make(map[*Globals.Package] bool);
|
||||||
|
|
||||||
// verify all packages
|
// verify all packages
|
||||||
filenames := new(map[string] bool);
|
filenames := make(map[string] bool);
|
||||||
for i := 0; i < comp.pkg_ref; i++ {
|
for i := 0; i < comp.pkg_ref; i++ {
|
||||||
pkg := comp.pkg_list[i];
|
pkg := comp.pkg_list[i];
|
||||||
// each pkg filename must appear only once
|
// each pkg filename must appear only once
|
||||||
@ -158,6 +160,6 @@ func (V *Verifier) Verify(comp *Globals.Compilation) {
|
|||||||
|
|
||||||
|
|
||||||
export func Verify(comp *Globals.Compilation) {
|
export func Verify(comp *Globals.Compilation) {
|
||||||
V := new(*Verifier);
|
V := new(Verifier);
|
||||||
V.Verify(comp);
|
V.Verify(comp);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user