@@ -21,31 +21,29 @@ namespace Mellis.Lang.Python3.Tests.Compiler
21
21
public class CompileExpressionTests
22
22
{
23
23
[ DataTestMethod ]
24
- [ DataRow ( typeof ( ArithmeticAdd ) , OperatorCode . AAdd , DisplayName = "comp op +" ) ]
25
- [ DataRow ( typeof ( ArithmeticSubtract ) , OperatorCode . ASub , DisplayName = "comp op -" ) ]
26
- [ DataRow ( typeof ( ArithmeticMultiply ) , OperatorCode . AMul , DisplayName = "comp op *" ) ]
27
- [ DataRow ( typeof ( ArithmeticDivide ) , OperatorCode . ADiv , DisplayName = "comp op /" ) ]
28
- [ DataRow ( typeof ( ArithmeticFloor ) , OperatorCode . AFlr , DisplayName = "comp op //" ) ]
29
- [ DataRow ( typeof ( ArithmeticModulus ) , OperatorCode . AMod , DisplayName = "comp op %" ) ]
30
- [ DataRow ( typeof ( ArithmeticPower ) , OperatorCode . APow , DisplayName = "comp op **" ) ]
31
- [ DataRow ( typeof ( BinaryAnd ) , OperatorCode . BAnd , DisplayName = "comp op a&b" ) ]
32
- [ DataRow ( typeof ( BinaryLeftShift ) , OperatorCode . BLsh , DisplayName = "comp op a<<b" ) ]
33
- [ DataRow ( typeof ( BinaryRightShift ) , OperatorCode . BRsh , DisplayName = "comp op a>>b" ) ]
34
- [ DataRow ( typeof ( BinaryOr ) , OperatorCode . BOr , DisplayName = "comp op a|b" ) ]
35
- [ DataRow ( typeof ( BinaryXor ) , OperatorCode . BXor , DisplayName = "comp op a^b" ) ]
36
- [ DataRow ( typeof ( CompareEquals ) , OperatorCode . CEq , DisplayName = "comp op a==b" ) ]
37
- [ DataRow ( typeof ( CompareNotEquals ) , OperatorCode . CNEq , DisplayName = "comp op a!=b" ) ]
38
- [ DataRow ( typeof ( CompareGreaterThan ) , OperatorCode . CGt , DisplayName = "comp op a>b" ) ]
39
- [ DataRow ( typeof ( CompareGreaterThanOrEqual ) , OperatorCode . CGtEq , DisplayName = "comp op a>=b" ) ]
40
- [ DataRow ( typeof ( CompareLessThan ) , OperatorCode . CLt , DisplayName = "comp op a<b" ) ]
41
- [ DataRow ( typeof ( CompareLessThanOrEqual ) , OperatorCode . CLtEq , DisplayName = "comp op a<=b" ) ]
42
- [ DataRow ( typeof ( CompareIn ) , OperatorCode . CIn , DisplayName = "comp op a in b" ) ]
43
- [ DataRow ( typeof ( CompareInNot ) , OperatorCode . CNIn , DisplayName = "comp op a not in b" ) ]
44
- [ DataRow ( typeof ( CompareIs ) , OperatorCode . CIs , DisplayName = "comp op a is b" ) ]
45
- [ DataRow ( typeof ( CompareIsNot ) , OperatorCode . CIsN , DisplayName = "comp op a is not b" ) ]
46
- [ DataRow ( typeof ( LogicalAnd ) , OperatorCode . LAnd , DisplayName = "comp op a&&b" ) ]
47
- [ DataRow ( typeof ( LogicalOr ) , OperatorCode . LOr , DisplayName = "comp op a||b" ) ]
48
- public void CompileBinaryTests ( Type operatorType , OperatorCode expectedCode )
24
+ [ DataRow ( typeof ( ArithmeticAdd ) , BasicOperatorCode . AAdd , DisplayName = "comp op +" ) ]
25
+ [ DataRow ( typeof ( ArithmeticSubtract ) , BasicOperatorCode . ASub , DisplayName = "comp op -" ) ]
26
+ [ DataRow ( typeof ( ArithmeticMultiply ) , BasicOperatorCode . AMul , DisplayName = "comp op *" ) ]
27
+ [ DataRow ( typeof ( ArithmeticDivide ) , BasicOperatorCode . ADiv , DisplayName = "comp op /" ) ]
28
+ [ DataRow ( typeof ( ArithmeticFloor ) , BasicOperatorCode . AFlr , DisplayName = "comp op //" ) ]
29
+ [ DataRow ( typeof ( ArithmeticModulus ) , BasicOperatorCode . AMod , DisplayName = "comp op %" ) ]
30
+ [ DataRow ( typeof ( ArithmeticPower ) , BasicOperatorCode . APow , DisplayName = "comp op **" ) ]
31
+ [ DataRow ( typeof ( BinaryAnd ) , BasicOperatorCode . BAnd , DisplayName = "comp op a&b" ) ]
32
+ [ DataRow ( typeof ( BinaryLeftShift ) , BasicOperatorCode . BLsh , DisplayName = "comp op a<<b" ) ]
33
+ [ DataRow ( typeof ( BinaryRightShift ) , BasicOperatorCode . BRsh , DisplayName = "comp op a>>b" ) ]
34
+ [ DataRow ( typeof ( BinaryOr ) , BasicOperatorCode . BOr , DisplayName = "comp op a|b" ) ]
35
+ [ DataRow ( typeof ( BinaryXor ) , BasicOperatorCode . BXor , DisplayName = "comp op a^b" ) ]
36
+ [ DataRow ( typeof ( CompareEquals ) , BasicOperatorCode . CEq , DisplayName = "comp op a==b" ) ]
37
+ [ DataRow ( typeof ( CompareNotEquals ) , BasicOperatorCode . CNEq , DisplayName = "comp op a!=b" ) ]
38
+ [ DataRow ( typeof ( CompareGreaterThan ) , BasicOperatorCode . CGt , DisplayName = "comp op a>b" ) ]
39
+ [ DataRow ( typeof ( CompareGreaterThanOrEqual ) , BasicOperatorCode . CGtEq , DisplayName = "comp op a>=b" ) ]
40
+ [ DataRow ( typeof ( CompareLessThan ) , BasicOperatorCode . CLt , DisplayName = "comp op a<b" ) ]
41
+ [ DataRow ( typeof ( CompareLessThanOrEqual ) , BasicOperatorCode . CLtEq , DisplayName = "comp op a<=b" ) ]
42
+ [ DataRow ( typeof ( CompareIn ) , BasicOperatorCode . CIn , DisplayName = "comp op a in b" ) ]
43
+ [ DataRow ( typeof ( CompareInNot ) , BasicOperatorCode . CNIn , DisplayName = "comp op a not in b" ) ]
44
+ [ DataRow ( typeof ( CompareIs ) , BasicOperatorCode . CIs , DisplayName = "comp op a is b" ) ]
45
+ [ DataRow ( typeof ( CompareIsNot ) , BasicOperatorCode . CIsN , DisplayName = "comp op a is not b" ) ]
46
+ public void CompileBasicBinaryTests ( Type operatorType , BasicOperatorCode expectedCode )
49
47
{
50
48
// Arrange
51
49
var compiler = new PyCompiler ( ) ;
@@ -57,7 +55,7 @@ public void CompileBinaryTests(Type operatorType, OperatorCode expectedCode)
57
55
out Mock < ExpressionNode > exprRhsMock ,
58
56
out NopOp exprRhsOp ) ;
59
57
60
- var opNode = ( BinaryOperator ) Activator . CreateInstance ( operatorType ,
58
+ var opNode = ( BinaryOperator ) Activator . CreateInstance ( operatorType ,
61
59
exprLhsMock . Object , exprRhsMock . Object ) ;
62
60
63
61
// Act
@@ -74,11 +72,11 @@ public void CompileBinaryTests(Type operatorType, OperatorCode expectedCode)
74
72
}
75
73
76
74
[ DataTestMethod ]
77
- [ DataRow ( typeof ( ArithmeticNegative ) , OperatorCode . ANeg , DisplayName = "comp op +b" ) ]
78
- [ DataRow ( typeof ( ArithmeticPositive ) , OperatorCode . APos , DisplayName = "comp op -b" ) ]
79
- [ DataRow ( typeof ( BinaryNot ) , OperatorCode . BNot , DisplayName = "comp op ~b" ) ]
80
- [ DataRow ( typeof ( LogicalNot ) , OperatorCode . LNot , DisplayName = "comp op !b" ) ]
81
- public void CompileUnaryTests ( Type operatorType , OperatorCode expectedCode )
75
+ [ DataRow ( typeof ( ArithmeticNegative ) , BasicOperatorCode . ANeg , DisplayName = "comp op +b" ) ]
76
+ [ DataRow ( typeof ( ArithmeticPositive ) , BasicOperatorCode . APos , DisplayName = "comp op -b" ) ]
77
+ [ DataRow ( typeof ( BinaryNot ) , BasicOperatorCode . BNot , DisplayName = "comp op ~b" ) ]
78
+ [ DataRow ( typeof ( LogicalNot ) , BasicOperatorCode . LNot , DisplayName = "comp op !b" ) ]
79
+ public void CompileBasicUnaryTests ( Type operatorType , BasicOperatorCode expectedCode )
82
80
{
83
81
// Arrange
84
82
var compiler = new PyCompiler ( ) ;
@@ -87,7 +85,7 @@ public void CompileUnaryTests(Type operatorType, OperatorCode expectedCode)
87
85
out Mock < ExpressionNode > exprMock ,
88
86
out NopOp exprOp ) ;
89
87
90
- var opNode = ( UnaryOperator ) Activator . CreateInstance ( operatorType ,
88
+ var opNode = ( UnaryOperator ) Activator . CreateInstance ( operatorType ,
91
89
SourceReference . ClrSource ,
92
90
exprMock . Object ) ;
93
91
@@ -102,6 +100,60 @@ public void CompileUnaryTests(Type operatorType, OperatorCode expectedCode)
102
100
exprMock . Verify ( o => o . Compile ( compiler ) , Times . Once ) ;
103
101
}
104
102
103
+ [ TestMethod ]
104
+ public void CompileShortCircuitAnd ( )
105
+ {
106
+ CompileShortCircuit ( typeof ( LogicalAnd ) , jumpOverRhsIfTrue : false ) ;
107
+ }
108
+
109
+ [ TestMethod ]
110
+ public void CompileShortCircuitOr ( )
111
+ {
112
+ CompileShortCircuit ( typeof ( LogicalOr ) , jumpOverRhsIfTrue : true ) ;
113
+ }
114
+
115
+ private static void CompileShortCircuit ( Type operatorType , bool jumpOverRhsIfTrue )
116
+ {
117
+ // Arrange
118
+ var compiler = new PyCompiler ( ) ;
119
+ compiler . CreateAndSetup (
120
+ out Mock < ExpressionNode > exprLhsMock ,
121
+ out NopOp exprLhsOp ) ;
122
+
123
+ compiler . CreateAndSetup (
124
+ out Mock < ExpressionNode > exprRhsMock ,
125
+ out NopOp exprRhsOp ) ;
126
+
127
+ var opNode = ( BinaryOperator ) Activator . CreateInstance ( operatorType ,
128
+ exprLhsMock . Object , exprRhsMock . Object ) ;
129
+
130
+ // Act
131
+ opNode . Compile ( compiler ) ;
132
+
133
+ // Assert
134
+ Assert . That . IsExpectedOpCode ( compiler , 0 , exprLhsOp ) ;
135
+ Assert . That . IsOpCode < VarPop > ( compiler , 2 ) ;
136
+ Assert . That . IsExpectedOpCode ( compiler , 3 , exprRhsOp ) ;
137
+
138
+ Assert . AreEqual ( 4 , compiler . Count ) ;
139
+
140
+ if ( jumpOverRhsIfTrue )
141
+ {
142
+ var jump = Assert . That . IsOpCode < JumpIfTrue > ( compiler , 1 ) ;
143
+ Assert . IsTrue ( jump . Peek ) ;
144
+ Assert . AreEqual ( 4 , jump . Target ) ;
145
+ }
146
+ else
147
+ {
148
+ var jump = Assert . That . IsOpCode < JumpIfFalse > ( compiler , 1 ) ;
149
+ Assert . IsTrue ( jump . Peek ) ;
150
+ Assert . AreEqual ( 4 , jump . Target ) ;
151
+ }
152
+
153
+ exprLhsMock . Verify ( o => o . Compile ( compiler ) , Times . Once ) ;
154
+ exprRhsMock . Verify ( o => o . Compile ( compiler ) , Times . Once ) ;
155
+ }
156
+
105
157
[ TestMethod ]
106
158
public void IdentifierTest ( )
107
159
{
@@ -154,7 +206,7 @@ void Action()
154
206
}
155
207
156
208
// Act
157
- var ex = Assert . ThrowsException < SyntaxUncompilableException > ( ( Action ) Action ) ;
209
+ var ex = Assert . ThrowsException < SyntaxUncompilableException > ( ( Action ) Action ) ;
158
210
159
211
// Assert
160
212
Assert . That . ErrorSyntaxFormatArgsEqual ( ex ,
@@ -202,9 +254,8 @@ public void FunctionCallSingleArgTest()
202
254
const int expectedLiteral = 5 ;
203
255
const string expectedIdentifier = "foo" ;
204
256
205
- var args = new [ ]
206
- {
207
- new LiteralInteger ( SourceReference . ClrSource , expectedLiteral ) ,
257
+ var args = new [ ] {
258
+ new LiteralInteger ( SourceReference . ClrSource , expectedLiteral ) ,
208
259
} ;
209
260
210
261
var callNode = new FunctionCall (
@@ -241,10 +292,9 @@ public void FunctionCallMultipleArgsTest()
241
292
242
293
const string expectedIdentifier = "foo" ;
243
294
244
- var args = new ExpressionNode [ ]
245
- {
295
+ var args = new ExpressionNode [ ] {
246
296
new LiteralInteger ( SourceReference . ClrSource , expectedLiteral1 ) ,
247
- new LiteralString ( SourceReference . ClrSource , expectedLiteral2 ) ,
297
+ new LiteralString ( SourceReference . ClrSource , expectedLiteral2 ) ,
248
298
new LiteralBoolean ( SourceReference . ClrSource , expectedLiteral3 ) ,
249
299
} ;
250
300
@@ -260,7 +310,7 @@ public void FunctionCallMultipleArgsTest()
260
310
// Assert
261
311
var foo = Assert . That . IsOpCode < VarGet > ( compiler , 0 ) ;
262
312
Assert . AreEqual ( expectedIdentifier , foo . Identifier ) ;
263
-
313
+
264
314
Assert . That . IsPushLiteralOpCode ( expectedLiteral1 , compiler , 1 ) ;
265
315
Assert . That . IsPushLiteralOpCode ( expectedLiteral2 , compiler , 2 ) ;
266
316
Assert . That . IsPushLiteralOpCode ( expectedLiteral3 , compiler , 3 ) ;
0 commit comments