compiler/PPCNotNode.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 15 Apr 2015 11:28:09 +0100
changeset 422 116d2b2af905
parent 414 0eaf09920532
child 438 20598d7ce9fa
permissions -rw-r--r--
To fold

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

"{ NameSpace: Smalltalk }"

PPCDelegateNode subclass:#PPCNotNode
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'PetitCompiler-Nodes'
!

!PPCNotNode methodsFor:'accessing'!

prefix
	^ #not
! !

!PPCNotNode methodsFor:'analysis'!

firstCharSet
	^ PPCharSetPredicate on: [:e | true ] 
!

isFirstSetTerminal
	^ true
! !

!PPCNotNode methodsFor:'compiling'!

compileWith: compiler effect: effect id: id

	compiler startMethod: id.
	compiler addVariable: 'memento'.
	compiler add: (compiler smartRemember: child).
	
	compiler call: (child compileWith: compiler).
	compiler add: (compiler smartRestore: child).

	compiler add: '^ error ifFalse: [ self error ] ifTrue: [ self clearError. nil ]'.
 ^ compiler stopMethod.
! !

!PPCNotNode methodsFor:'optimizing'!

optimize: params 
	(self rewrite: params) ifTrue: [ 
		^ true.
	].
	^ super optimize: params.
!

rewrite: changeStatus
	(child isKindOf: PPCAbstractLiteralNode) ifTrue: [  
		changeStatus change.
		^ PPCNotLiteralNode new
			name: self name;
			literal: self child literal;
			yourself
	]. 

	(child isKindOf: PPCMessagePredicateNode) ifTrue: [  
		changeStatus change.
		^ PPCNotMessagePredicateNode new
			name: self name;
			message: child message;
			yourself
	].

	(child isKindOf: PPCCharSetPredicateNode) ifTrue: [  
		changeStatus change.
		^ PPCNotCharSetPredicateNode new
			name: self name;
			predicate: child predicate;
			yourself
	].

	"Can do the fast version, because we throw away the result and return  nil"
	(self allNodes anySatisfy: [ :node | node asFast ~= node ]) ifTrue: [  
		changeStatus change.
		self replace: child with: (child transform: [:node | node asFast]).
	]
! !