Skip to content

Commit a3bd6e1

Browse files
committed
feat: add modular API shortcuts for compilation pipeline
1 parent 8f9d710 commit a3bd6e1

File tree

1 file changed

+77
-20
lines changed

1 file changed

+77
-20
lines changed

tlc.lua

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4403,8 +4403,66 @@ function VirtualMachine:execute()
44034403
return self:executeClosure()
44044404
end
44054405

4406+
--// Low-Level Shortcuts (Step-by-Step) //--
4407+
4408+
-- Code (string) -> Tokens (list)
4409+
local function tokenize(code)
4410+
return Tokenizer.new(code):tokenize()
4411+
end
4412+
4413+
-- Tokens (list) -> AST (table)
4414+
local function parseTokens(tokens)
4415+
return Parser.new(tokens):parse()
4416+
end
4417+
4418+
-- AST (table) -> Proto (table)
4419+
local function generate(ast)
4420+
return CodeGenerator.new(ast):generate()
4421+
end
4422+
4423+
-- Proto (table) -> Bytecode (string)
4424+
local function emit(proto)
4425+
return BytecodeEmitter.new(proto):emit()
4426+
end
4427+
4428+
-- Proto (table) -> Execution (results...)
4429+
local function execute(proto)
4430+
return VirtualMachine.new(proto):execute()
4431+
end
4432+
4433+
--// High-Level Shortcuts (Pipelines) //--
4434+
4435+
-- Code -> AST
4436+
local function parse(code)
4437+
return parseTokens(tokenize(code))
4438+
end
4439+
4440+
-- Code -> Proto
4441+
local function compileToProto(code)
4442+
return generate(parse(code))
4443+
end
4444+
4445+
-- Code -> Bytecode
4446+
local function compile(code)
4447+
return emit(compileToProto(code))
4448+
end
4449+
4450+
-- Code -> Execution
4451+
local function run(code)
4452+
return execute(compileToProto(code))
4453+
end
4454+
44064455
-- Now I'm just exporting everything...
44074456
return {
4457+
-- ████████╗ ██╗ ██████╗
4458+
-- ╚══██╔══╝ ██║ ██╔════╝
4459+
-- ██║ ██║ ██║
4460+
-- ██║ ██║ ██║
4461+
-- ██║ ███████╗ ╚██████╗
4462+
-- ╚═╝ ╚══════╝ ╚═════╝
4463+
-- Tiny Lua Compiler
4464+
-- MIT License
4465+
44084466
Tokenizer = Tokenizer,
44094467
Parser = Parser,
44104468
CodeGenerator = CodeGenerator,
@@ -4415,27 +4473,26 @@ return {
44154473
-- It is highly recommended to use these shortcut functions for all tasks!
44164474
-- Why? Because these functions act as a stable, future-proof entry points.
44174475
-- If the internal compilation steps (tokenizer, parser, codegen, etc.) ever change,
4418-
-- code that uses ONLY these functions is more likely to continue to work as expected.
4476+
-- code that uses these shortcuts will continue to work without modification.
44194477
-- If you use the classes directly, your code may break with future updates!
44204478
-- Always use the shortcut functions unless you are hacking on the compiler internals.
4421-
fullCompile = function(code)
4422-
assert(type(code) == "string", "Expected a string for 'code', got " .. type(code))
4423-
4424-
local tokens = Tokenizer.new(code):tokenize()
4425-
local ast = Parser.new(tokens):parse()
4426-
local proto = CodeGenerator.new(ast):generate()
4427-
local bytecode = BytecodeEmitter.new(proto):emit()
44284479

4429-
return bytecode
4430-
end,
4431-
compileAndRun = function(code)
4432-
assert(type(code) == "string", "Expected a string for 'code', got " .. type(code))
4433-
4434-
local tokens = Tokenizer.new(code):tokenize()
4435-
local ast = Parser.new(tokens):parse()
4436-
local proto = CodeGenerator.new(ast):generate()
4437-
4438-
local vm = VirtualMachine.new(proto)
4439-
return vm:execute()
4440-
end
4480+
-- Legacy API (Deprecated, kept for backward compatibility)
4481+
fullCompile = compile,
4482+
compileAndRun = run,
4483+
4484+
-- Standard API (High-Level)
4485+
-- Takes code and returns the desired output directly.
4486+
tokenize = tokenize, -- Code -> Tokens
4487+
parse = parse, -- Code -> AST
4488+
compileToProto = compileToProto, -- Code -> Proto
4489+
compile = compile, -- Code -> Bytecode
4490+
run = run, -- Code -> Execution
4491+
4492+
-- Low-Level API (Step-by-Step)
4493+
-- Takes the previous stage's output as input.
4494+
parseTokens = parseTokens, -- Tokens -> AST
4495+
generate = generate, -- AST -> Proto
4496+
emit = emit, -- Proto -> Bytecode
4497+
execute = execute, -- Proto -> Execution
44414498
}

0 commit comments

Comments
 (0)