compiler/PPCFSACodeGen.st
changeset 524 f6f68d32de73
parent 515 b5316ef15274
child 529 439c4057517f
equal deleted inserted replaced
515:b5316ef15274 524:f6f68d32de73
     1 "{ Package: 'stx:goodies/petitparser/compiler' }"
     1 "{ Package: 'stx:goodies/petitparser/compiler' }"
     2 
     2 
     3 "{ NameSpace: Smalltalk }"
     3 "{ NameSpace: Smalltalk }"
     4 
     4 
     5 PPCCodeGen subclass:#PPCFSACodeGen
     5 PPCCodeGen subclass:#PPCFSACodeGen
     6 	instanceVariableNames:'fsa backlinkStates compiler'
     6 	instanceVariableNames:''
     7 	classVariableNames:''
     7 	classVariableNames:''
     8 	poolDictionaries:''
     8 	poolDictionaries:''
     9 	category:'PetitCompiler-Scanner'
     9 	category:'PetitCompiler-Scanner'
    10 !
    10 !
    11 
    11 
    16 ! !
    16 ! !
    17 
    17 
    18 !PPCFSACodeGen methodsFor:'coding'!
    18 !PPCFSACodeGen methodsFor:'coding'!
    19 
    19 
    20 codeAbsoluteReturn: code
    20 codeAbsoluteReturn: code
    21     self add: '^ ', code
    21     self code: '^ ', code
    22 !
    22 !
    23 
    23 
    24 codeAssertPeek: t
    24 codeAssertPeek: t
    25     |   id  |
    25     |   id  |
    26     self assert: (t isKindOf: PEGFsaTransition).
    26     self assert: (t isKindOf: PEGFsaTransition).
    27 
    27 
    28     (t isPredicateTransition and: [t isEOF]) ifTrue: [ 
    28     (t isPredicateTransition and: [t isEOF]) ifTrue: [ 
    29         self addOnLine: 'currentChar isNil'.
    29         self codeOnLine: 'currentChar isNil'.
    30         ^ self
    30         ^ self
    31     ].
    31     ].
    32 
    32 
    33     
       
    34     (t isPredicateTransition) ifTrue: [ 
    33     (t isPredicateTransition) ifTrue: [ 
    35         self addOnLine: t predicate asString, ' value: currentChar codePoint'.
    34         self codeOnLine: t predicate asString, ' value: currentChar codePoint'.
    36         ^ self
    35         ^ self
    37     ].
    36     ].
    38 
    37 
    39     (t isAny) ifTrue: [ 
    38     (t isAny) ifTrue: [ 
    40         self addOnLine: 'true'.
    39         self codeOnLine: 'true'.
    41         ^ self
    40         ^ self
    42     ].
    41     ].
    43 
    42 
    44     
    43     
    45     (t isSingleCharacter) ifTrue: [ 
    44     (t isSingleCharacter) ifTrue: [ 
    46         self addOnLine: 'currentChar == ', t character storeString.
    45         self codeOnLine: 'currentChar == ', t character storeString.
    47         ^ self
    46         ^ self
    48     ].
    47     ].
    49 
    48 
    50     (t isNotSingleCharacter) ifTrue: [ 
    49     (t isNotSingleCharacter) ifTrue: [ 
    51         self addOnLine: 'currentChar ~~ ', t notCharacter storeString.
    50         self codeOnLine: 'currentChar ~~ ', t notCharacter storeString.
    52         ^ self
    51         ^ self
    53     ].
    52     ].
    54 
    53 
    55     (t isLetter) ifTrue: [ 
    54     (t isLetter) ifTrue: [ 
    56         self addOnLine: 'currentChar isLetter'.
    55         self codeOnLine: 'currentChar isLetter'.
    57         ^ self
    56         ^ self
    58     ].
    57     ].
    59 
    58 
    60     (t isWord) ifTrue: [ 
    59     (t isWord) ifTrue: [ 
    61         self addOnLine: 'currentChar isAlphaNumeric'.
    60         self codeOnLine: 'currentChar isAlphaNumeric'.
    62         ^ self
    61         ^ self
    63     ].
    62     ].
    64 
    63 
    65     (t isDigit) ifTrue: [ 
    64     (t isDigit) ifTrue: [ 
    66         self addOnLine: 'currentChar isDigit'.
    65         self codeOnLine: 'currentChar isDigit'.
    67         ^ self
    66         ^ self
    68     ].
    67     ].
    69 
    68 
    70     (t isSingleRange) ifTrue: [ 
    69     (t isSingleRange) ifTrue: [ 
    71         | begin end |
    70         | begin end |
    72         begin := t beginOfRange.
    71         begin := t beginOfRange.
    73         end := t endOfRange.
    72         end := t endOfRange.
    74         self addOnLine: 'self peekBetween: ', begin asString, ' and: ', end asString.
    73         self codeOnLine: 'self peekBetween: ', begin asString, ' and: ', end asString.
    75         ^ self
    74         ^ self
    76     ].
    75     ].
    77 
    76 
    78     
    77     
    79     id := idGen cachedSuchThat: [ :e | e = t characterSet ] 
    78     id := self idGen cachedSuchThat: [ :e | e = t characterSet ] 
    80                     ifNone: [ self idFor: t characterSet defaultName: 'characterSet' ].
    79                     ifNone: [ self idFor: t characterSet defaultName: 'characterSet' ].
    81     
    80     
    82     self addConstant: t characterSet as: id.
    81     self addConstant: t characterSet as: id.
    83     self addOnLine: '(currentChar isNotNil) and: [',  id, ' at: currentChar codePoint ]'.
    82     self codeOnLine: '(currentChar isNotNil) and: [',  id, ' at: currentChar codePoint ]'.
    84 !
    83 !
    85 
    84 
    86 codeAssertPeek: transition ifFalse: falseBlock
    85 codeAssertPeek: transition ifFalse: falseBlock
    87     self add: '('.
    86     self add: '('.
    88     self codeAssertPeek: transition.
    87     self codeAssertPeek: transition.
    91     self addOnLine: ']'.
    90     self addOnLine: ']'.
    92     self codeDot.
    91     self codeDot.
    93 !
    92 !
    94 
    93 
    95 codeAssertPeek: t ifTrue: block
    94 codeAssertPeek: t ifTrue: block
    96     self addOnLine: '('.
    95     self codeOnLine: '('.
    97     self codeAssertPeek: t.
    96     self codeAssertPeek: t.
    98     self addOnLine: ') ifTrue: ['.
    97     self codeOnLine: ') ifTrue: ['.
    99     self indent.
    98     self indent.
   100     self code: block.
    99     self code: block.
   101     self dedent.
   100     self dedent.
   102     self add: ']'.
   101     self code: ']'.
   103 !
   102 !
   104 
   103 
   105 codeAssertPeek: transition orReturn: priority
   104 codeAssertPeek: transition orReturn: priority
   106     self error: 'deprecated'.
   105     self error: 'deprecated'.
   107     self add: '('.
   106     self add: '('.
   128     self add: ']'.
   127     self add: ']'.
   129 !
   128 !
   130 
   129 
   131 codeEndBlockWhileTrue
   130 codeEndBlockWhileTrue
   132     self dedent.	
   131     self dedent.	
   133     self add: '] whileTrue.'.
   132     self code: '] whileTrue.'.
   134 !
   133 !
   135 
   134 
   136 codeIfFalse
   135 codeIfFalse
   137     self addOnLine: ' ifFalse: ['.
   136     self codeOnLine: ' ifFalse: ['.
   138 !
   137 !
   139 
   138 
   140 codeNextChar
   139 codeNextChar
   141     self add: 'self step.'
   140     self code: 'self step.'
   142 !
       
   143 
       
   144 codeNl
       
   145     self add: ''.
       
   146 !
   141 !
   147 
   142 
   148 codeNlAssertPeek: characterSet
   143 codeNlAssertPeek: characterSet
   149     self add: ''.
   144     self add: ''.
   150     self codeAssertPeek: characterSet.
   145     self codeAssertPeek: characterSet.
   159         ^ self codeNlReturnResult
   154         ^ self codeNlReturnResult
   160     ].
   155     ].
   161     self add: '^ self returnPriority: ', priority asString, '.'
   156     self add: '^ self returnPriority: ', priority asString, '.'
   162 !
   157 !
   163 
   158 
       
   159 codeRecordDistinctFailure: retval offset: value
       
   160     self add: 'self recordDistinctFailure: ', retval storeString, ' offset: ', value storeString, '.'
       
   161 !
       
   162 
   164 codeRecordDistinctMatch: retval offset: value
   163 codeRecordDistinctMatch: retval offset: value
   165     self add: 'self recordDistinctMatch: ', retval storeString, ' offset: ', value storeString, '.'
   164     self code: 'self recordDistinctMatch: ', retval storeString, ' offset: ', value storeString, '.'
   166 !
   165 !
   167 
   166 
   168 codeRecordMatch: state priority: priority
   167 codeRecordMatch: state priority: priority
   169     priority isNil ifTrue: [ 
   168     priority isNil ifTrue: [ 
   170         ^ self codeRecordMatch: state
   169         ^ self codeRecordMatch: state
   171     ].
   170     ].
   172     
   171     
   173     self add: 'self recordMatch: ', state storeString, ' priority: ', priority asString, '.'
   172     self add: 'self recordMatch: ', state storeString, ' priority: ', priority asString, '.'
   174 !
   173 !
   175 
   174 
       
   175 codeReturn: code
       
   176     arguments profile ifTrue:[ 
       
   177         self codeProfileStop.
       
   178     ].   
       
   179     self code: '^ '.
       
   180     self codeOnLine: code            
       
   181 !
       
   182 
   176 codeReturnResult
   183 codeReturnResult
   177     self addOnLine: '^ self return.'
   184     self addOnLine: '^ self return.'
   178 !
   185 !
   179 
   186 
   180 codeReturnResult: priority
   187 codeReturnResult: priority
       
   188     self error: 'deprecated?'.
   181     priority isNil ifTrue: [ 
   189     priority isNil ifTrue: [ 
   182         ^ self codeReturnResult
   190         ^ self codeReturnResult
   183     ].
   191     ].
   184 
   192     
   185     self addOnLine: '^ self returnPriority: ', priority asString, '.'
   193     self addOnLine: '^ self returnPriority: ', priority asString, '.'
   186 !
   194 !
   187 
   195 
   188 codeStartBlock
   196 codeStartBlock
   189     self add: '['.
   197     self code: '['.
   190     self indent.
   198     self indent.
   191 ! !
   199 ! !
   192 
   200 
   193 !PPCFSACodeGen methodsFor:'coding - results'!
   201 !PPCFSACodeGen methodsFor:'coding - results'!
   194 
   202 
       
   203 codeRecordDistinctFailure: retval
       
   204     self assert: retval isNil.
       
   205     
       
   206     self code: 'self recordDistinctFailure.'
       
   207 !
       
   208 
   195 codeRecordDistinctMatch: retval
   209 codeRecordDistinctMatch: retval
   196     self add: 'self recordDistinctMatch: ', retval storeString, '.'
   210     self code: 'self recordDistinctMatch: ', retval storeString, '.'
   197 !
   211 !
   198 
   212 
   199 codeRecordFailure: index
   213 codeRecordFailure: index
   200     self assert: index isInteger.
   214     self assert: index isInteger.
   201     self add: 'self recordFailure: ', index asString, '.'
   215     self code: 'self recordFailure: ', index asString, '.'
   202 !
   216 !
   203 
   217 
   204 codeRecordMatch: retval
   218 codeRecordMatch: retval
   205     self add: 'self recordMatch: ', retval storeString, '.'
   219     self add: 'self recordMatch: ', retval storeString, '.'
   206 !
   220 !
   207 
   221 
   208 codeRecordMatch: retval offset: offset
   222 codeRecordMatch: retval offset: offset
   209     self add: 'self recordMatch: ', retval storeString, ' offset: ', offset storeString, '.'
   223     self code: 'self recordMatch: ', retval storeString, ' offset: ', offset storeString, '.'
   210 !
   224 !
   211 
   225 
   212 codeReturn
   226 codeReturn
   213     self addOnLine: '^ self'
   227     self codeOnLine: '^ self'
   214 !
   228 !
   215 
   229 
   216 codeReturnDistinct
   230 codeReturnDistinct
   217     self addOnLine: '^ self returnDistinct.'
   231     self codeOnLine: '^ self returnDistinct.'
   218 ! !
   232 ! !
   219 
   233 
   220 !PPCFSACodeGen methodsFor:'intitialization'!
   234 !PPCFSACodeGen methodsFor:'intitialization'!
   221 
   235 
   222 initialize
   236 initialize
   223     super initialize.
   237     super initialize.
   224     
   238 ! !
   225     compiler := PPCCodeGen new.
   239 
   226     backlinkStates := IdentityDictionary new.
       
   227 ! !
       
   228