]> git.sur5r.net Git - cc65/blob - test/ref/otccex.c
added tests as prepared by oliver
[cc65] / test / ref / otccex.c
1 /*
2   !!DESCRIPTION!! OTCC Example (simple K&R Style)
3   !!ORIGIN!!      OTCC
4   !!LICENCE!!     GPL (?), read COPYING.GPL
5 */
6
7 /*
8  * Sample OTCC C example. You can uncomment the first line and install
9  * otcc in /usr/local/bin to make otcc scripts !  
10  */
11
12 /* Any preprocessor directive except #define are ignored. We put this
13    include so that a standard C compiler can compile this code too. */
14 #include <stdio.h>
15 #include <limits.h>
16
17 /* defines are handled, but macro arguments cannot be given. No
18    recursive defines are tolerated */
19 #define DEFAULT_BASE 10
20
21 #ifdef NO_IMPLICIT_FUNC_PROTOTYPES
22 help(char *name);
23 #endif
24
25 /*
26  * Only old style K&R prototypes are parsed. Only int arguments are
27  * allowed (implicit types).
28  * 
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.  
32  */
33 fib(n)
34 {
35     printf("[fib(%d)]", n);
36     if (n <= 2)
37         return 1;
38     else
39         return fib(n-1) + fib(n-2);
40 }
41
42 /* Identifiers are parsed the same way as C: begins with letter or
43    '_', and then letters, '_' or digits */
44 long fact(n)
45 {
46     /* local variables can be declared. Only 'int' type is supported */
47     int i;
48     long r;
49     r = 1;
50     /* 'while' and 'for' loops are supported */
51     for(i=2;i<=n;i++)
52         r = r * i;
53     return r;
54 }
55
56 /* Well, we could use printf, but it would be too easy */
57 print_num(long n,int b)
58 {
59     int tab, p, c;
60     /* Numbers can be entered in decimal, hexadecimal ('0x' prefix) and
61        octal ('0' prefix) */
62     /* more complex programs use malloc */
63     tab = malloc(0x100); 
64     p = tab;
65     while (1) {
66         c = n % b;
67         /* Character constants can be used */
68         if (c >= 10)
69             c = c + 'a' - 10;
70         else
71             c = c + '0';
72         *(char *)p = c;
73         p++;
74         n = n / b;
75         /* 'break' is supported */
76         if (n == 0)
77             break;
78     }
79     while (p != tab) {
80         p--;
81         printf("%c", *(char *)p);
82     }
83     free(tab);
84 }
85
86 /* 'main' takes standard 'argc' and 'argv' parameters */
87 mymain(int argc,char **argv)
88 {
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 */
93     int s, n, f, base;
94     
95     
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. */
102         s = *(int *)argv;
103         help(s);
104         return 1;
105     }
106     /* Any libc function can be used because OTCC uses dynamic linking */
107     n = atoi(argv[1]);
108     base = DEFAULT_BASE;
109     if (argc >= 3) {
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");
114             return 1;
115         }
116     }
117     printf("fib(%d) =\n", n);
118     print_num(fib(n), base);
119     printf("\n");
120
121     printf("fact(%d) = ", n);
122     if (n > 12) {
123         printf("Overflow");
124     } else {
125         /* why not using a function pointer ? */
126         f = &fact;
127         print_num((*(long (*)())f)(n), base);
128     }
129     printf("\n");
130     return 0;
131 }
132
133 /* functions can be used before being defined */
134 help(char *name)
135 {
136     printf("usage: %s n [base]\n", name);
137     printf("Compute fib(n) and fact(n) and output the result in base 'base'\n");
138 }
139
140 int main(void)
141 {
142     char *argv[3];
143     argv[0]="";
144     argv[1]="10"; /* n */
145     argv[2]="8"; /* base */
146     mymain(3, argv);
147     return 0;
148 }