|
@@ -20,6 +20,8 @@
|
20
|
20
|
|
21
|
21
|
PyMethodDef RPNExpr_methods[] = {
|
22
|
22
|
{"eval", (PyCFunction)rpnexpr_eval, METH_FASTCALL, "Evaluate an expression"},
|
|
23
|
+ {"random", (PyCFunction)rpnexpr_random, METH_CLASS | METH_VARARGS | METH_KEYWORDS,
|
|
24
|
+ "Return a new random RPN expression string"},
|
23
|
25
|
{"reset_stack", (PyCFunction)rpnexpr_reset_stack, METH_NOARGS,
|
24
|
26
|
"Reset stack memory storage (set all items to 0)"},
|
25
|
27
|
{"__getstate__", (PyCFunction)rpnexpr_getstate, METH_NOARGS,
|
|
@@ -553,3 +555,31 @@ PyObject* rpnexpr_repr(PyObject *self)
|
553
|
555
|
return res;
|
554
|
556
|
}
|
555
|
557
|
|
|
558
|
+PyObject* rpnexpr_random(PyObject *cls, PyObject *args, PyObject *kwds)
|
|
559
|
+{
|
|
560
|
+ long long int args_count, expr_sz;
|
|
561
|
+ char *expr, err_str[128];
|
|
562
|
+ PyObject *res;
|
|
563
|
+ char *names[] = {"args_count", "token_count", NULL};
|
|
564
|
+
|
|
565
|
+ expr_sz = 10;
|
|
566
|
+ if(!PyArg_ParseTupleAndKeywords(args, kwds, "L|L:pyrpn.random_expr", names,
|
|
567
|
+ &args_count, &expr_sz))
|
|
568
|
+ {
|
|
569
|
+ return NULL;
|
|
570
|
+ }
|
|
571
|
+
|
|
572
|
+ expr = rpn_random(expr_sz, args_count);
|
|
573
|
+ if(!expr)
|
|
574
|
+ {
|
|
575
|
+ snprintf(err_str, 128,
|
|
576
|
+ "Error generating random expression : %s",
|
|
577
|
+ strerror(errno));
|
|
578
|
+ PyErr_SetString(PyExc_RuntimeError, err_str);
|
|
579
|
+ return NULL;
|
|
580
|
+ }
|
|
581
|
+ res = Py_BuildValue("s", expr);
|
|
582
|
+ //free(expr);
|
|
583
|
+ return res;
|
|
584
|
+}
|
|
585
|
+
|