{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Postprocessing and preprocessing\n", "\n", "## Header preprocessing\n", "\n", "If you need to preprocess header code before the generation, you can create a function that transforms the source code, and store it inside `options.srcmlcpp_options.code_preprocess_function`\n", "\n", "For example:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "
\n", " \n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
int add(int, int b);\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", " \n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
def add(param_0: Int32, b: Int32) -> Int32:\n",
       "    pass\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
m.def("add",\n",
       "    add, py::arg("param_0"), py::arg("b"));\n",
       "
\n", "\n", "
\n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def preprocess_change_int(code: str) -> str:\n", " return code.replace(\"int\", \"Int32\") # This is a *very* dumb preprocessor\n", "\n", "\n", "cpp_code = \"\"\"\n", "int add(int, int b);\n", "\"\"\"\n", "\n", "import litgen\n", "from litgen.demo import litgen_demo\n", "\n", "options = litgen.LitgenOptions()\n", "options.srcmlcpp_options.code_preprocess_function = preprocess_change_int\n", "litgen_demo.demo(options, cpp_code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Post-processing of the stub and pydef files\n", "\n", "You can also post-process the stub and pydef files, i.e. apply a function that will be called after the code is generated. Below is a very dumb example:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "
\n", " \n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
int AnswerToTheUltimateQuestionOfLife_TheUniverse_AndEverything() { return 42; }\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", " \n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
# Copyright(c) 2023 - Pascal Thomet\n",
       "#    Yes, I claim the copyright on this magnificent function.\n",
       "#    ...At least, I tried...\n",
       "\n",
       "def answer_to_the_ultimate_question_of_life_the_universe_and_everything() -> int:\n",
       "    pass\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
// Copyright(c) 2023 - Pascal Thomet\n",
       "//    Yes, I claim the copyright on this magnificent function.\n",
       "//    ...At least, I tried...\n",
       "m.def("answer_to_the_ultimate_question_of_life_the_universe_and_everything",\n",
       "    AnswerToTheUltimateQuestionOfLife_TheUniverse_AndEverything);\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "cpp_code = \"\"\"\n", "int AnswerToTheUltimateQuestionOfLife_TheUniverse_AndEverything() { return 42; }\n", "\"\"\"\n", "\n", "\n", "from codemanip import code_utils\n", "\n", "\n", "def postprocess_stub(code: str) -> str:\n", " return (\n", " code_utils.unindent_code(\n", " \"\"\"\n", " # Copyright(c) 2023 - Pascal Thomet\n", " # Yes, I claim the copyright on this magnificent function.\n", " # ...At least, I tried...\n", " \"\"\"\n", " )\n", " + code\n", " )\n", "\n", "\n", "def postprocess_pydef(code: str) -> str:\n", " return (\n", " code_utils.unindent_code(\n", " \"\"\"\n", " // Copyright(c) 2023 - Pascal Thomet\n", " // Yes, I claim the copyright on this magnificent function.\n", " // ...At least, I tried...\n", " \"\"\"\n", " )\n", " + code\n", " )\n", "\n", "\n", "options = litgen.LitgenOptions()\n", "options.postprocess_stub_function = postprocess_stub\n", "options.postprocess_pydef_function = postprocess_pydef\n", "litgen_demo.demo(options, cpp_code, show_pydef=True)" ] } ], "metadata": { "kernelspec": { "display_name": "venv311", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.0" } }, "nbformat": 4, "nbformat_minor": 2 }