source: CIVL/examples/languageFeatures/conditionalExpression.cvl

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

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