*** empty log message ***
This commit is contained in:
parent
8bd2886a53
commit
734e366ee7
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: awk.h,v 1.92 2006-08-06 12:35:06 bacon Exp $
|
||||
* $Id: awk.h,v 1.93 2006-08-06 15:02:54 bacon Exp $
|
||||
*/
|
||||
|
||||
#ifndef _XP_AWK_AWK_H_
|
||||
@ -184,6 +184,9 @@ enum
|
||||
XP_AWK_ELVALUE, /* l-value required */
|
||||
XP_AWK_ETOOFEWARGS, /* too few arguments */
|
||||
XP_AWK_ETOOMANYARGS, /* too many arguments */
|
||||
XP_AWK_ETOOMANYGLOBALS, /* too many global variables */
|
||||
XP_AWK_ETOOMANYLOCALS, /* too many local variables */
|
||||
XP_AWK_ETOOMANYPARAMS, /* too many parameters */
|
||||
XP_AWK_EBREAK, /* break outside a loop */
|
||||
XP_AWK_ECONTINUE, /* continue outside a loop */
|
||||
XP_AWK_ENEXT, /* next illegal in BEGIN or END block */
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: awk_i.h,v 1.41 2006-08-06 08:15:29 bacon Exp $
|
||||
* $Id: awk_i.h,v 1.42 2006-08-06 15:02:55 bacon Exp $
|
||||
*/
|
||||
|
||||
#ifndef _XP_AWK_AWKI_H_
|
||||
@ -23,6 +23,7 @@ typedef struct xp_awk_tree_t xp_awk_tree_t;
|
||||
#include <xp/awk/func.h>
|
||||
#include <xp/awk/tree.h>
|
||||
#include <xp/awk/tab.h>
|
||||
#include <xp/awk/parse.h>
|
||||
#include <xp/awk/run.h>
|
||||
#include <xp/awk/extio.h>
|
||||
|
||||
@ -35,6 +36,10 @@ typedef struct xp_awk_tree_t xp_awk_tree_t;
|
||||
#include <crtdbg.h>
|
||||
#endif
|
||||
|
||||
#define XP_AWK_MAX_GLOBALS 9999
|
||||
#define XP_AWK_MAX_LOCALS 9999
|
||||
#define XP_AWK_MAX_PARAMS 9999
|
||||
|
||||
struct xp_awk_tree_t
|
||||
{
|
||||
xp_size_t nglobals; /* total number of globals */
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: err.c,v 1.33 2006-08-06 12:35:06 bacon Exp $
|
||||
* $Id: err.c,v 1.34 2006-08-06 15:02:55 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -78,6 +78,9 @@ const xp_char_t* xp_awk_geterrstr (xp_awk_t* awk)
|
||||
XP_T("l-value required"),
|
||||
XP_T("too few arguments"),
|
||||
XP_T("too many arguments"),
|
||||
XP_T("too many global variables"),
|
||||
XP_T("too many local variables"),
|
||||
XP_T("too many parameters"),
|
||||
XP_T("break outside a loop"),
|
||||
XP_T("continue outside a loop"),
|
||||
XP_T("next illegal in BEGIN or END block"),
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: jni.c,v 1.3 2006-08-06 08:15:29 bacon Exp $
|
||||
* $Id: jni.c,v 1.4 2006-08-06 15:02:55 bacon Exp $
|
||||
*/
|
||||
|
||||
#include "jni.h"
|
||||
@ -11,9 +11,13 @@
|
||||
|
||||
static xp_ssize_t __read_source (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t count);
|
||||
static xp_ssize_t __write_source (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t count);
|
||||
|
||||
static xp_ssize_t __call_java_read_source (
|
||||
JNIEnv* env, jobject obj, xp_char_t* buf, xp_size_t size);
|
||||
static xp_ssize_t __call_java_write_source (
|
||||
JNIEnv* env, jobject obj, xp_char_t* buf, xp_size_t size);
|
||||
|
||||
typedef struct srcio_data_t srcio_data_t;
|
||||
|
||||
@ -48,7 +52,6 @@ JNIEXPORT void JNICALL Java_xpkit_xpj_awk_Awk_open (JNIEnv* env, jobject obj)
|
||||
|
||||
(*env)->SetLongField (env, obj, fid, (jlong)awk);
|
||||
|
||||
|
||||
opt = XP_AWK_EXPLICIT | XP_AWK_UNIQUE | XP_AWK_DBLSLASHES |
|
||||
XP_AWK_SHADING | XP_AWK_IMPLICIT | XP_AWK_SHIFT |
|
||||
XP_AWK_EXTIO | XP_AWK_BLOCKLESS;
|
||||
@ -94,7 +97,7 @@ JNIEXPORT void JNICALL Java_xpkit_xpj_awk_Awk_parse (JNIEnv* env, jobject obj)
|
||||
srcio_data.obj = obj;
|
||||
|
||||
srcios.in = __read_source;
|
||||
srcios.out = XP_NULL;
|
||||
srcios.out = __write_source;
|
||||
srcios.custom_data = &srcio_data;
|
||||
|
||||
if (xp_awk_parse (awk, &srcios) == -1)
|
||||
@ -119,21 +122,30 @@ static xp_ssize_t __read_source (
|
||||
srcio_data = (srcio_data_t*)arg;
|
||||
|
||||
if (cmd == XP_AWK_IO_OPEN || cmd == XP_AWK_IO_CLOSE) return 0;
|
||||
if (cmd == XP_AWK_IO_NEXT) return -1;
|
||||
|
||||
if (cmd == XP_AWK_IO_READ)
|
||||
{
|
||||
return __call_java_read_source (
|
||||
srcio_data->env, srcio_data->obj, data, count);
|
||||
}
|
||||
else if (cmd == XP_AWK_IO_WRITE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static xp_ssize_t __write_source (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t count)
|
||||
{
|
||||
srcio_data_t* srcio_data;
|
||||
|
||||
srcio_data = (srcio_data_t*)arg;
|
||||
|
||||
if (cmd == XP_AWK_IO_OPEN || cmd == XP_AWK_IO_CLOSE) return 0;
|
||||
if (cmd == XP_AWK_IO_WRITE)
|
||||
{
|
||||
return __call_java_write_source (
|
||||
srcio_data->env, srcio_data->obj, data, count);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static xp_ssize_t __call_java_read_source (
|
||||
@ -163,3 +175,29 @@ static xp_ssize_t __call_java_read_source (
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
static xp_ssize_t __call_java_write_source (
|
||||
JNIEnv* env, jobject obj, xp_char_t* buf, xp_size_t size)
|
||||
{
|
||||
jclass class;
|
||||
jmethodID mid;
|
||||
jcharArray array;
|
||||
xp_ssize_t i;
|
||||
jchar* tmp;
|
||||
|
||||
class = (*env)->GetObjectClass(env, obj);
|
||||
|
||||
mid = (*env)->GetMethodID (env, class, "write_source", "([CI)I");
|
||||
if (mid == 0) return -1;
|
||||
|
||||
array = (*env)->NewCharArray (env, size);
|
||||
if (array == NULL) return -1;
|
||||
|
||||
// TODO: how to handle error..
|
||||
// TODO: what is xp_char_t is xp_mchar_t? use UTF8 ???
|
||||
tmp = (*env)->GetCharArrayElements (env, array, 0);
|
||||
for (i = 0; i < size; i++) tmp[i] = (jchar)buf[i];
|
||||
(*env)->ReleaseCharArrayElements (env, array, tmp, 0);
|
||||
|
||||
return (*env)->CallIntMethod (env, obj, mid, array, size);
|
||||
}
|
||||
|
176
ase/awk/parse.c
176
ase/awk/parse.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: parse.c,v 1.161 2006-08-06 12:35:06 bacon Exp $
|
||||
* $Id: parse.c,v 1.162 2006-08-06 15:02:55 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -210,11 +210,8 @@ static int __is_var (xp_awk_nde_t* nde);
|
||||
static int __deparse (xp_awk_t* awk);
|
||||
static int __deparse_func (xp_awk_pair_t* pair, void* arg);
|
||||
static int __put_char (xp_awk_t* awk, xp_char_t c);
|
||||
static int __put_charstr (xp_awk_t* awk, const xp_char_t* str);
|
||||
static int __put_charstrx (xp_awk_t* awk, const xp_char_t* str, xp_size_t len);
|
||||
static int __flush (xp_awk_t* awk);
|
||||
|
||||
|
||||
struct __kwent
|
||||
{
|
||||
const xp_char_t* name;
|
||||
@ -692,6 +689,14 @@ static xp_awk_nde_t* __parse_function (xp_awk_t* awk)
|
||||
}
|
||||
|
||||
/* push the parameter to the parameter list */
|
||||
if (xp_awk_tab_getsize (
|
||||
&awk->parse.params) >= XP_AWK_MAX_PARAMS)
|
||||
{
|
||||
xp_free (name_dup);
|
||||
xp_awk_tab_clear (&awk->parse.params);
|
||||
PANIC (awk, XP_AWK_ETOOMANYPARAMS);
|
||||
}
|
||||
|
||||
if (xp_awk_tab_add (
|
||||
&awk->parse.params,
|
||||
param, param_len) == (xp_size_t)-1)
|
||||
@ -1020,6 +1025,11 @@ static xp_awk_t* __add_global (
|
||||
PANIC (awk, XP_AWK_EDUPVAR);
|
||||
}
|
||||
|
||||
if (xp_awk_tab_getsize(&awk->parse.globals) >= XP_AWK_MAX_GLOBALS)
|
||||
{
|
||||
PANIC (awk, XP_AWK_ETOOMANYGLOBALS);
|
||||
}
|
||||
|
||||
if (xp_awk_tab_add (&awk->parse.globals, name, len) == (xp_size_t)-1)
|
||||
{
|
||||
PANIC (awk, XP_AWK_ENOMEM);
|
||||
@ -1087,7 +1097,8 @@ static xp_awk_t* __collect_locals (xp_awk_t* awk, xp_size_t nlocals)
|
||||
}
|
||||
|
||||
/* check if it conflicts with a paremeter name */
|
||||
if (xp_awk_tab_find (&awk->parse.params, 0, local, local_len) != (xp_size_t)-1)
|
||||
if (xp_awk_tab_find (&awk->parse.params,
|
||||
0, local, local_len) != (xp_size_t)-1)
|
||||
{
|
||||
PANIC (awk, XP_AWK_EDUPNAME);
|
||||
}
|
||||
@ -1100,7 +1111,13 @@ static xp_awk_t* __collect_locals (xp_awk_t* awk, xp_size_t nlocals)
|
||||
PANIC (awk, XP_AWK_EDUPVAR);
|
||||
}
|
||||
|
||||
if (xp_awk_tab_add (&awk->parse.locals, local, local_len) == (xp_size_t)-1)
|
||||
if (xp_awk_tab_getsize(&awk->parse.locals) >= XP_AWK_MAX_LOCALS)
|
||||
{
|
||||
PANIC (awk, XP_AWK_ETOOMANYLOCALS);
|
||||
}
|
||||
|
||||
if (xp_awk_tab_add (
|
||||
&awk->parse.locals, local, local_len) == (xp_size_t)-1)
|
||||
{
|
||||
PANIC (awk, XP_AWK_ENOMEM);
|
||||
}
|
||||
@ -1109,10 +1126,7 @@ static xp_awk_t* __collect_locals (xp_awk_t* awk, xp_size_t nlocals)
|
||||
|
||||
if (MATCH(awk,TOKEN_SEMICOLON)) break;
|
||||
|
||||
if (!MATCH(awk,TOKEN_COMMA))
|
||||
{
|
||||
PANIC (awk, XP_AWK_ECOMMA);
|
||||
}
|
||||
if (!MATCH(awk,TOKEN_COMMA)) PANIC (awk, XP_AWK_ECOMMA);
|
||||
|
||||
if (__get_token(awk) == -1) return XP_NULL;
|
||||
}
|
||||
@ -4040,7 +4054,7 @@ struct __deparse_func_t
|
||||
static int __deparse (xp_awk_t* awk)
|
||||
{
|
||||
xp_awk_chain_t* chain;
|
||||
xp_char_t tmp[128];
|
||||
xp_char_t tmp[64];
|
||||
struct __deparse_func_t df;
|
||||
int n;
|
||||
|
||||
@ -4057,52 +4071,50 @@ static int __deparse (xp_awk_t* awk)
|
||||
return -1;
|
||||
}
|
||||
|
||||
#define EXIT_DEPARSE(num) \
|
||||
do { n = -1; awk->errnum = num ; goto exit_deparse; } while(0)
|
||||
|
||||
if (awk->tree.nglobals > awk->tree.nbglobals)
|
||||
{
|
||||
xp_size_t i;
|
||||
|
||||
xp_assert (awk->tree.nglobals > 0);
|
||||
if (__put_charstr (awk, XP_T("global ")) == -1)
|
||||
{
|
||||
n = -1;
|
||||
awk->errnum = XP_AWK_ESRCOUTWRITE;
|
||||
goto exit_deparse;
|
||||
}
|
||||
if (xp_awk_putsrcstr (awk, XP_T("global ")) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
|
||||
for (i = awk->tree.nbglobals; i < awk->tree.nglobals - 1; i++)
|
||||
{
|
||||
xp_sprintf (tmp, xp_countof(tmp),
|
||||
XP_T("__global%lu, "), (unsigned long)i);
|
||||
if (__put_charstr (awk, tmp) == -1)
|
||||
{
|
||||
n = -1;
|
||||
awk->errnum = XP_AWK_ESRCOUTWRITE;
|
||||
goto exit_deparse;
|
||||
}
|
||||
if (xp_awk_putsrcstr (awk, tmp) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
}
|
||||
|
||||
xp_sprintf (tmp, xp_countof(tmp),
|
||||
XP_T("__global%lu;\n\n"), (unsigned long)i);
|
||||
if (__put_charstr (awk, tmp) == -1)
|
||||
{
|
||||
n = -1;
|
||||
awk->errnum = XP_AWK_ESRCOUTWRITE;
|
||||
goto exit_deparse;
|
||||
if (xp_awk_putsrcstr (awk, tmp) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
df.awk = awk;
|
||||
df.tmp = tmp;
|
||||
df.tmp_len = xp_countof(tmp);
|
||||
|
||||
xp_awk_map_walk (&awk->tree.afns, __deparse_func, &df);
|
||||
if (xp_awk_map_walk (&awk->tree.afns, __deparse_func, &df) == -1)
|
||||
{
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
}
|
||||
|
||||
if (awk->tree.begin != XP_NULL)
|
||||
{
|
||||
__put_charstr (awk, XP_T("BEGIN "));
|
||||
xp_awk_prnpt (awk->tree.begin);
|
||||
__put_char (awk, XP_T('\n'));
|
||||
if (xp_awk_putsrcstr (awk, XP_T("BEGIN ")) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
|
||||
if (xp_awk_prnpt (awk, awk->tree.begin) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
|
||||
if (__put_char (awk, XP_T('\n')) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
}
|
||||
|
||||
chain = awk->tree.chain;
|
||||
@ -4110,36 +4122,37 @@ static int __deparse (xp_awk_t* awk)
|
||||
{
|
||||
if (chain->pattern != XP_NULL)
|
||||
{
|
||||
/*xp_awk_prnpt (chain->pattern);*/
|
||||
xp_awk_prnptnpt (chain->pattern);
|
||||
if (xp_awk_prnptnpt (awk, chain->pattern) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
}
|
||||
|
||||
if (chain->action == XP_NULL)
|
||||
{
|
||||
/* blockless pattern */
|
||||
__put_char (awk, XP_T('\n'));
|
||||
if (__put_char (awk, XP_T('\n')) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
}
|
||||
else
|
||||
{
|
||||
xp_awk_prnpt (chain->action);
|
||||
if (xp_awk_prnpt (awk, chain->action) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
}
|
||||
|
||||
__put_char (awk, XP_T('\n'));
|
||||
if (__put_char (awk, XP_T('\n')) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
|
||||
chain = chain->next;
|
||||
}
|
||||
|
||||
if (awk->tree.end != XP_NULL)
|
||||
{
|
||||
__put_charstr (awk, XP_T("END "));
|
||||
xp_awk_prnpt (awk->tree.end);
|
||||
if (xp_awk_putsrcstr (awk, XP_T("END ")) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
if (xp_awk_prnpt (awk, awk->tree.end) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
}
|
||||
|
||||
if (__flush (awk) == -1)
|
||||
{
|
||||
n = -1;
|
||||
awk->errnum = XP_AWK_ESRCOUTWRITE;
|
||||
goto exit_deparse;
|
||||
}
|
||||
if (__flush (awk) == -1) EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
|
||||
exit_deparse:
|
||||
if (awk->src.ios->out (
|
||||
@ -4164,22 +4177,23 @@ static int __deparse_func (xp_awk_pair_t* pair, void* arg)
|
||||
xp_assert (xp_strxncmp (
|
||||
pair->key, pair->key_len, afn->name, afn->name_len) == 0);
|
||||
|
||||
__put_charstr (df->awk, XP_T("function \n"));
|
||||
__put_charstr (df->awk, afn->name);
|
||||
__put_charstr (df->awk, XP_T(" ("));
|
||||
if (xp_awk_putsrcstr (df->awk, XP_T("function ")) == -1) return -1;
|
||||
if (xp_awk_putsrcstr (df->awk, afn->name) == -1) return -1;
|
||||
if (xp_awk_putsrcstr (df->awk, XP_T(" (")) == -1) return -1;
|
||||
|
||||
for (i = 0; i < afn->nargs; )
|
||||
{
|
||||
xp_sprintf (df->tmp, df->tmp_len,
|
||||
XP_T("__arg%lu"), (unsigned long)i++);
|
||||
__put_charstr (df->awk, df->tmp);
|
||||
XP_T("__param%lu"), (unsigned long)i++);
|
||||
if (xp_awk_putsrcstr (df->awk, df->tmp) == -1) return -1;
|
||||
if (i >= afn->nargs) break;
|
||||
__put_charstr (df->awk, XP_T(", "));
|
||||
if (xp_awk_putsrcstr (df->awk, XP_T(", ")) == -1) return -1;
|
||||
}
|
||||
__put_charstr (df->awk, XP_T(")\n"));
|
||||
|
||||
xp_awk_prnpt (afn->body);
|
||||
__put_char (df->awk, XP_T('\n'));
|
||||
if (xp_awk_putsrcstr (df->awk, XP_T(")\n")) == -1) return -1;
|
||||
|
||||
if (xp_awk_prnpt (df->awk, afn->body) == -1) return -1;
|
||||
if (xp_awk_putsrcstr (df->awk, XP_T("\n")) == -1) return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -4194,30 +4208,6 @@ static int __put_char (xp_awk_t* awk, xp_char_t c)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __put_charstr (xp_awk_t* awk, const xp_char_t* str)
|
||||
{
|
||||
while (*str != XP_T('\0'))
|
||||
{
|
||||
if (__put_char (awk, *str) == -1) return -1;
|
||||
str++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __put_charstrx (xp_awk_t* awk, const xp_char_t* str, xp_size_t len)
|
||||
{
|
||||
const xp_char_t* end = str + len;
|
||||
|
||||
while (str < end)
|
||||
{
|
||||
if (__put_char (awk, *str) == -1) return -1;
|
||||
str++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __flush (xp_awk_t* awk)
|
||||
{
|
||||
xp_ssize_t n;
|
||||
@ -4239,3 +4229,29 @@ static int __flush (xp_awk_t* awk)
|
||||
awk->src.shared.buf_len = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int xp_awk_putsrcstr (xp_awk_t* awk, const xp_char_t* str)
|
||||
{
|
||||
while (*str != XP_T('\0'))
|
||||
{
|
||||
if (__put_char (awk, *str) == -1) return -1;
|
||||
str++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int xp_awk_putsrcstrx (
|
||||
xp_awk_t* awk, const xp_char_t* str, xp_size_t len)
|
||||
{
|
||||
const xp_char_t* end = str + len;
|
||||
|
||||
while (str < end)
|
||||
{
|
||||
if (__put_char (awk, *str) == -1) return -1;
|
||||
str++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: tab.h,v 1.10 2006-08-03 06:06:27 bacon Exp $
|
||||
* $Id: tab.h,v 1.11 2006-08-06 15:02:55 bacon Exp $
|
||||
*/
|
||||
|
||||
#ifndef _XP_AWK_TAB_H_
|
||||
@ -21,7 +21,6 @@ struct xp_awk_tab_t
|
||||
xp_char_t* name;
|
||||
xp_size_t name_len;
|
||||
}* buf;
|
||||
//xp_char_t** buf;
|
||||
xp_size_t size;
|
||||
xp_size_t capa;
|
||||
xp_bool_t __dynamic;
|
||||
|
469
ase/awk/tree.c
469
ase/awk/tree.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: tree.c,v 1.69 2006-08-03 05:05:47 bacon Exp $
|
||||
* $Id: tree.c,v 1.70 2006-08-06 15:02:55 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -86,36 +86,59 @@ static const xp_char_t* __print_outop_str[] =
|
||||
XP_T("")
|
||||
};
|
||||
|
||||
static void __print_tabs (int depth);
|
||||
static int __print_expression (xp_awk_nde_t* nde);
|
||||
static int __print_expression_list (xp_awk_nde_t* tree);
|
||||
static void __print_statements (xp_awk_nde_t* tree, int depth);
|
||||
#define PUT_SRCSTR(awk,str) \
|
||||
do { if (xp_awk_putsrcstr (awk, str) == -1) return- 1; } while (0)
|
||||
|
||||
static void __print_tabs (int depth)
|
||||
#define PUT_SRCSTRX(awk,str,len) \
|
||||
do { if (xp_awk_putsrcstrx (awk, str, len) == -1) return- 1; } while (0)
|
||||
|
||||
#define PRINT_TABS(awk,depth) \
|
||||
do { if (__print_tabs(awk,depth) == -1) return -1; } while (0)
|
||||
|
||||
#define PRINT_EXPRESSION(awk,nde) \
|
||||
do { if (__print_expression(awk,nde) == -1) return -1; } while (0)
|
||||
|
||||
#define PRINT_EXPRESSION_LIST(awk,nde) \
|
||||
do { if (__print_expression_list(awk,nde) == -1) return -1; } while (0)
|
||||
|
||||
#define PRINT_STATEMENTS(awk,nde,depth) \
|
||||
do { if (__print_statements(awk,nde,depth) == -1) return -1; } while (0)
|
||||
|
||||
static int __print_tabs (xp_awk_t* awk, int depth);
|
||||
static int __print_expression (xp_awk_t* awk, xp_awk_nde_t* nde);
|
||||
static int __print_expression_list (xp_awk_t* awk, xp_awk_nde_t* tree);
|
||||
static int __print_statements (xp_awk_t* awk, xp_awk_nde_t* tree, int depth);
|
||||
|
||||
static int __print_tabs (xp_awk_t* awk, int depth)
|
||||
{
|
||||
while (depth > 0)
|
||||
{
|
||||
xp_printf (XP_T("\t"));
|
||||
PUT_SRCSTR (awk, XP_T("\t"));
|
||||
depth--;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __print_expression (xp_awk_nde_t* nde)
|
||||
static int __print_expression (xp_awk_t* awk, xp_awk_nde_t* nde)
|
||||
{
|
||||
xp_char_t tmp[128];
|
||||
|
||||
switch (nde->type)
|
||||
{
|
||||
case XP_AWK_NDE_GRP:
|
||||
{
|
||||
xp_awk_nde_t* p = ((xp_awk_nde_grp_t*)nde)->body;
|
||||
|
||||
xp_printf (XP_T("("));
|
||||
PUT_SRCSTR (awk, XP_T("("));
|
||||
while (p != XP_NULL)
|
||||
{
|
||||
__print_expression (p);
|
||||
if (p->next != XP_NULL) xp_printf (XP_T(","));
|
||||
PRINT_EXPRESSION (awk, p);
|
||||
if (p->next != XP_NULL)
|
||||
PUT_SRCSTR (awk, XP_T(","));
|
||||
p = p->next;
|
||||
}
|
||||
xp_printf (XP_T(")"));
|
||||
PUT_SRCSTR (awk, XP_T(")"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -123,9 +146,12 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
{
|
||||
xp_awk_nde_ass_t* px = (xp_awk_nde_ass_t*)nde;
|
||||
|
||||
if (__print_expression (px->left) == -1) return -1;
|
||||
xp_printf (XP_T(" %s "), __assop_str[px->opcode]);
|
||||
if (__print_expression (px->right) == -1) return -1;
|
||||
PRINT_EXPRESSION (awk, px->left);
|
||||
PUT_SRCSTR (awk, XP_T(" "));
|
||||
PUT_SRCSTR (awk, __assop_str[px->opcode]);
|
||||
PUT_SRCSTR (awk, XP_T(" "));
|
||||
PRINT_EXPRESSION (awk, px->right);
|
||||
|
||||
xp_assert (px->right->next == XP_NULL);
|
||||
break;
|
||||
}
|
||||
@ -134,15 +160,21 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
{
|
||||
xp_awk_nde_exp_t* px = (xp_awk_nde_exp_t*)nde;
|
||||
|
||||
xp_printf (XP_T("("));
|
||||
if (__print_expression(px->left) == -1) return -1;
|
||||
PUT_SRCSTR (awk, XP_T("("));
|
||||
PRINT_EXPRESSION (awk, px->left);
|
||||
xp_assert (px->left->next == XP_NULL);
|
||||
xp_printf (XP_T(" %s "), __binop_str[px->opcode]);
|
||||
if (px->right->type == XP_AWK_NDE_ASS) xp_printf (XP_T("("));
|
||||
if (__print_expression (px->right) == -1) return -1;
|
||||
if (px->right->type == XP_AWK_NDE_ASS) xp_printf (XP_T(")"));
|
||||
|
||||
PUT_SRCSTR (awk, XP_T(" "));
|
||||
PUT_SRCSTR (awk, __binop_str[px->opcode]);
|
||||
PUT_SRCSTR (awk, XP_T(" "));
|
||||
|
||||
if (px->right->type == XP_AWK_NDE_ASS)
|
||||
PUT_SRCSTR (awk, XP_T("("));
|
||||
PRINT_EXPRESSION (awk, px->right);
|
||||
if (px->right->type == XP_AWK_NDE_ASS)
|
||||
PUT_SRCSTR (awk, XP_T(")"));
|
||||
xp_assert (px->right->next == XP_NULL);
|
||||
xp_printf (XP_T(")"));
|
||||
PUT_SRCSTR (awk, XP_T(")"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -151,9 +183,10 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
xp_awk_nde_exp_t* px = (xp_awk_nde_exp_t*)nde;
|
||||
xp_assert (px->right == XP_NULL);
|
||||
|
||||
xp_printf (XP_T("%s("), __unrop_str[px->opcode]);
|
||||
if (__print_expression (px->left) == -1) return -1;
|
||||
xp_printf (XP_T(")"));
|
||||
PUT_SRCSTR (awk, __unrop_str[px->opcode]);
|
||||
PUT_SRCSTR (awk, XP_T("("));
|
||||
PRINT_EXPRESSION (awk, px->left);
|
||||
PUT_SRCSTR (awk, XP_T(")"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -162,9 +195,10 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
xp_awk_nde_exp_t* px = (xp_awk_nde_exp_t*)nde;
|
||||
xp_assert (px->right == XP_NULL);
|
||||
|
||||
xp_printf (XP_T("%s("), __incop_str[px->opcode]);
|
||||
if (__print_expression (px->left) == -1) return -1;
|
||||
xp_printf (XP_T(")"));
|
||||
PUT_SRCSTR (awk, __incop_str[px->opcode]);
|
||||
PUT_SRCSTR (awk, XP_T("("));
|
||||
PRINT_EXPRESSION (awk, px->left);
|
||||
PUT_SRCSTR (awk, XP_T(")"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -173,9 +207,10 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
xp_awk_nde_exp_t* px = (xp_awk_nde_exp_t*)nde;
|
||||
xp_assert (px->right == XP_NULL);
|
||||
|
||||
xp_printf (XP_T("("));
|
||||
if (__print_expression (px->left) == -1) return -1;
|
||||
xp_printf (XP_T(")%s"), __incop_str[px->opcode]);
|
||||
PUT_SRCSTR (awk, XP_T("("));
|
||||
PRINT_EXPRESSION (awk, px->left);
|
||||
PUT_SRCSTR (awk, XP_T(")"));
|
||||
PUT_SRCSTR (awk, __incop_str[px->opcode]);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -183,52 +218,63 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
{
|
||||
xp_awk_nde_cnd_t* px = (xp_awk_nde_cnd_t*)nde;
|
||||
|
||||
xp_printf (XP_T("("));
|
||||
if (__print_expression(px->test) == -1) return -1;
|
||||
xp_printf (XP_T(")?"));
|
||||
PUT_SRCSTR (awk, XP_T("("));
|
||||
PRINT_EXPRESSION (awk, px->test);
|
||||
PUT_SRCSTR (awk, XP_T(")?"));
|
||||
|
||||
if (__print_expression(px->left) == -1) return -1;
|
||||
xp_printf (XP_T(":"));
|
||||
if (__print_expression(px->right) == -1) return -1;
|
||||
PRINT_EXPRESSION (awk, px->left);
|
||||
PUT_SRCSTR (awk, XP_T(":"));
|
||||
PRINT_EXPRESSION (awk, px->right);
|
||||
break;
|
||||
}
|
||||
|
||||
case XP_AWK_NDE_INT:
|
||||
{
|
||||
#if defined(__LCC__)
|
||||
xp_printf (XP_T("%lld"),
|
||||
xp_sprintf (tmp, xp_countof(tmp), XP_T("%lld"),
|
||||
(long long)((xp_awk_nde_int_t*)nde)->val);
|
||||
#elif defined(__BORLANDC__) || defined(_MSC_VER)
|
||||
xp_printf (XP_T("%I64d"),
|
||||
xp_sprintf (tmp, xp_countof(tmp), XP_T("%I64d"),
|
||||
(__int64)((xp_awk_nde_int_t*)nde)->val);
|
||||
#elif defined(vax) || defined(__vax) || defined(_SCO_DS)
|
||||
xp_printf (XP_T("%ld"),
|
||||
xp_sprintf (tmp, xp_countof(tmp), XP_T("%ld"),
|
||||
(long)((xp_awk_nde_int_t*)nde)->val);
|
||||
#else
|
||||
xp_printf (XP_T("%lld"),
|
||||
xp_sprintf (tmp, xp_countof(tmp), XP_T("%lld"),
|
||||
(long long)((xp_awk_nde_int_t*)nde)->val);
|
||||
#endif
|
||||
|
||||
PUT_SRCSTR (awk, tmp);
|
||||
break;
|
||||
}
|
||||
|
||||
case XP_AWK_NDE_REAL:
|
||||
{
|
||||
xp_printf (XP_T("%Lf"),
|
||||
xp_sprintf (tmp, xp_countof(tmp), XP_T("%Lf"),
|
||||
(long double)((xp_awk_nde_real_t*)nde)->val);
|
||||
PUT_SRCSTR (awk, tmp);
|
||||
break;
|
||||
}
|
||||
|
||||
case XP_AWK_NDE_STR:
|
||||
{
|
||||
/* TODO: buf, len */
|
||||
xp_printf (XP_T("\"%s\""), ((xp_awk_nde_str_t*)nde)->buf);
|
||||
/* TODO: ESCAPING */
|
||||
PUT_SRCSTR (awk, XP_T("\""));
|
||||
PUT_SRCSTRX (awk,
|
||||
((xp_awk_nde_str_t*)nde)->buf,
|
||||
((xp_awk_nde_str_t*)nde)->len);
|
||||
PUT_SRCSTR (awk, XP_T("\""));
|
||||
break;
|
||||
}
|
||||
|
||||
case XP_AWK_NDE_REX:
|
||||
{
|
||||
/* TODO: buf, len */
|
||||
xp_printf (XP_T("/%s/"), ((xp_awk_nde_rex_t*)nde)->buf);
|
||||
PUT_SRCSTR (awk, XP_T("/"));
|
||||
PUT_SRCSTRX (awk,
|
||||
((xp_awk_nde_rex_t*)nde)->buf,
|
||||
((xp_awk_nde_rex_t*)nde)->len);
|
||||
PUT_SRCSTR (awk, XP_T("/"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -236,7 +282,12 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
{
|
||||
xp_awk_nde_var_t* px = (xp_awk_nde_var_t*)nde;
|
||||
xp_assert (px->id.idxa != (xp_size_t)-1);
|
||||
xp_printf (XP_T("__arg%lu"), (unsigned long)px->id.idxa);
|
||||
|
||||
PUT_SRCSTR (awk, XP_T("__param"));
|
||||
xp_sprintf (tmp, xp_countof(tmp),
|
||||
XP_T("%lu"), (unsigned long)px->id.idxa);
|
||||
PUT_SRCSTR (awk, tmp);
|
||||
|
||||
xp_assert (px->idx == XP_NULL);
|
||||
break;
|
||||
}
|
||||
@ -245,10 +296,15 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
{
|
||||
xp_awk_nde_var_t* px = (xp_awk_nde_var_t*)nde;
|
||||
xp_assert (px->id.idxa != (xp_size_t)-1);
|
||||
xp_printf (XP_T("__arg%lu["), (unsigned long)px->id.idxa);
|
||||
xp_assert (px->idx != XP_NULL);
|
||||
__print_expression_list (px->idx);
|
||||
xp_printf (XP_T("]"));
|
||||
|
||||
PUT_SRCSTR (awk, XP_T("__param"));
|
||||
xp_sprintf (tmp, xp_countof(tmp),
|
||||
XP_T("%lu"), (unsigned long)px->id.idxa);
|
||||
PUT_SRCSTR (awk, tmp);
|
||||
PUT_SRCSTR (awk, XP_T("["));
|
||||
PRINT_EXPRESSION_LIST (awk, px->idx);
|
||||
PUT_SRCSTR (awk, XP_T("]"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -256,8 +312,9 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
{
|
||||
xp_awk_nde_var_t* px = (xp_awk_nde_var_t*)nde;
|
||||
xp_assert (px->id.idxa == (xp_size_t)-1);
|
||||
xp_printf (XP_T("%s"), px->id.name);
|
||||
xp_assert (px->idx == XP_NULL);
|
||||
|
||||
PUT_SRCSTRX (awk, px->id.name, px->id.name_len);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -265,10 +322,12 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
{
|
||||
xp_awk_nde_var_t* px = (xp_awk_nde_var_t*)nde;
|
||||
xp_assert (px->id.idxa == (xp_size_t)-1);
|
||||
xp_printf (XP_T("%s["), px->id.name);
|
||||
xp_assert (px->idx != XP_NULL);
|
||||
__print_expression_list (px->idx);
|
||||
xp_printf (XP_T("]"));
|
||||
|
||||
PUT_SRCSTRX (awk, px->id.name, px->id.name_len);
|
||||
PUT_SRCSTR (awk, XP_T("["));
|
||||
PRINT_EXPRESSION_LIST (awk, px->idx);
|
||||
PUT_SRCSTR (awk, XP_T("]"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -277,12 +336,14 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
xp_awk_nde_var_t* px = (xp_awk_nde_var_t*)nde;
|
||||
if (px->id.idxa != (xp_size_t)-1)
|
||||
{
|
||||
xp_printf (XP_T("__global%lu"),
|
||||
(unsigned long)px->id.idxa);
|
||||
PUT_SRCSTR (awk, XP_T("__global"));
|
||||
xp_sprintf (tmp, xp_countof(tmp),
|
||||
XP_T("%lu"), (unsigned long)px->id.idxa);
|
||||
PUT_SRCSTR (awk, tmp);
|
||||
}
|
||||
else
|
||||
{
|
||||
xp_printf (XP_T("%s"), px->id.name);
|
||||
PUT_SRCSTRX (awk, px->id.name, px->id.name_len);
|
||||
}
|
||||
xp_assert (px->idx == XP_NULL);
|
||||
break;
|
||||
@ -293,16 +354,20 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
xp_awk_nde_var_t* px = (xp_awk_nde_var_t*)nde;
|
||||
if (px->id.idxa != (xp_size_t)-1)
|
||||
{
|
||||
xp_printf (XP_T("__global%lu["),
|
||||
(unsigned long)px->id.idxa);
|
||||
PUT_SRCSTR (awk, XP_T("__global"));
|
||||
xp_sprintf (tmp, xp_countof(tmp),
|
||||
XP_T("%lu"), (unsigned long)px->id.idxa);
|
||||
PUT_SRCSTR (awk, tmp);
|
||||
PUT_SRCSTR (awk, XP_T("["));
|
||||
}
|
||||
else
|
||||
{
|
||||
xp_printf (XP_T("%s["), px->id.name);
|
||||
PUT_SRCSTRX (awk, px->id.name, px->id.name_len);
|
||||
PUT_SRCSTR (awk, XP_T("["));
|
||||
}
|
||||
xp_assert (px->idx != XP_NULL);
|
||||
__print_expression_list (px->idx);
|
||||
xp_printf (XP_T("]"));
|
||||
PRINT_EXPRESSION_LIST (awk, px->idx);
|
||||
PUT_SRCSTR (awk, XP_T("]"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -311,12 +376,14 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
xp_awk_nde_var_t* px = (xp_awk_nde_var_t*)nde;
|
||||
if (px->id.idxa != (xp_size_t)-1)
|
||||
{
|
||||
xp_printf (XP_T("__local%lu"),
|
||||
(unsigned long)px->id.idxa);
|
||||
PUT_SRCSTR (awk, XP_T("__local"));
|
||||
xp_sprintf (tmp, xp_countof(tmp),
|
||||
XP_T("%lu"), (unsigned long)px->id.idxa);
|
||||
PUT_SRCSTR (awk, tmp);
|
||||
}
|
||||
else
|
||||
{
|
||||
xp_printf (XP_T("%s"), px->id.name);
|
||||
PUT_SRCSTRX (awk, px->id.name, px->id.name_len);
|
||||
}
|
||||
xp_assert (px->idx == XP_NULL);
|
||||
break;
|
||||
@ -327,32 +394,38 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
xp_awk_nde_var_t* px = (xp_awk_nde_var_t*)nde;
|
||||
if (px->id.idxa != (xp_size_t)-1)
|
||||
{
|
||||
xp_printf (XP_T("__local%lu["),
|
||||
(unsigned long)px->id.idxa);
|
||||
PUT_SRCSTR (awk, XP_T("__local"));
|
||||
xp_sprintf (tmp, xp_countof(tmp),
|
||||
XP_T("%lu"), (unsigned long)px->id.idxa);
|
||||
PUT_SRCSTR (awk, tmp);
|
||||
PUT_SRCSTR (awk, XP_T("["));
|
||||
}
|
||||
else
|
||||
{
|
||||
xp_printf (XP_T("%s["), px->id.name);
|
||||
PUT_SRCSTRX (awk, px->id.name, px->id.name_len);
|
||||
PUT_SRCSTR (awk, XP_T("["));
|
||||
}
|
||||
xp_assert (px->idx != XP_NULL);
|
||||
__print_expression_list (px->idx);
|
||||
xp_printf (XP_T("]"));
|
||||
PRINT_EXPRESSION_LIST (awk, px->idx);
|
||||
PUT_SRCSTR (awk, XP_T("]"));
|
||||
break;
|
||||
}
|
||||
|
||||
case XP_AWK_NDE_POS:
|
||||
{
|
||||
xp_printf (XP_T("$"));
|
||||
__print_expression (((xp_awk_nde_pos_t*)nde)->val);
|
||||
PUT_SRCSTR (awk, XP_T("$"));
|
||||
PRINT_EXPRESSION (awk, ((xp_awk_nde_pos_t*)nde)->val);
|
||||
break;
|
||||
}
|
||||
|
||||
case XP_AWK_NDE_BFN:
|
||||
{
|
||||
xp_awk_nde_call_t* px = (xp_awk_nde_call_t*)nde;
|
||||
xp_printf (XP_T("%s ("), px->what.bfn.name);
|
||||
if (__print_expression_list (px->args) == -1) return -1;
|
||||
xp_printf (XP_T(")"));
|
||||
PUT_SRCSTRX (awk,
|
||||
px->what.bfn.name, px->what.bfn.name_len);
|
||||
PUT_SRCSTR (awk, XP_T(" ("));
|
||||
PRINT_EXPRESSION_LIST (awk, px->args);
|
||||
PUT_SRCSTR (awk, XP_T(")"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -360,9 +433,11 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
{
|
||||
/* TODO: use px->what.afn.name_len */
|
||||
xp_awk_nde_call_t* px = (xp_awk_nde_call_t*)nde;
|
||||
xp_printf (XP_T("%s ("), px->what.afn.name);
|
||||
if (__print_expression_list (px->args) == -1) return -1;
|
||||
xp_printf (XP_T(")"));
|
||||
PUT_SRCSTRX (awk,
|
||||
px->what.afn.name, px->what.afn.name_len);
|
||||
PUT_SRCSTR (awk, XP_T(" ("));
|
||||
PRINT_EXPRESSION_LIST (awk, px->args);
|
||||
PUT_SRCSTR (awk, XP_T(")"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -373,24 +448,26 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
(px->in_type == XP_AWK_IN_PIPE ||
|
||||
px->in_type == XP_AWK_IN_COPROC))
|
||||
{
|
||||
__print_expression (px->in);
|
||||
xp_printf (XP_T(" %s "),
|
||||
__getline_inop_str[px->in_type]);
|
||||
PRINT_EXPRESSION (awk, px->in);
|
||||
PUT_SRCSTR (awk, XP_T(" "));
|
||||
PUT_SRCSTR (awk, __getline_inop_str[px->in_type]);
|
||||
PUT_SRCSTR (awk, XP_T(" "));
|
||||
}
|
||||
|
||||
xp_printf (XP_T("getline"));
|
||||
PUT_SRCSTR (awk, XP_T("getline"));
|
||||
if (px->var != XP_NULL)
|
||||
{
|
||||
xp_printf (XP_T(" "));
|
||||
__print_expression (px->var);
|
||||
PUT_SRCSTR (awk, XP_T(" "));
|
||||
PRINT_EXPRESSION (awk, px->var);
|
||||
}
|
||||
|
||||
if (px->in != XP_NULL &&
|
||||
px->in_type == XP_AWK_IN_FILE)
|
||||
{
|
||||
xp_printf (XP_T(" %s "),
|
||||
__getline_inop_str[px->in_type]);
|
||||
__print_expression (px->in);
|
||||
PUT_SRCSTR (awk, XP_T(" "));
|
||||
PUT_SRCSTR (awk, __getline_inop_str[px->in_type]);
|
||||
PUT_SRCSTR (awk, XP_T(" "));
|
||||
PRINT_EXPRESSION (awk, px->in);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -404,23 +481,24 @@ static int __print_expression (xp_awk_nde_t* nde)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __print_expression_list (xp_awk_nde_t* tree)
|
||||
static int __print_expression_list (xp_awk_t* awk, xp_awk_nde_t* tree)
|
||||
{
|
||||
xp_awk_nde_t* p = tree;
|
||||
|
||||
while (p != XP_NULL)
|
||||
{
|
||||
if (__print_expression(p) == -1) return -1;
|
||||
PRINT_EXPRESSION (awk, p);
|
||||
p = p->next;
|
||||
if (p != XP_NULL) xp_printf (XP_T(","));
|
||||
if (p != XP_NULL) PUT_SRCSTR (awk, XP_T(","));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __print_statements (xp_awk_nde_t* tree, int depth)
|
||||
static int __print_statements (xp_awk_t* awk, xp_awk_nde_t* tree, int depth)
|
||||
{
|
||||
xp_awk_nde_t* p = tree;
|
||||
xp_char_t tmp[128];
|
||||
xp_size_t i;
|
||||
|
||||
while (p != XP_NULL)
|
||||
@ -430,8 +508,8 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
|
||||
{
|
||||
case XP_AWK_NDE_NULL:
|
||||
{
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T(";\n"));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T(";\n"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -439,24 +517,31 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
|
||||
{
|
||||
xp_awk_nde_blk_t* px = (xp_awk_nde_blk_t*)p;
|
||||
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("{\n"));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("{\n"));
|
||||
|
||||
if (px->nlocals > 0)
|
||||
{
|
||||
__print_tabs (depth + 1);
|
||||
xp_printf (XP_T("local "));
|
||||
PRINT_TABS (awk, depth + 1);
|
||||
PUT_SRCSTR (awk, XP_T("local "));
|
||||
|
||||
for (i = 0; i < px->nlocals - 1; i++)
|
||||
{
|
||||
xp_printf (XP_T("__local%lu, "), (unsigned long)i);
|
||||
}
|
||||
xp_printf (XP_T("__local%lu;\n"), (unsigned long)i);
|
||||
PUT_SRCSTR (awk, XP_T("__local"));
|
||||
xp_sprintf (tmp, xp_countof(tmp), XP_T("%lu"), (unsigned long)i);
|
||||
PUT_SRCSTR (awk, tmp);
|
||||
PUT_SRCSTR (awk, XP_T(", "));
|
||||
}
|
||||
|
||||
__print_statements (px->body, depth + 1);
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("}\n"));
|
||||
PUT_SRCSTR (awk, XP_T("__local"));
|
||||
xp_sprintf (tmp, xp_countof(tmp), XP_T("%lu"), (unsigned long)i);
|
||||
PUT_SRCSTR (awk, tmp);
|
||||
PUT_SRCSTR (awk, XP_T(";\n"));
|
||||
}
|
||||
|
||||
PRINT_STATEMENTS (awk, px->body, depth + 1);
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("}\n"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -464,25 +549,25 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
|
||||
{
|
||||
xp_awk_nde_if_t* px = (xp_awk_nde_if_t*)p;
|
||||
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("if ("));
|
||||
__print_expression (px->test);
|
||||
xp_printf (XP_T(")\n"));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("if ("));
|
||||
PRINT_EXPRESSION (awk, px->test);
|
||||
PUT_SRCSTR (awk, XP_T(")\n"));
|
||||
|
||||
xp_assert (px->then_part != XP_NULL);
|
||||
if (px->then_part->type == XP_AWK_NDE_BLK)
|
||||
__print_statements (px->then_part, depth);
|
||||
PRINT_STATEMENTS (awk, px->then_part, depth);
|
||||
else
|
||||
__print_statements (px->then_part, depth + 1);
|
||||
PRINT_STATEMENTS (awk, px->then_part, depth + 1);
|
||||
|
||||
if (px->else_part != XP_NULL)
|
||||
{
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("else\n"));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("else\n"));
|
||||
if (px->else_part->type == XP_AWK_NDE_BLK)
|
||||
__print_statements (px->else_part, depth);
|
||||
PRINT_STATEMENTS (awk, px->else_part, depth);
|
||||
else
|
||||
__print_statements (px->else_part, depth + 1);
|
||||
PRINT_STATEMENTS (awk, px->else_part, depth + 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -491,17 +576,17 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
|
||||
{
|
||||
xp_awk_nde_while_t* px = (xp_awk_nde_while_t*)p;
|
||||
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("while ("));
|
||||
__print_expression (px->test);
|
||||
xp_printf (XP_T(")\n"));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("while ("));
|
||||
PRINT_EXPRESSION (awk, px->test);
|
||||
PUT_SRCSTR (awk, XP_T(")\n"));
|
||||
if (px->body->type == XP_AWK_NDE_BLK)
|
||||
{
|
||||
__print_statements (px->body, depth);
|
||||
PRINT_STATEMENTS (awk, px->body, depth);
|
||||
}
|
||||
else
|
||||
{
|
||||
__print_statements (px->body, depth + 1);
|
||||
PRINT_STATEMENTS (awk, px->body, depth + 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -510,21 +595,21 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
|
||||
{
|
||||
xp_awk_nde_while_t* px = (xp_awk_nde_while_t*)p;
|
||||
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("do\n"));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("do\n"));
|
||||
if (px->body->type == XP_AWK_NDE_BLK)
|
||||
{
|
||||
__print_statements (px->body, depth);
|
||||
PRINT_STATEMENTS (awk, px->body, depth);
|
||||
}
|
||||
else
|
||||
{
|
||||
__print_statements (px->body, depth + 1);
|
||||
PRINT_STATEMENTS (awk, px->body, depth + 1);
|
||||
}
|
||||
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("while ("));
|
||||
__print_expression (px->test);
|
||||
xp_printf (XP_T(");\n"));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("while ("));
|
||||
PRINT_EXPRESSION (awk, px->test);
|
||||
PUT_SRCSTR (awk, XP_T(");\n"));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -532,31 +617,31 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
|
||||
{
|
||||
xp_awk_nde_for_t* px = (xp_awk_nde_for_t*)p;
|
||||
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("for ("));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("for ("));
|
||||
if (px->init != XP_NULL)
|
||||
{
|
||||
__print_expression (px->init);
|
||||
PRINT_EXPRESSION (awk, px->init);
|
||||
}
|
||||
xp_printf (XP_T("; "));
|
||||
PUT_SRCSTR (awk, XP_T("; "));
|
||||
if (px->test != XP_NULL)
|
||||
{
|
||||
__print_expression (px->test);
|
||||
PRINT_EXPRESSION (awk, px->test);
|
||||
}
|
||||
xp_printf (XP_T("; "));
|
||||
PUT_SRCSTR (awk, XP_T("; "));
|
||||
if (px->incr != XP_NULL)
|
||||
{
|
||||
__print_expression (px->incr);
|
||||
PRINT_EXPRESSION (awk, px->incr);
|
||||
}
|
||||
xp_printf (XP_T(")\n"));
|
||||
PUT_SRCSTR (awk, XP_T(")\n"));
|
||||
|
||||
if (px->body->type == XP_AWK_NDE_BLK)
|
||||
{
|
||||
__print_statements (px->body, depth);
|
||||
PRINT_STATEMENTS (awk, px->body, depth);
|
||||
}
|
||||
else
|
||||
{
|
||||
__print_statements (px->body, depth + 1);
|
||||
PRINT_STATEMENTS (awk, px->body, depth + 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -565,55 +650,49 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
|
||||
{
|
||||
xp_awk_nde_foreach_t* px = (xp_awk_nde_foreach_t*)p;
|
||||
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("for "));
|
||||
__print_expression (px->test);
|
||||
xp_printf (XP_T("\n"));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("for "));
|
||||
PRINT_EXPRESSION (awk, px->test);
|
||||
PUT_SRCSTR (awk, XP_T("\n"));
|
||||
if (px->body->type == XP_AWK_NDE_BLK)
|
||||
{
|
||||
__print_statements (px->body, depth);
|
||||
PRINT_STATEMENTS (awk, px->body, depth);
|
||||
}
|
||||
else
|
||||
{
|
||||
__print_statements (px->body, depth + 1);
|
||||
PRINT_STATEMENTS (awk, px->body, depth + 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case XP_AWK_NDE_BREAK:
|
||||
{
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("break;\n"));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("break;\n"));
|
||||
break;
|
||||
}
|
||||
|
||||
case XP_AWK_NDE_CONTINUE:
|
||||
{
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("continue;\n"));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("continue;\n"));
|
||||
break;
|
||||
}
|
||||
|
||||
case XP_AWK_NDE_RETURN:
|
||||
{
|
||||
__print_tabs (depth);
|
||||
PRINT_TABS (awk, depth);
|
||||
if (((xp_awk_nde_return_t*)p)->val == XP_NULL)
|
||||
{
|
||||
xp_printf (XP_T("return;\n"));
|
||||
PUT_SRCSTR (awk, XP_T("return;\n"));
|
||||
}
|
||||
else
|
||||
{
|
||||
xp_printf (XP_T("return "));
|
||||
PUT_SRCSTR (awk, XP_T("return "));
|
||||
xp_assert (((xp_awk_nde_return_t*)p)->val->next == XP_NULL);
|
||||
if (__print_expression(((xp_awk_nde_return_t*)p)->val) == 0)
|
||||
{
|
||||
xp_printf (XP_T(";\n"));
|
||||
}
|
||||
else
|
||||
{
|
||||
xp_awk_nde_return_t* x = (xp_awk_nde_return_t*)p;
|
||||
xp_printf (XP_T("***INTERNAL ERROR: unknown nde type - %d\n"), x->type);
|
||||
}
|
||||
|
||||
PRINT_EXPRESSION (awk, ((xp_awk_nde_return_t*)p)->val);
|
||||
PUT_SRCSTR (awk, XP_T(";\n"));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -621,47 +700,42 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
|
||||
case XP_AWK_NDE_EXIT:
|
||||
{
|
||||
xp_awk_nde_exit_t* px = (xp_awk_nde_exit_t*)p;
|
||||
__print_tabs (depth);
|
||||
PRINT_TABS (awk, depth);
|
||||
|
||||
if (px->val == XP_NULL)
|
||||
{
|
||||
xp_printf (XP_T("exit;\n"));
|
||||
PUT_SRCSTR (awk, XP_T("exit;\n"));
|
||||
}
|
||||
else
|
||||
{
|
||||
xp_printf (XP_T("exit "));
|
||||
PUT_SRCSTR (awk, XP_T("exit "));
|
||||
xp_assert (px->val->next == XP_NULL);
|
||||
if (__print_expression(px->val) == 0)
|
||||
{
|
||||
xp_printf (XP_T(";\n"));
|
||||
}
|
||||
else
|
||||
{
|
||||
xp_printf (XP_T("***INTERNAL ERROR: unknown nde type - %d\n"), px->type);
|
||||
}
|
||||
PRINT_EXPRESSION (awk, px->val);
|
||||
PUT_SRCSTR (awk, XP_T(";\n"));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case XP_AWK_NDE_NEXT:
|
||||
{
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("next;\n"));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("next;\n"));
|
||||
break;
|
||||
}
|
||||
|
||||
case XP_AWK_NDE_NEXTFILE:
|
||||
{
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("nextfile;\n"));
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("nextfile;\n"));
|
||||
break;
|
||||
}
|
||||
|
||||
case XP_AWK_NDE_DELETE:
|
||||
{
|
||||
__print_tabs (depth);
|
||||
xp_printf (XP_T("delete "));
|
||||
xp_awk_prnpt (((xp_awk_nde_delete_t*)p)->var);
|
||||
PRINT_TABS (awk, depth);
|
||||
PUT_SRCSTR (awk, XP_T("delete "));
|
||||
/* TODO: can't use __print_expression??? */
|
||||
xp_awk_prnpt (awk, ((xp_awk_nde_delete_t*)p)->var);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -669,61 +743,60 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
|
||||
{
|
||||
xp_awk_nde_print_t* px = (xp_awk_nde_print_t*)p;
|
||||
|
||||
__print_tabs (depth);
|
||||
PRINT_TABS (awk, depth);
|
||||
|
||||
xp_printf (XP_T("print"));
|
||||
PUT_SRCSTR (awk, XP_T("print"));
|
||||
if (px->args != XP_NULL)
|
||||
{
|
||||
xp_printf (XP_T(" "));
|
||||
__print_expression_list (px->args);
|
||||
PUT_SRCSTR (awk, XP_T(" "));
|
||||
PRINT_EXPRESSION_LIST (awk, px->args);
|
||||
}
|
||||
|
||||
if (px->out != XP_NULL)
|
||||
{
|
||||
xp_printf (XP_T(" %s "),
|
||||
__print_outop_str[px->out_type]);
|
||||
__print_expression (px->out);
|
||||
PUT_SRCSTR (awk, XP_T(" "));
|
||||
PUT_SRCSTR (awk, __print_outop_str[px->out_type]);
|
||||
PUT_SRCSTR (awk, XP_T(" "));
|
||||
PRINT_EXPRESSION (awk, px->out);
|
||||
}
|
||||
|
||||
xp_printf (XP_T(";\n"));
|
||||
PUT_SRCSTR (awk, XP_T(";\n"));
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
__print_tabs (depth);
|
||||
if (__print_expression(p) == 0)
|
||||
{
|
||||
xp_printf (XP_T(";\n"));
|
||||
}
|
||||
else
|
||||
{
|
||||
xp_printf (XP_T("***INTERNAL ERROR: unknown type - %d\n"), p->type);
|
||||
}
|
||||
PRINT_TABS (awk, depth);
|
||||
PRINT_EXPRESSION (awk, p);
|
||||
PUT_SRCSTR (awk, XP_T(";\n"));
|
||||
}
|
||||
}
|
||||
|
||||
p = p->next;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void xp_awk_prnpt (xp_awk_nde_t* tree)
|
||||
int xp_awk_prnpt (xp_awk_t* awk, xp_awk_nde_t* tree)
|
||||
{
|
||||
__print_statements (tree, 0);
|
||||
return __print_statements (awk, tree, 0);
|
||||
}
|
||||
|
||||
void xp_awk_prnptnpt (xp_awk_nde_t* tree)
|
||||
int xp_awk_prnptnpt (xp_awk_t* awk, xp_awk_nde_t* tree)
|
||||
{
|
||||
xp_awk_nde_t* nde = tree;
|
||||
|
||||
while (nde != XP_NULL)
|
||||
{
|
||||
__print_expression (nde);
|
||||
if (__print_expression (awk, nde) == -1) return -1;
|
||||
if (nde->next == XP_NULL) break;
|
||||
|
||||
xp_printf (XP_T(","));
|
||||
PUT_SRCSTR (awk, XP_T(","));
|
||||
nde = nde->next;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void xp_awk_clrpt (xp_awk_nde_t* tree)
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: tree.h,v 1.66 2006-08-06 08:15:29 bacon Exp $
|
||||
* $Id: tree.h,v 1.67 2006-08-06 15:02:55 bacon Exp $
|
||||
*/
|
||||
|
||||
#ifndef _XP_AWK_TREE_H_
|
||||
@ -362,8 +362,9 @@ struct xp_awk_nde_print_t
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void xp_awk_prnpt (xp_awk_nde_t* tree);
|
||||
void xp_awk_prnptnpt (xp_awk_nde_t* tree);
|
||||
int xp_awk_prnpt (xp_awk_t* awk, xp_awk_nde_t* tree);
|
||||
int xp_awk_prnptnpt (xp_awk_t* awk, xp_awk_nde_t* tree);
|
||||
|
||||
void xp_awk_clrpt (xp_awk_nde_t* tree);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: awk.c,v 1.68 2006-08-06 08:16:03 bacon Exp $
|
||||
* $Id: awk.c,v 1.69 2006-08-06 15:03:42 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk.h>
|
||||
@ -153,11 +153,6 @@ static xp_ssize_t process_source (
|
||||
return 0;
|
||||
}
|
||||
|
||||
case XP_AWK_IO_NEXT:
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
case XP_AWK_IO_READ:
|
||||
{
|
||||
if (size <= 0) return -1;
|
||||
@ -170,17 +165,47 @@ static xp_ssize_t process_source (
|
||||
*data = c;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static xp_ssize_t dump_source (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t size)
|
||||
{
|
||||
struct src_io* src_io = (struct src_io*)arg;
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case XP_AWK_IO_OPEN:
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
case XP_AWK_IO_CLOSE:
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
case XP_AWK_IO_WRITE:
|
||||
{
|
||||
xp_printf (XP_T("XP_AWK_IO_WRITE CALLED FOR SOURCE\n"));
|
||||
return -1;
|
||||
xp_size_t i;
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
#ifdef XP_CHAR_IS_MCHAR
|
||||
fputc (data[i], stdout);
|
||||
#else
|
||||
fputwc (data[i], stdout);
|
||||
#endif
|
||||
}
|
||||
return size;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
static xp_ssize_t process_extio_pipe (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t size)
|
||||
{
|
||||
@ -601,7 +626,7 @@ static int __main (int argc, xp_char_t* argv[])
|
||||
xp_awk_setopt (awk, opt);
|
||||
|
||||
srcios.in = process_source;
|
||||
srcios.out = XP_NULL;
|
||||
srcios.out = dump_source;
|
||||
srcios.custom_data = &src_io;
|
||||
|
||||
if (xp_awk_parse (awk, &srcios) == -1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user