Use throw() instead of returning errorObject() results directly

Add (type-of) for getting the string representation of an object's type.
This commit is contained in:
Sage Vaillancourt 2023-03-03 14:58:54 -05:00
parent c37a12e244
commit 8b9a351be1
7 changed files with 20 additions and 5 deletions

View File

@ -242,6 +242,7 @@ struct Environment defaultEnv()
#endif
#ifdef STANDALONE
pf(segfault),
pf(typeOf),
pf(print),
pf(numToChar),
pf(printEnvO),

View File

@ -734,7 +734,7 @@ inline Object withLen(size_t len, enum Type type)
inline Object constructLambda(const Object* params, const Object* body, struct Environment* env)
{
if (!params || !body) {
return errorObject(NULL_LAMBDA_LIST);
throw(NULL_LAMBDA_LIST, "fn params and body cannot be null");
}
if (params->type != TYPE_LIST) {

View File

@ -379,7 +379,7 @@ static Object run_script(Object* params, int length,
free(code);
return o;
}
return errorObject(SCRIPT_NOT_FOUND);
throw(SCRIPT_NOT_FOUND, "");
}
static void code_window_unload(Window* window)

View File

@ -277,7 +277,7 @@ int areEqual(const Object* obj1, const Object* obj2)
Object equ(Object* params, int length, unused struct Environment* env)
{
if (length < 2) {
return errorObject(NOT_ENOUGH_ARGUMENTS);
throw(NOT_ENOUGH_ARGUMENTS, "expected at least 2");
}
for (int i = 0; i < length - 1; i++) {

View File

@ -12,6 +12,12 @@ Object print(Object* params, int length, unused struct Environment* env)
return numberObject(0);
}
Object typeOf(Object* params, unused int length, unused struct Environment* env)
{
const char* typeString = getTypeName(&(params[0]));
return nullTerminated(typeString);
}
Object numToChar(Object* params, unused int length, unused struct Environment* env)
{
checkTypes(numToChar);

View File

@ -7,6 +7,14 @@
fn(print, "prn", "Prints the string representation of the given object to stdout.");
tfn(typeOf, "type-of",
({ anyType, returns(isStringy) }),
"Gets a string indicating the type of the given object",
"(type-of 10)", "TYPE_NUMBER",
"(type-of \"string\")", "TYPE_STRING",
"(type-of (fn () ()))", "TYPE_LAMBDA",
);
tfn(numToChar, "ch",
({ expect(isNumber), returns(isStringy) }),
"Gets a string containing the ascii character for the given number value.",

View File

@ -94,7 +94,7 @@ Object addTextLayer(Object* params, int length, struct Environment* env)
Object window = params[0];
Object text = params[1];
if (getPebbleType(window) != WINDOW) {
return errorObject(0);
throw(BAD_TYPE, "Expected a pebble window, but received %s", getTypeName(&window));
}
Layer* window_layer =
window_get_root_layer(accessPebbleObject(window)->window);
@ -179,6 +179,6 @@ Object doVibe(Object* params, int length, struct Environment* env)
(VibePattern) {.durations = pattern, .num_segments = l});
return trueObject();
} else {
return errorObject(NOT_A_LIST);
throw(NOT_A_LIST, "(vibe) requires a non-empty list!");
}
}