cell_t *sexp_parse(secd_t *secd, cell_t *port) { cell_t *prevport = SECD_NIL; if (not_nil(port)) { assert(cell_type(port) == CELL_PORT, "sexp_parse: not a port"); prevport = secd->input_port; // share_cell, drop_cell secd->input_port = share_cell(secd, port); } secd_parser_t p; init_parser(secd, &p); cell_t *res = sexp_read(secd, &p); if (not_nil(prevport)) { secd->input_port = prevport; //share_cell back drop_cell(secd, port); } return res; }
static void repl (sexp ctx, sexp env) { sexp_gc_var6(obj, tmp, res, in, out, err); sexp_gc_preserve6(ctx, obj, tmp, res, in, out, err); sexp_context_tracep(ctx) = 1; in = sexp_param_ref(ctx, env, sexp_global(ctx, SEXP_G_CUR_IN_SYMBOL)); out = sexp_param_ref(ctx, env, sexp_global(ctx, SEXP_G_CUR_OUT_SYMBOL)); err = sexp_param_ref(ctx, env, sexp_global(ctx, SEXP_G_CUR_ERR_SYMBOL)); if (in == NULL || out == NULL) { fprintf(stderr, "Standard I/O ports not found, aborting. Maybe a bad -x language?\n"); exit_failure(); } if (err == NULL) err = out; sexp_port_sourcep(in) = 1; while (1) { sexp_write_string(ctx, "> ", out); sexp_flush(ctx, out); sexp_maybe_block_port(ctx, in, 1); obj = sexp_read(ctx, in); sexp_maybe_unblock_port(ctx, in); if (obj == SEXP_EOF) break; if (sexp_exceptionp(obj)) { sexp_print_exception(ctx, obj, err); } else { sexp_context_top(ctx) = 0; if (!(sexp_idp(obj)||sexp_pairp(obj)||sexp_nullp(obj))) obj = sexp_make_lit(ctx, obj); tmp = sexp_env_bindings(env); res = sexp_eval(ctx, obj, env); #if SEXP_USE_WARN_UNDEFS sexp_warn_undefs(ctx, sexp_env_bindings(env), tmp, res); #endif if (res && sexp_exceptionp(res)) { sexp_print_exception(ctx, res, err); if (res != sexp_global(ctx, SEXP_G_OOS_ERROR)) sexp_stack_trace(ctx, err); } else if (res != SEXP_VOID) { sexp_write(ctx, res, out); sexp_write_char(ctx, '\n', out); } } } sexp_gc_release6(ctx); }
static void repl (sexp ctx, sexp env) { sexp in, out, err; sexp_gc_var3(obj, tmp, res); sexp_gc_preserve3(ctx, obj, tmp, res); sexp_context_tracep(ctx) = 1; in = sexp_param_ref(ctx, env, sexp_global(ctx, SEXP_G_CUR_IN_SYMBOL)); out = sexp_param_ref(ctx, env, sexp_global(ctx, SEXP_G_CUR_OUT_SYMBOL)); err = sexp_param_ref(ctx, env, sexp_global(ctx, SEXP_G_CUR_ERR_SYMBOL)); sexp_port_sourcep(in) = 1; while (1) { sexp_write_string(ctx, "> ", out); sexp_flush(ctx, out); sexp_maybe_block_port(ctx, in, 1); obj = sexp_read(ctx, in); sexp_maybe_unblock_port(ctx, in); if (obj == SEXP_EOF) break; if (sexp_exceptionp(obj)) { sexp_print_exception(ctx, obj, err); } else { tmp = sexp_env_bindings(env); sexp_context_top(ctx) = 0; res = sexp_eval(ctx, obj, env); if (sexp_exceptionp(res)) { sexp_print_exception(ctx, res, err); sexp_stack_trace(ctx, err); } else { #if SEXP_USE_WARN_UNDEFS sexp_warn_undefs(ctx, sexp_env_bindings(env), tmp); #endif if (res != SEXP_VOID) { sexp_write(ctx, res, out); sexp_write_char(ctx, '\n', out); } } } } sexp_gc_release3(ctx); }
static cell_t *read_token(secd_t *secd, secd_parser_t *p) { int tok; cell_t *inp = NULL; switch (tok = p->token) { case '(': ++p->nested; inp = read_list(secd, p); if (p->token != ')') goto error_exit; return inp; case TOK_NUM: return new_number(secd, p->numtok); case TOK_CHAR: return new_char(secd, p->numtok); case TOK_SYM: return new_symbol(secd, p->symtok); case TOK_STR: inp = new_string(secd, strmem(p->strtok)); drop_cell(secd, p->strtok); return inp; case TOK_EOF: return new_symbol(secd, EOF_OBJ); case TOK_QUOTE: case TOK_QQ: case TOK_UQ: case TOK_UQSPL: { const char *formname = special_form_for(tok); assert(formname, "No special form for token=%d\n", tok); inp = sexp_read(secd, p); assert_cell(inp, "sexp_read: reading subexpression failed"); return new_cons(secd, new_symbol(secd, formname), new_cons(secd, inp, SECD_NIL)); } case '#': switch (tok = lexnext(p)) { case '(': { cell_t *tmplist = read_list(secd, p); if (p->token != ')') { free_cell(secd, tmplist); goto error_exit; } inp = list_to_vector(secd, tmplist); free_cell(secd, tmplist); return inp; } case TOK_SYM: { if (p->symtok[0] == '.') { int op = secdop_by_name(p->symtok + 1); if (op < 0) goto error_exit; return new_op(secd, op); } if (str_eq(p->symtok, "u8")) { lexnext(p); inp = read_bytevector(p); if (p->token != ')') goto error_exit; return inp; } } } errorf("Unknown suffix for #\n"); } error_exit: if (inp) free_cell(secd, inp); errorf("read_token: failed\n"); return new_error(secd, SECD_NIL, "read_token: failed on token %1$d '%1$c'", p->token); }