]> Cypherpunks repositories - gostls13.git/commitdiff
- changed channel operators
authorRobert Griesemer <gri@golang.org>
Thu, 17 Jul 2008 00:00:48 +0000 (17:00 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 17 Jul 2008 00:00:48 +0000 (17:00 -0700)
- more work on packages

SVN=127671

usr/gri/gosrc/compilation.go
usr/gri/gosrc/export.go
usr/gri/gosrc/package.go
usr/gri/gosrc/parser.go
usr/gri/gosrc/scanner.go

index 9a76ae1f17133f1db6ed5dfece16a1ee9ea0c6e1..ac2ac59aff499f763afa8216c1ad47b29942d6ed 100644 (file)
@@ -11,6 +11,7 @@ import Universe "universe"
 import Package "package"
 import Scanner "scanner"
 import Parser "parser"
+import Export "export"
 
 
 export Compilation
@@ -22,20 +23,57 @@ type Compilation struct {
 }
 
 
-func (C *Compilation) Lookup(pkg_name string) *Package.Package {
-       panic "UNIMPLEMENTED";
+func (C *Compilation) Lookup(file_name string) *Package.Package {
+       for i := 0; i < C.nimports; i++ {
+               pkg := C.imports[i];
+               if pkg.file_name == file_name {
+                       return pkg;
+               }
+       }
        return nil;
 }
 
 
 func (C *Compilation) Insert(pkg *Package.Package) {
-       panic "UNIMPLEMENTED";
+       if C.Lookup(pkg.file_name) != nil {
+               panic "package already inserted";
+       }
+       pkg.pno = C.nimports;
+       C.imports[C.nimports] = pkg;
+       C.nimports++;
 }
 
 
 func (C *Compilation) InsertImport(pkg *Package.Package) *Package.Package {
-       panic "UNIMPLEMENTED";
-       return nil;
+       p := C.Lookup(pkg.file_name);
+       if (p == nil) {
+               // no primary package found
+               C.Insert(pkg);
+               p = pkg;
+       }
+       return p;
+}
+
+
+func BaseName(s string) string {
+       // TODO this is not correct for non-ASCII strings!
+       i := len(s);
+       for i >= 0 && s[i] != '/' {
+               if s[i] > 128 {
+                       panic "non-ASCII string"
+               }
+               i--;
+       }
+       return s[i + 1 : len(s)];
+}
+
+
+func FixExt(s string) string {
+       i := len(s) - 3;  // 3 == len(".go");
+       if s[i : len(s)] == ".go" {
+               s = s[0 : i];
+       }
+       return s + ".7"
 }
 
 
@@ -45,7 +83,8 @@ func (C *Compilation) Import(pkg_name string) (pno int) {
 
 
 func (C *Compilation) Export() {
-       panic "UNIMPLEMENTED";
+       file_name := FixExt(BaseName(C.src_name));  // strip src dir
+       Export.Export(file_name/*, C */);
 }
 
 
index 9cc2a030192ad2e9caa28bed61149eeac11b0c97..6a321cafdaab49cf99b09e705a1dfed705a4f8e5 100755 (executable)
@@ -8,6 +8,7 @@ import Globals "globals"
 import Object "object"
 import Type "type"
 import Package "package"
+//import Compilation "compilation"
 
 
 type Exporter struct {
@@ -253,7 +254,7 @@ func (E *Exporter) WritePackage(pkg *Package.Package) {
        E.pkg_ref++;
 
        E.WriteString(pkg.ident);
-       E.WriteString(pkg.path);
+       E.WriteString(pkg.file_name);
        E.WriteString(pkg.key);
 }
 
@@ -294,7 +295,7 @@ func (E *Exporter) Export(/*Compilation* comp, BBuffer* buf*/) {
 
 
 export Export
-func Export(/*Compilation* comp, BBuffer* buf*/) {
+func Export(file_name string /*comp *Compilation.Compilation*/) {
        /*
        Exporter exp;
        exp.Export(comp, buf);
index fa2578b963435e58270af1ace610e9715e4e8d61..bea260b572586f8e53e79bf0063c3089946a76a0 100644 (file)
@@ -9,10 +9,11 @@ import Globals "globals"
 export Package
 type Package struct {
        ref int;
+       file_name string;
        ident string;
-       path string;
        key string;
        scope *Globals.Scope;
+       pno int;
 }
 
 
index dc80cb878726cd1bfa03e686c1b93537604b1d54..f93567a2c63cca34f7bdf668fec1a7d6848cfdde 100644 (file)
@@ -292,8 +292,8 @@ func (P *Parser) ParseChannelType() *Globals.Type {
        P.Trace("ChannelType");
        P.Expect(Scanner.CHAN);
        switch P.tok {
-       case Scanner.LSS: fallthrough
-       case Scanner.GTR:
+       case Scanner.SEND: fallthrough
+       case Scanner.RECV:
                P.Next();
        }
        P.ParseType();
@@ -681,9 +681,8 @@ func (P *Parser) ParseUnaryExpr() {
        case Scanner.SUB: fallthrough;
        case Scanner.NOT: fallthrough;
        case Scanner.XOR: fallthrough;
-       case Scanner.LSS: fallthrough;
-       case Scanner.GTR: fallthrough;
        case Scanner.MUL: fallthrough;
+       case Scanner.RECV: fallthrough;
        case Scanner.AND:
                P.Next();
                P.ParseUnaryExpr();
@@ -702,12 +701,14 @@ func Precedence(tok int) int {
                return 1;
        case Scanner.LAND:
                return 2;
-       case Scanner.EQL, Scanner.NEQ, Scanner.LSS, Scanner.LEQ, Scanner.GTR, Scanner.GEQ:
+       case Scanner.SEND, Scanner.RECV:
                return 3;
-       case Scanner.ADD, Scanner.SUB, Scanner.OR, Scanner.XOR:
+       case Scanner.EQL, Scanner.NEQ, Scanner.LSS, Scanner.LEQ, Scanner.GTR, Scanner.GEQ:
                return 4;
-       case Scanner.MUL, Scanner.QUO, Scanner.REM, Scanner.SHL, Scanner.SHR, Scanner.AND:
+       case Scanner.ADD, Scanner.SUB, Scanner.OR, Scanner.XOR:
                return 5;
+       case Scanner.MUL, Scanner.QUO, Scanner.REM, Scanner.SHL, Scanner.SHR, Scanner.AND:
+               return 6;
        }
        return 0;
 }
index 2ae3031d57a9afdd745c451145450e882c64b8e2..a5c63e20f949997451d6c9daabde77c3c75c8a74 100644 (file)
@@ -14,6 +14,7 @@ export
        ADD, SUB, MUL, QUO, REM,
        EQL, NEQ, LSS, LEQ, GTR, GEQ,
        SHL, SHR,
+       SEND, RECV,
        ADD_ASSIGN, SUB_ASSIGN, MUL_ASSIGN, QUO_ASSIGN, REM_ASSIGN,
        AND_ASSIGN, OR_ASSIGN, XOR_ASSIGN, SHL_ASSIGN, SHR_ASSIGN,
        LAND, LOR,
@@ -67,6 +68,9 @@ const (
 
        SHL;
        SHR;
+       
+       SEND;
+       RECV;
 
        ADD_ASSIGN;
        SUB_ASSIGN;
@@ -171,6 +175,9 @@ func TokenName(tok int) string {
 
        case SHL: return "<<";
        case SHR: return ">>";
+       
+       case SEND: return "-<";
+       case RECV: return "<-";
 
        case ADD_ASSIGN: return "+=";
        case SUB_ASSIGN: return "-=";
@@ -767,7 +774,13 @@ func (S *Scanner) Scan () (tok, beg, end int) {
                case '{': tok = LBRACE;
                case '}': tok = RBRACE;
                case '+': tok = S.Select3(ADD, ADD_ASSIGN, '+', INC);
-               case '-': tok = S.Select3(SUB, SUB_ASSIGN, '-', DEC);
+               case '-':
+                       if S.ch == '<' {
+                               S.Next();
+                               tok = SEND;
+                       } else {
+                               tok = S.Select3(SUB, SUB_ASSIGN, '-', DEC);
+                       }
                case '*': tok = S.Select2(MUL, MUL_ASSIGN);
                case '/':
                        if S.ch == '/' || S.ch == '*' {
@@ -779,7 +792,13 @@ func (S *Scanner) Scan () (tok, beg, end int) {
                        tok = S.Select2(QUO, QUO_ASSIGN);
                case '%': tok = S.Select2(REM, REM_ASSIGN);
                case '^': tok = S.Select2(XOR, XOR_ASSIGN);
-               case '<': tok = S.Select4(LSS, LEQ, '<', SHL, SHL_ASSIGN);
+               case '<':
+                       if S.ch == '-' {
+                               S.Next();
+                               tok = RECV;
+                       } else {
+                               tok = S.Select4(LSS, LEQ, '<', SHL, SHL_ASSIGN);
+                       }
                case '>': tok = S.Select4(GTR, GEQ, '>', SHR, SHR_ASSIGN);
                case '=': tok = S.Select2(ASSIGN, EQL);
                case '!': tok = S.Select2(NOT, NEQ);