############################################################################### # Date: Sat Feb 24 02:06:20 CST 2007 # Author: John Quigley # Revision: $Id$ ############################################################################### Env := Object clone do ( ############################################################################ # Private slots. prot := Object clone do ( #------------------------------------------------------------------------ lookup := method ( symbol, val := nil for( idx, scopeCount, 0, -1, tab := scopes at(idx) val = tab at(symbol) if( val, return val ) ) nil ) #------------------------------------------------------------------------ add := method ( key, value, scopes at(scopeCount) atPut(key, value) scopes at(scopeCount) at(key) ) #------------------------------------------------------------------------ asString := method ( str := Sequence clone str appendSeq( "Environment[scopeCount=#{scopeCount}\n" interpolate ) scopes foreach( s, str appendSeq( "Scope[\n" ) s foreach( k, v, str appendSeq( "'#{k}'\n" interpolate ) ) str appendSeq( " ]\n" ) ) str appendSeq( "]" ) str ) #------------------------------------------------------------------------ addBindings := method ( formalList, actualList, localScope := Map clone Env bind( localScope, formalList, actualList ) addLocalScope( localScope ) ) #------------------------------------------------------------------------ addLocalScope := method ( localScope, newScopes := List clone scopes foreach( scope, newScopes append( scope ) ) newScopes append( localScope ) env := Env prot clone env scopes := newScopes env scopeCount := scopeCount + 1 env ) ) ############################################################################ # Public slots. bind := method ( scope, formalList, actualList, if ( formalList xtype == "CONS" ) then ( scope atPut( formalList car lexeme, actualList car ) Env bind( scope, formalList cdr, actualList cdr ) ) ) #---------------------------------------------------------------------------- new := method ( initialScope, env := prot clone env scopes := list( initialScope ) env scopeCount := 0 env ) ) # EOF