#include #include"structmember.h" #include "turmit.h" /** * @todo search for decref and memory leaks :/ */ typedef struct { PyObject VAR_HEAD; Py_ssize_t len; turmit_t *turmit; } CTurmit; static PyModuleDef cturmitmodule = { PyModuleDef_HEAD_INIT, "cturmit", "Module that implements turmits in C", -1, NULL, NULL, NULL, NULL, NULL }; static PyMemberDef CTurmit_members[] = { {"len", T_PYSSIZET, offsetof(CTurmit, len), 0, "Expression len"}, /* {"heap_sz", T_PYSSIZET, offsetof(CTurmit, heap_sz), 0, "Item heap counter"}, */ {NULL} /* Sentinel */ }; /* Magic methods */ Py_ssize_t CTurmit_len(CTurmit*); static PyMappingMethods CTurmit_asmapping[] = {{ (void*)CTurmit_len, }}; /* Other methods */ #define TURMIT_PYOP(NAME) PyObject* CTurmit_op_##NAME(CTurmit* self) {\ turmit_op_##NAME(self->turmit);\ Py_RETURN_NONE;\ } #define TURMIT_PYOPDEF(NAME) {#NAME, (PyCFunction)CTurmit_op_##NAME, \ METH_NOARGS, NULL} PyObject* CTurmit__push(CTurmit* self, PyObject *args); PyObject* CTurmit__pop(CTurmit* self); TURMIT_PYOP(mem_sz) TURMIT_PYOP(add) TURMIT_PYOP(sub) TURMIT_PYOP(bin_and) TURMIT_PYOP(dup) TURMIT_PYOP(lshift) TURMIT_PYOP(mod) TURMIT_PYOP(mul) TURMIT_PYOP(div) TURMIT_PYOP(bin_or) TURMIT_PYOP(bin_xor) TURMIT_PYOP(pop) TURMIT_PYOP(swp) TURMIT_PYOP(jmp) TURMIT_PYOP(jz) TURMIT_PYOP(jcmp) static PyMethodDef CTurmit_methods[] = { {"_push", (PyCFunction)CTurmit__push, METH_KEYWORDS | METH_VARARGS, "Push a value on the stack"}, {"_pop", (PyCFunction)CTurmit__pop, METH_NOARGS, "Pop a value from the stack"}, TURMIT_PYOPDEF(mem_sz), TURMIT_PYOPDEF(add), TURMIT_PYOPDEF(sub), TURMIT_PYOPDEF(bin_and), TURMIT_PYOPDEF(dup), TURMIT_PYOPDEF(lshift), TURMIT_PYOPDEF(mod), TURMIT_PYOPDEF(mul), TURMIT_PYOPDEF(div), TURMIT_PYOPDEF(bin_or), TURMIT_PYOPDEF(bin_xor), TURMIT_PYOPDEF(pop), TURMIT_PYOPDEF(swp), TURMIT_PYOPDEF(jmp), TURMIT_PYOPDEF(jz), TURMIT_PYOPDEF(jcmp), {NULL} /* Sentinel */ }; PyObject* CTurmit_get_shead(CTurmit *self, void *ref); PyObject* CTurmit_get__cur(CTurmit *self, void *ref); PyObject* CTurmit_get__stack(CTurmit *self, void *ref); static struct PyGetSetDef CTurmit_getset[] = { {"shead", (getter)CTurmit_get_shead, NULL, NULL, NULL}, {"_cur", (getter)CTurmit_get__cur, NULL, NULL, NULL}, {"_stack", (getter)CTurmit_get__stack, NULL, NULL, NULL}, {NULL} /* Sentinel */ }; static int _CTurmit_copy_init(CTurmit *self, CTurmit *t); static PyObject * CTurmit_new(PyTypeObject *type, PyObject *args, PyObject *kwds); /**@brief CTurmit __init__ function * @param CTurmit* self * @param PyObject* can contain an interger representing stack size * @param Pyobject* contains kwargs. Can be : stack_size, max_int, prog */ static int CTurmit_init(CTurmit *self, PyObject *args, PyObject *kwds); void CTurmit_dealloc(CTurmit *self); PyObject* CTurmit_call(PyObject *func, PyObject *args, PyObject *kwds); static PyTypeObject CTurmitType = { PyVarObject_HEAD_INIT(NULL, 0) "cturmit.CTurmit", /* tp_name */ sizeof(CTurmit), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor)CTurmit_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_reserved */ 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ CTurmit_asmapping, /* tp_as_mapping */ 0, /* tp_hash */ CTurmit_call, /* tp_call */ 0, /* tp_str */ 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ "CTurmit objects", /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ CTurmit_methods, /* tp_methods */ CTurmit_members, /* tp_members */ CTurmit_getset, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ (initproc)CTurmit_init, /* tp_init */ 0, /* tp_alloc */ CTurmit_new, /* tp_new */ };