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

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

rename model factory's statement methods into fragment methods, and make them return a fragment directly.

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

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