| | 35 | == Example == |
| | 36 | Here is a program that adds the elements in a file. The commandline argument is the filename. Inputs are the filename, the contents of the file, and the parameter B which is an upper bound on the number of elements that will be read. Output is to stdout. |
| | 37 | |
| | 38 | How do we annotate this file in such a way that it does not interfere with compilation, but provides all the information we need to build a TASS model? |
| | 39 | |
| | 40 | |
| | 41 | {{{ |
| | 42 | #include<stdio.h> |
| | 43 | #include<assert.h> |
| | 44 | /*@ _TASS_input {B>0} */ |
| | 45 | #define B 3 |
| | 46 | |
| | 47 | double sum = 0.0; |
| | 48 | |
| | 49 | /* arguments to main are inputs */ |
| | 50 | int main(int argc, char* argv[]) { |
| | 51 | char *filename; |
| | 52 | double result = 0.0; |
| | 53 | FILE *fp; |
| | 54 | double tmp; |
| | 55 | int i = 0; |
| | 56 | |
| | 57 | if (argc != 2) { |
| | 58 | printf("Usage: realadder filename\ |
| | 59 | "); |
| | 60 | fflush(stdout); |
| | 61 | return 1; |
| | 62 | } |
| | 63 | filename = argv[1]; |
| | 64 | fp = fopen(filename, "r"); |
| | 65 | assert(fp); |
| | 66 | while (1) { |
| | 67 | /*@ _TASS_corresponds spec@label */ |
| | 68 | int more = fscanf(fp, "%lf", &tmp); |
| | 69 | |
| | 70 | /*@ _TASS_assume(tmp>=0.0) */ |
| | 71 | if (more == EOF) break; |
| | 72 | i++; |
| | 73 | if (i>B) { |
| | 74 | printf("Too many numbers! Limit is %d.\ |
| | 75 | ", B); |
| | 76 | fflush(stdout); |
| | 77 | return 2; |
| | 78 | } |
| | 79 | sum += tmp; |
| | 80 | } |
| | 81 | printf("The sum is %lf\ |
| | 82 | ", sum); |
| | 83 | fclose(fp); |
| | 84 | return 0; |
| | 85 | } |
| | 86 | }}} |
| | 87 | |
| | 88 | Idea: the result of fscanf can be represented as a symbolic function. In other words, add functions: |
| | 89 | {{{ |
| | 90 | _TASS_read_int(string filename, int position) |
| | 91 | _TASS_read_real(string filename, int position) |
| | 92 | }}} |
| | 93 | Or something like that. |
| | 94 | |
| | 95 | |
| | 96 | |
| | 97 | Questions: |
| | 98 | * do we need to add char (and string?) datatypes to TASS? |
| | 99 | |