minilua::Interpreter class

An interpreter instance is used to parse and evaluate lua source code.

You can create the interpreter with initial source code or empty source code and then update the source code by calling Interpreter::parse and run the current source code by calling Interpreter::evaluate.

You can also apply a source change that was generated by the interpreter while evaluating and get the updated source code.

Example

minilua::Interpreter interpreter{initial_source_code};
interpreter.parse(new_source_code);
auto res = interpreter.evaluate();
if (res.source_change) {
  interpreter.apply_source_change(res.source_change);
}
interpreter.evaluate();

Constructors, destructors, conversion operators

Interpreter()
Initializes the interpreter with empty source code.
Interpreter(std::string initial_source_code)
Initializes the interpreter with the given source code.

Public functions

auto config() -> InterpreterConfig & -> auto
Returns the current configuration.
auto config() const -> const InterpreterConfig & -> auto
Returns the current configuration.
void set_config(InterpreterConfig)
Sets the configuration.
auto environment() const -> Environment & -> auto
Returns the environment for modification.
auto source_code() const -> const std::string & -> auto
Returns a view into the current source code.
auto parse(std::string source_code) -> ParseResult -> auto
Parse fresh source code.
auto apply_source_changes(std::vector<SourceChange>) -> RangeMap -> auto
Applies a list of single source changes.
auto evaluate() -> EvalResult -> auto
Run the parsed program.

Function documentation

minilua::Interpreter::Interpreter(std::string initial_source_code)

Initializes the interpreter with the given source code.

Throws an exception if there was an error parsing the source code.

auto minilua::Interpreter::environment() const -> Environment &

Returns the environment for modification.

auto minilua::Interpreter::source_code() const -> const std::string &

Returns a view into the current source code.

auto minilua::Interpreter::parse(std::string source_code) -> ParseResult

Parse fresh source code.

Errors are part of ParseResult.

auto minilua::Interpreter::apply_source_changes(std::vector<SourceChange>) -> RangeMap

Applies a list of single source changes.

This returns a map of ranges that were changed. Keys are the old ranges and values are the new ranges. Note that this map only contains ranges for the applied source changes. Other range might have moved and are not present in the map.

auto minilua::Interpreter::evaluate() -> EvalResult

Run the parsed program.

Throws an exception when there is an error in the program. E.g. when you try to add incompatible values in lua.

Currently this throws std::runtime_error.