Lecture #26: Some Object DesignSpecificsExampleObservationsGeneral Sketch of an AlgorithmSymbol TablesFrom Algorithm to Methods Sample Details of `gatherVarDecls' Part ISample Details of `gatherVarDecls', Part IIWhere the Work is Done: `defineIdsAsVars'Lecture #26: Some Object DesignOur Example: Handling scope rules for simple variables in the project.Problem Summary. Attach “VarDecls” to each IdNode that repre-sents a simple variable (including parameters ) . Call thisresolvingtheseidentifiers.=xx1+Var xAST Semantic Delegates Decl Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 1Specifics• One VarDecl object for eachdistinctvariable.• IdNodes that represent references to the same variable shouldhave same VarDecl.• Thus, n ew VarDecl for each pa rameter declar ation (in def or lambda),plus• New Var De cl for a variable in any region that contains at least oneassignment to it (unless previously declared as parameter), . . .• Unless variable is defined global.• Declarative regions ar e global region, each def, and each lambda.• Variables assigned to in class bodies (not inside a function) arenotsimple variable s.Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 2ExampleFor th e progr ama = 3y = 2z = 3def f (a, b):y = a+b+zreturn y‘unpyth –decls’ will show...a#1 = 3y#2 = 2z#3 = 3def f#4 (a#5 , b#6 ):y#7 = a#5 + b#6 + z# 3return y#7#1: VarDecl#2: VarDecl#3: VarDecl#4: FuncDecl#5: VarDecl in #4#6: VarDecl in #4#7: VarDecl in #4Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 3Observations• Must collect declarations first, because assignments may occur af-ter uses in te xt, even in correct programs:while x < N:if x > 0:f (y) # y used hereelse:y = 0 # y assigned he rex += 1...• Must also have all outer declarations collected before trying to re-solve inner ones.Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 4General Sketch of an AlgorithmTo resolve identifiers in declarative region R:Find and define all parameters immediately inside R (& check duplicates)Find global declarations immediately inside R (& check duplicates)Find {\b f def}s immediately inside R (& check duplicates)Find and define all assigned non-global variables immediately inside RLook up all identifiers immediately inside R and attach theresults of these lookups to the identifier nodes, andFor each declarative region, R’, immediately inside R,resolve identifiers in declarative region R’ (recursively).Forget a ll the definitions created above (the identifiers a re nowall connected to these definitions, so we do n’t need to remem b er them.•Immediately inside Rmeans “inside R, but not inside any other declar-ative region inside R. ”• Ha ndling classes s i milar, but we crea te attributes in that case.• In the outermost decla rative region, ignore global defi nitions.Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 5Symbol Tables• Our sketch requires a block-str uctur ed symbol table as in previouslectures.• From the algorithm, n eed to be able to:– Start a new block (for a declarati ve region)– Look up iden ti fers in the latest block– Look up iden ti fi ers in an y reg i on ( i nnermost to outermost)– Look up iden ti fi ers in outer mos t region (for globals )– Define identifier to refer to particular Decl in curr ent region.– Leave a block (and remove all declarations from it).• Each of these points indicates the need for a meth od in new type ofobject (I call it class Symtab).Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 6From Algorithm to MethodsWe’ll add methods to Stat icSemantics for each thing we need to dofrom al g orithm sketch:• Re solve identifiers in declarative region R: resolveIds• Define parameters: part of resolveIds• Find global declaration s: gatherGlobalDec ls• Find assigne d non-global variables: gatherVarDecls,gatherVarDeclsFromTargets, and defineIdsAsVars.Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 7Sample De tails of ‘gatherVarDecls’ Part IIn S taticSemantics, the default definition:/** Add variable declara t i o ns in the outermost declara tive region of THIS* to the innermost block of SYMS. Do es n o t add any declaration for* names previously declared in the inner m ost block of SYMS. REGION is* the declaration of the enclo s i n g declarat i v e region (a FuncDecl,* MethodDecl, or null for the glob a l reg i on). */void gatherVarDe c l s (Symtab syms, RegionDe c l region) {for (int i = 0; i < arity (); i += 1) {getChild (i).gatherVar D ecls (syms, region) ;}}Don’t want to look inside inner declar ative regions or classes, so inFuncDeclSem, LambdaSem, and Cl assDeclSem de fi ne:/** Do not go int o THI S looking for variable targets. */void gatherVarDe c l s (Symtab syms, RegionDe c l region) { }Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 8Sample De tails of ‘gatherVarDecls’, P art IINow we han dl e a ssignments , by defining in Assi gnSem:void gatherVarDe c l s (Symtab syms, RegionDe c l region) {getChild (0).gatherVar D eclsFromTarget s (syms, region);getChild (1).gatherVar D ecls (syms, region) ;}where ga therVarDeclsFromTargets i s de fined in StaticSemantics:/** Add variable declara tions for all assigned-to i dentifiers in* outermost target s in THIS to the current region of SYMS. */void gatherVarDe c l s FromTargets (Symtab syms, RegionDecl region) {for (int i = 0; i < ari t y (); i += 1)getChild (i).gat h e r VarDeclsFromTa r gets (syms, region);}and by default in TargetSem (supertype of s emantics for al l targets):/** By default, assignme n t to a target doe s not declare anything. */void gatherVarDe c l s FromTargets (Symtab syms, RegionDecl region) { }And fina l l y in the only target that matters, VarRefSem:void gatherVarDe c l s FromTargets (Symtab syms, RegionDecl region) {getChild (0).defineIds A sVars (syms, region);}Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 9Where the Work is Don e : ‘defineIdsAsVars’Default defini ti on in StaticSemanti cs:/** Enter a variable declara tion in SYMS for each identifier in THIS that* is not previously declar ed i n the inn er b l o ck o f SYMS. REGION is* the enclosing declar ative region’s decl aration. */void defineIdsAs V a r s (Symtab syms, RegionDe cl region) {for (int i = 0; i < ari t y (); i += 1)getChild (i).def i n e IdsAsVars (syms, region);}And the work is really done i n I dSem:void defineIdsAs V a r s (Symtab syms, RegionDe cl region) {String name
View Full Document