Change pointers for heap allocation
[muddle-interpreter.git] / src / main.c
1 /*
2 Copyright (C) 2017-2018 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 "read.h"
20 #include "eval.h"
21 #include "print.h"
22 #include "object.h"
23
24 #include <stdio.h>
25 #include <sys/mman.h>
26 #include <unistd.h>
27
28 // TODO: put these in interpreter-wide ctx object
29 pool_object *pool;
30 pool_ptr ptop;
31 object *vhp_base;
32 heap_ptr vhp;
33
34 // TODO: store these in current PROCESS
35 frame *cf;
36 object ret;
37 object *cst;
38
39 // Define the address spaces.
40 enum
41 {
42   // Max objects that can be in linked lists (must be < 2^32).
43   POOL_OBJCT = 1024,
44   // Max size, in objects, of control stack segment.
45   STACK_OBJCT = 256,
46   // Max size, in objects, of VECTOR heap.
47   VECTOR_OBJCT = 1024,
48   // Max objects reader can handle at once.
49   // TODO: allocate as VECTOR and eliminate this arbitrary limit
50   READER_OBJCT = 64
51 };
52
53 int
54 main ()
55 {
56   // The REST pool (in low mem).
57   pool =
58     mmap (0, POOL_OBJCT * sizeof (object), PROT_READ | PROT_WRITE,
59           MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
60   ptop = 1;                     // 0 is null
61
62   // The CONTROL STACKs (TODO: per-PROCESS).
63   object *cst_base =
64     mmap (0, STACK_OBJCT * sizeof (object), PROT_READ | PROT_WRITE,
65           MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
66   cst = cst_base;
67   cf = (frame *) cst;
68   cst += sizeof (frame) / sizeof (object);
69
70   // The VECTOR heap.
71   vhp_base =
72     mmap (0, VECTOR_OBJCT * sizeof (object), PROT_READ | PROT_WRITE,
73           MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
74   vhp = 1;
75
76   // Reader stack (TODO: dynamically allocate as VECTOR).
77   object rst_base[READER_OBJCT];
78   object *rst = rst_base + READER_OBJCT;
79
80   // TODO: push frames for `<REPEAT <PRINT <EVAL <READ>>>>`.
81   // Entire toplevel becomes `for (;;) cf->cont.fn();`
82   char buf[512];
83   ssize_t n;
84   while ((n = read (STDIN_FILENO, buf, sizeof (buf))) > 0)
85     {
86       // mock GC (no object persistence)
87       ptop = 1;
88       vhp = 1;
89       // terminate input
90       assert (buf[n - 1] == '\n');
91       buf[n - 1] = '\0';
92       // Read a thing
93       reader_stack st;
94       st.pos = rst;
95       st.framelen = 0;
96       const char *p = buf;
97       while (p && p[0])
98         {
99           p = read_token (p, &st);
100         }
101       assert (p);
102       if (!st.framelen)
103         continue;
104       assert (st.framelen == 1);
105       // Eval the thing
106       cf->prevcst = cst;
107       push_frame (eval, new_tuple (st.pos, 1), 0);
108       while (cf->cont.fn)
109         {
110           cf->cont.fn ();
111         }
112       // Print the thing
113       print_object (&ret);
114       printf ("\n");
115       // Loop!
116     }
117
118   munmap (cst_base, STACK_OBJCT * sizeof (object));
119   munmap (vhp_base, VECTOR_OBJCT * sizeof (object));
120   munmap (pool, POOL_OBJCT * sizeof (object));
121   return 0;
122 }