Implement global bindings
[muddle-interpreter.git] / src / object.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 "object.h"
20
21 #include <string.h>
22
23 uint32_t
24 list_length (const list_object * o)
25 {
26   const pool_object *p = POOL_OBJECT (o->val.head);
27   uint32_t n = 0;
28   while (p)
29     {
30       n++;
31       p = POOL_OBJECT (p->rest);
32     }
33   return n;
34 }
35
36 /*
37 static object *cons_new(evaltype type, value v, object *cdr) {
38     object o;
39     o.type = type;
40     o.v = v;
41     return cons(&o, cdr);
42 }
43 */
44
45 /*
46 object *cons(const object *car, const object *cdr) {
47     assert(car);
48     assert(cdr);
49     object *head = pool_alloc(1);
50     head->type = car->type;
51     head->rest = cdr->v.head;
52     head->v = car->v;
53     return head;
54 }
55 */
56
57 /*
58 static object rest(const object *lst) {
59     assert(lst);
60     object *head = OBJECT_OF_POOL_PTR(lst->v.head);
61     assert(head);
62     object o;
63     o.type = EVALTYPE_LIST;
64     o.rest = 0;
65     o.v.head = ((object*)head)->rest;
66     return o;
67 }
68 */
69
70 dope_object *
71 vec_dope (const vector_object * o)
72 {
73   return (dope_object *) & HEAP_OBJECT (o->val.body)[o->val.len];
74 }
75
76 dope_object *
77 uv_dope (const uvector_object * o)
78 {
79   return (dope_object *) & HEAP_OBJECT (o->val.body)[(o->val.len + 1) / 2 +
80                                                      1];
81 }
82
83 vector_object
84 vector_create (uint32_t capacity)
85 {
86   heap_ptr body = heap_alloc (capacity);
87   memset (HEAP_OBJECT (body), '\0', capacity * sizeof (object));
88   return new_vector (body + capacity, 0);
89 }
90
91 object *
92 stack_push (vector_object * v)
93 {
94   if (vec_dope (v)->len > v->val.len)
95     {
96       // TODO
97       assert (0 && "not implemented: GROW in stack_push");
98     }
99   v->val.len++;
100   return HEAP_OBJECT (--(v->val.body));
101 }