altos/telegps-v2.0: git ignore make results
[fw/altos] / src / lisp / ao_lisp_error.c
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include "ao_lisp.h"
16 #include <stdarg.h>
17
18 void
19 ao_lisp_error_poly(char *name, ao_poly poly, ao_poly last)
20 {
21         int first = 1;
22         printf("\t\t%s(", name);
23         if (ao_lisp_poly_type(poly) == AO_LISP_CONS) {
24                 if (poly) {
25                         while (poly) {
26                                 struct ao_lisp_cons *cons = ao_lisp_poly_cons(poly);
27                                 if (!first)
28                                         printf("\t\t         ");
29                                 else
30                                         first = 0;
31                                 ao_lisp_poly_print(cons->car);
32                                 printf("\n");
33                                 if (poly == last)
34                                         break;
35                                 poly = cons->cdr;
36                         }
37                         printf("\t\t         )\n");
38                 } else
39                         printf(")\n");
40         } else {
41                 ao_lisp_poly_print(poly);
42                 printf("\n");
43         }
44 }
45
46 static void tabs(int indent)
47 {
48         while (indent--)
49                 printf("\t");
50 }
51
52 void
53 ao_lisp_error_frame(int indent, char *name, struct ao_lisp_frame *frame)
54 {
55         int                     f;
56
57         tabs(indent);
58         printf ("%s{", name);
59         if (frame) {
60                 if (frame->type & AO_LISP_FRAME_PRINT)
61                         printf("recurse...");
62                 else {
63                         frame->type |= AO_LISP_FRAME_PRINT;
64                         for (f = 0; f < frame->num; f++) {
65                                 if (f != 0) {
66                                         tabs(indent);
67                                         printf("         ");
68                                 }
69                                 ao_lisp_poly_print(frame->vals[f].atom);
70                                 printf(" = ");
71                                 ao_lisp_poly_print(frame->vals[f].val);
72                                 printf("\n");
73                         }
74                         if (frame->prev)
75                                 ao_lisp_error_frame(indent + 1, "prev:   ", ao_lisp_poly_frame(frame->prev));
76                         frame->type &= ~AO_LISP_FRAME_PRINT;
77                 }
78                 tabs(indent);
79                 printf("        }\n");
80         } else
81                 printf ("}\n");
82 }
83
84
85 ao_poly
86 ao_lisp_error(int error, char *format, ...)
87 {
88         va_list args;
89
90         ao_lisp_exception |= error;
91         va_start(args, format);
92         vprintf(format, args);
93         va_end(args);
94         printf("\n");
95         printf("Value: "); ao_lisp_poly_print(ao_lisp_v); printf("\n");
96         printf("Stack:\n");
97         ao_lisp_stack_print(ao_lisp_stack_poly(ao_lisp_stack));
98         printf("Globals:\n\t");
99         ao_lisp_frame_print(ao_lisp_frame_poly(ao_lisp_frame_global));
100         printf("\n");
101         return AO_LISP_NIL;
102 }