compiler/PPCNotNode.st
changeset 391 553a5456963b
child 392 9b297f0d949c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compiler/PPCNotNode.st	Sun Oct 26 01:03:31 2014 +0000
@@ -0,0 +1,76 @@
+"{ Package: 'stx:goodies/petitparser/compiler' }"
+
+PPCDelegateNode subclass:#PPCNotNode
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	category:'PetitCompiler-Nodes'
+!
+
+PPCNotNode comment:''
+!
+
+!PPCNotNode methodsFor:'accessing'!
+
+prefix
+	^ #not
+! !
+
+!PPCNotNode methodsFor:'analysis'!
+
+isFirstSetTerminal
+	^ false
+! !
+
+!PPCNotNode methodsFor:'compiling'!
+
+compileWith: compiler effect: effect id: id
+
+	compiler startMethod: id.
+	compiler addVariable: 'memento'.
+	compiler add: (compiler smartRemember: child).
+	
+	compiler startTokenMode.
+	compiler call: (child compileWith: compiler).
+	compiler stopTokenMode.
+	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
+	] 
+! !
+