void repl(int level) { char prompt[30]; char *cmd; int quit = 0; int line = 1; lv_t *parsed_value; lv_t *env_sym; lv_t *result; lv_t *arg; lv_t *str; char sym_buf[20]; lexec_t *exec; exec = lisp_context_new(5); /* get r5rs environment */ while(!quit) { snprintf(prompt, sizeof(prompt), "%d:%d> ", level, line); // r! cmd = readline(prompt); if(!cmd) { printf("\n"); quit = 1; break; } if(!*cmd) continue; parsed_value = lisp_parse_string(cmd); if(!parsed_value) { fprintf(stderr, "synax error\n"); continue; } // e! result = lisp_execute(exec, parsed_value); // p! if(result && !is_nil(result)) { sprintf(sym_buf, "$%d", line); env_sym = lisp_create_symbol(sym_buf); c_hash_insert(L_CAR(exec->env), env_sym, result); dprintf(1, "%s = ", sym_buf); str = lisp_str_from_value(result); printf("%s\n", L_STR(str)); } // and l. ;) add_history(cmd); free(cmd); line++; } }
/** * (write obj) * (write obj port) * * write a representation of obj to the given port * (or current-output-port if unspecified) * * returns nil */ lv_t *p_write(lexec_t *exec, lv_t *v) { lv_t *str; assert(v && exec); rt_assert(c_list_length(v) == 1, le_arity, "display arity"); str = lisp_str_from_value(exec, L_CAR(v), 0); fprintf(stdout, "%s", L_STR(str)); fflush(stdout); return lisp_create_null(); }