Skip to content

ByteXenon/The-Tiny-Lua-Compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Untitled

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.

Features

  • 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.

Why should I care?

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.

But compilers are scary!

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.

Okay so where do I begin?

Awesome! Head on over to the the-tiny-lua-compiler.lua file.

I'm back, that didn't make sense

Ouch, I'm really sorry. Let me know how it can be improved, by emailing me at [email protected] or opening an issue on this repo.

Example usage?

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()

Tests

Run with lua tests/test.lua


cc-by-4.0 Open Source LGBTQ+ Friendly Transgender Flag