I was working on this a while ago and haven’t found much time for it lately, but I thought it could be interesting to some people. It is a fork of Lua that implements the PICO-8 dialect.
If you know how to embed Lua somewhere, then there are chances that you can embed PICO-8 compatible Lua instead! I use it for an unfinished toy emulator project.
- short if syntax (on one line)
- short print syntax (? at the beginning of a line)
- compound assignment operators: += /= etc.
- C style not equal operator: !=
- C++ style comments with //
- fixed-point arithmetic with overflows, infinity etc.
- the PICO-8 math library (shr, atan2, flr etc.)
- binary literals: 0b1001001.10010
- works in Windows and Linux, and probably in OS X too
The usual approach to the language extensions is through code parsing and patching. For instance, replacing all occurrences of <something>+=<blah> with <something>=<something>+(<blah>). This is what PICO-8 itself does, which is why it still has the occasional weird parsing issues (you can write x=0y=1 but not x+=0y=1). My approach is to implement these extensions in the Lua parser instead, which is elegant, more robust, and gives better error reporting.
It’s on GitHub: https://github.com/samhocevar/z8lua
Here is a z8lua session in my terminal; you can see familiar PICO-8 syntax in here:
FYI, z8lua now supports features from upcoming PICO-8 0.1.12d:
- support for full 8-bit character set (Japanese and extra punctuation)
- new ord() and chr() functions
- new ^= and ..= operators
- short while syntax on a single line: while(foo()) bar()
Also there is experimental support for Lua persistence using Eris, allowing for memory snapshots and advanced debugging.
I'd considered doing something broadly similar, but I can never find the energy these days.
Which version of Lua is backing this? I'm wondering how hard it'd be to diff your version with the base version and try applying the edits to the current Lua, just for the sake of having these extensions on regular Lua.
@Felice it’s based on Lua 5.2 (same as PICO-8); not sure what you mean by “regular Lua” but I consider that regular Lua 🙂 My goal is to emulate PICO-8 so that’s what made sense. Targeting Lua 5.3 might be possible but it’s a very different language, especially in the way it handles numbers, which would make the implementation of fixed-point arithmetic very tricky.
You can see a list of changes against upstream Lua on Github; I tried to split them by feature and one should be able to cherry-pick them.
- ord(), chr(), __tostring metamethod, color()/pal() etc. return values: https://pastebin.com/raw/z8XEqTV5
- short “while” syntax and ^= ..= operators: https://www.lexaloffle.com/bbs/?pid=73179#p
- extra character set: https://twitter.com/lexaloffle/status/1171727810953543680
- button repeat delay: https://twitter.com/lexaloffle/status/1176688167719587841
- fillp() patterns: https://twitter.com/lexaloffle/status/1173850519007195136 (also used the REPL cart to find the exact values since the BBS uses 0.1.12d)
[Please log in to post a comment]