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