compiler/PPCTokenCodeGenerator.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 12 May 2015 01:57:37 +0100
changeset 461 5986bf6d7d60
parent 459 4751c407bb40
child 465 f729f6cd3c76
permissions -rw-r--r--
Portability: fixes for Smalltalk/X * Do not use #crShow: - not present in Smalltalk/X * Do not use Array class>>with:withAll: * do not use detect:ifFound:ifAbsent:

"{ Package: 'stx:goodies/petitparser/compiler' }"

"{ NameSpace: Smalltalk }"

PPCCodeGenerator subclass:#PPCTokenCodeGenerator
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'PetitCompiler-Visitors'
!


!PPCTokenCodeGenerator methodsFor:'as yet unclassified'!

afterAccept: node retval: retval
    | return |
    return := super afterAccept: node retval: retval.
    return category: 'generated - tokens'.
    ^ return
! !

!PPCTokenCodeGenerator methodsFor:'visiting'!

visitTokenNode: node
    | id startVar endVar |
    startVar := compiler allocateTemporaryVariableNamed: 'start'.
    endVar := compiler allocateTemporaryVariableNamed: 'end'.

    id := compiler idFor: node.
    compiler rememberStrategy: (PPCCompilerTokenRememberStrategy on: compiler).	
    
    compiler codeAssign: 'context position + 1.' to: startVar.
    compiler codeStoreValueOf: [ self visit: node child ] intoVariable: #whatever.
    compiler add: 'error ifFalse: [ '.
    compiler indent.	
        compiler codeAssign: 'context position.' to: endVar.
    
        compiler codeTranscriptShow: 'current token type: ', id storeString.
        compiler codeAssign: id storeString, '.' to: 'currentTokenType'.
        compiler codeAssign: node tokenClass asString, ' on: (context collection) 
                                                                    start: ', startVar, '  
                                                                    stop: ', endVar, '
                                                                    value: nil.'
                    to: 'currentTokenValue := ', self retvalVar.
        compiler codeReturn.
    compiler dedent.
    compiler add: '].'.		
    compiler rememberStrategy: (PPCCompilerTokenizingRememberStrategy on: compiler).	
!

visitTrimmingTokenNode: node
    |  id  startVar endVar |
    
    startVar := compiler allocateTemporaryVariableNamed: 'start'.
    endVar := compiler allocateTemporaryVariableNamed:  'end'.
    
    id := compiler idFor: node.
    compiler rememberStrategy: (PPCCompilerTokenRememberStrategy on: compiler).
    
    
    compiler addComment: 'Consume Whitespace:'.
    compiler codeStoreValueOf: [ self visit: node whitespace ] intoVariable: #whatever.
    compiler nl.

    compiler codeAssign: 'context position + 1.' to: startVar.
    compiler codeStoreValueOf: [ self visit: node child ] intoVariable: #whatever.

    compiler add: 'error ifFalse: [ '.
    compiler indent.	
        compiler codeAssign: 'context position.' to: endVar.
    
        compiler addComment: 'Consume Whitespace:'.
        compiler codeStoreValueOf: [ self visit: node whitespace ] intoVariable: #whatever.
        compiler nl.
    
    
        compiler codeTranscriptShow: 'current token type: ', id storeString.
        compiler codeAssign: id storeString, '.' to: 'currentTokenType'.
        compiler codeAssign: node tokenClass asString, ' on: (context collection) 
                                                                start: ', startVar, ' 
                                                                stop: ', endVar, '
                                                                value: nil.'
                   to: 'currentTokenValue := ', self retvalVar.
        compiler codeReturn.
    compiler dedent.																
    compiler add: '].'	.
    compiler rememberStrategy: (PPCCompilerTokenizingRememberStrategy on: compiler).
! !

!PPCTokenCodeGenerator class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
! !