source: CIVL/examples/languageFeatures/conditionalExpression.cvl@ b8ffc0b

1.23 2.0 main test-branch
Last change on this file since b8ffc0b was e01203c, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

fix the bug of null expression type when translating away conditional expression

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@340 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.0 KB
Line 
1int sum(int k){
2 int sum = 0;
3 for(int i = 1; i <= k; i++)
4 sum += i;
5 return sum;
6}
7
8void main(){
9 int n = 2;
10 int i = 1;
11
12 // This would be
13 // Location Z:
14 // when (n > 2) s = sum(5) goto ...
15 // when (!(n > 2)) s = sum(8) goto ...
16 int s = sum(n > 2 ? 5 : 8);
17
18 //$when(n >= 2) $assert s == 9*8/2;
19 $assert s == 9 * (n > 2 ? 5 : 8) / 2; //has exception
20
21 // This should be translated into
22 // Location X:
23 // when(i>1) n = 1 goto ...
24 // when (!(i>1)) n = 2
25 n = i > 1 ? 1 : 2;
26 $assert n == 2;
27
28 // This should be translated into
29 // int v0;
30 // if(n > 1) v0 = 1; else v0 = 2;
31 // int v1;
32 // if(i < 1) v1 = 1; else v1 = 2;
33 // n = v0 + v1;
34 n = (n > 1 ? 1 : 2) + (i < 1 ? 1 : 2);
35 $assert n == 3;
36
37 // conditional expression as the guard of a loop
38 for(int j = 0; (j < 5 ? 1 : 0); j++){
39 n = i * j;
40 }
41 $assert n == 4;
42
43 // as the guard of when statement
44 $when(n < 1 ? 0 : 1) n = 3;
45
46 // as the guard of if statement
47 if(n < 1 ? 0 : 1) n = 5;
48 $assert n == 5;
49
50 // nested conditional expression
51 s = (s < 30 ? 10 : (n < 40 ? 50 : 6));
52 $assert s == 50;
53}
Note: See TracBrowser for help on using the repository browser.