Skip to content

Commit

Permalink
Implementation for C enum atoms
Browse files Browse the repository at this point in the history
---

+ Python class: `enumeration`
+ Tests included
+ Updated roadmap accordingly
  • Loading branch information
MatrixEditor committed Sep 8, 2024
1 parent 214d108 commit a9a2dda
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ python_add_library(
src/atomimpl/constatomobj.c
src/atomimpl/bytesatomobj.c
src/atomimpl/pstringatomobj.c
src/atomimpl/enumatomobj.c

src/atomimpl/builtins/builtinatomobj.c
src/atomimpl/builtins/repeatedatomobj.c
Expand All @@ -57,6 +58,8 @@ message(STATUS "Python ${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}")

add_custom_target(
genapi ALL
# execute python ./src/code_gen/genapi.py ./src/caterpillar/include/caterpillar/caterpillarapi.h.in ./src/caterpillarapi.c.in
# in the root directory
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/src/code_gen/genapi.py ${CMAKE_CURRENT_SOURCE_DIR}/src/caterpillar/include/caterpillar/caterpillarapi.h.in ${CMAKE_CURRENT_SOURCE_DIR}/src/caterpillarapi.c.in
BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/src/caterpillar/include/caterpillar/caterpillarapi.h ${CMAKE_CURRENT_SOURCE_DIR}/src/caterpillarapi.c
COMMENT "Generating Public Caterpillar API"
Expand Down
53 changes: 42 additions & 11 deletions docs/sphinx/source/development/roadmap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ Roadmap

.. role:: text-danger

.. role:: text-warning

Python API
----------

- |check_| Implementation of parsing process (unpack, pack)
- |check_| Struct class (:class:`Struct`) with wrapper function (:code:`@struct`)

- |uncheck_| Python docs and examples
- |uncheck_| Python tests

C API
-----
Expand All @@ -32,18 +35,46 @@ C API
Atom Objects:
^^^^^^^^^^^^^

- |check_| Integer (uint8-128 and int8-128) (C type: :c:type:`CpIntAtomObject`, Py type: :class:`int_t`) [:text-danger:`missing docs`]
- |check_| Float, Double (C type: :c:type:`CpFloatAtomObject`, Py type: :class:`float_t`) [:text-danger:`missing docs`]
- |check_| Boolean (C type: :c:type:`CpBoolAtomObject`, Py type: :class:`bool_t`), global instance: :code:`boolean` [:text-danger:`missing docs`]
- |check_| Char (C type: :c:type:`CpCharAtomObject`, Py type: :class:`char_t`), global instance: :code:`char` [:text-danger:`missing docs`]
- |check_| Padding (C type: :c:type:`CpPaddingAtomObject`, Py type: :class:`padding_t`), global instance: :code:`padding` [:text-danger:`missing docs`]
- |check_| String (C type: :c:type:`CpStringAtomObject`, Py type: :class:`string`) [:text-danger:`missing docs`]
- |uncheck_| Const (C type: :c:type:`CpConstAtomObject`, Py type: :class:`const_t`) [:text-danger:`missing docs`]
- |check_| Integer (uint8-128 and int8-128) (C type: :c:type:`CpIntAtomObject`, Py type: :class:`int_t`)
[:text-danger:`missing docs`]

- |check_| Float, Double (C type: :c:type:`CpFloatAtomObject`, Py type: :class:`float_t`)
[:text-danger:`missing docs`]

- |check_| Boolean (C type: :c:type:`CpBoolAtomObject`, Py type: :class:`bool_t`)
Global instance: :code:`boolean`,
[:text-danger:`missing docs`]

- |check_| Char (C type: :c:type:`CpCharAtomObject`, Py type: :class:`char_t`)
Global instance: :code:`char`,
[:text-danger:`missing docs`]

- |check_| Padding (C type: :c:type:`CpPaddingAtomObject`, Py type: :class:`padding_t`)
Global instance: :code:`padding`,
[:text-danger:`missing docs`]

- |check_| String (C type: :c:type:`CpStringAtomObject`, Py type: :class:`string`)
[:text-danger:`missing docs`],
[:text-warning:`missing perftest`]

- |check_| Const (C type: :c:type:`CpConstAtomObject`, Py type: :class:`const_t`)
[:text-danger:`missing docs`],
[:text-warning:`missing perftest`]

- |uncheck_| CString
- |uncheck_| Bytes (C type: :c:type:`CpBytesAtomObject`, Py type: :class:`octetstring`)
- |uncheck_| Enum
- |check_| Bytes (C type: :c:type:`CpBytesAtomObject`, Py type: :class:`octetstring`)
[:text-danger:`missing docs`],
[:text-warning:`missing perftest`]

- |check_| Enum (C type: :c:type:`CpEnumAtomObject`, Py type: :class:`enumeration`)
[:text-danger:`missing docs`],
[:text-warning:`missing perftest`]

- |uncheck_| Computed
- |uncheck_| PString
- |check_| PString (C type: :c:type:`CpPStringAtomObject`, Py type: :class:`pstring`)
[:text-danger:`missing docs`],
[:text-warning:`missing perftest`]

- |uncheck_| Prefixed
- |uncheck_| Lazy
- |uncheck_| uuid
Expand Down
2 changes: 1 addition & 1 deletion src/atomimpl/constatomobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ PyTypeObject CpConstAtom_Type = {
.tp_basicsize = sizeof(CpConstAtomObject),
.tp_dealloc = (destructor)cp_constatom_dealloc,
.tp_init = (initproc)cp_constatom_init,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_new = (newfunc)cp_constatom_new,
.tp_repr = (reprfunc)cp_constatom_repr,
.tp_members = cp_constatom_members,
Expand Down
187 changes: 187 additions & 0 deletions src/atomimpl/enumatomobj.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/* enum atom C implementation */

#include "caterpillar/caterpillar.h"

#include <structmember.h>

/* impl */
static PyObject*
cp_enumatom_type(CpEnumAtomObject* self)
{
if (self->m_enum_type) {
return Py_NewRef(self->m_enum_type);
}
return CpTypeOf(self->m_atom);
}

static PyObject*
cp_enumatom_size(CpEnumAtomObject* self, CpLayerObject* layer)
{
return _Cp_SizeOf(self->m_atom, layer);
}

static PyObject*
cp_enumatom_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
{
CpEnumAtomObject* atom = (CpEnumAtomObject*)type->tp_alloc(type, 0);
if (atom != NULL) {
CpBuiltinAtom_CATOM(atom).ob_pack = (packfunc)CpEnumAtom_Pack;
CpBuiltinAtom_CATOM(atom).ob_unpack = (unpackfunc)CpEnumAtom_Unpack;
CpBuiltinAtom_CATOM(atom).ob_pack_many = NULL;
CpBuiltinAtom_CATOM(atom).ob_unpack_many = NULL;
CpBuiltinAtom_CATOM(atom).ob_size = (sizefunc)cp_enumatom_size;
CpBuiltinAtom_CATOM(atom).ob_type = (typefunc)cp_enumatom_type;
CpBuiltinAtom_CATOM(atom).ob_bits = NULL;

atom->m_atom = NULL;
atom->m_enum_type = NULL;
atom->m_members = NULL;
atom->m_value2member_map = NULL;
atom->m_default = Py_NewRef(CpInvalidDefault);
}
return (PyObject*)atom;
}

static void
cp_enumatom_dealloc(CpEnumAtomObject* self)
{
Py_XDECREF(self->m_atom);
Py_XDECREF(self->m_members);
Py_XDECREF(self->m_enum_type);
Py_TYPE(self)->tp_free((PyObject*)self);
}

static int
cp_enumatom_init(CpEnumAtomObject* self, PyObject* args, PyObject* kwds)
{
static char* kwlist[] = { "atom", "enum_type", "default", NULL };
PyObject *atom = NULL, *enum_type = NULL, *default_value = NULL;
if (!PyArg_ParseTupleAndKeywords(
args, kwds, "OO|O", kwlist, &atom, &enum_type, &default_value)) {
return -1;
}
_Cp_SetObj(self->m_atom, atom);
_Cp_SetObj(self->m_enum_type, enum_type);
_Cp_SetObj(self->m_default, default_value);

_modulestate* mod = get_global_module_state();
PyObject* members =
PyObject_GetAttr(self->m_enum_type, mod->str__member_map_);
if (!members) {
return -1;
}

_Cp_SetObj(self->m_members, members);
PyObject* value2member_map =
PyObject_GetAttr(self->m_enum_type, mod->str__value2member_map_);
if (!value2member_map) {
return -1;
}
_Cp_SetObj(self->m_value2member_map, value2member_map);

return 0;
}

_CpEndian_ImplSetByteorder(CpEnumAtomObject, enumatom, self->m_atom);

static PyObject*
cp_enumatom_repr(CpEnumAtomObject* self)
{
return PyUnicode_FromFormat(
"<enum [<%s>] %R>", ((PyTypeObject *)self->m_enum_type)->tp_name, self->m_atom);
}

/* Public API */

/*CpAPI*/
int
CpEnumAtom_Pack(CpEnumAtomObject* self, PyObject* value, CpLayerObject* layer)
{
// TODO: check enum type - add strict flag
// if (!PyObject_TypeCheck(value, (PyTypeObject*)self->m_enum_type)) {
// PyErr_Format(PyExc_TypeError,
// "Expected a %s, got %s",
// Py_TYPE(self->m_enum_type)->tp_name,
// Py_TYPE(value)->tp_name);
// return -1;
// }
int res = 0;
if (PyObject_TypeCheck(value, (PyTypeObject*)self->m_enum_type)) {
PyObject* real_value = PyObject_GetAttrString(value, "value");
if (!real_value) {
return -1;
}
res = _Cp_Pack(real_value, self->m_atom, layer);
Py_DECREF(real_value);
} else {
res = _Cp_Pack(value, self->m_atom, layer);
}

return res;
}

/*CpAPI*/
PyObject*
CpEnumAtom_Unpack(CpEnumAtomObject* self, CpLayerObject* layer)
{
PyObject* value = _Cp_Unpack(self->m_atom, layer);
if (!value) {
return NULL;
}

PyObject* by_name = PyDict_GetItem(self->m_members, value);
if (by_name) {
Py_DECREF(value);
return Py_NewRef(by_name);
}
// not found
PyErr_Clear();

PyObject* by_value = PyDict_GetItem(self->m_value2member_map, value);
if (by_value) {
Py_DECREF(value);
return Py_NewRef(by_value);
}

// not found, fallback to default value
PyErr_Clear();

if (Cp_IsInvalidDefault(self->m_default)) {
// TODO: add strict parsing flag here
return value;
}
Py_DECREF(value);
return Py_NewRef(self->m_default);
}

/* docs */

/* type setup */

static PyMemberDef CpEnumAtom_Members[] = {
{ "atom", T_OBJECT, offsetof(CpEnumAtomObject, m_atom), READONLY },
{ "enum_type", T_OBJECT, offsetof(CpEnumAtomObject, m_enum_type), READONLY },
{ "members", T_OBJECT, offsetof(CpEnumAtomObject, m_members), READONLY },
{ "default", T_OBJECT, offsetof(CpEnumAtomObject, m_default), 0 },
{ NULL }
};

static PyMethodDef CpEnumAtom_Methods[] = {
_CpEndian_ImplSetByteorder_MethDef(enumatom, NULL),
{
NULL,
}
};

PyTypeObject CpEnumAtom_Type = {
PyVarObject_HEAD_INIT(NULL, 0) _Cp_NameStr(CpEnumAtom_NAME),
.tp_basicsize = sizeof(CpEnumAtomObject),
.tp_dealloc = (destructor)cp_enumatom_dealloc,
.tp_init = (initproc)cp_enumatom_init,
.tp_repr = (reprfunc)cp_enumatom_repr,
.tp_members = CpEnumAtom_Members,
.tp_methods = CpEnumAtom_Methods,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = NULL,
.tp_new = (newfunc)cp_enumatom_new,
};
7 changes: 6 additions & 1 deletion src/capi.dat
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ src:atomimpl/stringatomobj.c
src:atomimpl/constatomobj.c
src:atomimpl/bytesatomobj.c
src:atomimpl/pstringatomobj.c
src:atomimpl/enumatomobj.c
src:atomimpl/builtins/builtinatomobj.c
src:atomimpl/builtins/repeatedatomobj.c
src:atomimpl/builtins/conditionatomobj.c
Expand Down Expand Up @@ -98,6 +99,7 @@ type:35:_primitiveatomobj:CpPrimitiveAtomObject:-
type:36:_lengthinfoobj:CpLengthInfoObject:-
type:37:_bytesatomobj:CpBytesAtomObject:-
type:38:_pstringatomobj:CpPStringAtomObject:-
type:39:_enumatomobj:CpEnumAtomObject:-

func:50:CpEndian_IsLittleEndian:int:null
func:53:CpContext_New:CpContextObject*:+1
Expand Down Expand Up @@ -199,4 +201,7 @@ func:149:CpBytesAtom_Pack:int:null
func:150:CpBytesAtom_Unpack:PyObject*:+1
func:-:CpPStringAtom_New:CpPStringAtomObject*:+1
func:152:CpPStringAtom_Pack:int:null
func:153:CpPStringAtom_Unpack:PyObject*:+1
func:153:CpPStringAtom_Unpack:PyObject*:+1
func:-:CpEnumAtom_New:CpEnumAtomObject*:+1
func:154:CpEnumAtom_Pack:int:null
func:155:CpEnumAtom_Unpack:PyObject*:+1
51 changes: 51 additions & 0 deletions src/caterpillar/include/caterpillar/atoms/enum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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 ENUMATOMOBJ_H
#define ENUMATOMOBJ_H

#include "caterpillar/atoms/builtins.h"

struct _enumatomobj
{
CpBuiltinAtom_HEAD

/// Stores the mapping of the underlying enum type.
PyObject* m_members;

PyObject* m_value2member_map;
PyObject* m_default;

/// Reference to the atom that is responsible for
/// parsing the enum value
PyObject* m_atom;

/// Reference to the enum type
PyObject* m_enum_type;
};

#define CpEnumAtom_NAME "enumeration"
#define CpEnumAtom_CheckExact(op) Py_IS_TYPE((op), &CpEnumAtom_Type)
#define CpEnumAtom_Check(op) (PyObject_TypeCheck((op), &CpEnumAtom_Type))

static inline CpEnumAtomObject*
CpEnumAtom_New(PyObject* members, PyObject* atom)
{
return (CpEnumAtomObject*)CpObject_Create(
&CpEnumAtom_Type, "OO", members, atom);
}

#endif
1 change: 1 addition & 0 deletions src/caterpillar/include/caterpillar/caterpillar.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
#include "caterpillar/atoms/primitive.h"
#include "caterpillar/atoms/string.h"
#include "caterpillar/atoms/const.h"
#include "caterpillar/atoms/enum.h"

#endif
Loading

0 comments on commit a9a2dda

Please sign in to comment.