static List *callsettor(char *name, List *defn) { Push p; List *settor; if (specialvar(name) || (settor = varlookup2("set-", name, NULL)) == NULL) return defn; Ref(List *, lp, defn); Ref(List *, fn, settor); varpush(&p, "0", mklist(mkstr(name), NULL)); lp = listcopy(eval(append(fn, lp), NULL, 0)); varpop(&p); RefEnd(fn); RefReturn(lp); }
/* localbind -- recursively convert a Bindings list into dynamic binding */ static List *localbind(Binding *dynamic0, Binding *lexical0, Tree *body0, int evalflags) { if (dynamic0 == NULL) return walk(body0, lexical0, evalflags); else { Push p; Ref(List *, result, NULL); Ref(Tree *, body, body0); Ref(Binding *, dynamic, dynamic0); Ref(Binding *, lexical, lexical0); varpush(&p, dynamic->name, dynamic->defn); result = localbind(dynamic->next, lexical, body, evalflags); varpop(&p); RefEnd3(lexical, dynamic, body); RefReturn(result); } }
save() { reg char *sp; reg int outf; reg time_t *tp; char buf[80]; time_t tme; STAT junk; tp = &tme; if (Fromfile && getyn(SAMEFILEPROMPT)) strcpy(buf, Fromfile); else { over: prompt(FILEPROMPT); leaveok(Board, FALSE); refresh(); sp = buf; while ((*sp = readch()) != '\n') { if (*sp == killchar()) goto over; else if (*sp == erasechar()) { if (--sp < buf) sp = buf; else { addch('\b'); /* * if the previous char was a control * char, cover up two characters. */ if (*sp < ' ') addch('\b'); clrtoeol(); } } else addstr(unctrl(*sp++)); refresh(); } *sp = '\0'; leaveok(Board, TRUE); } /* * check for existing files, and confirm overwrite if needed */ if (sp == buf || (!Fromfile && stat(buf, &junk) > -1 && getyn(OVERWRITEFILEPROMPT) == FALSE)) return FALSE; if ((outf = creat(buf, 0644)) < 0) { error(strerror(errno)); return FALSE; } mvwaddstr(Score, ERR_Y, ERR_X, buf); wrefresh(Score); time(tp); /* get current time */ strcpy(buf, ctime(tp)); for (sp = buf; *sp != '\n'; sp++) continue; *sp = '\0'; varpush(outf, write); close(outf); wprintw(Score, " [%s]", buf); wclrtoeol(Score); wrefresh(Score); return TRUE; }
save() { reg char *sp; reg int outf; reg TIME *tp; char buf[80]; TIME tme; STAT junk; tp = &tme; if (Fromfile && getyn("Same file? ")) strcpy(buf, Fromfile); else { over: mvaddstr(MOVE_Y, MOVE_X, "file: "); clrtoeol(); leaveok(Board, FALSE); refresh(); sp = buf; while ((*sp = readch()) != '\n') { if (*sp == _tty.sg_kill) goto over; else if (*sp == _tty.sg_erase) { if (--sp < buf) sp = buf; else { addch('\b'); /* * if the previous char was a control * char, cover up two characters. */ if (*sp < ' ') addch('\b'); clrtoeol(); } } else addstr(unctrl(*sp++)); refresh(); } *sp = '\0'; leaveok(Board, TRUE); } /* * check for existing files, and confirm overwrite if needed */ if (sp == buf || (!Fromfile && stat(buf, &junk) > -1 && getyn("Overwrite File? ") == FALSE)) return FALSE; if ((outf = creat(buf, 0644)) < 0) { error(sys_errlist[errno]); return FALSE; } mvwaddstr(Score, ERR_Y, ERR_X, buf); wrefresh(Score); time(tp); /* get current time */ strcpy(buf, ctime(tp)); for (sp = buf; *sp != '\n'; sp++) continue; *sp = '\0'; varpush(outf, write); close(outf); wprintw(Score, " [%s]", buf); wclrtoeol(Score); wrefresh(Score); return TRUE; }
/* eval -- evaluate a list, producing a list */ extern List *eval(List *list0, Binding *binding0, int flags) { Closure *volatile cp; List *fn; if (++evaldepth >= maxevaldepth) fail("es:eval", "max-eval-depth exceeded"); Ref(List *, list, list0); Ref(Binding *, binding, binding0); Ref(char *, funcname, NULL); restart: if (list == NULL) { RefPop3(funcname, binding, list); --evaldepth; return true; } assert(list->term != NULL); if ((cp = getclosure(list->term)) != NULL) { switch (cp->tree->kind) { case nPrim: assert(cp->binding == NULL); list = prim(cp->tree->u[0].s, list->next, binding, flags); break; case nThunk: list = walk(cp->tree->u[0].p, cp->binding, flags); break; case nLambda: ExceptionHandler Push p; Ref(Tree *, tree, cp->tree); Ref(Binding *, context, bindargs(tree->u[0].p, list->next, cp->binding)); if (funcname != NULL) varpush(&p, "0", mklist(mkterm(funcname, NULL), NULL)); list = walk(tree->u[1].p, context, flags); if (funcname != NULL) varpop(&p); RefEnd2(context, tree); CatchException (e) if (termeq(e->term, "return")) { list = e->next; goto done; } throw(e); EndExceptionHandler break; case nList: { list = glom(cp->tree, cp->binding, TRUE); list = append(list, list->next); goto restart; } default: panic("eval: bad closure node kind %d", cp->tree->kind); } goto done; } /* the logic here is duplicated in $&whatis */ Ref(char *, name, getstr(list->term)); fn = varlookup2("fn-", name, binding); if (fn != NULL) { funcname = name; list = append(fn, list->next); RefPop(name); goto restart; } if (isabsolute(name)) { char *error = checkexecutable(name); if (error != NULL) fail("$&whatis", "%s: %s", name, error); list = forkexec(name, list, flags & eval_inchild); RefPop(name); goto done; } RefEnd(name); fn = pathsearch(list->term); if (fn != NULL && fn->next == NULL && (cp = getclosure(fn->term)) == NULL) { char *name = getstr(fn->term); list = forkexec(name, list, flags & eval_inchild); goto done; } list = append(fn, list->next); goto restart; done: --evaldepth; if ((flags & eval_exitonfalse) && !istrue(list)) exit(exitstatus(list)); RefEnd2(funcname, binding); RefReturn(list); }