originally inspired by The Super Tiny Compiler
Welcome to The Tiny Lua Compiler!
This is an ultra-simplified example of all the major pieces of a modern compiler written in easy to read Lua code.
- Self-compiling: This compiler can compile itself!
- Educational: Reading through the guided code will help you learn about how most compilers work from end to end.
That's fair, most people don't really have to think about compilers in their day jobs. However, compilers are all around you, tons of the tools you use are based on concepts borrowed from compilers.
Yes, they are. But that's our fault (the people who write compilers), we've taken something that is reasonably straightforward and made it so scary that most think of it as this totally unapproachable thing that only the nerdiest of the nerds are able to understand.
Awesome! Head on over to the the-tiny-lua-compiler.lua file.
Ouch, I'm really sorry. Let me know how it can be improved, by emailing me at ddavi142@asu.edu or opening an issue on this repo.
Currently, the compiler only supports Lua 5.1. Here's an example of how you can use it:
local tlc = require("the-tiny-lua-compiler")
-- Compile the input code
local inputCode = [[
for index = 1, 10 do
print(index)
end
print("Hello, World!")
]]
local tokens = tlc.Tokenizer.tokenize(inputCode)
local ast = tlc.Parser.parse(tokens)
local proto = tlc.InstructionGenerator.generate(ast)
local bytecode = tlc.Compiler.compile(proto)
local compiledFunction = loadstring(bytecode)
-- Run the compiled function
compiledFunction()
Run with lua tests/test.lua