]> Cypherpunks repositories - gostls13.git/commitdiff
- added missing file
authorRobert Griesemer <gri@golang.org>
Wed, 17 Dec 2008 02:03:18 +0000 (18:03 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 17 Dec 2008 02:03:18 +0000 (18:03 -0800)
R=r
OCL=21384
CL=21384

usr/gri/pretty/typechecker.go [new file with mode: 0644]

diff --git a/usr/gri/pretty/typechecker.go b/usr/gri/pretty/typechecker.go
new file mode 100644 (file)
index 0000000..1f74d80
--- /dev/null
@@ -0,0 +1,100 @@
+// 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 TypeChecker
+
+import (
+       AST "ast";
+       Universe "universe";
+       Globals "globals";
+       Object "object";
+       Type "type";
+)
+
+
+type State struct {
+       level int;
+       top_scope *Globals.Scope;
+}
+
+
+// ----------------------------------------------------------------------------
+// Support
+
+func (s *State) Error(pos int, msg string) {
+       panicln("error:" + msg);
+}
+
+
+// ----------------------------------------------------------------------------
+// Scopes
+
+func (s *State) OpenScope() {
+       s.top_scope = Globals.NewScope(s.top_scope);
+}
+
+
+func (s *State) CloseScope() {
+       s.top_scope = s.top_scope.parent;
+}
+
+
+func (s *State) Lookup(ident string) *Globals.Object {
+       for scope := s.top_scope; scope != nil; scope = scope.parent {
+               obj := scope.Lookup(ident);
+               if obj != nil {
+                       return obj;
+               }
+       }
+       return nil;
+}
+
+
+func (s *State) DeclareInScope(scope *Globals.Scope, obj *Globals.Object) {
+       if s.level > 0 {
+               panic("cannot declare objects in other packages");
+       }
+       obj.pnolev = s.level;
+       if scope.Lookup(obj.ident) != nil {
+               s.Error(obj.pos, `"` + obj.ident + `" is declared already`);
+               return;  // don't insert it into the scope
+       }
+       scope.Insert(obj);
+}
+
+
+func (s *State) Declare(obj *Globals.Object) {
+       s.DeclareInScope(s.top_scope, obj);
+}
+
+
+// ----------------------------------------------------------------------------
+// Common productions
+
+func (s *State) DeclareIdent(kind int) {
+       obj := Globals.NewObject(0, kind, "");
+       s.Declare(obj);
+}
+
+
+// ----------------------------------------------------------------------------
+
+func (s *State) CheckProgram(p *AST.Program) {
+       s.OpenScope();
+       
+       {       s.OpenScope();
+       
+               s.CloseScope();
+       }
+       
+       s.CloseScope();
+}
+
+
+// ----------------------------------------------------------------------------
+
+export func CheckProgram(p *AST.Program) {
+       var s State;
+       s.CheckProgram(p);
+}