539016e13b30e4b4151fbbdb21c723fd80f0720e
[muddle-interpreter.git] / src / print.c
1 /*
2 Copyright (C) 2017 Keziah Wesley
3
4 You can redistribute and/or modify this file under the terms of the
5 GNU Affero General Public License as published by the Free Software
6 Foundation, either version 3 of the License, or (at your option) any
7 later version.
8
9 This file 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 Affero General Public License for more details.
13
14 You should have received a copy of the GNU Affero General Public
15 License along with this file. If not, see
16 <http://www.gnu.org/licenses/>.
17 */
18
19 #include "print.h"
20 #include "object.h"
21
22 // TODO: "print" into buffer
23 #include <stdio.h>
24
25 #include <stdint.h>
26
27 static void
28 print_vector_body (const vector_object * o)
29 {
30   const object *p = OBJECT_OF_HEAP_PTR (o->body);
31   if (!p)
32     return;
33   if (o->len)
34     print_object (&p[0]);
35   for (uint32_t i = 1; i < o->len; i++)
36     {
37       printf (" ");
38       print_object (&p[i]);
39     }
40 }
41
42 static void
43 print_list_body (const list_object * o)
44 {
45   const pool_object *p = POOL_OBJECT (o->head);
46   if (!p)
47     return;
48   print_object ((const object *) p);
49   while ((p = POOL_OBJECT (p->rest)))
50     {
51       printf (" ");
52       print_object ((const object *) p);
53     }
54 }
55
56 void
57 print_object (const object * o)
58 {
59   switch (o->type)
60     {
61     case EVALTYPE_FIX32:
62       printf ("%d", o->fix32.val);
63       break;
64     case EVALTYPE_FIX64:
65       printf ("%ld", o->fix64.val);
66       break;
67     case EVALTYPE_LIST:
68       printf ("(");
69       print_list_body (&o->list);
70       printf (")");
71       break;
72     case EVALTYPE_FORM:
73       printf ("<");
74       print_list_body (&o->list);
75       printf (">");
76       break;
77     case EVALTYPE_VECTOR:
78       printf ("[");
79       print_vector_body (&o->vector);
80       printf ("]");
81       break;
82     default:
83       assert (0 && "I don't know how to print that");
84     }
85 }