Value * AssertFn (const char *name, State * state, int argc, Expr * argv[]) { int i; for (i = 0; i < argc; ++i) { char *v = Evaluate (state, argv[i]); if (v == NULL) { return NULL; } int b = BooleanString (v); free (v); if (!b) { int prefix_len; int len = argv[i]->end - argv[i]->start; char *err_src = malloc (len + 20); strcpy (err_src, "assert failed: "); prefix_len = strlen (err_src); memcpy (err_src + prefix_len, state->script + argv[i]->start, len); err_src[prefix_len + len] = '\0'; free (state->errmsg); state->errmsg = err_src; return NULL; } } return StringValue (strdup ("")); }
Value * IfElseFn (const char *name, State * state, int argc, Expr * argv[]) { if (argc != 2 && argc != 3) { free (state->errmsg); state->errmsg = strdup ("ifelse expects 2 or 3 arguments"); return NULL; } char *cond = Evaluate (state, argv[0]); if (cond == NULL) { return NULL; } if (BooleanString (cond) == true) { free (cond); return EvaluateValue (state, argv[1]); } else { if (argc == 3) { free (cond); return EvaluateValue (state, argv[2]); } else { return StringValue (cond); } } }
Value* LogicalNotFn(const char* name, State* state, int argc, Expr* argv[]) { char* val = Evaluate(state, argv[0]); if (val == NULL) return NULL; bool bv = BooleanString(val); free(val); return StringValue(strdup(bv ? "" : "t")); }
Value* LogicalOrFn(const char* name, State* state, int argc, Expr* argv[]) { char* left = Evaluate(state, argv[0]); if (left == NULL) return NULL; if (BooleanString(left) == false) { free(left); return EvaluateValue(state, argv[1]); } else { return StringValue(left); } }