@nodecraft/lua-ast
v0.9.0
Published
JavaScript parser/runner/generator for Lua using the luaparse library
Readme
Lua-AST
Lua parser and generator written in Typescript.
USAGE
Basic parsing:
import { getOriginalBase, parse, update } from 'lua-ast';
const data = getOriginalBase();
const chunk = parse('example=1');Execution is limited to 1,000 steps by default. Configure the budget when
creating execution data, or pass the same options to update:
const data = getOriginalBase({ maxSteps: 10_000 });
update(chunk, globals, { maxSteps: 10_000 });Statements, loop iterations, and Lua function calls consume steps. Exceeding
the budget throws ExecutionLimitError.
Getting globals from a script: Chunk.execute(data)
chunk.execute(data);
// data.globals === { example: 1 }Getting the generated script: Chunk.toString()
chunk.toString()
//=> 'example = 1'Setting a value on a global: Chunk.set(var, data) var needs to be a string data can be a Map, string, number, or boolean.
chunk.set('example', new Map([[1, 2]]))
chunk.execute(data);
// data.globals === { example: Map { 1 => 2 } }
chunk.toString()
/*=>
example =
{
2
}*/Setting a value on a table: Chunk.set([var, ...property], data) property is a string that represents the path through the var to get to the location to set. the last property can be the empty string in order to to append a value to the current table.
BUG: you currently can't use this on a variable that isn't already defined as a table.
chunk.set(['example', ''], 4)
chunk.execute(data);
// data.globals === { example: Map { 1 => 2, 2 => 4 } }
chunk.toString()
/*=>
example =
{
2,4
}*/Deleting a global variable: Chunk.delete(var)
data.globals = {};
chunk.delete('example');
chunk.execute(data);
// data.globals === { }
chunk.toString();
/*=> ''*/Deleting a value on a table: Chunk.delete([var, ...property], data?) property is a string that represents the path through the var to get to the location to delete. the last property can be the empty string in order to match a value corresponding to data.
chunk.set('example', new Map([['test', 'thing'], ['thing', 'test']]));
chunk.execute(data);
// data.globals === { example: Map { 'test' => 'thing', 'thing' => 'test' } }
chunk.delete(['example', 'test']);
chunk.execute(data);
// data.globals === { example: Map { 'thing' => 'test' } }
chunk.delete(['example', ''], 'test');
chunk.execute(data);
// data.globals === { example: Map {} }Updating values en-masse: LAST.update(chunk, globals)
data.globals = {};
update(chunk, {
execute: true
});
chunk.execute(data);
// data.globals === { execute: true }