From ce849baec2315755c599518fca79c895e8998be8 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Wed, 1 Mar 2023 12:53:04 -0800 Subject: updated eq() function to handle types better --- doc/precedent.1 | 2 +- src/eval.c | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/doc/precedent.1 b/doc/precedent.1 index a974cf8..a159148 100644 --- a/doc/precedent.1 +++ b/doc/precedent.1 @@ -59,7 +59,7 @@ literals or key references. The following are examples of key references with ar \-\- Stores the evaluation of its first argument under the name specified by the (string) evaluation of its second argument, in the same manner as store(). Update() differs from store when the key is not found in the local scope. In this case, update() searches upwards for the first scope containing the key, and overwrites its value. \fBeq()\fP -\-\- Evaluates all of its arguments. If they are all equivalent, returns 1. Otherwise, returns 0. +\-\- Evaluates all of its arguments. If they are all equivalent, returns 1. Otherwise, returns 0. Can accept ints, floats, or strings. Strings cannot be mixed with floats or ints. \fBif()\fP \-\- If its first argument is not 0, evaluates the rest of its arguments. Does not return a value. diff --git a/src/eval.c b/src/eval.c index 8adcf8c..081f098 100644 --- a/src/eval.c +++ b/src/eval.c @@ -128,43 +128,43 @@ sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) { return init_sym_int(1); } else if (hash == get_hash("eq")) { - litType_t type = NO_TYPE; + litType_t firstType = NO_TYPE; int firstInt = 0; float firstFloat = 0; char* firstString; for (int i = 0; i < symbol->argCount; i++) { sym_t* v = eval(symbol->args[i], symbolScope); /* initialize */ - if (type == NO_TYPE) { - type = v->litType; - if (type == INT) { + if (firstType == NO_TYPE) { + firstType = v->litType; + if (firstType == INT) { firstInt = symtoi(v); } - else if (type == FLOAT) { + else if (firstType == FLOAT) { firstFloat = symtof(v); } - else if (type == STRING) { + else if (firstType == STRING) { firstString = symtos(v); } destroy_sym(v); } else { /* interpret type */ - if (type == INT) { + if (v->litType == INT) { int si = symtoi(v); destroy_sym(v); - if (si != firstInt) { + if (si != (firstType == FLOAT ? firstFloat : firstInt)) { return init_sym_int(0); } } - else if (type == FLOAT) { + else if (v->litType == FLOAT) { float sf = symtof(v); destroy_sym(v); - if (sf != firstFloat) { + if (sf != (firstType == FLOAT ? firstFloat : firstInt)) { return init_sym_int(0); } } - else if (type == STRING) { + else if (v->litType == STRING) { char* ss = symtos(v); if (strcmp(ss, firstString) != 0) { destroy_sym(v); @@ -175,7 +175,7 @@ sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) { } } } - if (type == STRING) { + if (firstType == STRING) { free(firstString); } return init_sym_int(1); -- cgit v1.3.1