chiark / gitweb /
Initial import.
[mLib-python] / atom-base.c
CommitLineData
20bce5e9 1/* -*-c-*-
2 *
3 * $Id$
4 *
5 * Atom stuff
6 *
7 * (c) 2005 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the Python interface to mLib.
13 *
14 * mLib/Python is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * mLib/Python is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with mLib/Python; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29/*----- Header files ------------------------------------------------------*/
30
31#include <Python.h>
32
33#include <mLib/atom.h>
34#include <mLib/assoc.h>
35#include <mLib/dstr.h>
36
37#include "atom.h"
38#include "grim.h"
39
40/*----- Data structures ---------------------------------------------------*/
41
42static PyTypeObject atom_pytype;
43
44typedef struct entry {
45 assoc_base _b;
46 PyObject *a;
47} entry;
48
49/*----- Static variables --------------------------------------------------*/
50
51static assoc_table obarray;
52
53/*----- Main code ---------------------------------------------------------*/
54
55PyObject *atom_pywrap(atom *a)
56{
57 entry *e;
58 unsigned f = 0;
59
60 e = assoc_find(&obarray, a, sizeof(entry), &f);
61 if (!f) {
62 atom_pyobj *ao = PyObject_NEW(atom_pyobj, &atom_pytype);
63 ao->a = a;
64 e->a = (PyObject *)ao;
65 }
66 RETURN_OBJ(e->a);
67}
68
69PyObject *atom_pyintern(PyObject *x)
70{
71 atom *a;
72 const void *p;
73 int n;
74
75 if (ATOM_PYCHECK(x))
76 RETURN_OBJ(x);
77 if (x == Py_None)
78 a = atom_gensym(ATOM_GLOBAL);
79 else {
80 if (PyObject_AsReadBuffer(x, &p, &n)) return (0);
81 a = atom_nintern(ATOM_GLOBAL, p, n);
82 }
83 return (atom_pywrap(a));
84}
85
86static void atom_pydealloc(PyObject *me)
87 { fprintf(stderr, "ouch! freeing atom\n"); abort(); }
88
89static PyObject *atom_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
90{
91 PyObject *name;
92 static char *kwlist[] = { "name", 0 };
93
94 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &name))
95 return (0);
96 return (atom_pyintern(name));
97}
98
99static PyObject *aget_name(PyObject *me, void *hunoz)
100{
101 return (PyString_FromStringAndSize(ATOM_NAME(ATOM_A(me)),
102 ATOM_LEN(ATOM_A(me))));
103}
104
105static PyObject *aget_internedp(PyObject *me, void *hunoz)
106{
107 PyObject *rc = (ATOM_A(me)->f & ATOMF_GENSYM) ? Py_False : Py_True;
108 RETURN_OBJ(rc);
109}
110
111static PyGetSetDef atom_pygetset[] = {
112#define GETSETNAME(op, name) a##op##_##name
113 GET (name, "A.name -> NAME")
114 GET (internedp, "A.internedp -> BOOL")
115#undef GETSETNAME
116 { 0 }
117};
118
119static PyObject *atom_pyrichcompare(PyObject *x, PyObject *y, int op)
120{
121 PyObject *rc = 0;
122
123 switch (op) {
124 case Py_EQ: rc = (x == y) ? Py_True : Py_False; break;
125 case Py_NE: rc = (x != y) ? Py_True : Py_False; break;
126 default:
127 PyErr_SetString(PyExc_TypeError, "atoms are unordered");
128 return (0);
129 }
130 RETURN_OBJ(rc);
131}
132
133static PyObject *atom_pyrepr(PyObject *me)
134{
135 PyObject *s, *sr = 0;
136 PyObject *rc = 0;
137 char *p;
138 int n;
139 dstr d = DSTR_INIT;
140
141 if ((s = aget_name(me, 0)) == 0 ||
142 (sr = PyObject_Repr(s)) == 0 ||
143 PyString_AsStringAndSize(sr, &p, &n))
144 goto done;
145 dstr_puts(&d, "Atom(");
146 dstr_putm(&d, p, n);
147 dstr_puts(&d, ")");
148 rc = PyString_FromStringAndSize(d.buf, d.len);
149done:
150 Py_XDECREF(s);
151 Py_XDECREF(sr);
152 dstr_destroy(&d);
153 return (rc);
154}
155
156static long atom_pyhash(PyObject *me)
157 { long h = ATOM_HASH(ATOM_A(me)); if (h == -1) h = -2; return (h); }
158
159static PyTypeObject atom_pytype = {
160 PyObject_HEAD_INIT(0) 0, /* Header */
161 "atom.Atom", /* @tp_name@ */
162 sizeof(atom_pyobj), /* @tp_basicsize@ */
163 0, /* @tp_itemsize@ */
164
165 atom_pydealloc, /* @tp_dealloc@ */
166 0, /* @tp_print@ */
167 0, /* @tp_getattr@ */
168 0, /* @tp_setattr@ */
169 0, /* @tp_compare@ */
170 atom_pyrepr, /* @tp_repr@ */
171 0, /* @tp_as_number@ */
172 0, /* @tp_as_sequence@ */
173 0, /* @tp_as_mapping@ */
174 atom_pyhash, /* @tp_hash@ */
175 0, /* @tp_call@ */
176 atom_pyrepr, /* @tp_str@ */
177 0, /* @tp_getattro@ */
178 0, /* @tp_setattro@ */
179 0, /* @tp_as_buffer@ */
180 Py_TPFLAGS_DEFAULT, /* @tp_flags@ */
181
182 /* @tp_doc@ */
183"Atom.",
184
185 0, /* @tp_traverse@ */
186 0, /* @tp_clear@ */
187 atom_pyrichcompare, /* @tp_richcompare@ */
188 0, /* @tp_weaklistoffset@ */
189 0, /* @tp_iter@ */
190 0, /* @tp_iternexr@ */
191 0, /* @tp_methods@ */
192 0, /* @tp_members@ */
193 atom_pygetset, /* @tp_getset@ */
194 0, /* @tp_base@ */
195 0, /* @tp_dict@ */
196 0, /* @tp_descr_get@ */
197 0, /* @tp_descr_set@ */
198 0, /* @tp_dictoffset@ */
199 0, /* @tp_init@ */
200 PyType_GenericAlloc, /* @tp_alloc@ */
201 atom_pynew, /* @tp_new@ */
202 0, /* @tp_free@ */
203 0 /* @tp_is_gc@ */
204};
205
206PyObject *atom_pystartup(void)
207{
208 assoc_create(&obarray);
209 PyType_Ready(&atom_pytype);
210 return ((PyObject *)&atom_pytype);
211}
212
213/*----- That's all, folks -------------------------------------------------*/