2 !!DESCRIPTION!! OTCC Example (simple K&R Style)
4 !!LICENCE!! GPL (?), read COPYING.GPL
8 * Sample OTCC C example. You can uncomment the first line and install
9 * otcc in /usr/local/bin to make otcc scripts !
12 /* Any preprocessor directive except #define are ignored. We put this
13 include so that a standard C compiler can compile this code too. */
17 /* defines are handled, but macro arguments cannot be given. No
18 recursive defines are tolerated */
19 #define DEFAULT_BASE 10
21 #ifdef NO_IMPLICIT_FUNC_PROTOTYPES
26 * Only old style K&R prototypes are parsed. Only int arguments are
27 * allowed (implicit types).
29 * By benchmarking the execution time of this function (for example
30 * for fib(35)), you'll notice that OTCC is quite fast because it
31 * generates native i386 machine code.
35 printf("[fib(%d)]", n);
39 return fib(n-1) + fib(n-2);
42 /* Identifiers are parsed the same way as C: begins with letter or
43 '_', and then letters, '_' or digits */
46 /* local variables can be declared. Only 'int' type is supported */
50 /* 'while' and 'for' loops are supported */
56 /* Well, we could use printf, but it would be too easy */
57 print_num(long n,int b)
60 /* Numbers can be entered in decimal, hexadecimal ('0x' prefix) and
62 /* more complex programs use malloc */
67 /* Character constants can be used */
75 /* 'break' is supported */
81 printf("%c", *(char *)p);
86 /* 'main' takes standard 'argc' and 'argv' parameters */
87 mymain(int argc,char **argv)
89 /* no local name space is supported, but local variables ARE
90 supported. As long as you do not use a globally defined
91 variable name as local variable (which is a bad habbit), you
92 won't have any problem */
96 /* && and || operator have the same semantics as C (left to right
97 evaluation and early exit) */
98 if (argc != 2 && argc != 3) {
99 /* '*' operator is supported with explicit casting to 'int *',
100 'char *' or 'int (*)()' (function pointer). Of course, 'int'
101 are supposed to be used as pointers too. */
106 /* Any libc function can be used because OTCC uses dynamic linking */
110 base = atoi(argv[2]);
111 if (base < 2 || base > 36) {
112 /* external variables can be used too (here: 'stderr') */
113 fprintf(stdout, "Invalid base\n");
117 printf("fib(%d) =\n", n);
118 print_num(fib(n), base);
121 printf("fact(%d) = ", n);
125 /* why not using a function pointer ? */
127 print_num((*(long (*)())f)(n), base);
133 /* functions can be used before being defined */
136 printf("usage: %s n [base]\n", name);
137 printf("Compute fib(n) and fact(n) and output the result in base 'base'\n");
144 argv[1]="10"; /* n */
145 argv[2]="8"; /* base */