Codegen refactoring [2/x]: Added PPCCompiler>>codeIf:then:else:
this allows for more structured coding of conditionals. then/else code is evaluated
inside new block so it may allocate its own temporaries.
--- a/compiler/PPCCodeBlock.st Mon Jun 01 22:02:17 2015 +0100
+++ b/compiler/PPCCodeBlock.st Mon Jun 01 23:15:52 2015 +0100
@@ -9,7 +9,6 @@
category:'PetitCompiler-Compiler-Codegen'
!
-
!PPCCodeBlock class methodsFor:'instance creation'!
new
@@ -22,14 +21,10 @@
add: string
self nl.
- ((Smalltalk respondsTo:#isSmalltalkX) and:[ Smalltalk isSmalltalkX ]) ifTrue:[
- indentation * 4 timesRepeat: [ buffer nextPut: Character space ].
- ] ifFalse:[
- indentation timesRepeat: [ buffer nextPut: Character tab ].
- ].
+ self codeIndent.
self addOnLine: string.
- "Modified: / 21-05-2015 / 15:42:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+ "Modified: / 01-06-2015 / 22:58:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!
addOnLine: string
@@ -50,6 +45,22 @@
].
"Created: / 01-06-2015 / 21:07:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+codeIndent
+ self codeIndent:indentation
+
+ "Created: / 01-06-2015 / 22:58:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+codeIndent: level
+ ((Smalltalk respondsTo:#isSmalltalkX) and:[ Smalltalk isSmalltalkX ]) ifTrue:[
+ level * 4 timesRepeat: [ buffer nextPut: Character space ].
+ ] ifFalse:[
+ level timesRepeat: [ buffer nextPut: Character tab ].
+ ].
+
+ "Created: / 01-06-2015 / 22:58:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !
!PPCCodeBlock methodsFor:'code generation - variables'!
@@ -133,13 +144,3 @@
"Created: / 01-06-2015 / 21:26:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !
-!PPCCodeBlock class methodsFor:'documentation'!
-
-version
- ^ 'Path: stx/goodies/petitparser/compiler/PPCCodeBlock.st, Version: 1.0, User: jv, Time: 2015-06-01T21:57:38.671+01'
-!
-
-version_HG
- ^ 'Path: stx/goodies/petitparser/compiler/PPCCodeBlock.st, Version: 1.0, User: jv, Time: 2015-06-01T21:57:38.671+01'
-! !
-
--- a/compiler/PPCCodeGenerator.st Mon Jun 01 22:02:17 2015 +0100
+++ b/compiler/PPCCodeGenerator.st Mon Jun 01 23:15:52 2015 +0100
@@ -210,13 +210,13 @@
compiler addConstant: node block as: blockId.
compiler codeStoreValueOf: [ self visit: node child ] intoVariable: self retvalVar.
- compiler add: 'error ifFalse: ['.
- compiler codeReturn: blockId, ' value: ', self retvalVar.
- compiler add: '] ifTrue: ['.
- compiler codeReturn: 'failure'.
- compiler add: '].'.
+ compiler codeIf: 'error' then: [
+ compiler codeReturn: 'failure'.
+ ] else: [
+ compiler codeReturn: blockId, ' value: ', self retvalVar.
+ ]
- "Modified: / 05-05-2015 / 14:39:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+ "Modified (format): / 01-06-2015 / 23:03:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!
visitAndNode: node
--- a/compiler/PPCCompiler.st Mon Jun 01 22:02:17 2015 +0100
+++ b/compiler/PPCCompiler.st Mon Jun 01 23:15:52 2015 +0100
@@ -183,6 +183,12 @@
]
!
+codeBlock: contents
+ currentMethod codeBlock: contents
+
+ "Created: / 01-06-2015 / 22:35:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
codeClearError
self add: 'self clearError.'.
!
@@ -239,6 +245,25 @@
"Modified: / 10-05-2015 / 07:39:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!
+codeIf: condition then: then else: else
+ currentMethod
+ add: '(';
+ code: condition;
+ addOnLine: ')'.
+ then notNil ifTrue:[
+ currentMethod
+ addOnLine:' ifTrue:';
+ codeBlock: then.
+ ].
+ else notNil ifTrue:[
+ currentMethod
+ addOnLine:' ifFalse:';
+ codeBlock: else.
+ ].
+
+ "Created: / 01-06-2015 / 22:43:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
codeNextToken
self add: 'self nextToken.'
--- a/compiler/PPCMethod.st Mon Jun 01 22:02:17 2015 +0100
+++ b/compiler/PPCMethod.st Mon Jun 01 23:15:52 2015 +0100
@@ -100,6 +100,37 @@
^ ''
! !
+!PPCMethod methodsFor:'code generation'!
+
+code: aStringOrBlock
+ buffer code: aStringOrBlock.
+
+ "Created: / 01-06-2015 / 22:31:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+codeBlock: contents
+ | outerBlock innerBlock |
+
+ outerBlock := buffer.
+ innerBlock := PPCCodeBlock new.
+ innerBlock indentationLevel: outerBlock indentationLevel + 1.
+ [
+ buffer addOnLine:'['.
+ buffer := innerBlock.
+ self code: contents.
+ ] ensure:[
+ buffer := outerBlock.
+ buffer
+ code: (String streamContents:[:s | innerBlock codeOn: s]);
+ nl;
+ codeIndent.
+
+ buffer addOnLine:']'.
+ ]
+
+ "Created: / 01-06-2015 / 22:33:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
!PPCMethod methodsFor:'code generation - indenting'!
dedent