Lua Compiler

Code
Output

        

What Is the Lua Online Compiler?

This tool lets you write and run Lua scripts directly in your browser without installing Lua on your machine. Whether you are learning Lua for the first time or quickly testing table manipulation and metatable patterns, this compiler provides a fast way to execute Lua code online.

Lua scripts run from the top of the file. You can use print() to display output, define functions, work with tables as arrays or dictionaries, and use metatables to implement object-oriented patterns. All standard library functions including string, table, math, io, and os modules are available.

All code executes on a remote sandboxed server powered by the Piston API. Your browser sends the source code to the server, which runs the Lua interpreter inside an isolated container. Lua's dynamic type system, first-class functions, and coroutine support are fully available during execution.

How It Works

  1. Write your Lua code in the editor panel. The editor starts with a simple "Hello, Lua!" example using print(), but you can replace it with any valid Lua script.
  2. Provide input in the STDIN field if your program reads from standard input using io.read(). Leave this field empty if your program does not require input.
  3. Add command-line arguments in the Args field if your program reads from the global arg table. Enter arguments separated by spaces.
  4. Click the Run button to send your code to the Piston execution server. The server runs your Lua script inside a sandboxed container and captures all output.
  5. View the results in the Output panel. You will see everything your program prints to standard output, along with any errors including syntax errors, nil reference errors, or runtime exceptions.

Step-by-Step Example

Suppose you want to write a program that reads a name from standard input and uses a table with a metatable to create a simple greeting object. Here is how you would do it:

First, type the following code into the editor panel:

local Greeter = {}
Greeter.__index = Greeter

function Greeter.new(greeting)
    local self = setmetatable({}, Greeter)
    self.greeting = greeting or "Hello"
    return self
end

function Greeter:greet(name)
    if name and name ~= "" then
        print(self.greeting .. ", " .. name .. "!")
    else
        print(self.greeting .. ", stranger!")
    end
end

local g = Greeter.new("Welcome")
local name = io.read("*l")
g:greet(name)

This example demonstrates several Lua features: tables used as objects, metatables with __index for inheritance, the constructor pattern with setmetatable, the colon syntax for method calls, io.read() for reading input, and string concatenation with the .. operator.

Next, go to the STDIN field and type a name, for example Alice. This value will be read by io.read() when the program runs.

Now click the Run button. The server executes your Lua script with the provided input. After a moment, the Output panel displays the result:

Welcome, Alice!

If you leave the STDIN field empty and click Run again, the output changes to the fallback greeting. This workflow makes it easy to test different inputs and observe how metatables and object patterns behave in Lua.

Use Cases

Limitations and Notes

Frequently Asked Questions

What Lua version does this compiler use?

It uses the latest Lua version available through the Piston execution engine.

Do Lua tables work in this compiler?

Yes, tables are fully supported. You can use them as arrays, dictionaries, objects, and any other data structure that Lua tables can represent.

Can I use metatables?

Yes, metatables and metamethods work fully. You can use setmetatable, getmetatable, and all standard metamethods like __index, __newindex, __add, and __tostring.

Do coroutines work?

Yes, Lua coroutines are fully supported. You can use coroutine.create, coroutine.resume, coroutine.yield, and coroutine.wrap.

Can I load external C libraries?

No, external C libraries and native modules cannot be loaded. Only pure Lua code and the standard library are available.

Can I use LuaRocks packages?

No, the LuaRocks package manager is not available. Only Lua's built-in standard library functions can be used.

Does this support string patterns?

Yes, Lua string patterns are supported through string.find, string.match, string.gmatch, and string.gsub. Note that Lua uses its own pattern syntax, not regular expressions.

Can I pass command-line arguments?

Yes, command-line arguments are available through the global arg table. Enter arguments in the Args field separated by spaces.

Sources and References

Related Compilers