compiler/TScope.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 20 Sep 2015 12:01:42 +0100
changeset 13 97090c2baa33
parent 6 0c806a7f1888
child 15 10a95d798b36
permissions -rw-r--r--
Fixes/refactoring of scopes and bindings. Fixed initialization of scopes and bindings. Make typechecker to seed types.

"{ Package: 'jv:tea/compiler' }"

"{ NameSpace: Smalltalk }"

Object subclass:#TScope
	instanceVariableNames:'node parent children variables'
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Tea-Compiler-Bindings'
!

!TScope class methodsFor:'instance creation'!

node: node
    ^ self node: node parent: nil.

    "Created: / 25-08-2015 / 22:28:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

node: node parent: parent
    ^ self new initializeWithNode: node parent: parent

    "Created: / 25-08-2015 / 22:24:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!TScope methodsFor:'accessing'!

children
    ^ children ? #()

    "Modified: / 25-08-2015 / 22:28:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

node
    ^ node
!

parent
    ^ parent
!

variables
    ^ variables ? #()

    "Modified: / 19-09-2015 / 05:56:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!TScope methodsFor:'adding & removing'!

addSubScope: aTScope
    children isNil ifTrue:[ 
        children := OrderedCollection new: 5.
    ].
    children add: aTScope

    "Created: / 19-09-2015 / 06:05:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

addVariable: aTVariableBinding
    variables isNil ifTrue:[ 
        variables := OrderedCollection new.
    ].
    variables add: aTVariableBinding

    "Created: / 25-08-2015 / 22:42:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!TScope methodsFor:'initialization'!

initializeWithNode: n parent: p
    node := n.
    parent := p.
    parent notNil ifTrue:[ 
        parent addSubScope: self.  
    ].

    "Created: / 25-08-2015 / 22:25:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 19-09-2015 / 06:06:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!TScope methodsFor:'lookup'!

lookupVariable: name
    "Return binding for variable with given name or nil if not found"
    | variable |
    variables notNil ifTrue:[ 
        variable := variables detect:[:binding |  binding name = name ] ifNone:[ nil ].
    ].
    (variable isNil and:[parent notNil]) ifTrue:[
        variable := parent lookupVariable: name.  
    ].
    ^ variable

    "Created: / 25-08-2015 / 22:58:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 20-09-2015 / 06:11:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!TScope methodsFor:'testing'!

isBlockScope
    ^ node isBlock

    "Created: / 02-09-2015 / 08:57:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

isMethodScope
    ^ node isMethod

    "Created: / 02-09-2015 / 08:58:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !