/luax

Lua-like programming language for studying compiler&interpreter courses.

Primary LanguageCMIT LicenseMIT

Open Source Love

What is Luax?

Luax is a easy-to-learn, concise and powerful programming language. (Chinese Page)

Luax provides full documentation(from the language itself to internal design), making it a perfect project for beginner to learn how to make a interpreter.

Luax is distributed in source code, which contains the bytecode generator, luax virtual machine, standard library, a standalone executable interpreter, and full documentation.

Documentation

For Language User

For Language Hacker

Features

The luax programming language itself:

The C achieve of luax:

Here is a list:

  • dynamic type language (nil, bool, number, string, function, table)
  • basic statements (variable-declaration, if/while, break, continue, function-define)
  • mutli-assign, multi-return - a, b = 1, 2; function(a, b) return a+b, a-b; end
  • table - a container containing several key-values. The type of key or value can be any one of those types listed above.
  • meta table - a table defining what would happens when specific action made to a table(such as: get set call), example: read-only table
  • function - first-class citizen, example: link several functions

Examples

-- table: a container containing several key-values.
local tab = { 'key' : 'value', 1 : false };
tab.name = "I'm a table";
tab["func"] = function() return "I can hold a function"; end; -- tab["func"] equals to tab.func
tab[true] = "the type of my key can be a boolean";
tab[another_table] = "or a table";


-- function
local func = function(a, b) -- define a function
    return a + b, a - b;
end;
tab.a, tab.b = func(1, 2); -- return 3, -1

print(func(1, 2), 2);   -- 3, -1, 2
print((func(1, 2)), 2); -- 3, 2

local func_sum = function(a, b)
    local i, sum = 0, 0;
    while i < arguments.size then  -- every function has a variable named arguments
        sum += arguments[i];
        i += 1;
    end
    return sum;
end
print(func_sum(1, 2, 3)); -- 6
print(func_sum(1)); -- 1, argument b equals to nil now
--[[ 
  See more examples about function from doc/luax_reference_manual.md#function
]]

Build

mkdir build && cd build
cmake ..
make && make test
# find more platforms from [Get Started]