qse/ase/lsp/read.c

419 lines
9.4 KiB
C
Raw Normal View History

2005-02-04 15:39:11 +00:00
/*
2005-05-30 07:19:09 +00:00
* $Id: read.c,v 1.9 2005-05-30 07:15:35 bacon Exp $
2005-02-04 15:39:11 +00:00
*/
2005-02-04 16:00:37 +00:00
#include <xp/lisp/lisp.h>
#include <xp/lisp/token.h>
2005-04-24 07:48:16 +00:00
#include <xp/bas/assert.h>
#include <xp/bas/ctype.h>
2005-02-04 15:39:11 +00:00
2005-02-04 16:00:37 +00:00
#define IS_SPACE(x) xp_isspace(x)
#define IS_DIGIT(x) xp_isdigit(x)
#define IS_ALPHA(x) xp_isalpha(x)
#define IS_ALNUM(x) xp_isalnum(x)
2005-02-04 15:39:11 +00:00
#define IS_IDENT(c) \
2005-02-04 16:23:34 +00:00
((c) == XP_CHAR('+') || (c) == XP_CHAR('-') || \
(c) == XP_CHAR('*') || (c) == XP_CHAR('/') || \
(c) == XP_CHAR('%') || (c) == XP_CHAR('&') || \
(c) == XP_CHAR('<') || (c) == XP_CHAR('>') || \
(c) == XP_CHAR('=') || (c) == XP_CHAR('_') || \
(c) == XP_CHAR('?'))
2005-02-04 15:39:11 +00:00
#define TOKEN_CLEAR(lsp) xp_lisp_token_clear (lsp->token)
#define TOKEN_TYPE(lsp) lsp->token->type
#define TOKEN_IVALUE(lsp) lsp->token->ivalue
#define TOKEN_FVALUE(lsp) lsp->token->fvalue
#define TOKEN_SVALUE(lsp) lsp->token->buffer
#define TOKEN_SLENGTH(lsp) lsp->token->size
#define TOKEN_ADD_CHAR(lsp,ch) \
do { \
if (xp_lisp_token_addc (lsp->token, ch) == -1) { \
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_MEM; \
2005-02-04 15:39:11 +00:00
return -1; \
} \
} while (0)
#define TOKEN_COMPARE(lsp,str) xp_lisp_token_compare (lsp->token, str)
#define TOKEN_END 0
#define TOKEN_INT 1
#define TOKEN_FLOAT 2
#define TOKEN_STRING 3
#define TOKEN_LPAREN 4
#define TOKEN_RPAREN 5
#define TOKEN_IDENT 6
#define TOKEN_QUOTE 7
#define TOKEN_DOT 8
#define TOKEN_INVALID 50
#define TOKEN_UNTERM_STRING 51
#ifdef __cplusplus
extern "C" {
#endif
static xp_lisp_obj_t* read_obj (xp_lisp_t* lsp);
static xp_lisp_obj_t* read_list (xp_lisp_t* lsp);
static xp_lisp_obj_t* read_quote (xp_lisp_t* lsp);
static int read_token (xp_lisp_t* lsp);
static int read_number (xp_lisp_t* lsp, int negative);
static int read_ident (xp_lisp_t* lsp);
static int read_string (xp_lisp_t* lsp);
#ifdef __cplusplus
}
#endif
#define NEXT_CHAR(lsp) \
do { \
if (lsp->creader (&lsp->curc, lsp->creader_extra) == -1) { \
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_READ; \
2005-02-04 15:39:11 +00:00
return -1; \
} \
} while (0)
#define NEXT_TOKEN(lsp) \
do { \
if (read_token(lsp) == -1) return XP_NULL; \
} while (0)
void xp_lisp_set_creader (xp_lisp_t* lsp, xp_lisp_creader_t func, void* extra)
{
2005-02-05 05:18:20 +00:00
xp_assert (lsp != XP_NULL);
2005-02-04 15:39:11 +00:00
lsp->creader = func;
lsp->creader_extra = extra;
lsp->creader_just_set = 1;
}
xp_lisp_obj_t* xp_lisp_read (xp_lisp_t* lsp)
{
2005-02-05 05:18:20 +00:00
xp_assert (lsp != XP_NULL && lsp->creader != XP_NULL);
2005-02-04 15:39:11 +00:00
if (lsp->creader_just_set) {
// NEXT_CHAR (lsp);
if (lsp->creader (&lsp->curc, lsp->creader_extra) == -1) {
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_READ;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
lsp->creader_just_set = 0;
}
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_NONE;
2005-02-04 15:39:11 +00:00
NEXT_TOKEN (lsp);
if (lsp->mem->locked != XP_NULL) {
xp_lisp_unlock_all (lsp->mem->locked);
lsp->mem->locked = XP_NULL;
}
lsp->mem->locked = read_obj (lsp);
return lsp->mem->locked;
}
static xp_lisp_obj_t* read_obj (xp_lisp_t* lsp)
{
xp_lisp_obj_t* obj;
switch (TOKEN_TYPE(lsp)) {
case TOKEN_END:
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_END;
2005-02-04 15:39:11 +00:00
return XP_NULL;
case TOKEN_LPAREN:
NEXT_TOKEN (lsp);
return read_list (lsp);
case TOKEN_QUOTE:
NEXT_TOKEN (lsp);
return read_quote (lsp);
case TOKEN_INT:
obj = xp_lisp_make_int (lsp->mem, TOKEN_IVALUE(lsp));
2005-02-04 16:00:37 +00:00
if (obj == XP_NULL) lsp->error = XP_LISP_ERR_MEM;
2005-02-04 15:39:11 +00:00
xp_lisp_lock (obj);
return obj;
case TOKEN_FLOAT:
obj = xp_lisp_make_float (lsp->mem, TOKEN_FVALUE(lsp));
2005-02-04 16:00:37 +00:00
if (obj == XP_NULL) lsp->error = XP_LISP_ERR_MEM;
2005-02-04 15:39:11 +00:00
xp_lisp_lock (obj);
return obj;
case TOKEN_STRING:
obj = xp_lisp_make_string (
lsp->mem, TOKEN_SVALUE(lsp), TOKEN_SLENGTH(lsp));
2005-02-04 16:00:37 +00:00
if (obj == XP_NULL) lsp->error = XP_LISP_ERR_MEM;
2005-02-04 15:39:11 +00:00
xp_lisp_lock (obj);
return obj;
case TOKEN_IDENT:
2005-02-05 05:18:20 +00:00
xp_assert (lsp->mem->nil != XP_NULL && lsp->mem->t != XP_NULL);
2005-02-04 16:23:34 +00:00
if (TOKEN_COMPARE(lsp,XP_TEXT("nil")) == 0) obj = lsp->mem->nil;
else if (TOKEN_COMPARE(lsp,XP_TEXT("t")) == 0) obj = lsp->mem->t;
2005-02-04 15:39:11 +00:00
else {
obj = xp_lisp_make_symbol (
lsp->mem, TOKEN_SVALUE(lsp), TOKEN_SLENGTH(lsp));
2005-02-04 16:00:37 +00:00
if (obj == XP_NULL) lsp->error = XP_LISP_ERR_MEM;
2005-02-04 15:39:11 +00:00
xp_lisp_lock (obj);
}
return obj;
}
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_SYNTAX;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
static xp_lisp_obj_t* read_list (xp_lisp_t* lsp)
{
xp_lisp_obj_t* obj;
xp_lisp_obj_cons_t* p, * first = XP_NULL, * prev = XP_NULL;
while (TOKEN_TYPE(lsp) != TOKEN_RPAREN) {
if (TOKEN_TYPE(lsp) == TOKEN_END) {
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_SYNTAX; // unexpected end of input
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
if (TOKEN_TYPE(lsp) == TOKEN_DOT) {
if (prev == XP_NULL) {
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_SYNTAX; // unexpected .
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
NEXT_TOKEN (lsp);
obj = read_obj (lsp);
if (obj == XP_NULL) {
2005-02-04 16:00:37 +00:00
if (lsp->error == XP_LISP_ERR_END) {
2005-02-04 15:39:11 +00:00
//unexpected end of input
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_SYNTAX;
2005-02-04 15:39:11 +00:00
}
return XP_NULL;
}
prev->cdr = obj;
NEXT_TOKEN (lsp);
if (TOKEN_TYPE(lsp) != TOKEN_RPAREN) {
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_SYNTAX; // ) expected
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
break;
}
obj = read_obj (lsp);
if (obj == XP_NULL) {
2005-02-04 16:00:37 +00:00
if (lsp->error == XP_LISP_ERR_END) {
2005-02-04 15:39:11 +00:00
// unexpected end of input
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_SYNTAX;
2005-02-04 15:39:11 +00:00
}
return XP_NULL;
}
p = (xp_lisp_obj_cons_t*)xp_lisp_make_cons (
lsp->mem, lsp->mem->nil, lsp->mem->nil);
if (p == XP_NULL) {
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_MEM;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
xp_lisp_lock ((xp_lisp_obj_t*)p);
if (first == XP_NULL) first = p;
if (prev != XP_NULL) prev->cdr = (xp_lisp_obj_t*)p;
p->car = obj;
prev = p;
NEXT_TOKEN (lsp);
}
return (first == XP_NULL)? lsp->mem->nil: (xp_lisp_obj_t*)first;
}
static xp_lisp_obj_t* read_quote (xp_lisp_t* lsp)
{
xp_lisp_obj_t* cons, * tmp;
tmp = read_obj (lsp);
if (tmp == XP_NULL) {
2005-02-04 16:00:37 +00:00
if (lsp->error == XP_LISP_ERR_END) {
2005-02-04 15:39:11 +00:00
// unexpected end of input
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_SYNTAX;
2005-02-04 15:39:11 +00:00
}
return XP_NULL;
}
cons = xp_lisp_make_cons (lsp->mem, tmp, lsp->mem->nil);
if (cons == XP_NULL) {
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_MEM;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
xp_lisp_lock (cons);
cons = xp_lisp_make_cons (lsp->mem, lsp->mem->quote, cons);
if (cons == XP_NULL) {
2005-02-04 16:00:37 +00:00
lsp->error = XP_LISP_ERR_MEM;
2005-02-04 15:39:11 +00:00
return XP_NULL;
}
xp_lisp_lock (cons);
return cons;
}
static int read_token (xp_lisp_t* lsp)
{
2005-02-05 05:18:20 +00:00
xp_assert (lsp->creader != XP_NULL);
2005-02-04 15:39:11 +00:00
TOKEN_CLEAR (lsp);
for (;;) {
// skip white spaces
while (IS_SPACE(lsp->curc)) NEXT_CHAR (lsp);
// skip the comments here
2005-02-04 16:23:34 +00:00
if (lsp->curc == XP_CHAR(';')) {
2005-02-04 15:39:11 +00:00
do {
NEXT_CHAR (lsp);
2005-05-30 07:19:09 +00:00
} while (lsp->curc != XP_CHAR('\n') && lsp->curc != XP_CHAR_EOF);
2005-02-04 15:39:11 +00:00
}
else break;
}
2005-05-30 07:19:09 +00:00
if (lsp->curc == XP_CHAR_EOF) {
2005-02-04 15:39:11 +00:00
TOKEN_TYPE(lsp) = TOKEN_END;
return 0;
}
2005-02-04 16:23:34 +00:00
else if (lsp->curc == XP_CHAR('(')) {
2005-02-04 15:39:11 +00:00
TOKEN_ADD_CHAR (lsp, lsp->curc);
TOKEN_TYPE(lsp) = TOKEN_LPAREN;
NEXT_CHAR (lsp);
return 0;
}
2005-02-04 16:23:34 +00:00
else if (lsp->curc == XP_CHAR(')')) {
2005-02-04 15:39:11 +00:00
TOKEN_ADD_CHAR (lsp, lsp->curc);
TOKEN_TYPE(lsp) = TOKEN_RPAREN;
NEXT_CHAR (lsp);
return 0;
}
2005-02-04 16:23:34 +00:00
else if (lsp->curc == XP_CHAR('\'')) {
2005-02-04 15:39:11 +00:00
TOKEN_ADD_CHAR (lsp, lsp->curc);
TOKEN_TYPE(lsp) = TOKEN_QUOTE;
NEXT_CHAR (lsp);
return 0;
}
2005-02-04 16:23:34 +00:00
else if (lsp->curc == XP_CHAR('.')) {
2005-02-04 15:39:11 +00:00
TOKEN_ADD_CHAR (lsp, lsp->curc);
TOKEN_TYPE(lsp) = TOKEN_DOT;
NEXT_CHAR (lsp);
return 0;
}
2005-02-04 16:23:34 +00:00
else if (lsp->curc == XP_CHAR('-')) {
2005-02-04 15:39:11 +00:00
TOKEN_ADD_CHAR (lsp, lsp->curc);
NEXT_CHAR (lsp);
return (IS_DIGIT(lsp->curc))?
read_number (lsp, 1): read_ident (lsp);
}
else if (IS_DIGIT(lsp->curc)) {
return read_number (lsp, 0);
}
else if (IS_ALPHA(lsp->curc) || IS_IDENT(lsp->curc)) {
return read_ident (lsp);
}
2005-02-04 16:23:34 +00:00
else if (lsp->curc == XP_CHAR('\"')) {
2005-02-04 15:39:11 +00:00
NEXT_CHAR (lsp);
return read_string (lsp);
}
TOKEN_TYPE(lsp) = TOKEN_INVALID;
NEXT_CHAR (lsp); // consume
return 0;
}
static int read_number (xp_lisp_t* lsp, int negative)
{
2005-02-14 14:37:50 +00:00
xp_lisp_int_t ivalue = 0;
2005-02-04 15:39:11 +00:00
do {
2005-02-14 14:37:50 +00:00
ivalue = ivalue * 10 + (lsp->curc - XP_CHAR('0'));
2005-02-04 15:39:11 +00:00
TOKEN_ADD_CHAR (lsp, lsp->curc);
NEXT_CHAR (lsp);
} while (IS_DIGIT(lsp->curc));
2005-02-14 14:37:50 +00:00
if (negative) ivalue *= -1;
TOKEN_IVALUE(lsp) = ivalue;
2005-02-04 15:39:11 +00:00
TOKEN_TYPE(lsp) = TOKEN_INT;
2005-02-14 14:37:50 +00:00
/* TODO: read floating point numbers */
2005-02-04 15:39:11 +00:00
return 0;
}
static int read_ident (xp_lisp_t* lsp)
{
do {
TOKEN_ADD_CHAR (lsp, lsp->curc);
NEXT_CHAR (lsp);
} while (IS_ALNUM(lsp->curc) || IS_IDENT(lsp->curc));
TOKEN_TYPE(lsp) = TOKEN_IDENT;
return 0;
}
static int read_string (xp_lisp_t* lsp)
{
int escaped = 0;
2005-02-14 14:37:50 +00:00
xp_cint_t code = 0;
2005-02-04 15:39:11 +00:00
do {
2005-05-30 07:19:09 +00:00
if (lsp->curc == XP_CHAR_EOF) {
2005-02-04 15:39:11 +00:00
TOKEN_TYPE(lsp) = TOKEN_UNTERM_STRING;
return 0;
}
// TODO:
if (escaped == 3) {
/* \xNN */
}
else if (escaped == 2) {
/* \000 */
}
else if (escaped == 1) {
/* backslash + character */
2005-02-04 16:23:34 +00:00
if (lsp->curc == XP_CHAR('a'))
lsp->curc = XP_CHAR('\a');
else if (lsp->curc == XP_CHAR('b'))
lsp->curc = XP_CHAR('\b');
else if (lsp->curc == XP_CHAR('f'))
lsp->curc = XP_CHAR('\f');
else if (lsp->curc == XP_CHAR('n'))
lsp->curc = XP_CHAR('\n');
else if (lsp->curc == XP_CHAR('r'))
lsp->curc = XP_CHAR('\r');
else if (lsp->curc == XP_CHAR('t'))
lsp->curc = XP_CHAR('\t');
else if (lsp->curc == XP_CHAR('v'))
lsp->curc = XP_CHAR('\v');
else if (lsp->curc == XP_CHAR('0')) {
2005-02-04 15:39:11 +00:00
escaped = 2;
code = 0;
NEXT_CHAR (lsp);
continue;
}
2005-02-04 16:23:34 +00:00
else if (lsp->curc == XP_CHAR('x')) {
2005-02-04 15:39:11 +00:00
escaped = 3;
code = 0;
NEXT_CHAR (lsp);
continue;
}
}
2005-02-04 16:23:34 +00:00
else if (lsp->curc == XP_CHAR('\\')) {
2005-02-04 15:39:11 +00:00
escaped = 1;
NEXT_CHAR (lsp);
continue;
}
TOKEN_ADD_CHAR (lsp, lsp->curc);
NEXT_CHAR (lsp);
2005-02-04 16:23:34 +00:00
} while (lsp->curc != XP_CHAR('\"'));
2005-02-04 15:39:11 +00:00
TOKEN_TYPE(lsp) = TOKEN_STRING;
NEXT_CHAR (lsp);
return 0;
}