lval* lval_eval_sexpr(lval* v) { /* Evaluate Children */ for (int i = 0; i < v->count; i++) { v->cell[i] = lval_eval(v->cell[i]); } /* Error Checking */ for (int i = 0; i < v->count; i++) { if (v->cell[i]->type == LVAL_ERR) { return lval_take(v, i); } } /* Empty Expression */ if (v->count == 0) { return v; } /* Single Expression */ if (v->count == 1) { return lval_take(v, 0); } /* Ensure First Element is Symbol */ lval* f = lval_pop(v, 0); if (f->type != LVAL_SYM) { lval_del(f); lval_del(v); return lval_err("S-expression Does not start with symbol."); } /* Call builtin with operator */ lval* result = builtin_op(v, f->sym); lval_del(f); return result; }
lval_t* builtin(lval_t* a,const char* func){ if(strcmp("head", func) == 0) return builtin_head(a); if(strcmp("tail" ,func) == 0) return builtin_tail(a); if(strstr("+-/*", func) == 0) return builtin_op(a, func); lval_free(a); return lval_err("Unknown Function"); }
lval *builtin(lval *v, char *func) { if (strcmp("head", func) == 0) { return builtin_head(v); } if (strcmp("tail", func) == 0) { return builtin_tail(v); } if (strcmp("init", func) == 0) { return builtin_init(v); } if (strcmp("join", func) == 0) { return builtin_join(v); } if (strcmp("eval", func) == 0) { return builtin_eval(v); } if (strcmp("list", func) == 0) { return builtin_list(v); } if (strcmp("cons", func) == 0) { return builtin_cons(v); } if (strcmp("len", func) == 0) { return builtin_len(v); } return builtin_op(v, func); }
lval* lval_eval_sexpr(lval* v) { for (int i = 0; i < v->count; i++) { if (v->cell[i]->type != LVAL_ERR) { v->cell[i] = lval_eval(v->cell[i]); } else { return lval_take(v, i); } } if (v->count == 0) { return v; } if (v->count == 1) { return lval_take(v, 0); } lval* f = lval_pop(v, 0); if (f->type != LVAL_SYM) { lval_free(f); lval_free(v); return lval_err("S-Expression doesn't start with symbol!"); } lval* result = builtin_op(v, f->sym); lval_free(f); return result; }
lval* builtin(lval* a, char* func) { if (strcmp("join", func) == 0) { return builtin_join(a); } if (strcmp("list", func) == 0) { return builtin_list(a); } if (strcmp("eval", func) == 0) { return builtin_eval(a); } if (strcmp("tail", func) == 0) { return builtin_tail(a); } if (strcmp("head", func) == 0) { return builtin_head(a); } if (strstr("+-/*", func)) { return builtin_op(a, func); } lval_del(a); return lval_err("Unknown Function!"); }
lval *builtin(lenv *e, lval *a, char *func) { if (strcmp("eval", func) == 0) { return builtin_eval(e, a); } if (strcmp("head", func) == 0) { return builtin_head(e, a); } if (strcmp("join", func) == 0) { return builtin_join(e, a); } if (strcmp("list", func) == 0) { return builtin_list(e, a); } if (strcmp("tail", func) == 0) { return builtin_tail(e, a); } if (strstr("+-*%/", func)) { return builtin_op(e, a, func); } lval_del(a); return lval_err("Unknown function %s", func); }
inline lval_t* builtin_div(lenv*e, lval* a){ return builtin_op(e, a, "*"); }
lval *builtin_mod(lenv *e, lval *a) { return builtin_op(e, a, "%"); }
void simple_parser::add_builtin_op(symbol const & s, family_id fid, decl_kind kind) { SASSERT(!m_builtin.contains(s)); SASSERT(!m_vars.contains(s)); m_builtin.insert(s, builtin_op(fid, kind)); }
lval* builtin_sub(lenv* env, lval* node) { return builtin_op(env, node, '-'); }
// Math - multiplication lval* builtin_mul(lenv* e, lval* a) { return builtin_op(e, a, "*"); }
lval* builtin_mod(lenv* env, lval* node) { return builtin_op(env, node, '%'); }
lval* builtin_div(lenv* env, lval* node) { return builtin_op(env, node, '/'); }
lval* builtin_mul(lenv* env, lval* node) { return builtin_op(env, node, '*'); }
// Math - addition lval* builtin_add(lenv* e, lval* a) { return builtin_op(e, a, "+"); }
// Math - subtraction lval* builtin_sub(lenv* e, lval* a) { return builtin_op(e, a, "-"); }
lval* builtin_pow(lenv* e, lval* a) { return builtin_op(e, a, "^"); }
// Math - division lval* builtin_div(lenv* e, lval* a) { return builtin_op(e, a, "/"); }
lval* builtin_add(lenv* env, lval* node) { return builtin_op(env, node, '+'); }