Lab 10: Let's Learn script and Lua!
Overview
Last week, we learned how to use the visitor pattern to make it easier to extend programs written in certain languages (such as Java). We also learned how metaprogramming can making it less tedious, repetitious, and error-prone to write code.
This week, we will also learn some techniques for making programs more extensible and for making programming somewhat less tedious. In particular, we will see how embedding an entire programming language inside an application can make it easier to extend that application!
The application we will extend is called script
—a relatively simple UNIX utility. The
programming language that we will embed in script
is called Lua.
Takeaways
The goals of this lab are to become familiar with script
and with Lua, so that we can
combine them in the assignment. Here are some things you should take away from the lab,
which will help you on the assignment. You should be able to:
- Use the
script
program to log a session. - Describe in general terms what the
script
program does. - Write small programs in Lua, which requires you to
- Write code in Lua syntax.
- Apply Lua control structures.
- Describe and apply Lua’s techniques for iteration.
- Use Lua data structures, specifically tables.
Materials
Use the assignment workflow and the link provided on Piazza to get access to this week’s files.
We recommend that you do your work on the CS machine called shadowfax
. This machine
is similar to knuth: You access it in the same way
(ssh <username>@shadowfax.cs.hmc.edu
), and all your files are available there. The main
difference is that shadowfax
has the latest version of Lua installed, whereas knuth
does not.
Warmup
Part A: Getting used to script
script
is a utility that can record a log of your interactive shell sessions.
Run man script
to learn the purpose and arguments for the program, and start it up in
the directory that has your lab code by running
script -a session-log.txt
. This command will start a logging session.
IMPORTANT Script only finishes writing everything that’s been logged when you type exit
into the terminal.1 Don’t forget to do this.
Note: Script catches everything that is sent to that terminal,
including control codes for cursor positioning, and every typo and
correction you make. For this reason, after you’ve made a session log,
the best way to view it is not by opening it in an editor, but by
sending it straight to the terminal, with cat session-log.txt
.
Part B: Running Lua and script
together
(Make sure this part is done in a logged session)
Start lua
by typing lua
.
The Lua interpreter has a standard read-eval-print loop. Type
print("Hello World")
and see what
happens.
By default, Lua is expecting a statement (e.g., a function call). Sometimes, you might want to evaluate an expression instead (in Lua, many kinds of expressions are not statements), you do that by prefixing your expression with an equals sign:
= 42 + 12
Play with Lua
In the assignment, you will write some Lua code, so it is a good idea to take some time now to become familiar with it. Compared to other languages you may have seen in courses, Lua is most similar to JavaScript or Python. Use your experience with these languages, and use the resources below, to explore Lua a bit.
About Lua
Lua is a scripting language that was designed to be embedded in other applications. Embedding a scripting language in an application makes the application more extensible. For example, imagine you’ve written an image-editing program in C++. If you ship your program with an embedded scripting language, then users can extend your program, (for example, to add new filters), without having to change the original source code or recompile the program.
Many scripting languages can be embedded in programs. We are going to focus on Lua for several reasons:
-
Lua is a scripting language that probably few members of the class have seen (unlike, say, Python);
-
Lua has a syntax that isn’t the same as C/C++/Java (unlike say, JavaScript), thereby broadening your horizons and experience;
- Lua is designed specifically to be embeddable and as such is quite often used as an extension language in industry;
In the assignment, you will modify an application so that it embeds Lua as a scripting language, then write a Lua script for that application!
Running Lua
There are many ways to run Lua:
- You can run
lua
onshadowfax
. (This might be the preferred option because we provide you with some code to run, which you can check out onshadowfax
). - You can run a live demo in a browser.
- You can download and install it on your own machine.
Exploring Lua datatypes
With Lua running, read over this tutorial on Lua types and try some examples for yourself.
A few things to notice about basic types and operations in Lua:
- String concatenation
..
(not+
or++
). There is more information about strings in the strings tutorial. - Inequality is
~=
(not!=
or/=
). - Tables are like Python dictionaries, but with some support to make them look like arrays. There is more information about tables in the tables tutorial.
Exploring basic Lua control flow
With Lua running, read over this tutorial on Lua control-flow structures.
- Try some examples for yourself, particularly:
if/elseif/else/
,while/do
,repeat/until
, numericfor
, and iteratorfor
. - You can also read, run, and modify the provided code in
control.lua
, which has links to even more information about these control-flow structures
Exploring Lua functions
Look over the provided file functions.lua
. Notice how to define and call functions.
To learn more about functions in you, you can also read over this tutorial on Lua functions.
Exploring Lua iterators
Look over the provided file iteration.lua
. Notice how to define and use iterators.
To learn more about functions in you, you can also read over this tutorial on Lua
iterators. Note that this tutorial is a little out of
date. In particular, the new way of getting the length of an array t
is #t
instead of
table.getn(t)
.
Write a small program
In coolprog.lua
, write a small program that uses what you have learned. To help most
with the assignment, we recommend writing a program that reads in some information from
the user, does something with the information, then prints the results.
More resources for learning Lua
Some key resources for learning Lua include:
- The Lua Tutorial.
- The book Programming in Lua.
- An article about the design of Lua.
A word of warning, though: some of these resources are a bit outdated. As you work on the assignment, if something does not seem to work, double-check (e.g., by Googling) that you are using the most up-to-date Lua syntax for your needs. There is also the up-to-date Lua Reference Manual.
-
You can make
script
periodically flush the output to the log file to keep it up to date (rather than waiting until you have exited the session) using an extra command-line argument. BUT this argument is different on Macs and Linux. On Linux machines, likeknuth
, you can runscript -f -a session-log.txt
but on Macs (if you’re using a Mac directly rather than ssh-ing toknuth
), you instead need to runscript -t 5 -a session-log.txt
↩