|
| 1 | +Cloe-Engine Lua Shell |
| 2 | +===================== |
| 3 | + |
| 4 | +Cloe Engine provides a small Lua shell that you can use as a REPL or a way to |
| 5 | +run Lua scripts with access to the Cloe API without running a simulation. |
| 6 | + |
| 7 | +It currently has the following features: |
| 8 | + |
| 9 | +- Runs Lua files (passed as arguments) |
| 10 | +- Runs Lua strings (passed with `-c` option) |
| 11 | +- REPL session (by default or with `-i` flag) |
| 12 | +- Session history (press Up/Down in interactive session) |
| 13 | +- Multi-line editing (experimental) |
| 14 | +- Automatic value printing (experimental) |
| 15 | + |
| 16 | +You can start the Lua REPL with `cloe-engine shell`. |
| 17 | + |
| 18 | +Hello World |
| 19 | +----------- |
| 20 | + |
| 21 | +Let's demo the various ways we can print "Hello world!" to the console. |
| 22 | + |
| 23 | +### In the REPL |
| 24 | + |
| 25 | +Start the REPL and enter in the statement `print("Hello world!")`: |
| 26 | +```console |
| 27 | +$ cloe-engine shell |
| 28 | +Cloe 0.22.0 Lua interactive shell |
| 29 | +Press [Ctrl+D] or [Ctrl+C] to exit. |
| 30 | +> print("Hello world!") |
| 31 | +Hello world! |
| 32 | +> |
| 33 | +``` |
| 34 | + |
| 35 | +### Running a command |
| 36 | + |
| 37 | +Pass the string from the previous example to the shell with `-c`: |
| 38 | +```console |
| 39 | +$ cloe-engine shell -c 'print("Hello world!")' |
| 40 | +Hello world! |
| 41 | +``` |
| 42 | +You can pass more than one command with `-c` just by repeating it. |
| 43 | + |
| 44 | + |
| 45 | +### Running a file |
| 46 | + |
| 47 | +Create a file `hello.lua` with the following contents: |
| 48 | +```lua |
| 49 | +print("Hello world!") |
| 50 | +``` |
| 51 | +Now run it with `cloe-engine shell`: |
| 52 | +```console |
| 53 | +$ cloe-engine shell hello.lua |
| 54 | +Hello world! |
| 55 | +``` |
| 56 | + |
| 57 | +Multi-Line Editing |
| 58 | +------------------ |
| 59 | + |
| 60 | +If the statement entered on a line looks complete, the shell will run it. |
| 61 | +If there is an error in parsing indicating that the statement looks incomplete, |
| 62 | +the shell will prompt you for more input: |
| 63 | +``` |
| 64 | +> print( |
| 65 | +>> "Hello world!" |
| 66 | +>> ) |
| 67 | +Hello world! |
| 68 | +``` |
| 69 | +This isn't so important for the above example, but for loops, functions, and |
| 70 | +if-statements, it is: |
| 71 | +``` |
| 72 | +> function a() |
| 73 | +>> print( |
| 74 | +>> "Hello world!" |
| 75 | +>> ) |
| 76 | +>> end |
| 77 | +> a() |
| 78 | +Hello world! |
| 79 | +``` |
| 80 | + |
| 81 | +Whitespace |
| 82 | +---------- |
| 83 | + |
| 84 | +Lua does not care about whitespace very much. This means you can replace |
| 85 | +all newlines with spaces and the code works the same. |
| 86 | + |
| 87 | +Consider the following block of code: |
| 88 | +```lua |
| 89 | +print("...") |
| 90 | +io.write("[") |
| 91 | +for _, v in ipairs({1, 2, 3}) do |
| 92 | + io.write(v .. ",") |
| 93 | +end |
| 94 | +io.write("]\n") |
| 95 | +print("---") |
| 96 | +``` |
| 97 | +This can be minified in the following simple ways: |
| 98 | + |
| 99 | +1. Newlines can be replaced with spaces. |
| 100 | +2. Parentheses around plain strings and tables can be removed. |
| 101 | +3. Spaces before and after commas, quotes, parentheses, and brackets can be removed. |
| 102 | + |
| 103 | +This leads to the following minified code: |
| 104 | +```lua |
| 105 | +print"..."io.write"["for _,v in ipairs{1,2,3}do io.write(v..",")end print"]"print"---" |
| 106 | +``` |
| 107 | +This means that sending whole blocks of code from the command line or from |
| 108 | +another application or from code generation is a lot easier. |
| 109 | +``` |
| 110 | +$ cloe-engine shell -c 'print"..."io.write"["for _,v in ipairs{1,2,3}do io.write(v..",")end print"]"print"---"' |
| 111 | +... |
| 112 | +[1,2,3,] |
| 113 | +--- |
| 114 | +``` |
| 115 | +Of course I don't expect you'd really do this kind of crazy minification, but |
| 116 | +it demonstrates just how little Lua cares about whitespace. |
| 117 | + |
| 118 | +:::{note} |
| 119 | +This one little quirk can provide significant benefits over the Python |
| 120 | +scripting language, because it's very easy to compose generated code without |
| 121 | +running into syntax errors because of indentation requirements. |
| 122 | +::: |
0 commit comments