altos/scheme: Rework display/write code
[fw/altos] / src / scheme / ao_scheme_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_scheme.h"
16 #include <stdarg.h>
17
18 void
19 ao_scheme_vprintf(const char *format, va_list args)
20 {
21         char c;
22
23         while ((c = *format++) != '\0') {
24                 if (c == '%') {
25                         switch (c = *format++) {
26                         case 'v':
27                                 ao_scheme_poly_write((ao_poly) va_arg(args, unsigned int), true);
28                                 break;
29                         case 'V':
30                                 ao_scheme_poly_write((ao_poly) va_arg(args, unsigned int), false);
31                                 break;
32                         case 'p':
33                                 printf("%p", va_arg(args, void *));
34                                 break;
35                         case 'd':
36                                 printf("%d", va_arg(args, int));
37                                 break;
38                         case 's':
39                                 printf("%s", va_arg(args, char *));
40                                 break;
41                         default:
42                                 putchar(c);
43                                 break;
44                         }
45                 } else
46                         putchar(c);
47         }
48 }
49
50 void
51 ao_scheme_printf(const char *format, ...)
52 {
53         va_list args;
54         va_start(args, format);
55         ao_scheme_vprintf(format, args);
56         va_end(args);
57 }
58
59 ao_poly
60 ao_scheme_error(int error, const char *format, ...)
61 {
62         va_list args;
63
64         ao_scheme_exception |= error;
65         va_start(args, format);
66         ao_scheme_vprintf(format, args);
67         putchar('\n');
68         va_end(args);
69         ao_scheme_printf("Value:  %v\n", ao_scheme_v);
70         ao_scheme_printf("Frame:  %v\n", ao_scheme_frame_poly(ao_scheme_frame_current));
71         printf("Stack:\n");
72         ao_scheme_stack_write(ao_scheme_stack_poly(ao_scheme_stack), true);
73         ao_scheme_printf("Globals: %v\n", ao_scheme_frame_poly(ao_scheme_frame_global));
74         return AO_SCHEME_NIL;
75 }