diff --git a/ase/awk/Makefile.cl b/ase/awk/Makefile.cl index 0c2b05e0..1fb7678b 100644 --- a/ase/awk/Makefile.cl +++ b/ase/awk/Makefile.cl @@ -1,4 +1,4 @@ -SRCS = awk.c lex.c parse.c +SRCS = awk.c OBJS = $(SRCS:.c=.obj) OUT = xpawk.lib diff --git a/ase/awk/awk.c b/ase/awk/awk.c index b5c727a2..2687a0c8 100644 --- a/ase/awk/awk.c +++ b/ase/awk/awk.c @@ -1,9 +1,10 @@ /* - * $Id: awk.c,v 1.1 2005-11-06 12:01:29 bacon Exp $ + * $Id: awk.c,v 1.2 2005-11-07 16:02:44 bacon Exp $ */ #include #include +#include xp_awk_t* xp_awk_open (xp_awk_t* awk) { @@ -14,12 +15,63 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk) } else awk->__malloced = xp_false; + if (xp_str_open(&awk->lex.token, 128) == XP_NULL) { + if (awk->__malloced) xp_free (awk); + return XP_NULL; + } + awk->errnum = XP_AWK_ENOERR; + + awk->source_func = XP_NULL; + awk->input_func = XP_NULL; + awk->output_func = XP_NULL; + + awk->source_arg = XP_NULL; + awk->input_arg = XP_NULL; + awk->output_arg = XP_NULL; + + awk->lex.ungotc_count = 0; + awk->lex.curc = XP_CHAR_EOF; + return awk; } int xp_awk_close (xp_awk_t* awk) { + if (xp_awk_detach_source(awk) == -1) return -1; if (awk->__malloced) xp_free (awk); return 0; } + +int xp_awk_attach_source (xp_awk_t* awk, xp_awk_io_t source, void* arg) +{ + if (xp_awk_detach_source(awk) == -1) return -1; + + xp_assert (awk->source_func == XP_NULL); + + if (source(XP_AWK_IO_OPEN, arg, XP_NULL, 0) == -1) { + awk->errnum = XP_AWK_ESRCOP; + return -1; + } + + awk->source_func = source; + awk->source_arg = arg; + awk->curc = XP_CHAR_EOF; + return 0; +} + +int xp_awk_detach_source (xp_awk_t* awk) +{ + if (awk->source_func != XP_NULL) { + if (awk->source_func(XP_AWK_IO_CLOSE, awk->source_arg, XP_NULL, 0) == -1) { + awk->errnum = XP_AWK_ESRCCL; + return -1; + } + + awk->source_func = XP_NULL; + awk->source_arg = XP_NULL; + awk->curc = XP_CHAR_EOF; + } + + return 0; +} diff --git a/ase/awk/awk.h b/ase/awk/awk.h index 269c31f0..f837b174 100644 --- a/ase/awk/awk.h +++ b/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h,v 1.2 2005-11-06 12:01:29 bacon Exp $ + * $Id: awk.h,v 1.3 2005-11-07 16:02:44 bacon Exp $ */ #ifndef _XP_AWK_AWK_H_ @@ -10,7 +10,9 @@ enum { - XP_AWK_ENOERR + XP_AWK_ENOERR, + XP_AWK_ESRCOP, + XP_AWK_ESRCCL }; /* @@ -34,11 +36,23 @@ enum struct xp_awk_t { /* io functions */ + xp_awk_io_t source_func; xp_awk_io_t input_func; xp_awk_io_t output_func; + + void* source_arg; void* input_arg; void* output_arg; + /* source buffer management */ + struct { + xp_cint_t curc; + xp_cint_t ungotc[5]; + xp_size_t ungotc_count; + xp_str_t token; + int ttype; + } lex; + /* housekeeping */ int errnum; xp_bool_t __malloced; @@ -51,6 +65,9 @@ extern "C" { xp_awk_t* xp_awk_open (xp_awk_t* awk); int xp_awk_close (xp_awk_t* awk); +int xp_awk_attach_source (xp_awk_t* awk, xp_awk_io_t source, void* source_arg); +int xp_awk_detach_source (xp_awk_t* awk); + #ifdef __cplusplus } #endif diff --git a/ase/awk/hash.c b/ase/awk/hash.c new file mode 100644 index 00000000..aa719fb8 --- /dev/null +++ b/ase/awk/hash.c @@ -0,0 +1,5 @@ +/* + * $Id: hash.c,v 1.1 2005-11-07 16:02:44 bacon Exp $ + */ + +#include diff --git a/ase/awk/hash.h b/ase/awk/hash.h new file mode 100644 index 00000000..a5cfa870 --- /dev/null +++ b/ase/awk/hash.h @@ -0,0 +1,17 @@ +/* + * $Id: hash.h,v 1.1 2005-11-07 16:02:44 bacon Exp $ + */ + +#ifndef _XP_AWK_HASH_H_ +#define _XP_AWK_HASH_H_ + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/ase/awk/lex.c b/ase/awk/lex.c index dc4d51f9..7a9835be 100644 --- a/ase/awk/lex.c +++ b/ase/awk/lex.c @@ -1,86 +1,37 @@ /* - * $Id: lex.c,v 1.1 2005-11-06 12:01:29 bacon Exp $ + * $Id: lex.c,v 1.2 2005-11-07 16:02:44 bacon Exp $ */ #include #include #include -static int __get_char (xp_awk_lex_t* lex); -static int __unget_char (xp_awk_lex_t* lex, xp_cint_t c); -static int __skip_spaces (xp_awk_lex_t* lex); -static int __skip_comment (xp_awk_lex_t* lex); +static int __get_char (xp_awk_t* awk); +static int __unget_char (xp_awk_t* awk, xp_cint_t c); +static int __skip_spaces (xp_awk_t* awk); +static int __skip_comment (xp_awk_t* awk); -#define GET_CHAR(lex) \ - do { if (__get_char(lex) == -1) return -1; } while(0) -#define GET_CHAR_TO(lex, c) \ - do { if (__get_char(lex) == -1) return -1; c = (lex)->curc; } while(0) +#define GET_CHAR(awk) \ + do { if (__get_char(awk) == -1) return -1; } while(0) +#define GET_CHAR_TO(awk, c) \ + do { if (__get_char(awk) == -1) return -1; c = (awk)->lex.curc; } while(0) -#define SET_TOKEN_TYPE(lex,code) ((lex)->token.type = code) -#define ADD_TOKEN_STR(lex,str) \ - do { if (xp_str_cat(&(lex)->token, (str)) == -1) return -1; } while (0) +#define SET_TOKEN_TYPE(awk,code) ((awk)->lex.type = code) +#define ADD_TOKEN_STR(awk,str) \ + do { if (xp_str_cat(&(awk)->lex.token, (str)) == -1) return -1; } while (0) -xp_awk_lex_t* xp_awk_lex_open ( - xp_awk_lex_t* lex, xp_awk_t* awk) +int xp_awk_lex (xp_awk_t* awk) { - if (awk == XP_NULL) return XP_NULL; - - if (lex == XP_NULL) { - lex = (xp_awk_lex_t*) xp_malloc (xp_sizeof(xp_awk_lex_t)); - if (lex == XP_NULL) return XP_NULL; - lex->__malloced = xp_true; - } - else lex->__malloced = xp_false; - - if (xp_str_open (&lex->token, 128) == XP_NULL) { - if (lex->__malloced) xp_free (lex); - return XP_NULL; - } - - lex->awk = awk; - lex->ungotc_count = 0; - - /* if rewind is not supported, the following rewind call - * would fail. in this case, we just ignore the failure - * assuming that this lex can still be used in a single-pass - * compiler */ - xp_awk_lex_rewind(lex); - - if (__get_char(lex) == -1) { - xp_str_close (&lex->token); - if (lex->__malloced) xp_free (lex); - return XP_NULL; - } - - return lex; -} - -void xp_awk_lex_close (xp_awk_lex_t* lex) -{ - xp_str_close (&lex->token); - if (lex->__malloced) xp_free (lex); -} - -int xp_awk_lex_rewind (xp_awk_lex_t* lex) -{ - xp_awk_t* awk = lex->awk; - lex->ungotc_count = 0; - return awk->input_func (awk, XP_SCE_INPUT_REWIND, XP_NULL); -} - -int xp_awk_lex_fetch_token (xp_awk_lex_t* lex) -{ - xp_awk_t* awk = lex->awk; xp_cint_t c; int n; do { - if (__skip_spaces(lex) == -1) return -1; - if ((n = __skip_comment(lex)) == -1) return -1; + if (__skip_spaces(awk) == -1) return -1; + if ((n = __skip_comment(awk)) == -1) return -1; } while (n == 1); - xp_str_clear (&lex->token); - c = lex->curc; + xp_str_clear (&awk->lex.token); + c = awk->lex.curc; if (c == XP_CHAR_EOF) { SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_END); @@ -165,46 +116,43 @@ int xp_awk_lex_fetch_token (xp_awk_lex_t* lex) return 0; } -static int __get_char (xp_awk_lex_t* lex) +static int __get_char (xp_awk_t* awk) { - xp_awk_t* awk = lex->awk; - - if (lex->ungotc_count > 0) { - lex->curc = lex->ungotc[--lex->ungotc_count]; + if (awk->ungotc_count > 0) { + awk->curc = awk->ungotc[--awk->ungotc_count]; return 0; } - if (awk->input_func (awk, XP_SCE_INPUT_CONSUME, &lex->curc) == -1) { - awk->error_code = XP_SCE_ERROR_INPUT; + if (awk->source_func(XP_AWK_IO_DATA, + awk->source_arg, &awk->curc, 1) == -1) { + awk->error_code = XP_SCE_ESRCDT; return -1; } return 0; } -static int __unget_char (xp_awk_lex_t* lex, xp_cint_t c) +static int __unget_char (xp_awk_t* awk, xp_cint_t c) { - xp_awk_t* awk = lex->awk; - - if (lex->ungotc_count >= xp_countof(lex->ungotc)) { - awk->error_code = XP_SCE_ERROR_UNGET; + if (awk->ungotc_count >= xp_countof(awk->ungotc)) { + awk->error_code = XP_SCE_EUNGET; return -1; } - lex->ungotc[lex->ungotc_count++] = c; + lex->ungotc[awk->ungotc_count++] = c; return 0; } -static int __skip_spaces (xp_awk_lex_t* lex) +static int __skip_spaces (xp_awk_t* awk) { - xp_cint_t c = lex->curc; - while (xp_isspace(c)) GET_CHAR_TO (lex, c); + xp_cint_t c = awk->lex.curc; + while (xp_isspace(c)) GET_CHAR_TO (awk, c); return 0; } -static int __skip_comment (xp_awk_lex_t* lex) +static int __skip_comment (xp_awk_t* awk) { - xp_cint_t c = lex->curc; + xp_cint_t c = awk->curc; if (c != XP_CHAR('/')) return 0; GET_CHAR_TO (lex, c); diff --git a/ase/awk/lex.h b/ase/awk/lex.h index d8fb9cd4..bbc4c0db 100644 --- a/ase/awk/lex.h +++ b/ase/awk/lex.h @@ -1,13 +1,13 @@ /* - * $Id: lex.h,v 1.1 2005-11-06 12:01:29 bacon Exp $ + * $Id: lex.h,v 1.2 2005-11-07 16:02:44 bacon Exp $ */ #ifndef _XP_AWK_LEX_H_ #define _XP_AWK_LEX_H_ #include -#include +/* struct xp_awk_lex_t { xp_awk_t* awk; @@ -17,17 +17,13 @@ struct xp_awk_lex_t xp_size_t ungotc_count; xp_bool_t __malloced; }; - -typedef struct xp_awk_lex_t xp_awk_lex_t; +*/ #ifdef __cplusplus extern "C" { #endif -xp_awk_lex_t* xp_awk_lex_open (xp_awk_lex_t* lex, xp_awk_t* awk); -void xp_awk_lex_close (xp_awk_lex_t* lex); -int xp_awk_lex_rewind (xp_awk_lex_t* lex); -int xp_awk_lex_fetch_token (xp_awk_lex_t* lex); +int xp_awk_lex (xp_awk_t* awk); #ifdef __cplusplus } diff --git a/ase/awk/parse.c b/ase/awk/parse.c index 1843689f..52e9b697 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.1 2005-11-06 12:01:29 bacon Exp $ + * $Id: parse.c,v 1.2 2005-11-07 16:02:44 bacon Exp $ */ #include @@ -7,6 +7,5 @@ int xp_awk_parse (xp_awk_t* awk) { - return -1; }