beginnings of REPL
[muddle-interpreter.git] / src / alloc.c
diff --git a/src/alloc.c b/src/alloc.c
new file mode 100644 (file)
index 0000000..7209060
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+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 "alloc.h"
+#include "object.h"
+
+pool_ptr
+pool_copy_array_rev (const pool_object * objs, uint32_t len)
+{
+  if (!len)
+    return 0;
+  pool_object *xs = pool_alloc (len);
+  for (int i = 0; i < (int) len; i++)
+    {
+      xs[i].type = objs[len - 1 - (unsigned) i].type;
+      xs[i].rest = POOL_PTR (&xs[i + 1]);
+      xs[i].val = objs[len - 1 - (unsigned) i].val;
+    }
+  xs[len - 1].rest = 0;
+  return POOL_PTR (xs);
+}
+
+heap_ptr
+heap_copy_array_rev (const object * objs, uint32_t len)
+{
+  object *xs = heap_alloc (len);
+  for (int i = 0; i < (int) len; i++)
+    {
+      xs[i] = objs[len - 1 - (unsigned) i];
+    }
+  return HEAP_PTR_OF_OBJECT (xs);
+}