void parse_funcHead() { switch(next_token.type) { case TT_TYPE_DOUBLE: case TT_TYPE_INT: case TT_TYPE_STRING: func_init(); match(next_token.type); func_set_return_type(curr_token.type); match(TT_IDENTIFICATOR); func_set_name(curr_token.str); match(TT_PARENTHESES_OPEN); parse_paramSpec(); func_set_param_count(param_count); match(TT_PARENTHESES_CLOSE); symbol_t* funcRef = func_finish(); func_table_add(funcRef); parse_funcBody(funcRef); break; default: error("Syntactic error: Failed to parse the program", ERROR_SYN); } }
static PyObject * func_new(PyTypeObject* type, PyObject* args, PyObject* kw) { PyCodeObject *code; PyObject *globals; PyObject *name = Py_None; PyObject *defaults = Py_None; PyObject *closure = Py_None; PyFunctionObject *newfunc; Py_ssize_t nfree, nclosure; static char *kwlist[] = {"code", "globals", "name", "argdefs", "closure", 0}; if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function", kwlist, &PyCode_Type, &code, &PyDict_Type, &globals, &name, &defaults, &closure)) return NULL; if (name != Py_None && !PyString_Check(name)) { PyErr_SetString(PyExc_TypeError, "arg 3 (name) must be None or string"); return NULL; } if (defaults != Py_None && !PyTuple_Check(defaults)) { PyErr_SetString(PyExc_TypeError, "arg 4 (defaults) must be None or tuple"); return NULL; } nfree = PyTuple_GET_SIZE(code->co_freevars); if (!PyTuple_Check(closure)) { if (nfree && closure == Py_None) { PyErr_SetString(PyExc_TypeError, "arg 5 (closure) must be tuple"); return NULL; } else if (closure != Py_None) { PyErr_SetString(PyExc_TypeError, "arg 5 (closure) must be None or tuple"); return NULL; } } /* check that the closure is well-formed */ nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure); if (nfree != nclosure) return PyErr_Format(PyExc_ValueError, "%s requires closure of length %zd, not %zd", PyString_AS_STRING(code->co_name), nfree, nclosure); if (nclosure) { Py_ssize_t i; for (i = 0; i < nclosure; i++) { PyObject *o = PyTuple_GET_ITEM(closure, i); if (!PyCell_Check(o)) { return PyErr_Format(PyExc_TypeError, "arg 5 (closure) expected cell, found %s", o->ob_type->tp_name); } } } newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code, globals); if (newfunc == NULL) return NULL; if (name != Py_None) { func_set_name(newfunc, name); // Py_INCREF(name); // Py_DECREF(newfunc->func_name); // newfunc->func_name = name; } if (defaults != Py_None) { Py_INCREF(defaults); newfunc->func_defaults = defaults; } if (closure != Py_None) { Py_INCREF(closure); newfunc->func_closure = closure; } return (PyObject *)newfunc; }