oop string_join(oop strlist, oop sepstr) { size_t total_length = 0; size_t sep_length; int needsep = 0; oop pos; binary *result; char *c; if (!isbinary(sepstr)) die("string_join: sepstr is not a binary"); sep_length = oop_len(sepstr); for (pos = strlist; ispair(pos); pos = ((pair *) pos)->cdr) { oop s = ((pair *) pos)->car; if (!isbinary(s)) die("string_join: element of strlist is not a binary"); if (needsep) total_length += sep_length; total_length += oop_len(s); needsep = 1; } if (!isnil(pos)) die("string_join: strlist is not a list"); result = raw_alloc(sizeof(binary) + total_length); init_binary_header(result->header, TYPE_BINARY, total_length); c = result->data; needsep = 0; for (pos = strlist; ispair(pos); pos = ((pair *) pos)->cdr) { oop s = ((pair *) pos)->car; if (needsep) { memcpy(c, ((binary *) sepstr)->data, sep_length); c += sep_length; } memcpy(c, ((binary *) s)->data, oop_len(s)); c += oop_len(s); needsep = 1; } return result; }
static void write_pair(port *out, pointer object) { fprintf(out, "("); if (cdr(object) == NULL) { write(out, car(object)); fprintf(out, ")"); return; } if (!ispair(cdr(object))) { write(out, car(object)); fprintf(out, " . "); write(out, cdr(object)); fprintf(out, " "); } else while (object != NULL) { if (!ispair(object)) { fprintf(out, ". "); write(out, object); fprintf(out, " "); break; } write(out, car(object)); fprintf(out, " "); object = cdr(object); } fprintf(out, "\b)"); }
// determing hand checks int Checks::DetermineHand(vector<int >values, vector<char>suits) { determineValues.empty(); determineSuits.empty(); determineValues = values; determineSuits = suits; sort(determineValues.begin(), determineValues.end()); if ((flush(determineSuits)) && (straight(determineValues))) { if (determineValues[7] == 14) { return 100; } else { return 90; } } else if (flush(determineSuits) == 1) { return 80; } else if (straight(determineValues) == 1) { return 70; } else if (fourofakind(determineValues) == 1) { return 60; } else if (fullhouse(determineValues) == 1) { return 50; } else if (threeofakind(determineValues) == 1) { return 40; } else if (ispair(determineValues) == 2) { return 30; } else if (ispair(determineValues) == 1) { return 20; } else { return determineValues.back(); } }
bool isValid(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function if(s.size()==0) return false; stack<char> sck; sck.push(s[0]); int cur = 1; while(cur<s.size()) { char curchar = s[cur]; if(isright(curchar)) { if(sck.empty()) //Need to return false if the right parenthese is entered when sck is empty, we need to this case on the top because the second case need the stack to be not empty return false; if(!ispair(sck.top(),curchar)) //Need to check if sck is empty before peek, or the empty stack has been checked and returned if empty { return false; } if(!sck.empty()) //Need to pop in the valid case, cannot be on top of the second case sck.pop(); } else if(isleft(curchar)) sck.push(curchar); else return false; cur++; } if(cur!=s.size() || !sck.empty()) return false; return true; }
static bool list_begins_with(object_t *list, object_t *search) { if(ispair(list)) { object_t *pair_car = car(list); return issymbol(pair_car) && (pair_car == search); } return false; }
/* Return the second element of a pair */ static exp_t * prim_cdr(exp_t *args) { chkargs("cdr", args, 1); if (!ispair(car(args))) everr("cdr: the argument isn't a pair", car(args)); return cdar(args); }
Cell copy_cell(Cell c) { Cell r; if (!ispair(c)) return c; if (car(c) == COPIED) return cdr(c); r = free_ptr++; car(r) = car(c); if (car(c) == COMB_I) { Cell tmp = cdr(c); while (ispair(tmp) && car(tmp) == COMB_I) tmp = cdr(tmp); cdr(r) = tmp; } else cdr(r) = cdr(c); car(c) = COPIED; cdr(c) = r; return r; }
Cell translate(Cell t) { if (!ispair(t)) return t; if (car(t) == LAMBDA) return unabstract(translate(cdr(t))); else { Cell s; PUSH(cdr(t)); PUSH(translate(car(t))); s = translate(PUSHED(1)); s = pair(TOP, s); DROP(2); return s; } }
static void __attribute__((noreturn)) apply_implementation(closure *self, int argc, oop k, oop f, ...) { pair *prev = NULL; pair *arglist = mknull(); pair *p; int i; va_list vl; oop a; if (argc < 3) { wrong_variable_argc(argc, 3); } va_start(vl, f); for (i = (argc - 3) - 1; i >= 0; i--) { p = alloca(sizeof(pair)); a = va_arg(vl, oop); *p = (pair) mkpair(a, mknull()); if (prev == NULL) { arglist = p; } else { prev->cdr = p; } prev = p; } a = va_arg(vl, oop); if (!(ispair(a) || isnil(a))) { wrong_type(argc); } if (prev == NULL) { arglist = a; } else { prev->cdr = a; } va_end(vl); p = alloca(sizeof(pair)); *p = (pair) mkpair(k, arglist); arglist = p; checkedcallfun(f, -1, arglist); }
void unparse(Cell e) { if (ispair(e)) { putchar('`'); unparse(car(e)); unparse(cdr(e)); } else if (e == COMB_S) putchar('S'); else if (e == COMB_K) putchar('K'); else if (e == COMB_I) putchar('I'); else if (e == COMB_B) putchar('B'); else if (e == COMB_C) putchar('C'); else if (e == COMB_SP) printf("S'"); else if (e == COMB_BS) printf("B*"); else if (e == COMB_CP) printf("C'"); else if (e == COMB_KI) printf("`ki"); else putchar('?'); }
static pointer apply(pointer expr) { pointer tmp_op, result, farg, expanded; int flags; tmp_op = eval(sexpr_get_op(expr)); switch (op_type(tmp_op)) { case T_BUILT_IN_REGULAR_PROC: save_continuation(cont_env | cont_arg | cont_op); op = tmp_op; arg = eval_arg_list(sexpr_get_arg(expr)); result = (op_get_bltin_proc_code(op))(); restore_continuation(cont_env | cont_arg | cont_op); break; case T_BUILT_IN_SPECIAL_PROC: if (strcmp("set!", op_get_bltin_proc_name(tmp_op)) == 0 || strcmp("defmacro", op_get_bltin_proc_name(tmp_op)) == 0) flags = cont_arg | cont_op; else flags = cont_env | cont_arg | cont_op; save_continuation(flags); op = tmp_op; arg = sexpr_get_arg(expr); result = (op_get_bltin_proc_code(op))(); restore_continuation(flags); break; case T_EXTEND_PROC: save_continuation(cont_env | cont_arg | cont_op); op = tmp_op; arg = eval_arg_list(sexpr_get_arg(expr)); env = op_get_ext_proc_env(op); farg = op_get_ext_proc_farg(op); while (farg != NULL) { if (ispair(car(farg))) { add_new_binding(car(cdar(farg)), arg); break; } add_new_binding(car(farg), car(arg)); farg = cdr(farg); arg = cdr(arg); } result = eval(op_get_ext_proc_body(op)); restore_continuation(cont_env | cont_arg | cont_op); break; case T_MACRO: save_continuation(cont_env | cont_arg | cont_op); op = tmp_op; arg = sexpr_get_arg(expr); env = op_get_macro_env(op); farg = op_get_macro_farg(op); while (farg != NULL) { if (ispair(car(farg))) { add_new_binding(car(cdar(farg)), arg); break; } add_new_binding(car(farg), car(arg)); farg = cdr(farg); arg = cdr(arg); } expanded = eval(op_get_macro_body(op)); restore_continuation(cont_env | cont_arg | cont_op); result = eval(expanded); break; } return result; }
//TODO check number of arguments given to builtins object_t *eval(object_t *exp, object_t *env) { char comeback = 1; while(comeback) { comeback = 0; if(is_self_evaluating(exp)) { return exp; } if(list_begins_with(exp, quote_symbol)) { return cadr(exp); } // (define... ) if(list_begins_with(exp, define_symbol)) { object_t *var = cadr(exp); // (define a b) if(issymbol(var)) { object_t *val = caddr(exp); return define_var(env, var, val); } // (define (a ...) ...) TODO use scheme macro if(ispair(var)) { object_t *name = car(cadr(exp)), *formals = cdr(cadr(exp)), *body = cddr(exp), *lambda = cons(lambda_symbol, cons(formals, body)); exp = cons(define_symbol, cons(name, cons(lambda, empty_list))); comeback = 1; continue; } fprintf(stderr, "Syntax error.\n"); exit(-1); } // (set! a b) if(list_begins_with(exp, set_symbol)) { object_t *var = cadr(exp); object_t *val = caddr(exp); return set_var(env, var, val); } // (if c a b) if(list_begins_with(exp, if_symbol)) { exp = eval_if(env, cadr(exp), caddr(exp), cadddr(exp)); comeback = 1; continue; } // (cond ...) if(list_begins_with(exp, cond_symbol)) { object_t *tail = cons(void_symbol, empty_list); object_t *ifs = tail; //empty_list; object_t *rules = reverse_list(cdr(exp)); while(!isemptylist(rules)) { object_t *rule = car(rules), *condition = car(rule), *consequence = cadr(rule); if(isemptylist(consequence)) { consequence = cons(void_obj, empty_list); } ifs = cons(if_symbol, cons(condition, cons(consequence, cons(ifs, empty_list)))); rules = cdr(rules); } exp = ifs; comeback = 1; continue; } // (begin ...) if(list_begins_with(exp, begin_symbol)) { object_t *result = empty_list, *exps; for(exps = cdr(exp); ! isemptylist(exps); exps = cdr(exps)) { result = eval(car(exps), env); } return result; } if(list_begins_with(exp, lambda_symbol)) { object_t *fn = cons(begin_symbol, cdr(cdr(exp))); return make_compound_proc(empty_list, cadr(exp), fn, env); } // (let ...) if(list_begins_with(exp, let_symbol)) { //if(! issymbol(cadr(exp))) object_t *bindings = cadr(exp); object_t *body = cddr(exp); object_t *formals = empty_list; object_t *values = empty_list; while(!isemptylist(bindings)) { formals = cons(caar(bindings), formals); values = cons(cadr(car(bindings)), values); bindings = cdr(bindings); } exp = cons(cons(lambda_symbol, cons(formals, body)), values); comeback = 1; continue; } if(issymbol(exp)) { return var_get_value(env, exp); } if(ispair(exp)) { object_t *exp_car = car(exp); object_t *fn = eval(exp_car, env); //var_get_value(env, car); if(!iscallable(fn)) { fprintf(stderr, "object_t is not callable\n"); exit(-1); } object_t *args = cdr(exp); object_t *evaluated_args = evaluate_list(env, args, empty_list); if(isprimitiveproc(fn)) { return fn->value.prim_proc.fn(evaluated_args); } else if(iscompoundproc(fn)) { object_t *fn_formals = fn->value.compound_proc.formals; object_t *fn_body = fn->value.compound_proc.body; object_t *fn_env = fn->value.compound_proc.env; ARGS_EQ(evaluated_args, list_size(fn_formals)); exp = fn_body; env = extend_environment(fn_formals, evaluated_args, fn_env); comeback = 1; continue; } assert(0); } } fprintf(stderr, "Unable to evaluate expression: \n"); write(exp); exit(-1); }
void eval(Cell root) { Cell *bottom = rd_stack.sp; PUSH(root); for (;;) { while (ispair(TOP)) PUSH(car(TOP)); if (TOP == COMB_I && APPLICABLE(1)) { /* I x -> x */ POP; TOP = cdr(TOP); } else if (TOP == COMB_S && APPLICABLE(3)) { /* S f g x -> f x (g x) */ Cell a = alloc(2); SET(a+0, ARG(1), ARG(3)); /* f x */ SET(a+1, ARG(2), ARG(3)); /* g x */ DROP(3); SET(TOP, a+0, a+1); /* f x (g x) */ } else if (TOP == COMB_K && APPLICABLE(2)) { /* K x y -> I x */ Cell x = ARG(1); DROP(2); SET(TOP, COMB_I, x); TOP = cdr(TOP); /* shortcut reduction of I */ } else if (TOP == COMB_B && APPLICABLE(3)) { /* B f g x -> f (g x) */ Cell f, gx; gx = pair(ARG(2), ARG(3)); f = ARG(1); DROP(3); SET(TOP, f, gx); } else if (TOP == COMB_C && APPLICABLE(3)) { /* C f g x -> f x g */ Cell fx, g; fx = pair(ARG(1), ARG(3)); g = ARG(2); DROP(3); SET(TOP, fx, g); } else if (TOP == COMB_SP && APPLICABLE(4)) { /* SP c f g x -> c (f x) (g x) */ Cell a = alloc(3); SET(a+0, ARG(2), ARG(4)); /* f x */ SET(a+1, ARG(3), ARG(4)); /* g x */ SET(a+2, ARG(1), a+0); /* c (f x) */ DROP(4); SET(TOP, a+2, a+1); /* c (f x) (g x) */ } else if (TOP == COMB_BS && APPLICABLE(4)) { /* BS c f g x -> c (f (g x)) */ Cell a, c; a = alloc(2); SET(a+0, ARG(3), ARG(4)); /* g x */ SET(a+1, ARG(2), a+0); /* f (g x) */ c = ARG(1); DROP(4); SET(TOP, c, a+1); /* c (f (g x)) */ } else if (TOP == COMB_CP && APPLICABLE(4)) { /* BS c f g x -> c (f x) g */ Cell a, g; a = alloc(2); SET(a+0, ARG(2), ARG(4)); /* f x */ SET(a+1, ARG(1), a+0); /* c (f x) */ g = ARG(3); DROP(4); SET(TOP, a+1, g); /* c (f x) g */ } else if (TOP == COMB_IOTA && APPLICABLE(1)) { /* IOTA x -> x S K */ Cell xs = pair(ARG(1), COMB_S); POP; SET(TOP, xs, COMB_K); } else if (TOP == COMB_KI && APPLICABLE(2)) { /* KI x y -> I y */ DROP(2); car(TOP) = COMB_I; } else if (TOP == COMB_CONS && APPLICABLE(3)) { /* CONS x y f -> f x y */ Cell fx, y; fx = pair(ARG(3), ARG(1)); y = ARG(2); DROP(3); SET(TOP, fx, y); } else if (TOP == COMB_READ && APPLICABLE(2)) { /* READ NIL f -> CONS CHAR(c) (READ NIL) f -> I KI f */ int c = read_char(); if (c == EOF) { POP; SET(TOP, COMB_I, COMB_KI); } else { Cell a = alloc(2); SET(a+0, COMB_CONS, mkchar(c == EOF ? 256 : c)); SET(a+1, COMB_READ, NIL); POP; SET(TOP, a+0, a+1); } } else if (TOP == COMB_WRITE && APPLICABLE(1)) { /* WRITE x -> x PUTC RETURN */ POP; Cell a = pair(cdr(TOP), COMB_PUTC); /* x PUTC */ SET(TOP, a, COMB_RETURN); /* x PUTC RETURN */ } else if (TOP == COMB_PUTC && APPLICABLE(3)) { /* PUTC x y i -> putc(eval(x INC NUM(0))); WRITE y */ Cell a = alloc(2); SET(a+0, ARG(1), COMB_INC); /* x INC */ SET(a+1, a+0, mkint(0)); /* x INC NUM(0) */ DROP(2); eval(a+1); if (!isint(TOP)) errexit("invalid output format (result was not a number)\n"); if (intof(TOP) >= 256) errexit("invalid character %d\n", intof(TOP)); putchar(intof(TOP)); POP; ARG(1) = cdr(TOP); /* y */ POP; car(TOP) = COMB_WRITE; /* WRITE y */ } else if (TOP == COMB_RETURN) return; else if (TOP == COMB_INC && APPLICABLE(1)) { /* INC x -> eval(x)+1 */ Cell c = ARG(1); POP; eval(c); c = POP; if (!isint(c)) errexit("invalid output format (attempted to apply inc to a non-number)\n"); SET(TOP, COMB_I, mkint(intof(c) + 1)); } else if (ischar(TOP) && APPLICABLE(2)) { int c = charof(TOP); if (c <= 0) { /* CHAR(0) f z -> z */ Cell z = ARG(2); DROP(2); SET(TOP, COMB_I, z); } else { /* CHAR(n+1) f z -> f (CHAR(n) f z) */ Cell a = alloc(2); Cell f = ARG(1); SET(a+0, mkchar(c-1), f); /* CHAR(n) f */ SET(a+1, a+0, ARG(2)); /* CHAR(n) f z */ DROP(2); SET(TOP, f, a+1); /* f (CHAR(n) f z) */ } } else if (isint(TOP) && APPLICABLE(1)) errexit("invalid output format (attempted to apply a number)\n"); else return; reductions++; } }
Cell unabstract(Cell t) { if (isint(t)) { if (t == mkint(0)) return COMB_I; else return pair(COMB_K, mkint(intof(t)-1)); } else if (ispair(t)) { Cell f, g; PUSH(cdr(t)); PUSH(unabstract(car(t))); g = PUSHED(1) = unabstract(PUSHED(1)); f = TOP; if (IS_K1(f)) { if (g == COMB_I) { /* S (K x) I => x */ f = cdr(f); } else if (IS_K1(g)) { /* S (K x) (K y) => K (x y) */ car(g) = cdr(f); /* x y */ cdr(f) = g; /* K (x y) */ } else if (IS_B2(g)) { /* S (K x) (B y z) => B* x y z */ car(f) = COMB_BS; /* B* x */ car(car(g)) = f; /* B* x y z */ f = g; } else { /* S (K x) y => B x y */ car(f) = COMB_B; /* B x */ f = pair(f, g); /* B x y */ } } else if (IS_K1(g)) { if (IS_B2(f)) { /* S (B x y) (K z) => C' x y z */ car(car(f)) = COMB_CP; car(g) = f; f = g; } else { /* S x (K y) => C x y */ f = cdr(g); SET(g, COMB_C, TOP); /* C x */ f = pair(g, f); /* C x y */ } } else if (IS_B2(f)) { /* S (B x y) z => S' x y z */ car(car(f)) = COMB_SP; /* S' x y */ f = pair(f, g); /* S' x y z */ } else { /* S x y */ f = pair(COMB_S, f); f = pair(f, PUSHED(1)); } DROP(2); return f; } else return pair(COMB_K, t); }
/* Test if the expression is a pair */ static exp_t * prim_pair(exp_t *args) { chkargs("pair?", args, 1); return ispair(car(args)) ? true : false; }
int Checks::determinecpuhand(vector<int> cvalues, vector<char>csuits) { //gotoxy(30, 20); // checking the hands starting with the highest possible hand and going down from there. dvalues.empty(); dsuits.empty(); dvalues = cvalues; dsuits = csuits; sort(dvalues.begin(), dvalues.end()); if ((flush(dsuits)) && (straight(dvalues))) { if (dvalues[4] == 14) { chand = " ROYAL FLUSH!!!"; return 100; } else { chand = "STRAIGHT FLUSH!"; return 90; } } else if (flush(dsuits) == 1) { chand = "Flush!"; return 80; } else if (straight(dvalues) == 1) { chand = "Straight!"; return 70; } else if (fourofakind(dvalues) == 1) { chand = "Four of a Kind!"; return 60; } else if (fullhouse(dvalues) == 1) { chand = "Full House!"; return 50; } else if (threeofakind(dvalues) == 1) { chand = "Three of a Kind!"; return 40; } else if (ispair(dvalues) == 2) { chand = "Two Pair"; return 30; } else if (ispair(dvalues) == 1) { chand = "Pair!"; return 20; } else if(ispair(dvalues) == 0) { chand = "Absolutely nothing!"; return 4; } return dvalues.back(); }
// determing hand checks int Checks::determinehand(vector<int >pvalues, vector<char>psuits) { dvalues.empty(); dsuits.empty(); dvalues = pvalues; dsuits = psuits; if(foldc = true) { inhand = "FOLD!"; } //gotoxy(30, 20); // checking the hands starting with the highest possible hand and going down from there. sort(dvalues.begin(), dvalues.end()); if ((flush(dsuits)) && (straight(dvalues))) { if (dvalues[7] = 14) { inhand = "Royal Flush!!"; return 100; } else { inhand = "Straight Flush!!"; return 90; } } else if (flush(dsuits) == 1) { inhand = "Flush!!"; return 80; } else if (straight(dvalues) == 1) { inhand = "Straight!!"; return 70; } else if (fourofakind(dvalues) == 1) { inhand = "Four of a Kind!!"; return 60; } else if (fullhouse(dvalues) == 1) { inhand = "Fullhouse!!"; return 50; } else if (threeofakind(dvalues) == 1) { inhand = "Three of a Kind!!"; return 40; } else if (ispair(dvalues) == 2) { inhand = "Two Pair!!"; return 30; } else if (ispair(dvalues) == 1) { inhand = "Pair!!"; return 20; } else { inhand = "Absolutely Nothing!!"; return dvalues.back(); } }