From bcb9706c6b27df322e7b032b7ad65dcb011d5a12 Mon Sep 17 00:00:00 2001 From: erentar Date: Sun, 30 Jun 2024 14:03:57 +0200 Subject: [PATCH] Add language support for Lua --- docs/languages.md | 1 + src/jupytext/languages.py | 1 + .../inputs/ipynb_lua/lua_example.ipynb | 152 ++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 tests/data/notebooks/inputs/ipynb_lua/lua_example.ipynb diff --git a/docs/languages.md b/docs/languages.md index 660335ee..055d0905 100644 --- a/docs/languages.md +++ b/docs/languages.md @@ -15,6 +15,7 @@ Jupytext works with notebooks in any of the following languages: - Java - Javascript - Julia +- Lua - Matlab - OCaml - Octave diff --git a/src/jupytext/languages.py b/src/jupytext/languages.py index 37d01406..28133a45 100644 --- a/src/jupytext/languages.py +++ b/src/jupytext/languages.py @@ -91,6 +91,7 @@ "comment_suffix": "*/", }, ".xsh": {"language": "xonsh", "comment": "#"}, + ".lua": {"language": "lua", "comment": "--"}, } _COMMENT_CHARS = [ diff --git a/tests/data/notebooks/inputs/ipynb_lua/lua_example.ipynb b/tests/data/notebooks/inputs/ipynb_lua/lua_example.ipynb new file mode 100644 index 00000000..3ec0c6d5 --- /dev/null +++ b/tests/data/notebooks/inputs/ipynb_lua/lua_example.ipynb @@ -0,0 +1,152 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Source: https://www.lua.org/pil/19.3.html" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Sort" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Another useful function on arrays is table.sort, which we have seen before. It receives the array to be sorted, plus an optional order function. This order function receives two arguments and must return true if the first argument should come first in the sorted array. If this function is not provided, sort uses the default less-than operation (corresponding to the `<ยด operator). " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A common mistake is to try to order the indices of a table. In a table, the indices form a set, and have no order whatsoever. If you want to order them, you have to copy them to an array and then sort the array. Let us see an example. Suppose that you read a source file and build a table that gives, for each function name, the line where that function is defined; something like this: " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "lines = {\n", + " luaH_set = 10,\n", + " luaH_get = 24,\n", + " luaH_present = 48,\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " Now you want to print these function names in alphabetical order. If you traverse this table with pairs, the names appear in an arbitrary order. However, you cannot sort them directly, because these names are keys of the table. However, when you put these names into an array, then you can sort them. First, you must create an array with those names, then sort it, and finally print the result: " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "luaH_get\n", + "luaH_present\n", + "luaH_set\n" + ] + } + ], + "source": [ + "a = {}\n", + "for n in pairs(lines) do table.insert(a, n) end\n", + "table.sort(a)\n", + "for i,n in ipairs(a) do print(n) end" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note that, for Lua, arrays also have no order. But we know how to count, so we get ordered values as long as we access the array with ordered indices. That is why you should always traverse arrays with ipairs, rather than pairs. The first imposes the key order 1, 2, ..., whereas the latter uses the natural arbitrary order of the table. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As a more advanced solution, we can write an iterator that traverses a table following the order of its keys. An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array, and then iterates on the array. At each step, it returns the key and value from the original table: " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "function pairsByKeys (t, f)\n", + " local a = {}\n", + " for n in pairs(t) do table.insert(a, n) end\n", + " table.sort(a, f)\n", + " local i = 0 -- iterator variable\n", + " local iter = function () -- iterator function\n", + " i = i + 1\n", + " if a[i] == nil then return nil\n", + " else return a[i], t[a[i]]\n", + " end\n", + " end\n", + " return iter\n", + "end" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " With this function, it is easy to print those function names in alphabetical order. The loop " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "luaH_get\t24\n", + "luaH_present\t48\n", + "luaH_set\t10\n" + ] + } + ], + "source": [ + "for name, line in pairsByKeys(lines) do\n", + " print(name, line)\n", + "end" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Lua", + "language": "lua", + "name": "lua" + }, + "language_info": { + "file_extension": ".lua", + "mimetype": "text/x-lua", + "name": "lua", + "version": "5.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}