qse/ase/lsp/init.c

122 lines
2.6 KiB
C
Raw Normal View History

2005-09-18 10:18:35 +00:00
/*
2005-09-20 09:17:06 +00:00
* $Id: init.c,v 1.7 2005-09-20 09:17:06 bacon Exp $
2005-09-18 10:18:35 +00:00
*/
#include <xp/lsp/lsp.h>
#include <xp/bas/memory.h>
#include <xp/bas/assert.h>
xp_lsp_t* xp_lsp_open (xp_lsp_t* lsp,
xp_size_t mem_ubound, xp_size_t mem_ubound_inc)
{
if (lsp == XP_NULL) {
lsp = (xp_lsp_t*)xp_malloc(xp_sizeof(xp_lsp_t));
if (lsp == XP_NULL) return lsp;
lsp->__malloced = xp_true;
}
else lsp->__malloced = xp_false;
if (xp_lsp_token_open(&lsp->token, 0) == XP_NULL) {
if (lsp->__malloced) xp_free (lsp);
return XP_NULL;
}
lsp->errnum = XP_LSP_ERR_NONE;
2005-09-20 09:17:06 +00:00
lsp->opt_undef_symbol = 1;
//lsp->opt_undef_symbol = 0;
2005-09-18 10:18:35 +00:00
lsp->curc = XP_CHAR_EOF;
lsp->input_func = XP_NULL;
lsp->output_func = XP_NULL;
2005-09-18 13:06:43 +00:00
lsp->input_arg = XP_NULL;
lsp->output_arg = XP_NULL;
2005-09-18 10:18:35 +00:00
lsp->mem = xp_lsp_mem_new (mem_ubound, mem_ubound_inc);
if (lsp->mem == XP_NULL) {
xp_lsp_token_close (&lsp->token);
if (lsp->__malloced) xp_free (lsp);
return XP_NULL;
}
2005-09-19 03:05:37 +00:00
if (xp_lsp_add_builtin_prims (lsp->mem) == -1) {
2005-09-18 10:18:35 +00:00
xp_lsp_mem_free (lsp->mem);
xp_lsp_token_close (&lsp->token);
if (lsp->__malloced) xp_free (lsp);
return XP_NULL;
}
2005-09-20 08:05:32 +00:00
lsp->max_eval_depth = 0; // TODO: put restriction here....
lsp->cur_eval_depth = 0;
2005-09-18 10:18:35 +00:00
return lsp;
}
void xp_lsp_close (xp_lsp_t* lsp)
{
xp_assert (lsp != XP_NULL);
xp_lsp_mem_free (lsp->mem);
xp_lsp_token_close (&lsp->token);
if (lsp->__malloced) xp_free (lsp);
}
2005-09-18 13:06:43 +00:00
int xp_lsp_attach_input (xp_lsp_t* lsp, xp_lsp_io_t input, void* arg)
2005-09-18 10:18:35 +00:00
{
if (xp_lsp_detach_input(lsp) == -1) return -1;
xp_assert (lsp->input_func == XP_NULL);
2005-09-18 13:06:43 +00:00
if (input(XP_LSP_IO_OPEN, arg, XP_NULL, 0) == -1) {
2005-09-18 10:18:35 +00:00
/* TODO: set error number */
return -1;
}
2005-09-20 08:05:32 +00:00
2005-09-18 10:18:35 +00:00
lsp->input_func = input;
2005-09-18 13:06:43 +00:00
lsp->input_arg = arg;
2005-09-20 08:05:32 +00:00
lsp->curc = XP_CHAR_EOF;
2005-09-18 10:18:35 +00:00
return 0;
}
int xp_lsp_detach_input (xp_lsp_t* lsp)
{
if (lsp->input_func != XP_NULL) {
2005-09-18 13:06:43 +00:00
if (lsp->input_func(XP_LSP_IO_CLOSE, lsp->input_arg, XP_NULL, 0) == -1) {
2005-09-18 10:18:35 +00:00
/* TODO: set error number */
return -1;
}
lsp->input_func = XP_NULL;
2005-09-18 13:06:43 +00:00
lsp->input_arg = XP_NULL;
2005-09-20 08:05:32 +00:00
lsp->curc = XP_CHAR_EOF;
2005-09-18 10:18:35 +00:00
}
return 0;
}
2005-09-18 13:06:43 +00:00
int xp_lsp_attach_output (xp_lsp_t* lsp, xp_lsp_io_t output, void* arg)
2005-09-18 10:18:35 +00:00
{
if (xp_lsp_detach_output(lsp) == -1) return -1;
xp_assert (lsp->output_func == XP_NULL);
2005-09-18 13:06:43 +00:00
if (output(XP_LSP_IO_OPEN, arg, XP_NULL, 0) == -1) {
2005-09-18 10:18:35 +00:00
/* TODO: set error number */
return -1;
}
lsp->output_func = output;
2005-09-18 13:06:43 +00:00
lsp->output_arg = arg;
2005-09-18 10:18:35 +00:00
return 0;
}
int xp_lsp_detach_output (xp_lsp_t* lsp)
{
if (lsp->output_func != XP_NULL) {
2005-09-18 13:06:43 +00:00
if (lsp->output_func(XP_LSP_IO_CLOSE, lsp->output_arg, XP_NULL, 0) == -1) {
2005-09-18 10:18:35 +00:00
/* TODO: set error number */
return -1;
}
lsp->output_func = XP_NULL;
2005-09-18 13:06:43 +00:00
lsp->output_arg = XP_NULL;
2005-09-18 10:18:35 +00:00
}
return 0;
}