Skip to content

Commit 91c2712

Browse files
author
Gwenael Casaccio
committed
Update the visitor pattern
1 parent 79b88db commit 91c2712

13 files changed

+114
-10
lines changed

IRFunction.st

+6-5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ Instruction subclass: IRFunction [
8989
^ variables at: aSymbol ifAbsent: aBlock
9090
]
9191

92+
variablesDo: aOneArgBlock [
93+
94+
^ variables do: aOneArgBlock
95+
]
96+
9297
instructions [
9398

9499
^ instructions
@@ -106,11 +111,7 @@ Instruction subclass: IRFunction [
106111

107112
accept: aVisitor [
108113

109-
aVisitor visitFunction: self.
110-
variables do: [ :each |
111-
each accept: aVisitor ].
112-
instructions do: [ :each |
113-
each accept: aVisitor ]
114+
aVisitor visitFunction: self
114115
]
115116
]
116117

IRInterpret.st

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ IRVisitor subclass: IRInterpret [
3535
i := 0.
3636
aFunction argumentDo: [ :each |
3737
i := i + 1.
38-
variables at: each name put: (argsValue at: i) ]
38+
variables at: each name put: (argsValue at: i) ].
39+
super visitFunction: aFunction
3940
]
4041

4142
variableValue: aSymbol [

IRPrinter.st

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ IRVisitor subclass: IRPrinter [
2929

3030
| i keywords |
3131
keywords := aFunction name keywords.
32-
keywords size = 1 ifTrue: [ ^ stream
32+
keywords size = 1 ifTrue: [ stream
3333
nextPutAll: keywords first;
34-
nl ].
34+
nl.
35+
^ super visitFunction: aFunction ].
3536
i := 1.
3637
keywords do: [ :each |
3738
stream
3839
nextPutAll: each , ' ', (aFunction argumentAt: i) name, ' ', (aFunction argumentAt: i) type name.
3940
i := i + 1.
4041
i > keywords size ifFalse: [ stream nextPutAll: ' ' ] ].
41-
stream nl
42+
stream nl.
43+
super visitFunction: aFunction
4244
]
4345

4446
visitVariableDeclaration: aVariable [

IRVisitor.st

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ Object subclass: IRVisitor [
1313
]
1414

1515
visitFunction: aFunction [
16+
17+
aFunction variablesDo: [ :each |
18+
each accept: self ].
19+
aFunction instructionsDo: [ :each |
20+
each accept: self ]
1621
]
1722

1823
visitInteger: anInteger [

Ret.st

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
Instruction subclass: Ret [
2+
3+
Ret class >> value: anObject [
4+
]
5+
6+
| value |
7+
8+
value: anobject [
9+
10+
value := anObject
11+
]
12+
13+
value [
14+
15+
^ value
16+
]
217
]
318

Thunder.st

+5
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,10 @@ Object subclass: Thunder [
122122

123123
self addInstruction: (Ret new)
124124
]
125+
126+
ret: anObject [
127+
128+
self addInstruction: (Ret value: anObject)
129+
]
125130
]
126131

UChar.st

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
Type subclass: UChar [
22

3+
UChar class >> cname [
4+
5+
^ 'unsigned char'
6+
]
7+
38
UChar class >> readFrom: aByteArray at: anInteger [
49

510
^ (aByteArray at: anInteger + 1) ifNil: [ self error: 'Memory is not initialized' ]

UInt.st

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
Type subclass: UInt [
2+
3+
UInt class >> cname [
4+
5+
^ 'unsigned int'
6+
]
27
]
38

package.xml

+3
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@
88
<filein>tests/LifeTimeIntervalTest.st</filein>
99
<filein>tests/LinearScanRegisterAllocTest.st</filein>
1010
<filein>tests/InterpretTest.st</filein>
11+
<filein>tests/CTranslatorTest.st</filein>
1112
<file>tests/ThunderTest.st</file>
1213
<file>tests/VariableTest.st</file>
1314
<file>tests/PrinterTest.st</file>
1415
<file>tests/LifeTimeIntervalTest.st</file>
1516
<file>tests/LinearScanRegisterAllocTest.st</file>
1617
<file>tests/InterpretTest.st</file>
18+
<file>tests/CTranslatorTest.st</file>
1719
<sunit>
1820
GSTThunder.ThunderTest
1921
GSTThunder.VariableTest
2022
GSTThunder.PrinterTest
2123
GSTThunder.LifeTimeIntervalTest
2224
GSTThunder.LinearScanRegisterAllocTest
2325
GSTThunder.InterpretTest
26+
GSTThunder.CTranslatorTest
2427
</sunit>
2528
</test>
2629
<filein>Extends.st</filein>

tests/CTranslatorTest.st

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
TestCase subclass: CTranslatorTest [
2+
3+
testFunction [
4+
<category: 'testing'>
5+
6+
| string th |
7+
th := Thunder new.
8+
th
9+
function: #'foo:bar:' parameters: {#nb->UInt. #size->UChar} return: UInt.
10+
11+
string := (CTranslator on: th function) stream contents.
12+
self assert: string = 'unsigned int foobar (unsigned int nb, unsigned char size)
13+
'
14+
]
15+
]
16+

tests/PrinterTest.st

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ a UInt
2828
variable: #b type: Int.
2929

3030
string := (IRPrinter on: th function) stream contents.
31-
3231
self assert: string = 'foo
3332
b Int
3433
a UInt

tr/c.st

+44
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
IRVisitor subclass: CTranslator [
22

3+
CTranslator class >> on: aProgram [
4+
5+
^ self new
6+
initialize;
7+
visitProgram: aProgram;
8+
yourself
9+
]
10+
11+
| stream |
12+
13+
initialize [
14+
15+
stream := WriteStream on: String new
16+
]
17+
18+
stream [
19+
20+
^ stream
21+
]
22+
23+
visitProgram: aProgram [
24+
25+
aProgram accept: self
26+
]
27+
28+
visitFunction: aFunction [
29+
30+
| keywords |
31+
stream nextPutAll: aFunction return cname, ' '.
32+
keywords := aFunction name keywords.
33+
keywords size = 1 ifTrue: [ ^ stream
34+
nextPutAll: keywords first, '( void ) ';
35+
nl ].
36+
keywords do: [ :each | stream nextPutAll: (each copyFrom: 1 to: each size - 1) ].
37+
stream nextPutAll: ' ('.
38+
1 to: keywords size do: [ :i |
39+
stream
40+
nextPutAll: (aFunction argumentAt: i) type cname, ' ', (aFunction argumentAt: i) name.
41+
i = keywords size ifFalse: [ stream nextPutAll: ', ' ] ].
42+
stream
43+
nextPutAll: ')';
44+
nl
45+
]
46+
347
visitVariableDeclaration: aVariable [
448

549
stream

tr/lightning.st

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
IRVisitor subclass: Lightning [
2+
]
3+

0 commit comments

Comments
 (0)