compiler/PPCCodeGenerator.st
changeset 459 4751c407bb40
parent 452 9f4558b3be66
child 460 87a3d30ab570
child 464 f6d77fee9811
equal deleted inserted replaced
452:9f4558b3be66 459:4751c407bb40
     9 	category:'PetitCompiler-Visitors'
     9 	category:'PetitCompiler-Visitors'
    10 !
    10 !
    11 
    11 
    12 !PPCCodeGenerator class methodsFor:'as yet unclassified'!
    12 !PPCCodeGenerator class methodsFor:'as yet unclassified'!
    13 
    13 
       
    14 new
       
    15     ^ self basicNew
       
    16         initialize;
       
    17         yourself 
       
    18 !
       
    19 
    14 on: aPPCCompiler
    20 on: aPPCCompiler
    15     ^ self new 
    21     ^ self new 
    16         compiler: aPPCCompiler;
    22         compiler: aPPCCompiler;
    17         yourself
    23         yourself
    18 ! !
    24 ! !
    19 
    25 
    20 !PPCCodeGenerator methodsFor:'accessing'!
    26 !PPCCodeGenerator methodsFor:'accessing'!
    21 
    27 
    22 compiler: aPPCCompiler
    28 compiler: aPPCCompiler
    23     compiler := aPPCCompiler 
    29     compiler := aPPCCompiler 
       
    30 !
       
    31 
       
    32 guards
       
    33     ^ arguments guards
    24 ! !
    34 ! !
    25 
    35 
    26 !PPCCodeGenerator methodsFor:'hooks'!
    36 !PPCCodeGenerator methodsFor:'hooks'!
    27 
    37 
    28 afterAccept: node retval: retval
    38 afterAccept: node retval: retval
    46 
    56 
    47 !PPCCodeGenerator methodsFor:'support'!
    57 !PPCCodeGenerator methodsFor:'support'!
    48 
    58 
    49 addGuard: node
    59 addGuard: node
    50     |  guard firsts id |
    60     |  guard firsts id |
    51     (arguments guards not or: [(guard := PPCGuard on: node) makesSense not]) ifTrue: [ ^ self].
    61     (self guards not or: [(guard := PPCGuard on: node) makesSense not]) ifTrue: [ ^ self].
    52 
    62 
    53     id := compiler idFor: node.
    63     id := compiler idFor: node.
    54     firsts := node firstSetWithTokens.
    64     firsts := node firstSetWithTokens.
    55 
    65 
    56     
    66     
   243     (self isOpen: child) ifTrue: [ 
   253     (self isOpen: child) ifTrue: [ 
   244         "already processing..."
   254         "already processing..."
   245         ^ nil
   255         ^ nil
   246     ].
   256     ].
   247 
   257 
   248     "TODO JK: this is is wrong,.. to tired now to fix this :("
       
   249 "	(self isCached: child) ifTrue: [ 
       
   250         node replace: child with: (self cachedValue: child).
       
   251         ^ nil
       
   252     ]. 
       
   253 "
       
   254     ^ self visit: child.
   258     ^ self visit: child.
   255 !
   259 !
   256 
   260 
   257 visitChoiceNode: node
   261 visitChoiceNode: node
   258     | firsts guard whitespaceConsumed elementVar |
   262     | firsts guard whitespaceConsumed elementVar |
   266     elementVar := compiler allocateTemporaryVariableNamed: 'element'.
   270     elementVar := compiler allocateTemporaryVariableNamed: 'element'.
   267     "	
   271     "	
   268         If we want to compile in guard and the choice starts with trimming token, 
   272         If we want to compile in guard and the choice starts with trimming token, 
   269         we should invoke the whitespace parser
   273         we should invoke the whitespace parser
   270     "
   274     "
   271     (arguments guards and: [ firsts allSatisfy: [ :e | e isTrimmingTokenNode ] ]) ifTrue: [  
   275     (self guards and: [ firsts allSatisfy: [ :e | e isTrimmingTokenNode ] ]) ifTrue: [  
   272         self compileTokenWhitespace: firsts anyOne.
   276         self compileTokenWhitespace: firsts anyOne.
   273         whitespaceConsumed := true.
   277         whitespaceConsumed := true.
   274     ].
   278     ].
   275         
   279         
   276     1 to: node children size do: [ :idx  | |child allowGuard |
   280     1 to: node children size do: [ :idx  | |child allowGuard |
   277         child := node children at: idx.
   281         child := node children at: idx.
   278         allowGuard := whitespaceConsumed.
   282         allowGuard := whitespaceConsumed.
   279                                 
   283                                 
   280         (allowGuard and: [arguments guards and: [ (guard := PPCGuard on: child) makesSense ]]) ifTrue: [         
   284         (allowGuard and: [self guards and: [ (guard := PPCGuard on: child) makesSense ]]) ifTrue: [         
   281             guard id: (compiler idFor: guard prefixed: #guard).
   285             guard id: (compiler idFor: guard prefixed: #guard).
   282             guard compileGuard: compiler.
   286             guard compileGuard: compiler.
   283             compiler add: ' ifTrue: [ '.
   287             compiler add: ' ifTrue: [ '.
   284             compiler indent.
   288             compiler indent.
   285                 compiler add: 'self clearError.'.
   289                 compiler add: 'self clearError.'.
   395     
   399     
   396     compiler codeStoreValueOf: [ self visit: node child  ] intoVariable: #whatever.
   400     compiler codeStoreValueOf: [ self visit: node child  ] intoVariable: #whatever.
   397     compiler smartRestore: node child from: mementoVar.
   401     compiler smartRestore: node child from: mementoVar.
   398 
   402 
   399     compiler add: '^ error ifFalse: [ self error ] ifTrue: [ self clearError. nil ]'.
   403     compiler add: '^ error ifFalse: [ self error ] ifTrue: [ self clearError. nil ]'.
       
   404 
       
   405     "Modified: / 05-05-2015 / 14:29:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   400 !
   406 !
   401 
   407 
   402 visitOptionalNode: node
   408 visitOptionalNode: node
   403     compiler codeStoreValueOf: [ self visit: node child ] intoVariable: self retvalVar.
   409     compiler codeStoreValueOf: [ self visit: node child ] intoVariable: self retvalVar.
   404     compiler add: 'error ifTrue: [ '.
   410     compiler add: 'error ifTrue: [ '.
   509 
   515 
   510     "Modified: / 23-04-2015 / 22:03:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   516     "Modified: / 23-04-2015 / 22:03:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   511 !
   517 !
   512 
   518 
   513 visitStarAnyNode: node
   519 visitStarAnyNode: node
   514 
   520     | retvalVar sizeVar |
   515     compiler addVariable: 'retval size'.
   521 
   516     compiler add: 'size := context size - context position.'.
   522     retvalVar := compiler allocateReturnVariable.
   517     compiler add: 'retval := Array new: size.'.
   523     sizeVar := compiler allocateTemporaryVariableNamed: 'size'.  
   518     compiler add: '(1 to: size) do: [ :e | retval at: e put: context next ].'.
   524     compiler add: sizeVar , ' := context size - context position.'.
   519     compiler add: '^ retval'.
   525     compiler add: retvalVar,' := Array new: ',sizeVar,'.'.
   520     
   526     compiler add: '(1 to: ',sizeVar,') do: [ :e | ',retvalVar,' at: e put: context next ].'.
       
   527     compiler codeReturn.
       
   528     
       
   529     "Modified: / 05-05-2015 / 14:13:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   521 !
   530 !
   522 
   531 
   523 visitStarCharSetPredicateNode: node
   532 visitStarCharSetPredicateNode: node
   524     | classification classificationId |
   533     | classification classificationId |
   525     
   534