53class PrimitiveExpeditor {
54 class PrimitiveExpeditorInitializer;
59 struct PrimitiveDescription {
84 static PrimitiveExpeditorInitializer
describe(
const String &name,
Lisp::Type returnType,
int argsCount, ...) {
85 sLastDescription.name = name;
86 sLastDescription.argsCount = argsCount;
87 sLastDescription.returnType = returnType;
88 memset(sLastDescription.argsTypes, 0,
sizeof(sLastDescription.argsTypes));
92 va_start(args, argsCount);
94 for (
auto i = 0; i < argsCount; i++) {
95 auto type = va_arg(args,
int);
96 sLastDescription.argsTypes[i] =
static_cast<Lisp::Type>(type);
101 if (DescriptionModeGuard::isDescriptionMode()) {
102 longjmp(sDescriptionJumper, 1);
117 volatile auto guard = DescriptionModeGuard();
119 if (setjmp(sDescriptionJumper) == 0) {
122 return sLastDescription;
133 return mRegisterProxy;
141 return length(
list());
153 auto paramType = param->type;
154 switch (expectedType) {
156 if (paramType == TINT) {
162 if (paramType == TNIL || paramType == TTRUE) {
168 if (paramType == TINT || paramType == TNIL || paramType == TTRUE) {
174 if (paramType == TSYMBOL) {
180 if (paramType == TCELL) {
200 mEvalList = eval_list(mRoot, mEnv, mList);
223 return eval(mRoot, mEnv, obj);
232 error(
"[%s] %s", mDescription.name.c_str(), msg);
243 error_wrong_params_number();
256 error_wrong_params_number();
260 for (uint8_t i = 0; i < length; i++, param = param->cdr) {
262 error(
"[%s] Type for %d parameter is not set", mDescription.name.c_str(), i);
265 error(
"[%s] Unexpected end of params list at %d", mDescription.name.c_str(), i);
284 va_start(args, length);
285 for (uint8_t i = 0; i < length; i++) {
286 types[i] =
static_cast<Lisp::Type>(va_arg(args,
int));
298 assertArgs(mDescription.argsCount, mDescription.argsTypes);
311 error(
"[%s] Trying to get %d arg out of %d", mDescription.name.c_str(), idx, length);
315 for (
auto i = 0; i < idx; i++, param = param->cdr);
341 return arg->value != 0;
405 return value ? True : Nil;
415 return make_int(mRoot, value);
425 return make_symbol(mRoot, value);
438 : mDescription(description), mRoot(root), mEnv(env), mList(
list), mEvalList(nullptr), mRegisterProxy(description.name, &sRegister) {
449 error(
"[%s] Invalid type of %d parameter, expected <%s>, got <%s>", mDescription.name.c_str(), idx,
Lisp::str(expectedType),
Lisp::str(actualType));
455 void error_wrong_params_number() {
456 error(
"[%s] Wrong number of params", mDescription.name.c_str());
459 PrimitiveDescription mDescription;
465 RegisterManagerProxy mRegisterProxy;
466 static RegisterManager sRegister;
468 static jmp_buf sDescriptionJumper;
469 static PrimitiveDescription sLastDescription;
477 class DescriptionModeGuard {
482 DescriptionModeGuard() { sDescriptionMode =
true; }
487 ~DescriptionModeGuard() { sDescriptionMode =
false; }
494 static bool isDescriptionMode() {
return sDescriptionMode; }
497 static bool sDescriptionMode;
506 class PrimitiveExpeditorInitializer {
517 return {PrimitiveExpeditor::sLastDescription, root, env,
list};
Type
Enumeration of supported Lisp data types.
Definition LispHelper.h:94
@ BoolInt
Definition LispHelper.h:98
@ Symbol
Definition LispHelper.h:99
@ Cell
Definition LispHelper.h:100
@ Bool
Definition LispHelper.h:97
@ Int
Definition LispHelper.h:96
@ Any
Definition LispHelper.h:102
static bool correct(Lisp::Type type)
Checks if a type value is within the valid range.
Definition LispHelper.h:111
static Lisp::Type getType(lisp::Object obj)
Determines the Lisp::Type of a given Lisp object.
Definition LispHelper.h:138
static const char * str(Lisp::Type type)
Converts a type enumeration value to a human-readable string.
Definition LispHelper.h:120
Definition PrimitiveExpeditor.h:53
int getArgInt(int idx, bool acceptsBool=true)
Gets an integer value from an argument.
Definition PrimitiveExpeditor.h:358
RegisterManagerProxy & getAssignedRegister()
Gets the register proxy assigned to this expeditor.
Definition PrimitiveExpeditor.h:132
static PrimitiveDescription extractDescription(Primitive *primitive)
Extracts description metadata from a primitive function.
Definition PrimitiveExpeditor.h:116
static PrimitiveExpeditorInitializer describe(const String &name, Lisp::Type returnType, int argsCount,...)
Describes a primitive function by setting up its metadata.
Definition PrimitiveExpeditor.h:84
const char * getArgSymbol(int idx)
Gets a symbol value from an argument.
Definition PrimitiveExpeditor.h:387
Object makeBool(bool value)
Creates a boolean Lisp object.
Definition PrimitiveExpeditor.h:404
void terminate(const char *msg)
Terminates execution with an error message.
Definition PrimitiveExpeditor.h:231
Object evalObj(VarObject obj)
Evaluates a single object in the current environment.
Definition PrimitiveExpeditor.h:222
int getArgsLength()
Gets the number of arguments in the current function call.
Definition PrimitiveExpeditor.h:140
Object makeSymbol(const char *value)
Creates a symbol Lisp object.
Definition PrimitiveExpeditor.h:424
bool getArgBool(int idx, bool acceptsInt=true)
Gets a boolean value from an argument.
Definition PrimitiveExpeditor.h:328
Object getArg(int idx)
Gets an argument at the specified index.
Definition PrimitiveExpeditor.h:308
void assertDescribedArgs()
Asserts arguments against the primitive's description.
Definition PrimitiveExpeditor.h:297
void assertArgsCount(int length)
Asserts that the function was called with the expected number of arguments.
Definition PrimitiveExpeditor.h:241
void assertArgs(uint8_t length, const Lisp::Type *types)
Asserts that arguments match the expected types.
Definition PrimitiveExpeditor.h:254
Object makeInt(int value)
Creates an integer Lisp object.
Definition PrimitiveExpeditor.h:414
void assertArgs(uint8_t length,...)
Variadic version of assertArgs.
Definition PrimitiveExpeditor.h:280
Object evalList()
Evaluates the argument list if not already evaluated.
Definition PrimitiveExpeditor.h:198
Object list()
Gets the argument list (evaluated or not).
Definition PrimitiveExpeditor.h:209
static RegisterManager & getRegisterManager()
Gets the static register manager instance.
Definition PrimitiveExpeditor.h:71
bool checkType(Object param, Lisp::Type expectedType)
Checks if a parameter matches an expected Lisp type.
Definition PrimitiveExpeditor.h:152
Definition RegisterManager.h:37
Definition RegisterManagerProxy.h:35
struct Obj * Object
A pointer to a Lisp object structure.
Definition LispHelper.h:61
void * Root
A generic pointer representing the root of a Lisp environment.
Definition LispHelper.h:76
struct Obj ** VarObject
A pointer to a pointer to a Lisp object structure.
Definition LispHelper.h:69
Contains type definitions and utilities for interacting with the Lisp interpreter.
Contains descriptions and implementations of primitive functions for hardware interaction.
Definition LispPrimitives.cpp:21
Contains all classes and functions related to the Uniot Core.