]> Cypherpunks repositories - gostls13.git/commitdiff
- steps towards augmenting ast with declaration info
authorRobert Griesemer <gri@golang.org>
Tue, 6 Jan 2009 22:54:18 +0000 (14:54 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 6 Jan 2009 22:54:18 +0000 (14:54 -0800)
  (will help produce html output where we can click on identifiers and
  get to the declaration)
- snapshot before changing back to old new

R=r
OCL=22159
CL=22159

usr/gri/pretty/compilation.go
usr/gri/pretty/printer.go
usr/gri/pretty/typechecker.go

index 3168354870cf58b30536f936dc41a109bd5750fd..fb7f41600125c33db52a501ec692d22e3c92d738 100644 (file)
@@ -136,7 +136,7 @@ export func Compile(src_file string, flags *Flags) (*AST.Program, int) {
        prog := parser.ParseProgram();
 
        if err.nerrors == 0 {
-               TypeChecker.CheckProgram(prog);
+               TypeChecker.CheckProgram(&err, prog);
        }
 
        return prog, err.nerrors;
index f799d0f882680e2431d811412c355a07fba44c3c..cbfdc5fc80a279ab55f2c99a3355a85441b91ff9 100644 (file)
@@ -846,7 +846,7 @@ func (P *Printer) Program(p *AST.Program) {
        P.Expr(p.ident);
        P.newlines = 1;
        for i := 0; i < p.decls.Len(); i++ {
-               P.Declaration(p.decls.At(i), false);
+               P.Declaration(p.decls.At(i).(*AST.Decl), false);
        }
        P.newlines = 1;
 }
index 1f74d8025c4c7296b7e4c76181b7ff79a9779ba1..b7d796257f9c0f57832392bad32c1ee0a9d4478e 100644 (file)
@@ -6,6 +6,7 @@ package TypeChecker
 
 import (
        AST "ast";
+       Scanner "scanner";
        Universe "universe";
        Globals "globals";
        Object "object";
@@ -14,16 +15,42 @@ import (
 
 
 type State struct {
+       // setup
+       err Scanner.ErrorHandler;
+
+       // state
        level int;
        top_scope *Globals.Scope;
 }
 
 
+func (s *State) Init(err Scanner.ErrorHandler) {
+       s.err = err;
+}
+
+
 // ----------------------------------------------------------------------------
 // Support
 
+func unimplemented() {
+       panic("unimplemented");
+}
+
+
+func unreachable() {
+       panic("unreachable");
+}
+
+
+func assert(pred bool) {
+       if !pred {
+               panic("assertion failed");
+       }
+}
+
+
 func (s *State) Error(pos int, msg string) {
-       panicln("error:" + msg);
+       s.err.Error(pos, msg);
 }
 
 
@@ -72,19 +99,87 @@ func (s *State) Declare(obj *Globals.Object) {
 // ----------------------------------------------------------------------------
 // Common productions
 
-func (s *State) DeclareIdent(kind int) {
-       obj := Globals.NewObject(0, kind, "");
-       s.Declare(obj);
+func (s *State) DeclareIdent(ident *AST.Expr, kind int, typ *AST.Type) {
+       // ident is either a comma-separated list or a single ident
+       switch ident.tok {
+       case Scanner.IDENT:
+               obj := Globals.NewObject(ident.pos, kind, ident.s);
+               s.Declare(obj);
+       case Scanner.COMMA:
+               s.DeclareIdent(ident.x, kind, typ);
+               s.DeclareIdent(ident.y, kind, typ);             
+       default:
+               unreachable();
+       }
 }
 
 
 // ----------------------------------------------------------------------------
 
+func (s *State) CheckType() {
+}
+
+
+func (s *State) CheckDeclaration(d *AST.Decl) {
+       if d.tok != Scanner.FUNC && d.list != nil {
+               // group of parenthesized declarations
+               for i := 0; i < d.list.Len(); i++ {
+                       s.CheckDeclaration(d.list.At(i).(*AST.Decl))
+               }
+               
+       } else {
+               // single declaration
+               switch d.tok {
+               case Scanner.IMPORT:
+                       assert(d.ident == nil || d.ident.tok == Scanner.IDENT);
+                       if d.ident != nil {
+                               s.DeclareIdent(d.ident, d.tok, d.typ);
+                       } else {
+                       }
+
+               case Scanner.EXPORT:
+                       // TODO
+
+               case Scanner.CONST:
+                       s.DeclareIdent(d.ident, d.tok, d.typ);
+
+               case Scanner.VAR:
+                       s.DeclareIdent(d.ident, d.tok, d.typ);
+
+               case Scanner.TYPE:
+                       assert(d.ident.tok == Scanner.IDENT);
+                       // types may be forward-declared
+                       obj := s.Lookup(d.ident.s);
+                       if obj != nil {
+                               // TODO check if proper forward-declaration
+
+                       } else {
+                               s.DeclareIdent(d.ident, d.tok, d.typ);
+                       }
+
+               case Scanner.FUNC:
+                       assert(d.ident.tok == Scanner.IDENT);
+                       if d.typ.key != nil {
+                               // method
+                               // TODO
+                       } else {
+                               s.DeclareIdent(d.ident, d.tok, d.typ);
+                       }
+
+               default:
+                       unreachable();
+               }
+       }
+}
+
+
 func (s *State) CheckProgram(p *AST.Program) {
        s.OpenScope();
        
        {       s.OpenScope();
-       
+               for i := 0; i < p.decls.Len(); i++ {
+                       s.CheckDeclaration(p.decls.At(i).(*AST.Decl));
+               }
                s.CloseScope();
        }
        
@@ -94,7 +189,10 @@ func (s *State) CheckProgram(p *AST.Program) {
 
 // ----------------------------------------------------------------------------
 
-export func CheckProgram(p *AST.Program) {
+export func CheckProgram(err Scanner.ErrorHandler, p *AST.Program) {
+       return;  // DISABLED FOR NOW
+       
        var s State;
+       s.Init(err);
        s.CheckProgram(p);
 }