-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc8f68b
commit fac0cd3
Showing
11 changed files
with
160 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,98 @@ | ||
/* constatom C implementation */ | ||
#include "caterpillar/caterpillar.h" | ||
|
||
#include <structmember.h> | ||
|
||
static PyObject* | ||
cp_constatom__type__(CpConstAtomObject* self) | ||
{ | ||
return CpTypeOf(self->m_atom); | ||
} | ||
|
||
static PyObject* | ||
cp_constatom__size__(CpConstAtomObject* self, CpLayerObject* layer) | ||
{ | ||
return _Cp_SizeOf(self->m_atom, layer); | ||
} | ||
|
||
static PyObject* | ||
cp_constatom_new(PyTypeObject* type, PyObject* args, PyObject* kwds) | ||
{ | ||
CpConstAtomObject* self = (CpConstAtomObject*)type->tp_alloc(type, 0); | ||
if (self != NULL) { | ||
CpFieldCAtom_CATOM(self).ob_pack = (packfunc)CpConstAtom_Pack; | ||
CpFieldCAtom_CATOM(self).ob_unpack = (unpackfunc)CpConstAtom_Unpack; | ||
CpFieldCAtom_CATOM(self).ob_pack_many = NULL; | ||
CpFieldCAtom_CATOM(self).ob_unpack_many = NULL; | ||
CpFieldCAtom_CATOM(self).ob_size = (sizefunc)cp_constatom__size__; | ||
CpFieldCAtom_CATOM(self).ob_type = (typefunc)cp_constatom__type__; | ||
CpFieldCAtom_CATOM(self).ob_bits = NULL; | ||
|
||
self->m_atom = NULL; | ||
self->m_value = NULL; | ||
} | ||
return (PyObject*)self; | ||
} | ||
|
||
static void | ||
cp_constatom_dealloc(CpConstAtomObject* self) | ||
{ | ||
Py_XDECREF(self->m_atom); | ||
Py_XDECREF(self->m_value); | ||
Py_TYPE(self)->tp_free((PyObject*)self); | ||
} | ||
|
||
static int | ||
cp_constatom_init(CpConstAtomObject* self, PyObject* args, PyObject* kwds) | ||
{ | ||
static char* kwlist[] = { "atom", "value", NULL }; | ||
PyObject* atom = NULL; | ||
PyObject* value = NULL; | ||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &atom, &value)) { | ||
return -1; | ||
} | ||
_Cp_SetObj(self->m_atom, atom); | ||
_Cp_SetObj(self->m_value, value); | ||
return 0; | ||
} | ||
|
||
/* Public API */ | ||
|
||
/*CpAPI*/ | ||
int | ||
CpConstAtom_Pack(CpConstAtomObject* self, PyObject* value, CpLayerObject* layer) | ||
{ | ||
return _Cp_Pack(self->m_value, self->m_atom, layer); | ||
} | ||
|
||
/*CpAPI*/ | ||
PyObject* | ||
CpConstAtom_Unpack(CpConstAtomObject* self, CpLayerObject* layer) | ||
{ | ||
PyObject* res = _Cp_Unpack(self->m_atom, layer); | ||
if (!res) { | ||
return NULL; | ||
} | ||
|
||
if (PyObject_RichCompareBool(res, self->m_value, Py_NE)) { | ||
PyErr_Format(PyExc_ValueError, | ||
"Value is not equal to constant (parsed=%R, constant=%R)", | ||
res, | ||
self->m_value); | ||
Py_DECREF(res); | ||
return NULL; | ||
} | ||
// REVISIT: Should we free the 'res' object here? | ||
return res; | ||
} | ||
|
||
/* type setup */ | ||
|
||
PyTypeObject CpConstAtom_Type = { | ||
PyVarObject_HEAD_INIT(NULL, 0) _Cp_NameStr(CpConstAtom_NAME), | ||
.tp_basicsize = sizeof(CpConstAtomObject), | ||
.tp_dealloc = (destructor)cp_constatom_dealloc, | ||
.tp_init = (initproc)cp_constatom_init, | ||
.tp_flags = Py_TPFLAGS_DEFAULT, | ||
.tp_new = (newfunc)cp_constatom_new, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,35 @@ | ||
/** | ||
* Copyright (C) MatrixEditor 2024 | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef CONSTATOMOBJ_H | ||
#define CONSTATOMOBJ_H | ||
|
||
#include "caterpillar/caterpillarapi.h" | ||
#include "caterpillar/field.h" | ||
|
||
struct _constatomobj | ||
{ | ||
CpFieldCAtom_HEAD | ||
|
||
PyObject *m_value; | ||
PyObject *m_atom; | ||
}; | ||
|
||
#define CpConstAtom_NAME "const_t" | ||
#define CpConstAtom_CheckExact(op) Py_IS_TYPE((op), &CpConstAtom_Type) | ||
#define CpConstAtom_Check(op) PyObject_TypeCheck((op), &CpConstAtom_Type) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters