8#ifndef SEN_CORE_LANG_VM_H
9#define SEN_CORE_LANG_VM_H
87 return std::get<T>(value);
92[[nodiscard]]
inline bool holds(
const Value& value)
noexcept
94 return std::holds_alternative<T>(value);
131 [[nodiscard]] const uint8_t*
code() const noexcept;
134 [[nodiscard]]
int count() const noexcept;
146 [[nodiscard]]
std::
size_t printSimpleInstruction(
std::string_view name,
std::
size_t offset) const;
147 [[nodiscard]]
std::
size_t printConstantInstruction(
std::string_view name,
std::
size_t offset) const;
148 [[nodiscard]]
std::
size_t printJumpInstruction(
std::string_view name,
std::
size_t offset) const;
149 [[nodiscard]]
std::
size_t printVariableInstruction(
std::
size_t offset) const;
152 std::vector<uint8_t> code_;
190 [[nodiscard]] SEN_ALWAYS_INLINE uint8_t readByte();
191 [[nodiscard]] SEN_ALWAYS_INLINE uint16_t readShort();
192 [[nodiscard]] SEN_ALWAYS_INLINE
Value readConstant();
193 [[nodiscard]]
inline Value pop();
194 inline void push(
Value value);
201 inline void notEqual();
202 inline void lowerThan();
203 inline void lowerOrEqualThan();
204 inline void greaterThan();
205 inline void greaterOrEqualThan();
208 inline void jumpIfFalse(uint16_t offset);
209 inline void jumpIfTrue(uint16_t offset);
210 inline void fetchVariable(uint8_t index);
211 inline void between();
214 template <
typename C>
215 inline void mathOperation(
const Value& a,
const Value& b, C op);
217 template <
typename C>
218 inline void comparisonOperation(
const Value& a,
const Value& b, C op);
221 using Stack = std::stack<Value, StaticVector<Value, stackMax>>;
224 const Chunk* currentChunk_ =
nullptr;
226 const uint8_t* ip_ =
nullptr;
Result<T, E> is a template type that can be used to return and propagate errors. The intent is to rep...
Definition result.h:135
Contiguous view of elements of type T. Inspired by http://www.open-std.org/jtc1/sc22/wg21/docs/papers...
Definition span.h:34
Stack-based, exception-free and resizable vector with fixed-capacity.
Definition static_vector.h:571
A chunk of byte code. It holds code and constants needed by the code. Constants are accessed by index...
Definition vm.h:101
const Value & getConstant(uint8_t offset) const
The constant stored at a given slot.
bool isValid() const noexcept
Returns true if it contains code.
uint8_t getOrRegisterVariable(const std::string &name)
Registers a variable and obtains the index.
std::size_t disassembleInstruction(std::size_t offset) const
Prints a human-readable view of the instruction at a given offset.
static void printValue(const Value &value)
Prints a value.
void patch(std::size_t offset, uint8_t byte)
Patches the a code byte at some offset.
Span< const std::string > getVariables() const
The variables used by this chunk.
void addCode(uint8_t byte)
Appends a byte to the code. The line is just for debugging.
void disassemble(const std::string &name) const
Prints a human-readable view of the chunk.
const uint8_t * code() const noexcept
The stored code.
int count() const noexcept
The size of the code.
uint8_t addConstant(const Value &value)
Stores a constant and returns the index that can be used to refer to it.
std::string what
Definition vm.h:170
QueryStatement parse(const std::string &query) const
Parse a query string into a statement. Throws in case of error.
std::string what
Definition vm.h:176
Result< Chunk, CompileError > compile(const QueryStatement &statement) const
Compile source code into a chunk.
Result< Value, RuntimeError > interpret(const Chunk &chunk, Environment environment={})
Interpret a chunk of code.
Compilation failure report.
Definition vm.h:169
Runtime error report.
Definition vm.h:175
bool holds(const Value &value) noexcept
True if the variant holds T.
Definition vm.h:92
std::function< Value()> ValueGetter
Gets a value from some source.
Definition vm.h:78
Span< ValueGetter > Environment
The environment is an indexed list of value getter functions.
Definition vm.h:81
OpCode
Instructions that can be executed by the virtual machine.
Definition vm.h:36
T extract(const Value &value)
Get T out of the value variant.
Definition vm.h:85
constexpr std::size_t stackMax
Definition vm.h:32
std::variant< float32_t, float64_t, int32_t, uint32_t, int64_t, uint64_t, bool, std::string, uint8_t, int16_t, uint16_t, VariantAccessError > Value
A value that can be in the stack.
Definition vm.h:64
@ multiply
Definition stl_expression.h:40
@ add
Definition stl_expression.h:38
@ divide
Definition stl_expression.h:41
@ subtract
Definition stl_expression.h:39
@ negate
Definition stl_expression.h:25
@ opJumpIfTrue
Jumps to an offset if the top-most value is true.
Definition vm.h:52
@ opJumpIfFalse
Jumps to an offset if the top-most value is false.
Definition vm.h:53
@ opMul
Multiplies the two top-most values on the stack.
Definition vm.h:42
@ opEqual
Compares the two top-most values for equality.
Definition vm.h:44
@ opLowerThan
Compares the two top-most values for <.
Definition vm.h:48
@ opOr
Logical or of the two top-most values.
Definition vm.h:51
@ opDiv
Divides the two top-most values on the stack.
Definition vm.h:43
@ opLowerOrEqualThan
Compares the two top-most values for <=.
Definition vm.h:49
@ opReturn
Finishes execution.
Definition vm.h:37
@ opSub
Subtracts the two top-most values on the stack.
Definition vm.h:41
@ opAnd
Logical and of the two top-most values.
Definition vm.h:50
@ opConstant
Push a constant to the stack. Requires an index as the argument.
Definition vm.h:38
@ opGreaterThan
Compares the two top-most values for >.
Definition vm.h:46
@ opBetween
Checks that the top and second values are <= and >= to the third value.
Definition vm.h:55
@ opNegate
Negates the value at the top of the stack.
Definition vm.h:39
@ opNotEqual
Compares the two top-most values for inequality.
Definition vm.h:45
@ opGreaterOrEqualThan
Compares the two top-most values for >=.
Definition vm.h:47
@ opAdd
Adds the two top-most values on the stack.
Definition vm.h:40
@ opFetchVariable
Fetches a variable in a given index and pushes the value.
Definition vm.h:54
@ equal
Definition stl_token.h:49
Definition stl_statement.h:204
Represents a variant that could not be accessed.
Definition vm.h:60
float float32_t
Definition numbers.h:16
double float64_t
Definition numbers.h:17
Definition code_location.h:14