beginnings of REPL
[muddle-interpreter.git] / src / print.c
diff --git a/src/print.c b/src/print.c
new file mode 100644 (file)
index 0000000..64a6115
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+Copyright (C) 2017 Keziah Wesley
+
+You can redistribute and/or modify this file under the terms of the
+GNU Affero General Public License as published by the Free Software
+Foundation, either version 3 of the License, or (at your option) any
+later version.
+
+This file is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public
+License along with this file. If not, see
+<http://www.gnu.org/licenses/>.
+*/
+
+#include "print.h"
+#include "object.h"
+
+// TODO: "print" into buffer
+#include <stdio.h>
+
+#include <stdint.h>
+
+static void
+print_vector_body (const vector_object * o)
+{
+  const object *p = OBJECT_OF_HEAP_PTR (o->body);
+  if (!p)
+    return;
+  if (o->len)
+    print_object (&p[0]);
+  for (uint32_t i = 1; i < o->len; i++)
+    {
+      printf (" ");
+      print_object (&p[i]);
+    }
+}
+
+static void
+print_list_body (const list_object * o)
+{
+  const pool_object *p = POOL_OBJECT (o->head);
+  if (!p)
+    return;
+  print_object ((const object *) p);
+  while ((p = POOL_OBJECT (p->rest)))
+    {
+      printf (" ");
+      print_object ((const object *) p);
+    }
+}
+
+void
+print_object (const object * o)
+{
+  switch (o->type)
+    {
+    case EVALTYPE_FIX32:
+      printf ("%u", o->fix32.val);
+      break;
+    case EVALTYPE_FIX64:
+      printf ("%lu", o->fix64.val);
+      break;
+    case EVALTYPE_LIST:
+      printf ("(");
+      print_list_body (&o->list);
+      printf (")");
+      break;
+    case EVALTYPE_FORM:
+      printf ("<");
+      print_list_body (&o->list);
+      printf (">");
+      break;
+    case EVALTYPE_VECTOR:
+      printf ("[");
+      print_vector_body (&o->vector);
+      printf ("]");
+      break;
+    default:
+      assert (0 && "I don't know how to print that");
+    }
+}