*** empty log message ***

This commit is contained in:
hyung-hwan 2005-11-07 16:02:44 +00:00
parent 6d7806a828
commit 700ce0450f
8 changed files with 132 additions and 98 deletions

View File

@ -1,4 +1,4 @@
SRCS = awk.c lex.c parse.c SRCS = awk.c
OBJS = $(SRCS:.c=.obj) OBJS = $(SRCS:.c=.obj)
OUT = xpawk.lib OUT = xpawk.lib

View File

@ -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 <xp/awk/awk.h> #include <xp/awk/awk.h>
#include <xp/bas/memory.h> #include <xp/bas/memory.h>
#include <xp/bas/assert.h>
xp_awk_t* xp_awk_open (xp_awk_t* awk) 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; 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->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; return awk;
} }
int xp_awk_close (xp_awk_t* awk) int xp_awk_close (xp_awk_t* awk)
{ {
if (xp_awk_detach_source(awk) == -1) return -1;
if (awk->__malloced) xp_free (awk); if (awk->__malloced) xp_free (awk);
return 0; 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;
}

View File

@ -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_ #ifndef _XP_AWK_AWK_H_
@ -10,7 +10,9 @@
enum enum
{ {
XP_AWK_ENOERR XP_AWK_ENOERR,
XP_AWK_ESRCOP,
XP_AWK_ESRCCL
}; };
/* /*
@ -34,11 +36,23 @@ enum
struct xp_awk_t struct xp_awk_t
{ {
/* io functions */ /* io functions */
xp_awk_io_t source_func;
xp_awk_io_t input_func; xp_awk_io_t input_func;
xp_awk_io_t output_func; xp_awk_io_t output_func;
void* source_arg;
void* input_arg; void* input_arg;
void* output_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 */ /* housekeeping */
int errnum; int errnum;
xp_bool_t __malloced; xp_bool_t __malloced;
@ -51,6 +65,9 @@ extern "C" {
xp_awk_t* xp_awk_open (xp_awk_t* awk); xp_awk_t* xp_awk_open (xp_awk_t* awk);
int xp_awk_close (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 #ifdef __cplusplus
} }
#endif #endif

5
ase/awk/hash.c Normal file
View File

@ -0,0 +1,5 @@
/*
* $Id: hash.c,v 1.1 2005-11-07 16:02:44 bacon Exp $
*/
#include <xp/awk/hash.h>

17
ase/awk/hash.h Normal file
View File

@ -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

View File

@ -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 <xp/awk/lex.h> #include <xp/awk/lex.h>
#include <xp/bas/memory.h> #include <xp/bas/memory.h>
#include <xp/bas/ctype.h> #include <xp/bas/ctype.h>
static int __get_char (xp_awk_lex_t* lex); static int __get_char (xp_awk_t* awk);
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);
static int __skip_spaces (xp_awk_lex_t* lex); static int __skip_spaces (xp_awk_t* awk);
static int __skip_comment (xp_awk_lex_t* lex); static int __skip_comment (xp_awk_t* awk);
#define GET_CHAR(lex) \ #define GET_CHAR(awk) \
do { if (__get_char(lex) == -1) return -1; } while(0) do { if (__get_char(awk) == -1) return -1; } while(0)
#define GET_CHAR_TO(lex, c) \ #define GET_CHAR_TO(awk, c) \
do { if (__get_char(lex) == -1) return -1; c = (lex)->curc; } while(0) 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 SET_TOKEN_TYPE(awk,code) ((awk)->lex.type = code)
#define ADD_TOKEN_STR(lex,str) \ #define ADD_TOKEN_STR(awk,str) \
do { if (xp_str_cat(&(lex)->token, (str)) == -1) return -1; } while (0) do { if (xp_str_cat(&(awk)->lex.token, (str)) == -1) return -1; } while (0)
xp_awk_lex_t* xp_awk_lex_open ( int xp_awk_lex (xp_awk_t* awk)
xp_awk_lex_t* 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; xp_cint_t c;
int n; int n;
do { do {
if (__skip_spaces(lex) == -1) return -1; if (__skip_spaces(awk) == -1) return -1;
if ((n = __skip_comment(lex)) == -1) return -1; if ((n = __skip_comment(awk)) == -1) return -1;
} while (n == 1); } while (n == 1);
xp_str_clear (&lex->token); xp_str_clear (&awk->lex.token);
c = lex->curc; c = awk->lex.curc;
if (c == XP_CHAR_EOF) { if (c == XP_CHAR_EOF) {
SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_END); 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; 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 (awk->ungotc_count > 0) {
awk->curc = awk->ungotc[--awk->ungotc_count];
if (lex->ungotc_count > 0) {
lex->curc = lex->ungotc[--lex->ungotc_count];
return 0; return 0;
} }
if (awk->input_func (awk, XP_SCE_INPUT_CONSUME, &lex->curc) == -1) { if (awk->source_func(XP_AWK_IO_DATA,
awk->error_code = XP_SCE_ERROR_INPUT; awk->source_arg, &awk->curc, 1) == -1) {
awk->error_code = XP_SCE_ESRCDT;
return -1; return -1;
} }
return 0; 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 (awk->ungotc_count >= xp_countof(awk->ungotc)) {
awk->error_code = XP_SCE_EUNGET;
if (lex->ungotc_count >= xp_countof(lex->ungotc)) {
awk->error_code = XP_SCE_ERROR_UNGET;
return -1; return -1;
} }
lex->ungotc[lex->ungotc_count++] = c; lex->ungotc[awk->ungotc_count++] = c;
return 0; 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; xp_cint_t c = awk->lex.curc;
while (xp_isspace(c)) GET_CHAR_TO (lex, c); while (xp_isspace(c)) GET_CHAR_TO (awk, c);
return 0; 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; if (c != XP_CHAR('/')) return 0;
GET_CHAR_TO (lex, c); GET_CHAR_TO (lex, c);

View File

@ -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_ #ifndef _XP_AWK_LEX_H_
#define _XP_AWK_LEX_H_ #define _XP_AWK_LEX_H_
#include <xp/awk/awk.h> #include <xp/awk/awk.h>
#include <xp/bas/string.h>
/*
struct xp_awk_lex_t struct xp_awk_lex_t
{ {
xp_awk_t* awk; xp_awk_t* awk;
@ -17,17 +17,13 @@ struct xp_awk_lex_t
xp_size_t ungotc_count; xp_size_t ungotc_count;
xp_bool_t __malloced; xp_bool_t __malloced;
}; };
*/
typedef struct xp_awk_lex_t xp_awk_lex_t;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
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);
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);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -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 <xp/awk/awk.h> #include <xp/awk/awk.h>
@ -7,6 +7,5 @@
int xp_awk_parse (xp_awk_t* awk) int xp_awk_parse (xp_awk_t* awk)
{ {
return -1; return -1;
} }