summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2016-11-10 23:25:56 -0800
committerKeith Packard <keithp@keithp.com>2016-11-17 22:18:39 -0800
commit2ed769dae3f003951924be9e3a392fd6024a8d5e (patch)
tree1dcbf9833baeeb5a6d809e6505d1eea1e702e5e4
parentdd2382eb2185735822a0036ba0ef436869a71922 (diff)
altos/lisp: Make read() return eof atom on end of file
Also make it an exception to hit eof in the middle of an sexpr. Signed-off-by: Keith Packard <keithp@keithp.com>
-rw-r--r--src/lisp/ao_lisp.h2
-rw-r--r--src/lisp/ao_lisp_read.c18
-rw-r--r--src/lisp/ao_lisp_rep.c3
3 files changed, 17 insertions, 6 deletions
diff --git a/src/lisp/ao_lisp.h b/src/lisp/ao_lisp.h
index d265ea7b..6122a2ed 100644
--- a/src/lisp/ao_lisp.h
+++ b/src/lisp/ao_lisp.h
@@ -42,6 +42,7 @@ extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST];
#define _ao_lisp_atom_delay _atom("delay")
#define _ao_lisp_atom_eval _atom("eval")
#define _ao_lisp_atom_read _atom("read")
+#define _ao_lisp_atom_eof _atom("eof")
#else
#include "ao_lisp_const.h"
#ifndef AO_LISP_POOL
@@ -76,6 +77,7 @@ extern uint16_t ao_lisp_top;
#define AO_LISP_DIVIDE_BY_ZERO 0x02
#define AO_LISP_INVALID 0x04
#define AO_LISP_UNDEFINED 0x08
+#define AO_LISP_EOF 0x10
extern uint8_t ao_lisp_exception;
diff --git a/src/lisp/ao_lisp_read.c b/src/lisp/ao_lisp_read.c
index 3a2ef7f1..7a5751ce 100644
--- a/src/lisp/ao_lisp_read.c
+++ b/src/lisp/ao_lisp_read.c
@@ -12,6 +12,7 @@
* General Public License for more details.
*/
+#define DBG_EVAL 0
#include "ao_lisp.h"
#include "ao_lisp_read.h"
@@ -270,7 +271,7 @@ lex(void)
for (;;) {
c = lexc();
if (lex_class & ENDOFFILE)
- return AO_LISP_NIL;
+ return END;
if (lex_class & WHITE)
continue;
@@ -278,7 +279,7 @@ lex(void)
if (lex_class & COMMENT) {
while ((c = lexc()) != '\n') {
if (lex_class & ENDOFFILE)
- return AO_LISP_NIL;
+ return END;
}
continue;
}
@@ -364,6 +365,8 @@ static struct ao_lisp_cons *read_stack;
static int
push_read_stack(int cons, int in_quote)
{
+ DBGI("push read stack %p %d\n", read_cons, in_quote);
+ DBG_IN();
if (cons) {
read_stack = ao_lisp_cons_cons(ao_lisp_cons_poly(read_cons),
ao_lisp_cons_cons(ao_lisp_int_poly(in_quote),
@@ -394,6 +397,8 @@ pop_read_stack(int cons)
read_cons_tail = 0;
read_stack = 0;
}
+ DBG_OUT();
+ DBGI("pop read stack %p %d\n", read_cons, in_quote);
return in_quote;
}
@@ -413,6 +418,7 @@ ao_lisp_read(void)
been_here = 1;
}
parse_token = lex();
+ DBGI("token %d (%s)\n", parse_token, token_string);
cons = 0;
in_quote = 0;
@@ -424,12 +430,15 @@ ao_lisp_read(void)
cons++;
in_quote = 0;
parse_token = lex();
+ DBGI("token %d (%s)\n", parse_token, token_string);
}
switch (parse_token) {
- case ENDOFFILE:
+ case END:
default:
- v = AO_LISP_NIL;
+ if (cons)
+ ao_lisp_error(AO_LISP_EOF, "unexpected end of file");
+ return _ao_lisp_atom_eof;
break;
case NAME:
atom = ao_lisp_atom_intern(token_string);
@@ -490,6 +499,7 @@ ao_lisp_read(void)
}
parse_token = lex();
+ DBGI("token %d (%s)\n", parse_token, token_string);
}
return v;
}
diff --git a/src/lisp/ao_lisp_rep.c b/src/lisp/ao_lisp_rep.c
index d780186a..ef7dbaf2 100644
--- a/src/lisp/ao_lisp_rep.c
+++ b/src/lisp/ao_lisp_rep.c
@@ -20,9 +20,8 @@ ao_lisp_read_eval_print(void)
ao_poly in, out = AO_LISP_NIL;
for(;;) {
in = ao_lisp_read();
- if (!in)
+ if (in == _ao_lisp_atom_eof)
break;
-// printf ("in: "); ao_lisp_poly_print(in); printf("\n");
out = ao_lisp_eval(in);
if (ao_lisp_exception) {
ao_lisp_exception = 0;