Toy 1.1.6
The Toy programming language is an imperative bytecode-intermediate embedded scripting language
Loading...
Searching...
No Matches
toy_interpreter.h
Go to the documentation of this file.
1
7#ifndef TOY_INTERPRETER_H_
8#define TOY_INTERPRETER_H_
9
10#include <stdbool.h>
11#include <stddef.h>
12
13#include "toy_common.h"
14#include "toy_literal.h"
15#include "toy_literal_array.h"
17#include "toy_scope.h"
18
19//the interpreter acts depending on the bytecode instructions
25typedef struct Toy_Interpreter {
26 //input
27 const unsigned char *bytecode;
28 int length;
29 int count;
33 //operation
37 //Library APIs
40 //debug outputs
45 int depth;
46 bool panic;
48
49//native API
60TOY_API bool Toy_injectNativeFn(Toy_Interpreter *interpreter, const char *name, Toy_NativeFn func);
61
71TOY_API bool Toy_injectNativeHook(Toy_Interpreter *interpreter, const char *name, Toy_HookFn hook);
72
84TOY_API bool Toy_callLiteralFn(Toy_Interpreter *interpreter, Toy_Literal func, Toy_LiteralArray *arguments, Toy_LiteralArray *returns);
85
96TOY_API bool Toy_callFn(Toy_Interpreter *interpreter, const char *name, Toy_LiteralArray *arguments, Toy_LiteralArray *returns);
97
98//utilities for the host program
111TOY_API bool Toy_parseIdentifierToValue(Toy_Interpreter *interpreter, Toy_Literal *literalPtr);
112
123TOY_API void Toy_setInterpreterPrint(Toy_Interpreter *interpreter, Toy_PrintFn printOutput);
124
135TOY_API void Toy_setInterpreterAssert(Toy_Interpreter *interpreter, Toy_PrintFn assertOutput);
136
147TOY_API void Toy_setInterpreterError(Toy_Interpreter *interpreter, Toy_PrintFn errorOutput);
148
149//main access
156TOY_API void Toy_initInterpreter(Toy_Interpreter *interpreter); //start of program
157
168TOY_API void Toy_runInterpreter(Toy_Interpreter *interpreter, const unsigned char *bytecode, size_t length); //run the code
169
182TOY_API void Toy_resetInterpreter(Toy_Interpreter *interpreter); //use this to reset the interpreter's environment between runs
183
190TOY_API void Toy_freeInterpreter(Toy_Interpreter *interpreter); //end of program
191
192#endif /* TOY_INTERPRETER_H_ */
Definition: toy_interpreter.h:25
const unsigned char * bytecode
Definition: toy_interpreter.h:27
Toy_LiteralArray literalCache
Definition: toy_interpreter.h:31
Toy_PrintFn printOutput
Definition: toy_interpreter.h:41
Toy_PrintFn errorOutput
Definition: toy_interpreter.h:43
int count
Definition: toy_interpreter.h:29
Toy_LiteralDictionary * hooks
Definition: toy_interpreter.h:38
bool panic
Definition: toy_interpreter.h:46
int depth
Definition: toy_interpreter.h:45
int length
Definition: toy_interpreter.h:28
Toy_PrintFn assertOutput
Definition: toy_interpreter.h:42
Toy_Scope * scope
Definition: toy_interpreter.h:34
int codeStart
Definition: toy_interpreter.h:30
Toy_LiteralArray stack
Definition: toy_interpreter.h:35
@????@
Definition: toy_literal_array.h:20
Definition: toy_literal_dictionary.h:31
Definition: toy_literal.h:85
Definition: toy_scope.h:21
TOY_API void Toy_initInterpreter(Toy_Interpreter *interpreter)
Initializes the Toy_Interpreter. It allocates memory for internal systems such as the stack,...
TOY_API void Toy_setInterpreterAssert(Toy_Interpreter *interpreter, Toy_PrintFn assertOutput)
Sets the function called by the assert keyword on failure. By default, the following wrapper is used:...
TOY_API void Toy_runInterpreter(Toy_Interpreter *interpreter, const unsigned char *bytecode, size_t length)
Takes a Toy_Interpreter and bytecode (as well as the length of the bytecode), checks its version info...
TOY_API bool Toy_parseIdentifierToValue(Toy_Interpreter *interpreter, Toy_Literal *literalPtr)
Sometimes, native functions will receive Toy_Literal identifiers instead of the values - the values c...
TOY_API bool Toy_injectNativeHook(Toy_Interpreter *interpreter, const char *name, Toy_HookFn hook)
Will inject the given native function hook into the Toy_Interpreter's hook cache, with the name passe...
TOY_API bool Toy_callFn(Toy_Interpreter *interpreter, const char *name, Toy_LiteralArray *arguments, Toy_LiteralArray *returns)
Utility function that will find a Toy_literal within the Toy_Interpreter's scope with a name that mat...
TOY_API void Toy_freeInterpreter(Toy_Interpreter *interpreter)
Frees a Toy_Interpreter, clearing all of the memory used within. That interpreter is no longer valid ...
TOY_API void Toy_setInterpreterPrint(Toy_Interpreter *interpreter, Toy_PrintFn printOutput)
This function sets the function called by the print keyword. By default, the following wrapper is use...
TOY_API bool Toy_injectNativeFn(Toy_Interpreter *interpreter, const char *name, Toy_NativeFn func)
Will inject the given native function func into the Toy_Interpreter's current scope,...
TOY_API bool Toy_callLiteralFn(Toy_Interpreter *interpreter, Toy_Literal func, Toy_LiteralArray *arguments, Toy_LiteralArray *returns)
Calls a Toy_Literal which contains a function, with the arguments to that function passed in as argum...
TOY_API void Toy_setInterpreterError(Toy_Interpreter *interpreter, Toy_PrintFn errorOutput)
Sets the function called when an error occurs within the interpreter. By default, the following wrapp...
TOY_API void Toy_resetInterpreter(Toy_Interpreter *interpreter)
Frees any environment that the scripts have built up, and generates a new one. It also injects severa...
void(* Toy_PrintFn)(const char *)
@???@
Definition: toy_literal.h:48
int(* Toy_HookFn)(struct Toy_Interpreter *interpreter, struct Toy_Literal identifier, struct Toy_Literal alias)
@???@
Definition: toy_literal.h:41
int(* Toy_NativeFn)(struct Toy_Interpreter *interpreter, struct Toy_LiteralArray *arguments)
@???@
Definition: toy_literal.h:30