Debian Codegraph @

src/c/dpp.c

raw symbols SCIP
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * dpp.c - defun preprocessor
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 /*
16 * Usage:
17 * dpp [in-file [out-file]]
18 *
19 * The file named in-file is preprocessed and the output will be
20 * written to the file whose name is out-file. If in-file is "-"
21 * program is read from standard input, while if out-file is "-"
22 * C-program is written to standard output.
23 *
24 *
25 * The function definition:
26 *
27 * @(defun name ({var}*
28 * [&optional {var | (var [initform [svar]])}*]
29 * [&rest var]
30 * [&key {var |
31 * ({var | (keyword var)} [initform [svar]])}*
32 * [&allow_other_keys]]
33 * [&aux {var | (var [initform])}*])
34 *
35 * C-declaration
36 *
37 * @ {
38 *
39 * C-body
40 *
41 * } @)
42 *
43 * name is the name of the lisp function
44 *
45 * &optional may be abbreviated as &o.
46 * &rest may be abbreviated as &r.
47 * &key may be abbreviated as &k.
48 * &allow_other_keys may be abbreviated as &aok.
49 * &aux may be abbreviated as &a.
50 *
51 * Each variable becomes a C variable.
52 *
53 * Each supplied-p parameter becomes a boolean C variable.
54 *
55 * Initforms are C expressions.
56 * If an expression contains non-alphanumeric characters,
57 * it should be surrounded by backquotes (`).
58 *
59 *
60 * Function return:
61 *
62 * @(return {form}*);
63 *
64 * Return function expands into a lexical block {}, so if it's
65 * used inside IF/ELSE, then it should be enclosed, even if we
66 * use sole @(return);, because ";" will be treated as the next
67 * instruction.
68 *
69 * Symbols:
70 *
71 * @'name'
72 *
73 * Expands into a C statement, whole value is the given symbol
74 * from symbols_list.h
75 *
76 * @[name]
77 *
78 * Expands into a C statement, whole value is a fixnum
79 * corresponding to the index in the builtin symbols table of the
80 * given symbol from symbols_list.h. Used for handling type errors.
81 */
82
83 #include <stdlib.h>
84 #include <stdio.h>
85 #include <ctype.h>
86 #include <string.h>
87 #include <stdarg.h>
88 #include <stdbool.h>
89
90 #define DPP
91 #include <ecl/config.h>
92 #include <ecl/config-internal.h>
93 #include "symbols_list.h"
94
95 /* #define POOLSIZE 2048 */
96 #define POOLSIZE 4096
97 #define MAXREQ 16
98 #define MAXOPT 16
99 #define MAXKEY 16
100 #define MAXAUX 16
101 #define MAXRES 16
102
103 #define TRUE 1
104 #define FALSE 0
105
106 FILE *in, *out;
107
108 char filename[BUFSIZ];
109 int lineno;
110 int tab;
111 int tab_save;
112
113 char pool[POOLSIZE];
114 char *poolp;
115
116 char *function;
117 int function_code;
118 char *function_symbol;
119 char *function_c_name;
120
121 char *required[MAXREQ];
122 int nreq;
123
124 int the_env_defined = 0;
125 enum vararg_status_t {
126 VARARG_NOT_DEFINED,
127 VARARG_SIMPLE,
128 VARARG_ECL};
129 enum vararg_status_t vararg_status = VARARG_NOT_DEFINED;
130
131 struct optional {
132 char *o_var;
133 char *o_init;
134 char *o_svar;
135 } optional[MAXOPT];
136 int nopt;
137
138 bool rest_flag;
139 char *rest_var;
140
141 bool key_flag;
142 struct keyword {
143 char *k_key;
144 char *k_var;
145 char *k_init;
146 char *k_svar;
147 } keyword[MAXKEY];
148 int nkey;
149 bool allow_other_keys_flag;
150
151 struct aux {
152 char *a_var;
153 char *a_init;
154 } aux[MAXAUX];
155 int naux;
156
157 char *result[MAXRES];
158 int nres;
159
160 void
161 put_lineno(void)
162 {
163 static int flag = 0;
164 if (flag)
165 fprintf(out, "#line %d\n", lineno);
166 else {
167 flag++;
168 fprintf(out, "#line %d \"%s\"\n", lineno, filename);
169 }
170 }
171
172 void
173 error(char *s, ...)
174 {
175 char msg[2048];
176 va_list args;
177 va_start(args, s);
178 vsnprintf(msg, 2048, s, args);
179 printf("Error in line %d: %s.\n", lineno, msg);
180 va_end(args);
181 exit(1);
182 }
183
184 void
185 error_symbol(char *s)
186 {
187 printf("Error in line %d: illegal symbol %s.\n", lineno, s);
188 exit(1);
189 }
190
191 int
192 readc(void)
193 {
194 int c;
195
196 c = getc(in);
197 if (feof(in)) {
198 if (function != NULL)
199 error("unexpected end of file");
200 exit(0);
201 }
202 if (c == '\n') {
203 lineno++;
204 tab = 0;
205 } else if (c == '\t')
206 tab++;
207 return(c);
208 }
209
210 int
211 nextc(void)
212 {
213 int c;
214
215 while (isspace(c = readc()))
216 ;
217 return(c);
218 }
219
220 void
221 unreadc(int c)
222 {
223 if (c == '\n')
224 --lineno;
225 else if (c == '\t')
226 --tab;
227 ungetc(c, in);
228 }
229
230 void
231 put_tabs(int n)
232 {
233 put_lineno();
234 while (n--)
235 putc('\t', out);
236 }
237
238 void
239 pushc(int c)
240 {
241 if (poolp >= &pool[POOLSIZE])
242 error("buffer pool overflow");
243 *poolp++ = c;
244 }
245
246 void
247 pushstr(const char *s)
248 {
249 while (*s)
250 pushc(*(s++));
251 }
252
253 int
254 search_keyword(const char *name)
255 {
256 int i;
257 char c[256];
258
259 for (i=0; name[i] && i<255; i++)
260 if (name[i] == '_')
261 c[i] = '-';
262 else
263 c[i] = name[i];
264 if (i == 255)
265 error("Too long keyword");
266 c[i] = 0;
267 for (i = 0; cl_symbols[i].name != NULL; i++) {
268 if (cl_symbols[i].name[0] == ':')
269 if (!strcasecmp(c, cl_symbols[i].name+1))
270 return i;
271 }
272 printf("Keyword not found: %s.\n", c);
273 return 0;
274 }
275
276 char *
277 search_symbol(char *name, int *symbol_code, int code)
278 {
279 int i;
280 for (i = 0; cl_symbols[i].name != NULL; i++) {
281 if (!strcasecmp(name, cl_symbols[i].name)) {
282 name = poolp;
283 if (code) {
284 pushstr("ecl_make_fixnum(/*");
285 pushstr(cl_symbols[i].name);
286 pushstr("*/");
287 if (i >= 1000)
288 pushc((i / 1000) % 10 + '0');
289 if (i >= 100)
290 pushc((i / 100) % 10 + '0');
291 if (i >= 10)
292 pushc((i / 10) % 10 + '0');
293 pushc(i % 10 + '0');
294 pushstr(")");
295 pushc(0);
296 } else if (i == 0) {
297 pushstr("ECL_NIL");
298 pushc(0);
299 } else {
300 pushstr("ECL_SYM(\"");
301 pushstr(cl_symbols[i].name);
302 pushstr("\",");
303 if (i >= 1000)
304 pushc((i / 1000) % 10 + '0');
305 if (i >= 100)
306 pushc((i / 100) % 10 + '0');
307 if (i >= 10)
308 pushc((i / 10) % 10 + '0');
309 pushc(i % 10 + '0');
310 pushstr(")");
311 pushc(0);
312 }
313 if (symbol_code)
314 *symbol_code = i;
315 return name;
316 }
317 }
318 return NULL;
319 }
320
321 char *
322 read_symbol(int code)
323 {
324 char c, *name = poolp;
325 char end = code? ']' : '\'';
326
327 c = readc();
328 while (c != end) {
329 if (c == '_') c = '-';
330 pushc(c);
331 c = readc();
332 }
333 pushc(0);
334
335 name = search_symbol(poolp = name, 0, code);
336 if (name == NULL) {
337 name = poolp;
338 printf("\nUnknown symbol: %s\n", name);
339 pushstr("unknown");
340 }
341 return name;
342 }
343
344 char *
345 read_string()
346 {
347 char c, *str = poolp;
348 char end = '"';
349 pushstr("ecl_make_constant_base_string(\"");
350 do {
351 c = readc();
352 pushc(c);
353 } while (c != end);
354 pushstr(", -1)");
355 pushc(0);
356 return str;
357 }
358
359 char *
360 search_function(char *name)
361 {
362 int i;
363 for (i = 0; cl_symbols[i].name != NULL; i++) {
364 if (cl_symbols[i].translation != NULL &&
365 !strcasecmp(name, cl_symbols[i].name)) {
366 name = poolp;
367 pushstr(cl_symbols[i].translation);
368 pushc(0);
369 return name;
370 }
371 }
372 return name;
373 }
374
375 char *
376 read_function()
377 {
378 char c, *name = poolp;
379
380 c = readc();
381 if (c == '"') {
382 c = readc();
383 while (c != '"') {
384 pushc(c);
385 c = readc();
386 }
387 pushc(0);
388 return name;
389 }
390 while (c != '(' && !isspace(c) && c != ')' && c != ',') {
391 if (c == '_') c = '-';
392 pushc(c);
393 c = readc();
394 }
395 unreadc(c);
396 pushc(0);
397 return name;
398 }
399
400 char *
401 translate_function(char *name)
402 {
403 char *output = search_function(name);
404 if (output == NULL) {
405 printf("\nUnknown function: %s\n", name);
406 pushstr("unknown");
407 output = poolp;
408 }
409 return output;
410 }
411
412 char *
413 read_token(void)
414 {
415 int c;
416 int left_paren = 0;
417 char *p;
418
419 p = poolp;
420 c = readc();
421 while (isspace(c))
422 c = readc();
423 do {
424 if (c == '(') {
425 left_paren++;
426 pushc(c);
427 } else if (c == ')') {
428 if (left_paren == 0) {
429 break;
430 } else {
431 left_paren--;
432 pushc(c);
433 }
434 } else if (isspace(c) && left_paren == 0) {
435 do
436 c = readc();
437 while (isspace(c));
438 break;
439 } else if (c == '@') {
440 c = readc();
441 if (c == '\'') {
442 (void)read_symbol(0);
443 poolp--;
444 } else if (c == '[') {
445 (void)read_symbol(1);
446 poolp--;
447 } else if (c == '@') {
448 pushc(c);
449 } else if (c == '"') {
450 read_string();
451 poolp--;
452 } else {
453 char *name;
454 unreadc(c);
455 poolp = name = read_function();
456 (void)translate_function(poolp);
457 }
458 } else {
459 pushc(c);
460 }
461 c = readc();
462 } while (1);
463 unreadc(c);
464 pushc('\0');
465 return(p);
466 }
467
468 void
469 reset(void)
470 {
471 int i;
472
473 the_env_defined = 0;
474 poolp = pool;
475 function = NULL;
476 function_symbol = "";
477 function_c_name = "";
478 nreq = 0;
479 for (i = 0; i < MAXREQ; i++)
480 required[i] = NULL;
481 nopt = 0;
482 for (i = 0; i < MAXOPT; i++)
483 optional[i].o_var
484 = optional[i].o_init
485 = optional[i].o_svar
486 = NULL;
487 rest_flag = FALSE;
488 rest_var = "ARGS";
489 key_flag = FALSE;
490 nkey = 0;
491 for (i = 0; i < MAXKEY; i++)
492 keyword[i].k_key
493 = keyword[i].k_var
494 = keyword[i].k_init
495 = keyword[i].k_svar
496 = NULL;
497 allow_other_keys_flag = FALSE;
498 naux = 0;
499 for (i = 0; i < MAXAUX; i++)
500 aux[i].a_var
501 = aux[i].a_init
502 = NULL;
503 vararg_status = VARARG_NOT_DEFINED;
504 }
505
506 void
507 get_function(void)
508 {
509 function = read_function();
510 function_symbol = search_symbol(function, &function_code, 0);
511 if (function_symbol == NULL) {
512 function_symbol = poolp;
513 pushstr("ECL_NIL");
514 pushc('\0');
515 }
516 function_c_name = translate_function(function);
517 }
518
519 void
520 check_nargs(void)
521 {
522 int narg_declared = cl_symbols[function_code].narg;
523 int nreq_declared = narg_declared >= 0 ? narg_declared : (-narg_declared -1);
524 if (nreq != nreq_declared) {
525 error("Function %s: wrong declaration for number of required arguments, expected %d, but got %d",
526 cl_symbols[function_code].name, nreq, nreq_declared);
527 }
528 if (narg_declared > 0 &&
529 (nopt > 0 || rest_flag || key_flag)) {
530 error("Detected optional, keyword or rest arguments for function with fixed number of arguments");
531 }
532 }
533
534 void
535 get_lambda_list(void)
536 {
537 int c;
538 char *p;
539
540 if ((c = nextc()) != '(')
541 error("( expected");
542 for (;;) {
543 if ((c = nextc()) == ')')
544 return;
545 if (c == '&') {
546 p = read_token();
547 goto _OPT;
548 }
549 unreadc(c);
550 p = read_token();
551 if (nreq >= MAXREQ)
552 error("too many required variables");
553 required[nreq++] = p;
554 }
555
556 _OPT:
557 if (strcmp(p, "optional") != 0 && strcmp(p, "o") != 0)
558 goto _REST;
559 for (;; nopt++) {
560 if ((c = nextc()) == ')')
561 return;
562 if (c == '&') {
563 p = read_token();
564 goto _REST;
565 }
566 if (nopt >= MAXOPT)
567 error("too many optional argument");
568 if (c == '(') {
569 optional[nopt].o_var = read_token();
570 if ((c = nextc()) == ')')
571 continue;
572 unreadc(c);
573 optional[nopt].o_init = read_token();
574 if ((c = nextc()) == ')')
575 continue;
576 unreadc(c);
577 optional[nopt].o_svar = read_token();
578 if (nextc() != ')')
579 error(") expected");
580 } else {
581 unreadc(c);
582 optional[nopt].o_var = read_token();
583 }
584 }
585
586 _REST:
587 if (strcmp(p, "rest") != 0 && strcmp(p, "r") != 0)
588 goto _KEY;
589 rest_flag = TRUE;
590 if ((c = nextc()) == ')' || c == '&')
591 error("&rest var missing");
592 unreadc(c);
593 rest_var = read_token();
594 if ((c = nextc()) == ')')
595 return;
596 if (c != '&')
597 error("& expected");
598 p = read_token();
599
600 _KEY:
601 if (strcmp(p, "key") != 0 && strcmp(p, "k") != 0)
602 goto _AUX;
603 key_flag = TRUE;
604 for (;; nkey++) {
605 if ((c = nextc()) == ')')
606 return;
607 if (c == '&') {
608 p = read_token();
609 if (strcmp(p, "allow_other_keys") == 0 ||
610 strcmp(p, "aok") == 0) {
611 allow_other_keys_flag = TRUE;
612 if ((c = nextc()) == ')')
613 return;
614 if (c != '&')
615 error("& expected");
616 p = read_token();
617 }
618 goto _AUX;
619 }
620 if (nkey >= MAXKEY)
621 error("too many optional argument");
622 if (c == '(') {
623 if ((c = nextc()) == '(') {
624 p = read_token();
625 if (p[0] != ':' || p[1] == '\0')
626 error("keyword expected");
627 keyword[nkey].k_key = p + 1;
628 keyword[nkey].k_var = read_token();
629 if (nextc() != ')')
630 error(") expected");
631 } else {
632 unreadc(c);
633 keyword[nkey].k_key
634 = keyword[nkey].k_var
635 = read_token();
636 }
637 if ((c = nextc()) == ')')
638 continue;
639 unreadc(c);
640 keyword[nkey].k_init = read_token();
641 if ((c = nextc()) == ')')
642 continue;
643 unreadc(c);
644 keyword[nkey].k_svar = read_token();
645 if (nextc() != ')')
646 error(") expected");
647 } else {
648 unreadc(c);
649 keyword[nkey].k_key
650 = keyword[nkey].k_var
651 = read_token();
652 }
653 }
654
655 _AUX:
656 if (strcmp(p, "aux") != 0 && strcmp(p, "a") != 0)
657 error("illegal lambda-list keyword");
658 for (;;) {
659 if ((c = nextc()) == ')')
660 return;
661 if (c == '&')
662 error("illegal lambda-list keyword");
663 if (naux >= MAXAUX)
664 error("too many auxiliary variable");
665 if (c == '(') {
666 aux[naux].a_var = read_token();
667 if ((c = nextc()) == ')')
668 continue;
669 unreadc(c);
670 aux[naux].a_init = read_token();
671 if (nextc() != ')')
672 error(") expected");
673 } else {
674 unreadc(c);
675 aux[naux].a_var = read_token();
676 }
677 naux++;
678 }
679 }
680
681 void
682 get_return(void)
683 {
684 int c;
685
686 nres = 0;
687 for (;;) {
688 if ((c = nextc()) == ')')
689 return;
690 unreadc(c);
691 result[nres++] = read_token();
692 }
693 }
694
695 void
696 put_fhead(void)
697 {
698 int i;
699
700 put_lineno();
701 fprintf(out, "cl_object %s(cl_narg narg", function_c_name);
702 for (i = 0; i < nreq; i++)
703 fprintf(out, ", cl_object %s", required[i]);
704 fprintf(out, ", ...)\n{\n");
705 }
706
707 void
708 put_declaration(void)
709 {
710 int i;
711 int simple_varargs;
712
713 put_lineno();
714 the_env_defined = 1;
715 fprintf(out,
716 "#if defined(__clang__) || defined(__GNUC__)\n"
717 "\t__attribute__((unused)) const cl_env_ptr the_env = ecl_process_env();\n"
718 "#else\n"
719 "\tconst cl_env_ptr the_env = ecl_process_env();\n"
720 "#endif\n");
721 for (i = 0; i < nopt; i++) {
722 put_lineno();
723 fprintf(out, "\tcl_object %s;\n", optional[i].o_var);
724 }
725 for (i = 0; i < nopt; i++)
726 if (optional[i].o_svar != NULL) {
727 put_lineno();
728 fprintf(out, "\tbool %s;\n", optional[i].o_svar);
729 }
730 if (key_flag) {
731 put_lineno();
732 if (nkey) {
733 fprintf(out, "\tstatic cl_object KEYS[%d] = {", nkey);
734 for (i = 0; i < nkey; i++) {
735 if (i > 0)
736 fprintf(out, ", ");
737 fprintf(out, "(cl_object)(cl_symbols+%d)", search_keyword(keyword[i].k_key));
738 }
739 fprintf(out, "};\n");
740 } else {
741 fprintf(out, "\tcl_object *KEYS = NULL;\n");
742 }
743 }
744 for (i = 0; i < nkey; i++) {
745 fprintf(out, "\tcl_object %s;\n", keyword[i].k_var);
746 if (keyword[i].k_svar != NULL)
747 fprintf(out, "\tbool %s;\n", keyword[i].k_svar);
748 }
749 for (i = 0; i < naux; i++) {
750 put_lineno();
751 fprintf(out, "\tcl_object %s;\n", aux[i].a_var);
752 }
753 if (nopt == 0 && !rest_flag && !key_flag) {
754 put_lineno();
755 fprintf(out, "\tif (ecl_unlikely(narg!=%d))", nreq);
756 fprintf(out, "\t FEwrong_num_arguments(ecl_make_fixnum(%d));\n",
757 function_code);
758 } else {
759 simple_varargs = !rest_flag && !key_flag && ((nreq + nopt) < 32);
760 if (key_flag) {
761 put_lineno();
762 /* We do this because Microsoft VC++ does not support arrays of zero size */
763 if (nkey) {
764 fprintf(out, "\tcl_object KEY_VARS[%d];\n", 2*nkey);
765 } else {
766 fprintf(out, "\tcl_object *KEY_VARS = NULL;\n");
767 }
768 }
769 put_lineno();
770 if (simple_varargs) {
771 vararg_status = VARARG_SIMPLE;
772 fprintf(out,"\tva_list %s;\n\tva_start(%s, %s);\n",
773 rest_var, rest_var, ((nreq > 0) ? required[nreq-1] : "narg"));
774 } else {
775 vararg_status = VARARG_ECL;
776 fprintf(out,"\tecl_va_list %s;\n\tecl_va_start(%s, %s, narg, %d);\n",
777 rest_var, rest_var, ((nreq > 0) ? required[nreq-1] : "narg"),
778 nreq);
779 }
780 put_lineno();
781 fprintf(out, "\tif (ecl_unlikely(narg < %d", nreq);
782 if (nopt > 0 && !rest_flag && !key_flag) {
783 fprintf(out, "|| narg > %d", nreq + nopt);
784 }
785 fprintf(out, ")) FEwrong_num_arguments(ecl_make_fixnum(%d));\n", function_code);
786 for (i = 0; i < nopt; i++) {
787 put_lineno();
788 fprintf(out, "\tif (narg > %d) {\n", nreq+i);
789 put_lineno();
790 fprintf(out, simple_varargs?
791 "\t\t%s = va_arg(%s,cl_object);\n":
792 "\t\t%s = ecl_va_arg(%s);\n",
793 optional[i].o_var, rest_var);
794 if (optional[i].o_svar) {
795 put_lineno();
796 fprintf(out, "\t\t%s = TRUE;\n", optional[i].o_svar);
797 }
798 put_lineno();
799 fprintf(out, "\t} else {\n");
800 put_lineno();
801 fprintf(out, "\t\t%s = %s;\n",
802 optional[i].o_var,
803 optional[i].o_init == NULL ? "ECL_NIL" : optional[i].o_init);
804 if (optional[i].o_svar) {
805 put_lineno();
806 fprintf(out, "\t\t%s = FALSE;\n", optional[i].o_svar);
807 }
808 put_lineno();
809 fprintf(out, "\t}\n");
810 }
811 if (key_flag) {
812 put_lineno();
813 fprintf(out, "\tcl_parse_key(ARGS, %d, KEYS, KEY_VARS, NULL, %d);\n",
814 nkey, allow_other_keys_flag);
815 for (i = 0; i < nkey; i++) {
816 put_lineno();
817 fprintf(out, "\tif (KEY_VARS[%d]==ECL_NIL) {\n", nkey+i);
818 if (keyword[i].k_init != NULL) {
819 put_lineno();
820 fprintf(out, "\t %s = %s;\n", keyword[i].k_var, keyword[i].k_init);
821 } else {
822 put_lineno();
823 fprintf(out, "\t %s = ECL_NIL;\n", keyword[i].k_var);
824 }
825 if (keyword[i].k_svar != NULL) {
826 put_lineno();
827 fprintf(out, "\t %s = FALSE;\n", keyword[i].k_svar);
828 }
829 fprintf(out, "\t} else {\n");
830 if (keyword[i].k_svar != NULL) {
831 put_lineno();
832 fprintf(out, "\t %s = TRUE;\n", keyword[i].k_svar);
833 }
834 put_lineno();
835 fprintf(out, "\t %s = KEY_VARS[%d];\n\t}\n", keyword[i].k_var, i);
836 }
837 }
838 }
839 for (i = 0; i < naux; i++) {
840 put_lineno();
841 fprintf(out, "\t%s = %s;\n", aux[i].a_var,
842 aux[i].a_init == NULL ? "ECL_NIL" : aux[i].a_init);
843 }
844 }
845
846 void unregister_varargs()
847 {
848 if (vararg_status == VARARG_SIMPLE) {
849 fprintf(out, "va_end(%s);\n", rest_var);
850 }
851 else if (vararg_status == VARARG_ECL) {
852 fprintf(out, "ecl_va_end(%s);\n", rest_var);
853 }
854 }
855
856 void
857 put_return(void)
858 {
859 int i, t;
860
861 t = tab_save+1;
862
863 fprintf(out, "{\n");
864 if (!the_env_defined) {
865 put_tabs(t);
866 fprintf(out, "const cl_env_ptr the_env = ecl_process_env();\n");
867 }
868 if (nres == 0) {
869 fprintf(out, "the_env->nvalues = 0; return ECL_NIL;\n");
870 } else {
871 put_tabs(t);
872 for (i = 0; i < nres; i++) {
873 put_tabs(t);
874 fprintf(out, "cl_object __value%d = %s;\n", i, result[i]);
875 }
876 put_tabs(t);
877 fprintf(out, "the_env->nvalues = %d;\n", nres);
878 for (i = nres-1; i >= 0; i--) {
879 put_tabs(t);
880 fprintf(out, "the_env->values[%d] = __value%d;\n", i, i);
881 }
882 put_tabs(t);
883 unregister_varargs();
884 put_tabs(t);
885 fprintf(out, "return __value0;\n");
886 }
887 put_tabs(tab_save);
888 fprintf(out, "}\n");
889 }
890
891 int
892 jump_to_at(void)
893 {
894 int c;
895 GO_ON:
896 while ((c = readc()) != '@')
897 putc(c, out);
898 if ((c = readc()) == '@') {
899 putc(c, out);
900 goto GO_ON;
901 }
902 return c;
903 }
904
905 void
906 main_loop(void)
907 {
908 int c;
909 int in_defun=0;
910 char *p;
911
912 lineno = 1;
913
914 reset();
915 put_lineno();
916 LOOP:
917 c = jump_to_at();
918 if (c == ')') {
919 if (!in_defun)
920 error("unmatched @) found");
921 in_defun = 0;
922 putc('}',out);
923 reset();
924 goto LOOP;
925 } else if (c == '\'') {
926 char *p;
927 char* tmp = poolp;
928 p = read_symbol(0);
929 pushc('\0');
930 fprintf(out,"%s",p);
931 poolp = tmp;
932 goto LOOP;
933 } else if (c == '[') {
934 char *p;
935 char * tmp = poolp;
936 p = read_symbol(1);
937 pushc('\0');
938 fprintf(out,"%s",p);
939 poolp = tmp;
940 goto LOOP;
941 } else if (c == '"') {
942 char *p;
943 char * tmp = poolp;
944 p = read_string();
945 fprintf(out,"%s",p);
946 poolp = tmp;
947 goto LOOP;
948 } else if (c != '(') {
949 char *p;
950 char * tmp = poolp;
951 unreadc(c);
952 //poolp = pool;
953 p = read_function();
954 fprintf(out,"%s",translate_function(p));
955 poolp = tmp;
956 goto LOOP;
957 }
958 p = read_token();
959 if (strcmp(p, "defun") == 0) {
960 if (in_defun)
961 error("@) expected before new function definition");
962 in_defun = 1;
963 get_function();
964 get_lambda_list();
965 check_nargs();
966 put_fhead();
967 put_lineno();
968 c = jump_to_at();
969 put_declaration();
970 put_lineno();
971 } else if (strcmp(p, "return") == 0) {
972 tab_save = tab;
973 get_return();
974 put_return();
975 } else
976 error_symbol(p);
977 goto LOOP;
978 }
979
980 int
981 main(int argc, char **argv)
982 {
983 char outfile[BUFSIZ];
984 #ifdef _MSC_VER
985 char *p;
986 #endif
987 if (argc < 2 || !strcmp(argv[1],"-")) {
988 in = stdin;
989 strcpy(filename, "-");
990 } else {
991 in = fopen(argv[1],"r");
992 strncpy(filename, argv[1], BUFSIZ-1);
993 filename[BUFSIZ-1] = '\0';
994 }
995 #ifdef _MSC_VER
996 /* Convert all backslashes in filename into slashes,
997 * to avoid warnings when compiling with MSVC
998 */
999 for ( p=filename; *p; p++ )
1000 if ( *p == '\\' )
1001 *p = '/';
1002 #endif
1003 if (argc < 3 || !strcmp(argv[2],"-")) {
1004 out = stdout;
1005 strcpy(outfile, "-");
1006 } else {
1007 out = fopen(argv[2],"w");
1008 strncpy(outfile, argv[2], BUFSIZ-1);
1009 outfile[BUFSIZ-1] = '\0';
1010 }
1011 if (in == NULL)
1012 error("can't open input file");
1013 if (out == NULL)
1014 error("can't open output file");
1015 printf("dpp: %s -> %s\n", filename, outfile);
1016 main_loop();
1017 return 0;
1018 }