Skip to content

Repository files navigation

LuASM

A library to parse and execute custom ASM.

It is a light‑weight Lua library that lets you define, parse and later execute a custom assembly‑like language.
And the library is deliberately minimal:

  • No external dependencies – pure Lua 5.1+.
  • Pluggable instruction set – you decide which mnemonics exist and how their operands are interpreted.
  • Configurable syntax – label delimiters, immediate prefixes, register prefixes, etc., are all driven by a settings table.
  • Tokenizer abstraction – you can feed source from a string, a file, or any other stream by providing a get_next_line method.
  • Execute the code - you can also define the behaviour of the instruction and with that execute this code.

Installation

Because LuASM is a single Lua file, installation is straightforward:

# Clone the repository (or copy the file into your project)
git clone https://github.com/quickwrite/luasm.git

Or, if you just need the file:

-- In your project directoryluasm.lua#<-- the file you just saw

No external libraries are required; the code runs on any Lua interpreter (5.1, 5.2, 5.3, LuaJIT, etc.).

Quick Start

localLuASM=require("luasm")
-- 1. Define the instruction setlocalinstructions= {
LuASM.instruction("mov", {"imm", "reg"}, {}),
LuASM.instruction("mov", {"reg", "reg"}, {}),
LuASM.instruction("add", {"reg", "reg"}, {}),
LuASM.instruction("jmp", {"label"}, {}),
}
-- 2. Create a runner (use default settings)localasm=LuASM:new(instructions, {})
-- 3. Tokenize a source stringlocalsrc=[[start: mov 10 r0 add r0 r1 jmp start]]localtokenizer=asm:string_tokenizer(src)
-- 4. Parselocalresult=asm:parse(tokenizer)
print("Lines parsed:", result.parsed_lines)
forname, infoinpairs(result.labels) doprint("Label: " ..name.." -> line: " ..info.location)
endfori, instrinipairs(result.instructions) doprint(i, instr.op) -- currently just the instruction nameend

Which should result in something like this:

Lines parsed: 4
Label: start -> line: 1
1 mov
2 add
3 jmp

Examples

To see other examples, they can be found in the examples/ directory. To execute them the README inside this directory should be followed.

These examples are numbered so that they can be viewed in sequential order.

Version

This project is currently in Version 0.1.0. This is the first version that can actively be used.

License

LuASM is released under the MIT License – you are free to use, modify, and distribute it in your projects. See the LICENSE file for the full text.

About

A library to parse and execute custom ASM.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages