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