Implement OBLISTs
[muddle-interpreter.git] / src / atom.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 "alloc.h"
20 #include "atom.h"
21
22 #include <string.h>
23
24 typedef struct atom_body
25 {
26   evaltype type;                // UNBOUND/LOCI
27   // bindid
28   // value ptr
29   // oblist ptr
30   // type ptr
31   char pname[];
32 } atom_body;
33
34 atom_object
35 atom_create (const char *name, uint32_t namelen)
36 {
37   // C-compatible strings for simplicity
38   namelen += 1;
39   heap_ptr body = atom_body_alloc (namelen);
40   atom_body *content = (atom_body *) HEAP_OBJECT (body);
41   memcpy (&content->pname, name, namelen - 1);
42   content->pname[namelen - 1] = '\0';
43   atom_object new = new_atom (body, namelen);
44   return new;
45 }
46
47 heap_ptr
48 atom_body_alloc (uint32_t namelen)
49 {
50   return heap_alloc_uv ((sizeof (atom_body) + namelen + 63) / 64);
51 }
52
53 const char *
54 atom_pname (atom_object o)
55 {
56   return ATOM_BODY (o.val.body)->pname;
57 }