compiler/TObjectWithProperties.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 25 Sep 2015 03:51:15 +0100
changeset 16 17a2d1d9f205
parent 15 10a95d798b36
permissions -rw-r--r--
Added standalone Tea compiler - teak It allows for compilation of .tea files from the command line.

"
    Copyright (C) 2015-now Jan Vrany

    This code is not an open-source (yet). You may use this code
    for your own experiments and projects, given that:

    * all modification to the code will be sent to the
      original author for inclusion in future releases
    * this is not used in any commercial software

    This license is provisional and may (will) change in
    a future.
"
"{ Package: 'jv:tea/compiler' }"

"{ NameSpace: Smalltalk }"

Object subclass:#TObjectWithProperties
	instanceVariableNames:'properties'
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Tea-Compiler-Internals'
!

!TObjectWithProperties class methodsFor:'documentation'!

copyright
"
    Copyright (C) 2015-now Jan Vrany

    This code is not an open-source (yet). You may use this code
    for your own experiments and projects, given that:

    * all modification to the code will be sent to the
      original author for inclusion in future releases
    * this is not used in any commercial software

    This license is provisional and may (will) change in
    a future.
"
! !

!TObjectWithProperties methodsFor:'accessing-properties'!

hasProperty: aKey
    "Test if the property aKey is present."
    
    ^ properties notNil and: [ properties includesKey: aKey ]

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

propertyAt: aKey
    "Answer the property value associated with aKey."
    
    ^ self propertyAt: aKey ifAbsent: [ self error: 'Property not found' ]

    "Created: / 20-08-2015 / 18:23:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

propertyAt: aKey ifAbsent: aBlock
    "Answer the property value associated with aKey or, if aKey 
     isn't found, answer the result of evaluating aBlock."
    
    ^ properties isNil
        ifTrue: [ aBlock value ]
        ifFalse: [ properties at: aKey ifAbsent: aBlock ]

    "Created: / 20-08-2015 / 18:23:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

propertyAt: aKey ifAbsentPut: aBlock
    "Answer the property associated with aKey or, if aKey isn't found 
     store the result of evaluating aBlock as new value."
    
    ^ self propertyAt: aKey ifAbsent: [ self propertyAt: aKey put: aBlock value ]

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

propertyAt: aKey put: anObject
    "Set the property at aKey to be anObject. If aKey is not found, create a new entry 
     for aKey and set is value to anObject. Answer anObject."

    ^ (properties ifNil: [ properties := RBSmallDictionary new: 1 ])
            at: aKey put: anObject

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

removeProperty: aKey
    "Remove the property with aKey. Answer the property or raise 
     an error if aKey isn't found."
    
    ^ self removeProperty: aKey ifAbsent: [ self error: 'Property not found' ].

    "Created: / 20-08-2015 / 18:30:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

removeProperty: aKey ifAbsent: aBlock
    "Remove the property with aKey. Answer the value or, if aKey isn't found, answer the result of evaluating aBlock."
    
    | answer |
    properties isNil ifTrue: [ ^ aBlock value ].
    answer := properties removeKey: aKey ifAbsent: aBlock.
    properties isEmpty ifTrue: [ properties := nil ].
    ^ answer

    "Created: / 20-08-2015 / 18:30:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!TObjectWithProperties methodsFor:'attributes access'!

objectAttributes
    ^ properties

    "Created: / 20-08-2015 / 18:32:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 23-09-2015 / 21:11:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

objectAttributes:aCollection
    properties := aCollection

    "Created: / 23-09-2015 / 21:11:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !