Commit 7d8364a6 authored by Juergen Nickelsen's avatar Juergen Nickelsen
Browse files

more files

	new file:   COPYRIGHT
	new file:   session.c
	new file:   session.h
parent 4de31efe
Copyright (c) 2010, 2011 Juergen Nickelsen <ni@jnickelsen.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
/* Copyright (c) 2010, 2011 Juergen Nickelsen <ni@jnickelsen.de>
* See the file COPYRIGHT for details.
*/
#include <stdlib.h>
#include "session.h"
#include "io.h"
#include "reader.h"
#include "printer.h"
#include "eval.h"
#include "signals.h"
#include "gc.h"
#include "xmemory.h"
#define PROMPT "> "
session_context_t *new_session(obp_t in, obp_t out, int interactive)
{
session_context_t *sc = xcalloc(sizeof(*sc), 1, "new session");
sc->name = xstrdup(AS(in, PORT)->name, "session name");
sc->in = in;
sc->out = out;
sc->is_interactive = interactive;
new_reader(sc);
return sc;
}
void free_session(session_context_t *sc)
{
if (sc->tokbuf) {
free(sc->tokbuf);
}
free(sc->name);
free(sc);
}
obp_t repl(session_context_t *sc, int level)
{
PROTECT;
PROTVAR(value);
PROTVAR(expr);
protect(sc->in);
protect(sc->out);
while (1) {
if (sc->is_interactive) {
port_printf(sc->out,
"%ld obs %ld evals %ld applys %ld bindings\n",
object_count, eval_count, apply_count,
bind_count);
port_print(sc->out, PROMPT);
port_flush(sc->out);
}
expr = read_expr(sc);
if (traceflag) {
port_print(sc->out, "read expression: ");
print_expr(expr, sc->out);
terpri(0);
}
if (!expr) {
break;
}
value = eval(expr, sc, level);
if (sc->is_interactive) {
if (!IS_EXIT(value)) {
print_expr(value, sc->out);
terpri(sc->out);
}
} else {
if (IS_EXIT(value)) {
break;
}
}
}
if (sc->is_interactive) {
terpri(sc->out);
}
UNPROTECT;
return value;
}
/* EOF */
/* Copyright (c) 2010, 2011 Juergen Nickelsen <ni@jnickelsen.de>
* See the file COPYRIGHT for details.
*/
#ifndef __SESSION_H_INC
#define __SESSION_H_INC
#include "cbasics.h"
#include "strbuf.h"
typedef struct SESSION {
obp_t in;
obp_t out;
char *name; /* name of input stream */
strbuf_t tokbuf; /* content of multi-character tokens */
obp_t tok_atom; /* the value in case of T_ISATOM */
int pushback_token;
int lineno;
int column;
uint is_interactive:1;
} session_context_t;
session_context_t *new_session(obp_t in, obp_t out, int interactive);
void free_session(session_context_t *sc);
obp_t repl(session_context_t *sc, int level);
#endif /* __SESSION_H_INC */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment