altos/scheme: Add ports. Split scheme code up.
[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_vfprintf(FILE *out, 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(out, (ao_poly) va_arg(args, unsigned int), true);
28                                 break;
29                         case 'V':
30                                 ao_scheme_poly_write(out, (ao_poly) va_arg(args, unsigned int), false);
31                                 break;
32                         case 'p':
33                                 fprintf(out, "%p", va_arg(args, void *));
34                                 break;
35                         case 'd':
36                                 fprintf(out, "%d", va_arg(args, int));
37                                 break;
38                         case 'x':
39                                 fprintf(out, "%x", va_arg(args, int));
40                                 break;
41                         case 's':
42                                 fprintf(out, "%s", va_arg(args, char *));
43                                 break;
44                         default:
45                                 putc(c, out);
46                                 break;
47                         }
48                 } else
49                         putc(c, out);
50         }
51 }
52
53 void
54 ao_scheme_fprintf(FILE *out, const char *format, ...)
55 {
56         va_list args;
57         va_start(args, format);
58         ao_scheme_vfprintf(out, format, args);
59         va_end(args);
60 }
61
62 ao_poly
63 ao_scheme_error(int error, const char *format, ...)
64 {
65         va_list args;
66
67         ao_scheme_exception |= error;
68         va_start(args, format);
69         ao_scheme_vfprintf(stdout, format, args);
70         putchar('\n');
71         va_end(args);
72         ao_scheme_fprintf(stdout, "Value:  %v\n", ao_scheme_v);
73         ao_scheme_fprintf(stdout, "Frame:  %v\n", ao_scheme_frame_poly(ao_scheme_frame_current));
74         printf("Stack:\n");
75         ao_scheme_stack_write(stdout, ao_scheme_stack_poly(ao_scheme_stack), true);
76         ao_scheme_fprintf(stdout, "Globals: %v\n", ao_scheme_frame_poly(ao_scheme_frame_global));
77         return AO_SCHEME_NIL;
78 }