*** empty log message ***

This commit is contained in:
hyung-hwan 2005-09-18 13:06:43 +00:00
parent 2b2520afaf
commit db07f6b704
5 changed files with 69 additions and 62 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: init.c,v 1.2 2005-09-18 12:20:43 bacon Exp $
* $Id: init.c,v 1.3 2005-09-18 13:06:43 bacon Exp $
*/
#include <xp/lsp/lsp.h>
@ -28,6 +28,8 @@ xp_lsp_t* xp_lsp_open (xp_lsp_t* lsp,
lsp->curc = XP_CHAR_EOF;
lsp->input_func = XP_NULL;
lsp->output_func = XP_NULL;
lsp->input_arg = XP_NULL;
lsp->output_arg = XP_NULL;
lsp->mem = xp_lsp_mem_new (mem_ubound, mem_ubound_inc);
if (lsp->mem == XP_NULL) {
@ -70,55 +72,59 @@ int xp_lsp_error (xp_lsp_t* lsp, xp_char_t* buf, xp_size_t size)
return lsp->errnum;
}
int xp_lsp_attach_input (xp_lsp_t* lsp, xp_lsp_io_t input)
int xp_lsp_attach_input (xp_lsp_t* lsp, xp_lsp_io_t input, void* arg)
{
if (xp_lsp_detach_input(lsp) == -1) return -1;
xp_assert (lsp->input_func == XP_NULL);
if (input(lsp, XP_LSP_IO_OPEN, XP_NULL) == -1) {
if (input(XP_LSP_IO_OPEN, arg, XP_NULL, 0) == -1) {
/* TODO: set error number */
return -1;
}
lsp->input_func = input;
lsp->input_arg = arg;
return 0;
}
int xp_lsp_detach_input (xp_lsp_t* lsp)
{
if (lsp->input_func != XP_NULL) {
if (lsp->input_func(lsp, XP_LSP_IO_CLOSE, XP_NULL) == -1) {
if (lsp->input_func(XP_LSP_IO_CLOSE, lsp->input_arg, XP_NULL, 0) == -1) {
/* TODO: set error number */
return -1;
}
lsp->input_func = XP_NULL;
lsp->input_arg = XP_NULL;
}
return 0;
}
int xp_lsp_attach_output (xp_lsp_t* lsp, xp_lsp_io_t output)
int xp_lsp_attach_output (xp_lsp_t* lsp, xp_lsp_io_t output, void* arg)
{
if (xp_lsp_detach_output(lsp) == -1) return -1;
xp_assert (lsp->output_func == XP_NULL);
if (output(lsp, XP_LSP_IO_OPEN, XP_NULL) == -1) {
if (output(XP_LSP_IO_OPEN, arg, XP_NULL, 0) == -1) {
/* TODO: set error number */
return -1;
}
lsp->output_func = output;
lsp->output_arg = arg;
return 0;
}
int xp_lsp_detach_output (xp_lsp_t* lsp)
{
if (lsp->output_func != XP_NULL) {
if (lsp->output_func(lsp, XP_LSP_IO_CLOSE, XP_NULL) == -1) {
if (lsp->output_func(XP_LSP_IO_CLOSE, lsp->output_arg, XP_NULL, 0) == -1) {
/* TODO: set error number */
return -1;
}
lsp->output_func = XP_NULL;
lsp->output_arg = XP_NULL;
}
return 0;

View File

@ -1,5 +1,5 @@
/*
* $Id: lsp.h,v 1.9 2005-09-18 12:20:43 bacon Exp $
* $Id: lsp.h,v 1.10 2005-09-18 13:06:43 bacon Exp $
*/
#ifndef _XP_LSP_LSP_H_
@ -48,13 +48,18 @@ enum
*/
typedef struct xp_lsp_t xp_lsp_t;
typedef int (*xp_lsp_io_t) (xp_lsp_t* lsp, int cmd, void* arg);
/*
* TYPEDEF: xp_lsp_io_t
* Defines an IO handler
*/
typedef xp_ssize_t (*xp_lsp_io_t) (
int cmd, void* arg, xp_char_t* data, xp_size_t count);
enum
{
XP_LSP_IO_OPEN,
XP_LSP_IO_CLOSE,
XP_LSP_IO_CHAR,
XP_LSP_IO_STR
XP_LSP_IO_DATA
};
struct xp_lsp_t
@ -74,6 +79,8 @@ struct xp_lsp_t
/* io functions */
xp_lsp_io_t input_func;
xp_lsp_io_t output_func;
void* input_arg;
void* output_arg;
/* memory manager */
xp_lsp_mem_t* mem;
@ -113,7 +120,7 @@ int xp_lsp_error (xp_lsp_t* lsp, xp_char_t* buf, xp_size_t size);
/*
* FUNCTION: xp_lsp_attach_input
*/
int xp_lsp_attach_input (xp_lsp_t* lsp, xp_lsp_io_t input);
int xp_lsp_attach_input (xp_lsp_t* lsp, xp_lsp_io_t input, void* arg);
/*
* FUNCTION: xp_lsp_detach_input
@ -123,7 +130,7 @@ int xp_lsp_detach_input (xp_lsp_t* lsp);
/*
* FUNCTION: xp_lsp_attach_output
*/
int xp_lsp_attach_output (xp_lsp_t* lsp, xp_lsp_io_t output);
int xp_lsp_attach_output (xp_lsp_t* lsp, xp_lsp_io_t output, void* arg);
/*
* FUNCTION: xp_lsp_detach_output

View File

@ -1,9 +1,10 @@
/*
* $Id: print.c,v 1.7 2005-09-18 12:20:43 bacon Exp $
* $Id: print.c,v 1.8 2005-09-18 13:06:43 bacon Exp $
*/
#include <xp/lsp/lsp.h>
#include <xp/bas/stdio.h>
#include <xp/bas/string.h>
void xp_lsp_print_debug (xp_lsp_obj_t* obj)
{
@ -60,7 +61,7 @@ void xp_lsp_print_debug (xp_lsp_obj_t* obj)
#define OUTPUT_STR(lsp,str) \
do { \
if (lsp->output_func(lsp, XP_LSP_IO_STR, (void*)str) == -1) { \
if (lsp->output_func(XP_LSP_IO_DATA, lsp->output_arg, (void*)str, xp_strlen(str)) == -1) { \
lsp->errnum = XP_LSP_ERR_OUTPUT; \
return -1; \
} \

View File

@ -1,5 +1,5 @@
/*
* $Id: read.c,v 1.12 2005-09-18 12:20:43 bacon Exp $
* $Id: read.c,v 1.13 2005-09-18 13:06:43 bacon Exp $
*/
#include <xp/lsp/lsp.h>
@ -48,26 +48,16 @@
#define TOKEN_UNTERM_STRING 51
#define NEXT_CHAR(lsp) \
do { \
if (lsp->input_func == XP_NULL) { \
lsp->errnum = XP_LSP_ERR_INPUT_NOT_ATTACHED; \
return -1; \
} \
else if (lsp->input_func(lsp, XP_LSP_IO_CHAR, XP_NULL) == -1) { \
lsp->errnum = XP_LSP_ERR_INPUT; \
return -1; \
} \
} while (0)
do { if (read_char(lsp) == -1) return -1;} while (0)
#define NEXT_TOKEN(lsp) \
do { \
if (read_token(lsp) == -1) return XP_NULL; \
} while (0)
do { if (read_token(lsp) == -1) return XP_NULL; } while (0)
static xp_lsp_obj_t* read_obj (xp_lsp_t* lsp);
static xp_lsp_obj_t* read_list (xp_lsp_t* lsp);
static xp_lsp_obj_t* read_quote (xp_lsp_t* lsp);
static int read_char (xp_lsp_t* lsp);
static int read_token (xp_lsp_t* lsp);
static int read_number (xp_lsp_t* lsp, int negative);
static int read_ident (xp_lsp_t* lsp);
@ -75,15 +65,7 @@ static int read_string (xp_lsp_t* lsp);
xp_lsp_obj_t* xp_lsp_read (xp_lsp_t* lsp)
{
/*NEXT_CHAR (lsp);*/
if (lsp->input_func == XP_NULL) {
lsp->errnum = XP_LSP_ERR_INPUT_NOT_ATTACHED;
return XP_NULL;
}
else if (lsp->input_func(lsp, XP_LSP_IO_CHAR, XP_NULL) == -1) {
lsp->errnum = XP_LSP_ERR_INPUT;
return XP_NULL;
}
if (read_char(lsp) == -1) return XP_NULL;
lsp->errnum = XP_LSP_ERR_NONE;
NEXT_TOKEN (lsp);
@ -239,6 +221,25 @@ static xp_lsp_obj_t* read_quote (xp_lsp_t* lsp)
return cons;
}
static int read_char (xp_lsp_t* lsp)
{
xp_ssize_t n;
if (lsp->input_func == XP_NULL) {
lsp->errnum = XP_LSP_ERR_INPUT_NOT_ATTACHED;
return -1;
}
n = lsp->input_func(XP_LSP_IO_DATA, lsp->input_arg, &lsp->curc, 1);
if (n == -1) {
lsp->errnum = XP_LSP_ERR_INPUT;
return -1;
}
if (n == 0) lsp->curc = XP_CHAR_EOF;
return 0;
}
static int read_token (xp_lsp_t* lsp)
{
xp_assert (lsp->input_func != XP_NULL);

View File

@ -3,36 +3,33 @@
#include <xp/bas/ctype.h>
#include <xp/bas/stdcli.h>
#include <xp/bas/locale.h>
#include <xp/bas/sio.h>
#ifdef __linux
#include <mcheck.h>
#endif
static int get_char (xp_lsp_t* lsp, int cmd, void* arg)
static xp_ssize_t get_input (int cmd, void* arg, xp_char_t* data, xp_size_t size)
{
xp_cint_t c;
xp_ssize_t n;
switch (cmd) {
case XP_LSP_IO_OPEN:
case XP_LSP_IO_CLOSE:
return 0;
case XP_LSP_IO_CHAR:
c = xp_fgetc (xp_stdin);
if (c == XP_CHAR_EOF) {
if (xp_ferror(xp_stdin)) return -1;
}
break;
case XP_LSP_IO_STR:
return -1;
case XP_LSP_IO_DATA:
if (size < 0) return -1;
n = xp_sio_getc (xp_sio_in, data);
if (n == 0) return 0;
if (n != 1) return -1;
return n;
}
return 0;
return -1;
}
static int put_char (xp_lsp_t* lsp, int cmd, void* arg)
static xp_ssize_t put_output (int cmd, void* arg, xp_char_t* data, xp_size_t size)
{
switch (cmd) {
@ -40,16 +37,11 @@ static int put_char (xp_lsp_t* lsp, int cmd, void* arg)
case XP_LSP_IO_CLOSE:
return 0;
case XP_LSP_IO_CHAR:
xp_fputc (*(xp_char_t*)arg, xp_stdout);
break;
case XP_LSP_IO_STR:
xp_fputs ((xp_char_t*)arg, xp_stdout);
break;
case XP_LSP_IO_DATA:
return xp_sio_putsx (xp_sio_out, data, size);
}
return 0;
return -1;
}
@ -159,8 +151,8 @@ int xp_main (int argc, xp_char_t* argv[])
xp_printf (XP_TEXT("LSP 0.0001\n"));
xp_lsp_attach_input (lsp, get_char);
xp_lsp_attach_output (lsp, put_char);
xp_lsp_attach_input (lsp, get_input, XP_NULL);
xp_lsp_attach_output (lsp, put_output, XP_NULL);
for (;;) {
xp_printf (XP_TEXT("%s> "), argv[0]);