From 003f0ad6afacbed76dfc09788d1117c44cc42222 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 16 Dec 2008 18:03:18 -0800 Subject: [PATCH] - added missing file R=r OCL=21384 CL=21384 --- usr/gri/pretty/typechecker.go | 100 ++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 usr/gri/pretty/typechecker.go diff --git a/usr/gri/pretty/typechecker.go b/usr/gri/pretty/typechecker.go new file mode 100644 index 0000000000..1f74d8025c --- /dev/null +++ b/usr/gri/pretty/typechecker.go @@ -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); +} -- 2.48.1