StatementNode.st
author Claus Gittinger <cg@exept.de>
Fri, 29 Aug 2003 20:28:24 +0200
changeset 1449 612489f03d26
parent 1384 0db9682870d9
child 1504 eb26e6d08d71
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

"{ Package: 'stx:libcomp' }"

ParseNode subclass:#StatementNode
	instanceVariableNames:'expression nextStatement lineNr'
	classVariableNames:''
	poolDictionaries:''
	category:'System-Compiler-Support'
!

!StatementNode class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    node for parse-trees, representing statements
    This is a helper class for the compiler.

    [author:]
        Claus Gittinger
"
! !

!StatementNode class methodsFor:'instance creation'!

expression:e
    ^ (self basicNew) expression:e
! !

!StatementNode methodsFor:'accessing'!

expression
    ^ expression
!

expression:e
    expression := e
!

last
    "return the last statement in a list"

    |last this|

    "this could be done more elegant - but with lots of recursion"
    last := self.
    this := self.
    [this notNil] whileTrue:[
	last := this.
	this := this nextStatement
    ].
    ^ last
!

lineNumber
    "return lineNr"

    ^ lineNr

    "Created: / 14.5.1998 / 19:31:06 / cg"
!

lineNumber:something
    "set lineNr"

    lineNr := something.

    "Created: / 14.5.1998 / 19:31:11 / cg"
!

nextStatement
    ^ nextStatement
!

nextStatement:s
    nextStatement := s
! !

!StatementNode methodsFor:'code generation'!

codeAllForSideEffectOn:aStream inBlock:b for:aCompiler
    |thisStatement|

    thisStatement := self.
    [thisStatement notNil] whileTrue:[
        thisStatement codeForSideEffectOn:aStream inBlock:b for:aCompiler.
        thisStatement := thisStatement nextStatement
    ].
!

codeForSideEffectOn:aStream inBlock:b for:aCompiler
    "generate code for this statement - value not needed"

    expression notNil ifTrue:[
        expression codeForSideEffectOn:aStream inBlock:b for:aCompiler
    ]
!

codeOn:aStream inBlock:b for:aCompiler
    "generate code for this statement"

    expression notNil ifTrue:[
        expression codeOn:aStream inBlock:b for:aCompiler
    ]
! !

!StatementNode methodsFor:'converting'!

asCollectionOfStatements
    "returns a orderedCollection of statements,
     representing the list of which the receiver is the head"

    |coll stat|

    coll := OrderedCollection new.
    stat := self.
    [stat notNil] whileTrue:[
        coll add:stat.
        stat := stat nextStatement.
    ].
    ^ coll
! !

!StatementNode methodsFor:'enumerating'!

nodeDo:anEnumerator
    "helper for parse tree walking"

    |stats this|

    "/ too stupid: ST/X statements are linked as a list ...

    stats := OrderedCollection new.
    this := self.
    [this notNil] whileTrue:[
        stats add:this expression.
        this := this nextStatement
    ].
    ^ anEnumerator doSequence:self temporaries:#() statements:stats

    "Created: 19.6.1997 / 16:45:34 / cg"
    "Modified: 19.6.1997 / 17:06:47 / cg"
! !

!StatementNode methodsFor:'evaluation'!

evaluateAllIn:anEnvironment
    |lastValue thisStatement|

    "this could be done more elegant - but with lots of recursion"
    thisStatement := self.
    [thisStatement notNil] whileTrue:[
        lastValue := thisStatement evaluateExpressionIn:anEnvironment.
        thisStatement := thisStatement nextStatement
    ].
    ^ lastValue
!

evaluateExpressionIn:anEnvironment
    ^ expression evaluateIn:anEnvironment
!

evaluateIn:anEnvironment
    ^ self evaluateAllIn:anEnvironment.
! !

!StatementNode methodsFor:'printing & storing'!

printAllOn:aStream 
    self printAllOn:aStream indent:4
!

printAllOn:aStream indent:i
    |thisStatement|

    thisStatement := self.
    [thisStatement notNil] whileTrue:[
        aStream spaces:i.
        thisStatement printOn:aStream indent:i.
        thisStatement nextStatement notNil ifTrue:[
            aStream cr.
        ].
        thisStatement := thisStatement nextStatement
    ]
!

printOn:aStream indent:i
    expression printOn:aStream indent:i.
! !

!StatementNode methodsFor:'queries'!

collectBlocksInto:aCollection
    |this ex|

    this := self.
    [this notNil] whileTrue:[
        ex := this expression.
        ex notNil ifTrue:[
            ex collectBlocksInto:aCollection.
        ].
        this := this nextStatement
    ].

    "Created: 23.10.1996 / 15:44:13 / cg"
    "Modified: 20.4.1997 / 12:10:18 / cg"
!

isConstant
    nextStatement notNil ifTrue:[^ false].
    ^ expression isConstant
!

listEndsWithReturn
    |thisStatement lastStatement|

    thisStatement := self.
    [thisStatement notNil] whileTrue:[
        lastStatement := thisStatement.
        thisStatement := thisStatement nextStatement
    ].

    ^ lastStatement isReturnNode

    "Created: 19.8.1996 / 14:32:47 / cg"
! !

!StatementNode class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libcomp/StatementNode.st,v 1.28 2003-03-28 14:41:25 cg Exp $'
! !