compiler/TScope.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 31 Aug 2015 18:37:31 +0100
changeset 5 976f21e29d37
parent 3 97ee341d3e9f
child 6 0c806a7f1888
permissions -rw-r--r--
Added TSourceReader to allow reading source files. Initial work on T environment...

"{ 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
! !

!TScope methodsFor:'initialization'!

initializeWithNode: n parent: p
    node := n.
    parent := p.

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

!TScope methodsFor:'instance creation'!

subScope: methodOrBlockNode
    children isNil ifTrue:[ 
        children := OrderedCollection new: 5.
    ].
    ^ children add: (self class node: methodOrBlockNode parent: self)

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

!TScope methodsFor:'lookup'!

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>"
!

lookupVariable: name
    ^ variables 
        detect:[:binding |  binding name = name ] 
        ifNone:[ parent notNil ifTrue:[ parent lookupVariable: name ] ifFalse:[ self error:'variable not found' ] ]

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