diff --git a/notebooks/xeus-c-lite-demo.ipynb b/notebooks/xeus-c-lite-demo.ipynb new file mode 100644 index 00000000..475a7a30 --- /dev/null +++ b/notebooks/xeus-c-lite-demo.ipynb @@ -0,0 +1,281 @@ +{ + "metadata": { + "kernelspec": { + "name": "xc23", + "display_name": "C", + "language": "c" + }, + "language_info": { + "codemirror_mode": "text/x-c++src", + "file_extension": ".cpp", + "mimetype": "text/x-c++src", + "name": "C++", + "version": "0" + } + }, + "nbformat_minor": 5, + "nbformat": 4, + "cells": [ + { + "id": "aa9d4f33-5bd8-42f2-918a-e654d4363577", + "cell_type": "markdown", + "source": "
\n

C kernel based on xeus

\n
\n\nA Jupyter kernel for C based on the `CppInterOp`, a clang based C++ Interoperability Library and the `xeus` native implementation of the Jupyter protocol, xeus.\n\n- GitHub repository: https://github.com/compiler-research/xeus-cpp\n- Documentation: https://xeus-cpp.readthedocs.io/en/latest/", + "metadata": {} + }, + { + "id": "9af8aa44-7786-47b6-b94b-8579470acb3a", + "cell_type": "markdown", + "source": "## Usage\n\n
\n \n \n
\n To run the selected code cell, hit
Shift + Enter
\n
\n
", + "metadata": { + "jp-MarkdownHeadingCollapsed": true + } + }, + { + "id": "be1c8c6c-3fbe-4f53-9deb-8496c43d26ad", + "cell_type": "markdown", + "source": "## Output and Error streams\n\n`stdout` and `stderr` are redirected to the notebook frontend.", + "metadata": {} + }, + { + "id": "9bd7f767-6c22-4a1b-a1e2-cd4184fd0367", + "cell_type": "code", + "source": "#include \n\nputs(\"some output\");", + "metadata": { + "trusted": true, + "vscode": { + "languageId": "c++" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": "some output\n" + } + ], + "execution_count": 1 + }, + { + "id": "9622f20f-5925-4544-a97b-aada3a14209a", + "cell_type": "code", + "source": "perror(\"some error\");", + "metadata": { + "trusted": true, + "vscode": { + "languageId": "c++" + } + }, + "outputs": [], + "execution_count": 2 + }, + { + "id": "7af0c962-17dc-47d4-9772-b8a06e2bda3a", + "cell_type": "code", + "source": "int j = 5;\nprintf(\"%d\\n\", j);", + "metadata": { + "trusted": true, + "vscode": { + "languageId": "c++" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": "5\n" + } + ], + "execution_count": 3 + }, + { + "id": "a39c7021-ee9f-442e-8ea3-63ca23aa5d46", + "cell_type": "markdown", + "source": "# Interpreting the C programming language\n# You can define functions, structs, etc ...", + "metadata": {} + }, + { + "id": "e5b116ce-ced1-4aa4-b14e-ef7d2606202e", + "cell_type": "markdown", + "source": "## Functions", + "metadata": {} + }, + { + "id": "86b08f22-e16c-4eac-917d-ae6eeb7ec7cb", + "cell_type": "code", + "source": "double sqr(double a)\n{\n return a * a;\n}", + "metadata": { + "trusted": true, + "vscode": { + "languageId": "c++" + } + }, + "outputs": [], + "execution_count": 4 + }, + { + "id": "5aff6711-54bf-4280-a496-c9f7c683eee5", + "cell_type": "code", + "source": "double a = 2.5;\ndouble asqr;\nasqr = sqr(a);\nprintf(\"%lf\\n\", asqr);", + "metadata": { + "trusted": true, + "vscode": { + "languageId": "c++" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": "6.250000\n" + } + ], + "execution_count": 5 + }, + { + "id": "5b3959b0-9dc7-41a4-bba1-e20abd0765f7", + "cell_type": "markdown", + "source": "## Structs", + "metadata": {} + }, + { + "id": "d981a53b-8185-49c5-8a30-02453cc1b9e9", + "cell_type": "code", + "source": "struct foo\n{\n double value;\n};\n\nvoid foo_print(struct foo f)\n{\n printf(\"Foo value = %lf\\n\", f.value);\n}", + "metadata": { + "trusted": true, + "vscode": { + "languageId": "c++" + } + }, + "outputs": [], + "execution_count": 6 + }, + { + "id": "195d513f-d5cb-4e3d-a6cb-ae99dfcd9aab", + "cell_type": "code", + "source": "struct foo bar = {.value = 1.2};\nfoo_print(bar);", + "metadata": { + "trusted": true, + "vscode": { + "languageId": "c++" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": "Foo value = 1.200000\n" + } + ], + "execution_count": 7 + }, + { + "id": "6ce20171-18cb-4373-874b-89e6a2d2e425", + "cell_type": "markdown", + "source": "## Documentation\n\n - Documentation for types of the standard library is retrieved on cppreference.com.", + "metadata": {} + }, + { + "id": "c723686c-a70b-4e68-a175-b2224f0616aa", + "cell_type": "code", + "source": "?printf", + "metadata": { + "trusted": true, + "vscode": { + "languageId": "c++" + } + }, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/html": "\n ", + "text/plain": "https://en.cppreference.com/w/cpp/io/c/fprintf" + }, + "metadata": {} + } + ], + "execution_count": 8 + }, + { + "id": "73aa865d-f643-4b72-9701-f6e312914690", + "cell_type": "markdown", + "source": "# Taking input from the user", + "metadata": {} + }, + { + "id": "6efd2bbc-525a-4659-9431-aefc819e8cb7", + "cell_type": "code", + "source": "#include \n\nchar name[BUFSIZ];\nif (fgets(name, sizeof(name), stdin) != NULL)\n{\n name[strcspn(name, \"\\n\")] = '\\0';\n}", + "metadata": { + "trusted": true + }, + "outputs": [], + "execution_count": 9 + }, + { + "id": "8ec65830-4cb5-4d01-a860-f6c46ac4f60f", + "cell_type": "code", + "source": "printf(\"Your name is %s\\n\", name);", + "metadata": { + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": "Your name is \n" + } + ], + "execution_count": 10 + }, + { + "id": "662e4153-898b-46be-b6fe-eb2cc7981923", + "cell_type": "markdown", + "source": "## Another sample using external (LocalStorage) file.", + "metadata": {} + }, + { + "id": "1696cdd8-7d62-49ec-be53-309253ce1c67", + "cell_type": "code", + "source": "%%file input.txt\nXeus Cpp WASM", + "metadata": { + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": "Overwriting input.txt\n" + } + ], + "execution_count": 11 + }, + { + "id": "d0589ce1-52f8-46a9-9fac-d4531b89001b", + "cell_type": "code", + "source": "#include \n\nFILE* in;\nin = fopen(\"input.txt\", \"r\");\n\nfscanf(in, \"%255[^\\n]\", name);\nprintf(\"%s\\n\", name);\n\nfclose(in);", + "metadata": { + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": "Xeus Cpp WASM\n" + } + ], + "execution_count": 12 + }, + { + "id": "b1c16911-f62f-49e1-a73c-0916b7d07503", + "cell_type": "code", + "source": "", + "metadata": { + "trusted": true + }, + "outputs": [], + "execution_count": null + } + ] +} \ No newline at end of file