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