Browse Source

Starts to implement sequence methods for RPNExpr

Starting with __len__
Next we have to implement token acces as a sequence in RPNExpr, but to do
this we have to implement a module allowing token access from python.
Yann Weber 1 year ago
parent
commit
869f22a3ed
2 changed files with 34 additions and 39 deletions
  1. 26
    39
      python_rpnexpr.c
  2. 8
    0
      python_rpnexpr.h

+ 26
- 39
python_rpnexpr.c View File

@@ -1,5 +1,5 @@
1 1
 /*
2
- * Copyright (C) 2020 Weber Yann
2
+ * Copyright (C) 2020,2023 Weber Yann
3 3
  * 
4 4
  * This file is part of pyrpn.
5 5
  * 
@@ -46,46 +46,25 @@ PyMemberDef RPNExpr_members[] = {
46 46
 	{NULL}
47 47
 };
48 48
 
49
+PySequenceMethods RPNExpr_seq_methods = {
50
+	.sq_length = rpnexpr_len,
51
+};
52
+
49 53
 PyTypeObject RPNExprType = {
50 54
 	PyVarObject_HEAD_INIT(NULL, 0)
51
-	"pyrpn.RPNExpr",                     /* tp_name */
52
-	sizeof(PyRPNExpr_t),                        /* tp_basicsize */
53
-	0,                                               /* tp_itemsize */
54
-	(destructor)rpnexpr_del, /* tp_dealloc */
55
-	0,                                               /* tp_print */
56
-	0,                                               /* tp_getattr */
57
-	0,                                               /* tp_setattr */
58
-	0,                                               /* tp_reserved */
59
-	rpnexpr_repr,                                    /* tp_repr */
60
-	0,                                               /* tp_as_number */
61
-	0,                                               /* tp_as_sequence */
62
-	0,                              /* tp_as_mapping */
63
-	0,                                               /* tp_hash  */
64
-	0,                                               /* tp_call */
65
-	rpnexpr_str,                                               /* tp_str */
66
-	0,                                               /* tp_getattro */
67
-	0,                                               /* tp_setattro */
68
-	0,                                               /* tp_as_buffer */
69
-	Py_TPFLAGS_DEFAULT |
70
-	Py_TPFLAGS_BASETYPE,   /* tp_flags */
71
-	"RPN expression evaluator",                /* tp_doc */
72
-	0,                                               /* tp_traverse */
73
-	0,                                               /* tp_clear */
74
-	0,                                               /* tp_richcompare */
75
-	0,                                               /* tp_weaklistoffset */
76
-	0,                                               /* tp_iter */
77
-	0,                                               /* tp_iternext */
78
-	RPNExpr_methods,                        /* tp_methods */
79
-	RPNExpr_members,                        /* tp_members */
80
-	0,                                               /* tp_getset */
81
-	0,                                               /* tp_base */
82
-	0,                                               /* tp_dict */
83
-	0,                                               /* tp_descr_get */
84
-	0,                                               /* tp_descr_set */
85
-	0,                                               /* tp_dictoffset */
86
-	rpnexpr_init,          /* tp_init */
87
-	0,                                               /* tp_alloc */
88
-	rpnexpr_new,                            /* tp_new */
55
+	.tp_name = "pyrpn.RPNExpr",
56
+	.tp_doc = "RPN expression evaluator",
57
+	.tp_basicsize = sizeof(PyRPNExpr_t),
58
+	.tp_itemsize = 0,
59
+	.tp_del = rpnexpr_del,
60
+	.tp_repr = rpnexpr_repr,
61
+	.tp_str = rpnexpr_str,
62
+	.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
63
+	.tp_methods = RPNExpr_methods,
64
+	.tp_as_sequence = &RPNExpr_seq_methods,
65
+	.tp_members = RPNExpr_members,
66
+	.tp_init = rpnexpr_init,
67
+	.tp_new = rpnexpr_new,
89 68
 };
90 69
 
91 70
 PyObject* rpnexpr_new(PyTypeObject *subtype, PyObject *args, PyObject* kwds)
@@ -529,6 +508,13 @@ err:
529 508
 }
530 509
 
531 510
 
511
+Py_ssize_t rpnexpr_len(PyObject *self)
512
+{
513
+	PyRPNExpr_t *expr_self = (PyRPNExpr_t*)self;
514
+	return (Py_ssize_t)expr_self->rpn->toks.tokens_sz;
515
+}
516
+
517
+
532 518
 PyObject* rpnexpr_eval(PyObject* self, PyObject** argv, Py_ssize_t argc)
533 519
 {
534 520
 	PyRPNExpr_t *expr_self;
@@ -844,6 +830,7 @@ int rpnexpr_pyobj_to_mutation_params(PyObject* py_params, rpn_mutation_params_t
844 830
 		PyErr_SetString(PyExc_ValueError, "Bad value for .weight_add_elt field");
845 831
 		return -1;
846 832
 	}
833
+	rpn_mutation_init_params(params);
847 834
 	return 0;
848 835
 }
849 836
 

+ 8
- 0
python_rpnexpr.h View File

@@ -41,6 +41,9 @@
41 41
  * @ingroup python_type
42 42
  *
43 43
  * This file is the header of the RPNExpr Python class
44
+ *
45
+ * @todo implements tp_as_sequence in order to give access to tokens from
46
+ * expr. To do this we have to implement a python module representing tokens.
44 47
  */
45 48
 
46 49
 /**@brief RPNExpr Python class methods list
@@ -144,6 +147,11 @@ PyObject* rpnexpr_setstate(PyObject *cls, PyObject *state);
144 147
  */
145 148
 PyObject* rpnexpr_copy(PyObject *cls, PyObject *noargs);
146 149
 
150
+/**@brief RPNExpr.__len__() method
151
+ * @return A integer with the number of tokens in expression
152
+ */
153
+Py_ssize_t rpnexpr_len(PyObject *self);
154
+
147 155
 /**@brief Eval an RPN expression given arguments and return the
148 156
  * value
149 157
  * @param self RPNExpr instance

Loading…
Cancel
Save