compiler/TScope.st
changeset 13 97090c2baa33
parent 6 0c806a7f1888
child 15 10a95d798b36
equal deleted inserted replaced
12:d716a8181fc1 13:97090c2baa33
    35     ^ node
    35     ^ node
    36 !
    36 !
    37 
    37 
    38 parent
    38 parent
    39     ^ parent
    39     ^ parent
       
    40 !
       
    41 
       
    42 variables
       
    43     ^ variables ? #()
       
    44 
       
    45     "Modified: / 19-09-2015 / 05:56:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    40 ! !
    46 ! !
    41 
    47 
    42 !TScope methodsFor:'initialization'!
    48 !TScope methodsFor:'adding & removing'!
    43 
    49 
    44 initializeWithNode: n parent: p
    50 addSubScope: aTScope
    45     node := n.
       
    46     parent := p.
       
    47 
       
    48     "Created: / 25-08-2015 / 22:25:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    49 ! !
       
    50 
       
    51 !TScope methodsFor:'instance creation'!
       
    52 
       
    53 subScope: methodOrBlockNode
       
    54     children isNil ifTrue:[ 
    51     children isNil ifTrue:[ 
    55         children := OrderedCollection new: 5.
    52         children := OrderedCollection new: 5.
    56     ].
    53     ].
    57     ^ children add: (self class node: methodOrBlockNode parent: self)
    54     children add: aTScope
    58 
    55 
    59     "Created: / 25-08-2015 / 22:28:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    56     "Created: / 19-09-2015 / 06:05:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    60 ! !
    57 !
    61 
       
    62 !TScope methodsFor:'lookup'!
       
    63 
    58 
    64 addVariable: aTVariableBinding
    59 addVariable: aTVariableBinding
    65     variables isNil ifTrue:[ 
    60     variables isNil ifTrue:[ 
    66         variables := OrderedCollection new.
    61         variables := OrderedCollection new.
    67     ].
    62     ].
    68     variables add: aTVariableBinding
    63     variables add: aTVariableBinding
    69 
    64 
    70     "Created: / 25-08-2015 / 22:42:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    65     "Created: / 25-08-2015 / 22:42:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    71 !
    66 ! !
       
    67 
       
    68 !TScope methodsFor:'initialization'!
       
    69 
       
    70 initializeWithNode: n parent: p
       
    71     node := n.
       
    72     parent := p.
       
    73     parent notNil ifTrue:[ 
       
    74         parent addSubScope: self.  
       
    75     ].
       
    76 
       
    77     "Created: / 25-08-2015 / 22:25:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    78     "Modified: / 19-09-2015 / 06:06:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    79 ! !
       
    80 
       
    81 !TScope methodsFor:'lookup'!
    72 
    82 
    73 lookupVariable: name
    83 lookupVariable: name
    74     ^ variables 
    84     "Return binding for variable with given name or nil if not found"
    75         detect:[:binding |  binding name = name ] 
    85     | variable |
    76         ifNone:[ parent notNil ifTrue:[ parent lookupVariable: name ] ifFalse:[ self error:'variable not found' ] ]
    86     variables notNil ifTrue:[ 
       
    87         variable := variables detect:[:binding |  binding name = name ] ifNone:[ nil ].
       
    88     ].
       
    89     (variable isNil and:[parent notNil]) ifTrue:[
       
    90         variable := parent lookupVariable: name.  
       
    91     ].
       
    92     ^ variable
    77 
    93 
    78     "Created: / 25-08-2015 / 22:58:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    94     "Created: / 25-08-2015 / 22:58:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    95     "Modified: / 20-09-2015 / 06:11:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    79 ! !
    96 ! !
    80 
    97 
    81 !TScope methodsFor:'testing'!
    98 !TScope methodsFor:'testing'!
    82 
    99 
    83 isBlockScope
   100 isBlockScope