diff --git a/src/pebblisp.c b/src/pebblisp.c index bd25c56..58d8cb8 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -9,24 +9,38 @@ #define printf(...) APP_LOG(APP_LOG_LEVEL_DEBUG, __VA_ARGS__) #endif +/** + * Inserts a variable into the environment with a given name and value. + * + * If `argForms` (symbol) and `argForms->forward` (value) are lists of the same + * length, define each symbol element with the corresponding value element. + * I.e. `(def (a b) (5 20))` would store `a` as `5` and `b` as `20`. + * + * @param argForms The symbol(s) and value(s) to define in the environment + * @param env The environment to add the new definition to + * @return The symbol(s) defined + */ Object evalDefArgs(const Object *argForms, struct Environment *env) { const Object *newSymbol = argForms; + const Object *newValue = argForms->forward; const char *name = newSymbol->string; - if(argForms->type == TYPE_LIST && argForms->forward->type == TYPE_LIST) { - FOR_POINTERS_IN_LISTS(argForms, argForms->forward) { - Object newValue = eval(P2, env); - addToEnv(env, P1->string, newValue); - cleanObject(&newValue); + // Handles multi-definitions + if(newSymbol->type == TYPE_LIST && newValue->type == TYPE_LIST + && listLength(newSymbol) == listLength(newValue)) { + FOR_POINTERS_IN_LISTS(newSymbol, newValue) { + Object finalValue = eval(P2, env); + addToEnv(env, P1->string, finalValue); + cleanObject(&finalValue); } return cloneObject(*newSymbol); } - Object newValue = eval(newSymbol->forward, env); + Object finalValue = eval(newSymbol->forward, env); - addToEnv(env, name, newValue); - cleanObject(&newValue); + addToEnv(env, name, finalValue); + cleanObject(&finalValue); return cloneObject(*newSymbol); }