restructured runtime context functions to support more flexibility
- New functions include: qse_awk_rtx_open () qse_awk_rtx_close () qse_awk_rtx_loop () qse_awk_rtx_call () - deprecated qse_awk_run ()
This commit is contained in:
parent
0f4fcd737e
commit
44d4f890f4
@ -731,8 +731,7 @@ static void print_usage (const qse_char_t* argv0)
|
||||
if (base == QSE_NULL) base = qse_strrchr(argv0, QSE_T('\\'));
|
||||
if (base == QSE_NULL) base = argv0; else base++;
|
||||
|
||||
qse_printf (QSE_T("Usage: %s [-m main] [-si file]? [-so file]? [-ci file]* [-co file]* [-a arg]* [-w o:n]* \n"), base);
|
||||
qse_printf (QSE_T(" -m main Specify the main function name\n"));
|
||||
qse_printf (QSE_T("Usage: %s [-si file]? [-so file]? [-ci file]* [-co file]* [-a arg]* [-w o:n]* \n"), base);
|
||||
qse_printf (QSE_T(" -si file Specify the input source file\n"));
|
||||
qse_printf (QSE_T(" The source code is read from stdin when it is not specified\n"));
|
||||
qse_printf (QSE_T(" -so file Specify the output source file\n"));
|
||||
@ -758,7 +757,6 @@ static int awk_main (int argc, qse_char_t* argv[])
|
||||
TestAwk awk;
|
||||
|
||||
int mode = 0;
|
||||
const qse_char_t* mainfn = NULL;
|
||||
const qse_char_t* srcin = QSE_T("");
|
||||
const qse_char_t* srcout = NULL;
|
||||
const qse_char_t* args[256];
|
||||
@ -781,8 +779,7 @@ static int awk_main (int argc, qse_char_t* argv[])
|
||||
else if (qse_strcmp(argv[i], QSE_T("-ci")) == 0) mode = 3;
|
||||
else if (qse_strcmp(argv[i], QSE_T("-co")) == 0) mode = 4;
|
||||
else if (qse_strcmp(argv[i], QSE_T("-a")) == 0) mode = 5;
|
||||
else if (qse_strcmp(argv[i], QSE_T("-m")) == 0) mode = 6;
|
||||
else if (qse_strcmp(argv[i], QSE_T("-w")) == 0) mode = 7;
|
||||
else if (qse_strcmp(argv[i], QSE_T("-w")) == 0) mode = 6;
|
||||
else if (qse_strcmp(argv[i], QSE_T("-v")) == 0)
|
||||
{
|
||||
verbose = true;
|
||||
@ -887,18 +884,7 @@ static int awk_main (int argc, qse_char_t* argv[])
|
||||
args[nargs++] = argv[i];
|
||||
mode = 0;
|
||||
}
|
||||
else if (mode == 6) // entry point
|
||||
{
|
||||
if (mainfn != NULL)
|
||||
{
|
||||
print_usage (argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
mainfn = argv[i];
|
||||
mode = 0;
|
||||
}
|
||||
else if (mode == 7) // word replacement
|
||||
else if (mode == 6) // word replacement
|
||||
{
|
||||
const qse_char_t* p;
|
||||
qse_size_t l;
|
||||
@ -938,7 +924,7 @@ static int awk_main (int argc, qse_char_t* argv[])
|
||||
|
||||
awk.enableRunCallback ();
|
||||
|
||||
if (awk.run (mainfn, args, nargs) == -1)
|
||||
if (awk.run (args, nargs) == -1)
|
||||
{
|
||||
qse_fprintf (stderr, QSE_T("cannot run: LINE[%d] %s\n"),
|
||||
awk.getErrorLine(), awk.getErrorMessage());
|
||||
|
@ -587,7 +587,7 @@ static int awk_main (int argc, qse_char_t* argv[])
|
||||
{
|
||||
qse_awk_t* awk;
|
||||
|
||||
qse_awk_runcbs_t runcbs;
|
||||
qse_awk_rcb_t rcb;
|
||||
|
||||
int i;
|
||||
int runarg_count = 0;
|
||||
@ -625,14 +625,14 @@ static int awk_main (int argc, qse_char_t* argv[])
|
||||
goto oops;
|
||||
}
|
||||
|
||||
runcbs.on_start = on_run_start;
|
||||
runcbs.on_enter = on_run_enter;
|
||||
runcbs.on_statement = on_run_statement;
|
||||
runcbs.on_exit = on_run_exit;
|
||||
runcbs.on_end = on_run_end;
|
||||
runcbs.data = &ao;
|
||||
rcb.on_start = on_run_start;
|
||||
rcb.on_enter = on_run_enter;
|
||||
rcb.on_statement = on_run_statement;
|
||||
rcb.on_exit = on_run_exit;
|
||||
rcb.on_end = on_run_end;
|
||||
rcb.data = &ao;
|
||||
|
||||
if (qse_awk_runsimple (awk, ao.icf, &runcbs) == -1)
|
||||
if (qse_awk_runsimple (awk, ao.icf, &rcb) == -1)
|
||||
{
|
||||
qse_printf (
|
||||
QSE_T("RUN ERROR: CODE [%d] LINE [%u] %s\n"),
|
||||
|
@ -875,9 +875,6 @@ public:
|
||||
*
|
||||
* This method executes the parse tree formed by Awk::parse.
|
||||
*
|
||||
* @param main Name of an entry point.
|
||||
* If it is set, Awk::run executes the function of the specified
|
||||
* name instead of entering BEGIN/pattern/END blocks.
|
||||
* @param args Pointer to an array of character strings.
|
||||
* If it is specified, the charater strings are passed to
|
||||
* an AWK program. The values can be accesed with ARGC & ARGV
|
||||
@ -897,8 +894,7 @@ public:
|
||||
* with Awk::enableRunCallback and Awk::disableRunCallback.
|
||||
* Call Awk::getErrorCode to get extended error information.
|
||||
*/
|
||||
virtual int run (const char_t* main = QSE_NULL,
|
||||
const char_t** args = QSE_NULL, size_t nargs = 0);
|
||||
virtual int run (const char_t** args = QSE_NULL, size_t nargs = 0);
|
||||
|
||||
/**
|
||||
* Requests aborting execution of the parse tree
|
||||
@ -1128,7 +1124,7 @@ private:
|
||||
|
||||
mmgr_t mmgr;
|
||||
ccls_t ccls;
|
||||
qse_awk_prmfns_t prmfns;
|
||||
qse_awk_prm_t prm;
|
||||
};
|
||||
|
||||
/////////////////////////////////
|
||||
|
@ -35,7 +35,7 @@ class StdAwk: public Awk
|
||||
public:
|
||||
StdAwk ();
|
||||
int open ();
|
||||
int run (const char_t* main, const char_t** args, size_t nargs);
|
||||
int run (const char_t** args, size_t nargs);
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -52,10 +52,10 @@ typedef struct qse_awk_rtx_t qse_awk_rtx_t; /* (R)untime con(T)e(X)t */
|
||||
typedef struct qse_awk_val_t qse_awk_val_t;
|
||||
typedef struct qse_awk_eio_t qse_awk_eio_t; /* (E)xternal (IO) */
|
||||
|
||||
typedef struct qse_awk_prmfns_t qse_awk_prmfns_t;
|
||||
typedef struct qse_awk_srcios_t qse_awk_srcios_t;
|
||||
typedef struct qse_awk_runios_t qse_awk_runios_t;
|
||||
typedef struct qse_awk_runcbs_t qse_awk_runcbs_t;
|
||||
typedef struct qse_awk_prm_t qse_awk_prm_t;
|
||||
typedef struct qse_awk_sio_t qse_awk_sio_t;
|
||||
typedef struct qse_awk_rio_t qse_awk_rio_t;
|
||||
typedef struct qse_awk_rcb_t qse_awk_rcb_t;
|
||||
typedef struct qse_awk_rexfns_t qse_awk_rexfns_t;
|
||||
|
||||
typedef qse_real_t (*qse_awk_pow_t) (
|
||||
@ -108,7 +108,7 @@ struct qse_awk_eio_t
|
||||
qse_awk_eio_t* next;
|
||||
};
|
||||
|
||||
struct qse_awk_prmfns_t
|
||||
struct qse_awk_prm_t
|
||||
{
|
||||
qse_awk_pow_t pow; /* required */
|
||||
qse_awk_sprintf_t sprintf; /* required */
|
||||
@ -117,14 +117,14 @@ struct qse_awk_prmfns_t
|
||||
void* data; /* optional */
|
||||
};
|
||||
|
||||
struct qse_awk_srcios_t
|
||||
struct qse_awk_sio_t
|
||||
{
|
||||
qse_awk_io_t in;
|
||||
qse_awk_io_t out;
|
||||
void* data;
|
||||
};
|
||||
|
||||
struct qse_awk_runios_t
|
||||
struct qse_awk_rio_t
|
||||
{
|
||||
qse_awk_io_t pipe;
|
||||
qse_awk_io_t file;
|
||||
@ -132,7 +132,7 @@ struct qse_awk_runios_t
|
||||
void* data;
|
||||
};
|
||||
|
||||
struct qse_awk_runcbs_t
|
||||
struct qse_awk_rcb_t
|
||||
{
|
||||
int (*on_start) (
|
||||
qse_awk_rtx_t* rtx, void* data);
|
||||
@ -730,24 +730,24 @@ void qse_awk_setccls (
|
||||
);
|
||||
/******/
|
||||
|
||||
/****f* AWK/qse_awk_getprmfns
|
||||
/****f* AWK/qse_awk_getprm
|
||||
* NAME
|
||||
* qse_awk_getprmfns - get primitive functions
|
||||
* qse_awk_getprm - get primitive functions
|
||||
* SYNOPSIS
|
||||
*/
|
||||
qse_awk_prmfns_t* qse_awk_getprmfns (
|
||||
qse_awk_prm_t* qse_awk_getprm (
|
||||
qse_awk_t* awk
|
||||
);
|
||||
/******/
|
||||
|
||||
/****f* AWK/qse_awk_setprmfns
|
||||
/****f* AWK/qse_awk_setprm
|
||||
* NAME
|
||||
* qse_awk_setprmfns - set primitive functions
|
||||
* qse_awk_setprm - set primitive functions
|
||||
* SYNOPSIS
|
||||
*/
|
||||
void qse_awk_setprmfns (
|
||||
qse_awk_t* awk,
|
||||
qse_awk_prmfns_t* prmfns
|
||||
void qse_awk_setprm (
|
||||
qse_awk_t* awk,
|
||||
qse_awk_prm_t* prm
|
||||
);
|
||||
/******/
|
||||
|
||||
@ -973,35 +973,8 @@ void qse_awk_clrfnc (
|
||||
* SYNOPSIS
|
||||
*/
|
||||
int qse_awk_parse (
|
||||
qse_awk_t* awk,
|
||||
qse_awk_srcios_t* srcios
|
||||
);
|
||||
/******/
|
||||
|
||||
/****f* AWK/qse_awk_run
|
||||
* NAME
|
||||
* qse_awk_run - execute a parsed program
|
||||
* DESCRIPTION
|
||||
* A runtime context is required for it to start running the program.
|
||||
* Once a runtime context is created, the program starts to run.
|
||||
* The failure of context creation is reported by the return value of -1.
|
||||
* However, the runtime error after context creation is reported differently
|
||||
* depending on the callbacks specified. When no callback is specified
|
||||
* (i.e. runcbs is QSE_NULL), the qse_awk_run() function returns -1 on an
|
||||
* error and awk->errnum is set accordingly. If a callback is specified
|
||||
* (i.e. runcbs is not QSE_NULL), the qse_awk_run() returns 0 on both success
|
||||
* and failure. Instead, the on_end handler of the callback is triggered with
|
||||
* the relevant error number. The third parameter to on_end is the error
|
||||
* number.
|
||||
* SYNOPSIS
|
||||
*/
|
||||
int qse_awk_run (
|
||||
qse_awk_t* awk,
|
||||
const qse_char_t* main,
|
||||
qse_awk_runios_t* runios,
|
||||
qse_awk_runcbs_t* runcbs,
|
||||
const qse_cstr_t* runarg,
|
||||
void* data
|
||||
qse_awk_t* awk,
|
||||
qse_awk_sio_t* sio
|
||||
);
|
||||
/******/
|
||||
|
||||
@ -1031,12 +1004,24 @@ int qse_awk_parsesimple (
|
||||
/****f* AWK/qse_awk_runsimple
|
||||
* NAME
|
||||
* qse_awk_runsimple - run a parsed program
|
||||
* DESCRIPTION
|
||||
* A runtime context is required for it to start running the program.
|
||||
* Once a runtime context is created, the program starts to run.
|
||||
* The failure of context creation is reported by the return value of -1.
|
||||
* However, the runtime error after context creation is reported differently
|
||||
* depending on the callbacks specified. When no callback is specified
|
||||
* (i.e. rcb is QSE_NULL), the qse_awk_run() function returns -1 on an
|
||||
* error and awk->errnum is set accordingly. If a callback is specified
|
||||
* (i.e. rcb is not QSE_NULL), the qse_awk_run() returns 0 on both success
|
||||
* and failure. Instead, the on_end handler of the callback is triggered with
|
||||
* the relevant error number. The third parameter to on_end is the error
|
||||
* number.
|
||||
* SYNOPSIS
|
||||
*/
|
||||
int qse_awk_runsimple (
|
||||
qse_awk_t* awk,
|
||||
qse_char_t** icf /* input console files */,
|
||||
qse_awk_runcbs_t* cbs /* callbacks */
|
||||
qse_awk_rcb_t* cbs /* callbacks */
|
||||
);
|
||||
/******/
|
||||
|
||||
@ -1124,6 +1109,55 @@ qse_size_t qse_awk_longtostr (
|
||||
qse_char_t* buf,
|
||||
qse_size_t size
|
||||
);
|
||||
|
||||
/****f* AWK/qse_awk_rtx_open
|
||||
* NAME
|
||||
* qse_awk_rtx_open - create a runtime context
|
||||
* SYNOPSIS
|
||||
*/
|
||||
qse_awk_rtx_t* qse_awk_rtx_open (
|
||||
qse_awk_t* awk,
|
||||
qse_awk_rio_t* ios,
|
||||
qse_awk_rcb_t* cbs,
|
||||
const qse_cstr_t* arg,
|
||||
void* data
|
||||
);
|
||||
/******/
|
||||
|
||||
/****f* AWK/qse_awk_rtx_close
|
||||
* NAME
|
||||
* qse_awk_rtx_close - destroy a runtime context
|
||||
* SYNOPSIS
|
||||
*/
|
||||
void qse_awk_rtx_close (
|
||||
qse_awk_rtx_t* rtx
|
||||
);
|
||||
/******/
|
||||
|
||||
/****f* AWK/qse_awk_rtx_loop
|
||||
* NAME
|
||||
* qse_awk_rtx_loop - run BEGIN/pattern-action/END blocks
|
||||
* SYNOPSIS
|
||||
*/
|
||||
int qse_awk_rtx_loop (
|
||||
qse_awk_rtx_t* rtx
|
||||
);
|
||||
/******/
|
||||
|
||||
/****f* AWK/qse_awk_rtx_call
|
||||
* NAME
|
||||
* qse_awk_rtx_call - call a function
|
||||
* SYNOPSIS
|
||||
*/
|
||||
int qse_awk_rtx_call (
|
||||
qse_awk_rtx_t* rtx,
|
||||
const qse_char_t* name,
|
||||
qse_awk_val_t** args,
|
||||
qse_size_t nargs
|
||||
);
|
||||
/******/
|
||||
|
||||
|
||||
/****f* AWK/qse_awk_stopall
|
||||
* NAME
|
||||
* qse_awk_stopall - stop all runtime contexts
|
||||
|
@ -16,7 +16,6 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
#include <qse/awk/Awk.hpp>
|
||||
#include <qse/cmn/str.h>
|
||||
#include "../cmn/mem.h"
|
||||
@ -74,14 +73,14 @@ void Awk::EIO::setHandle (void* handle)
|
||||
Awk::EIO::operator Awk::Awk* () const
|
||||
{
|
||||
// it assumes that the Awk object is set to the data field.
|
||||
// make sure that it happens in Awk::run () - runios.data = this;
|
||||
// make sure that it happens in Awk::run () - rio.data = this;
|
||||
return (Awk::Awk*)eio->data;
|
||||
}
|
||||
|
||||
Awk::EIO::operator Awk::awk_t* () const
|
||||
{
|
||||
// it assumes that the Awk object is set to the data field.
|
||||
// make sure that it happens in Awk::run () - runios.data = this;
|
||||
// make sure that it happens in Awk::run () - rio.data = this;
|
||||
return (Awk::awk_t*)(Awk::Awk*)eio->data;
|
||||
}
|
||||
|
||||
@ -1074,9 +1073,9 @@ Awk::Awk (): awk (QSE_NULL), functionMap (QSE_NULL),
|
||||
ccls.to = transCase;
|
||||
ccls.data = this;
|
||||
|
||||
prmfns.pow = pow;
|
||||
prmfns.sprintf = sprintf;
|
||||
prmfns.data = this;
|
||||
prm.pow = pow;
|
||||
prm.sprintf = sprintf;
|
||||
prm.data = this;
|
||||
}
|
||||
|
||||
Awk::~Awk ()
|
||||
@ -1187,7 +1186,7 @@ int Awk::open ()
|
||||
}
|
||||
|
||||
qse_awk_setccls (awk, &ccls);
|
||||
qse_awk_setprmfns (awk, &prmfns);
|
||||
qse_awk_setprm (awk, &prm);
|
||||
|
||||
|
||||
//functionMap = qse_map_open (
|
||||
@ -1317,44 +1316,44 @@ int Awk::parse ()
|
||||
{
|
||||
QSE_ASSERT (awk != QSE_NULL);
|
||||
|
||||
qse_awk_srcios_t srcios;
|
||||
qse_awk_sio_t sio;
|
||||
|
||||
srcios.in = sourceReader;
|
||||
srcios.out = sourceWriter;
|
||||
srcios.data = this;
|
||||
sio.in = sourceReader;
|
||||
sio.out = sourceWriter;
|
||||
sio.data = this;
|
||||
|
||||
int n = qse_awk_parse (awk, &srcios);
|
||||
int n = qse_awk_parse (awk, &sio);
|
||||
if (n == -1) retrieveError ();
|
||||
return n;
|
||||
}
|
||||
|
||||
int Awk::run (const char_t* main, const char_t** args, size_t nargs)
|
||||
int Awk::run (const char_t** args, size_t nargs)
|
||||
{
|
||||
QSE_ASSERT (awk != QSE_NULL);
|
||||
|
||||
size_t i;
|
||||
qse_awk_runios_t runios;
|
||||
qse_awk_runcbs_t runcbs;
|
||||
qse_awk_rio_t rio;
|
||||
qse_awk_rcb_t rcb;
|
||||
qse_xstr_t* runarg = QSE_NULL;
|
||||
|
||||
// make sure that the run field is set in Awk::onRunStart.
|
||||
Run runctx (this);
|
||||
|
||||
runios.pipe = pipeHandler;
|
||||
runios.file = fileHandler;
|
||||
runios.console = consoleHandler;
|
||||
runios.data = this;
|
||||
rio.pipe = pipeHandler;
|
||||
rio.file = fileHandler;
|
||||
rio.console = consoleHandler;
|
||||
rio.data = this;
|
||||
|
||||
QSE_MEMSET (&runcbs, 0, QSE_SIZEOF(runcbs));
|
||||
runcbs.on_start = onRunStart;
|
||||
QSE_MEMSET (&rcb, 0, QSE_SIZEOF(rcb));
|
||||
rcb.on_start = onRunStart;
|
||||
if (runCallback)
|
||||
{
|
||||
runcbs.on_end = onRunEnd;
|
||||
runcbs.on_enter = onRunEnter;
|
||||
runcbs.on_statement = onRunStatement;
|
||||
runcbs.on_exit = onRunExit;
|
||||
rcb.on_end = onRunEnd;
|
||||
rcb.on_enter = onRunEnter;
|
||||
rcb.on_statement = onRunStatement;
|
||||
rcb.on_exit = onRunExit;
|
||||
}
|
||||
runcbs.data = &runctx;
|
||||
rcb.data = &runctx;
|
||||
|
||||
if (nargs > 0)
|
||||
{
|
||||
@ -1385,7 +1384,7 @@ int Awk::run (const char_t* main, const char_t** args, size_t nargs)
|
||||
}
|
||||
|
||||
int n = qse_awk_run (
|
||||
awk, main, &runios, &runcbs,
|
||||
awk, &rio, &rcb,
|
||||
(qse_cstr_t*)runarg, &runctx
|
||||
);
|
||||
if (n == -1) retrieveError ();
|
||||
|
@ -72,7 +72,7 @@ int StdAwk::open ()
|
||||
return 0;
|
||||
}
|
||||
|
||||
int StdAwk::run (const char_t* main, const char_t** args, size_t nargs)
|
||||
int StdAwk::run (const char_t** args, size_t nargs)
|
||||
{
|
||||
qse_ntime_t now;
|
||||
|
||||
@ -81,7 +81,7 @@ int StdAwk::run (const char_t* main, const char_t** args, size_t nargs)
|
||||
|
||||
::srand (this->seed);
|
||||
|
||||
return Awk::run (main, args, nargs);
|
||||
return Awk::run (args, nargs);
|
||||
}
|
||||
|
||||
int StdAwk::sin (Run& run, Return& ret, const Argument* args, size_t nargs,
|
||||
|
@ -342,17 +342,16 @@ void qse_awk_setccls (qse_awk_t* awk, qse_ccls_t* ccls)
|
||||
awk->ccls = ccls;
|
||||
}
|
||||
|
||||
qse_awk_prmfns_t* qse_awk_getprmfns (qse_awk_t* awk)
|
||||
qse_awk_prm_t* qse_awk_getprm (qse_awk_t* awk)
|
||||
{
|
||||
return awk->prmfns;
|
||||
return awk->prm;
|
||||
}
|
||||
|
||||
void qse_awk_setprmfns (qse_awk_t* awk, qse_awk_prmfns_t* prmfns)
|
||||
void qse_awk_setprm (qse_awk_t* awk, qse_awk_prm_t* prm)
|
||||
{
|
||||
QSE_ASSERT (prmfns->pow != QSE_NULL);
|
||||
QSE_ASSERT (prmfns->sprintf != QSE_NULL);
|
||||
|
||||
awk->prmfns = prmfns;
|
||||
QSE_ASSERT (prm->pow != QSE_NULL);
|
||||
QSE_ASSERT (prm->sprintf != QSE_NULL);
|
||||
awk->prm = prm;
|
||||
}
|
||||
|
||||
int qse_awk_getoption (qse_awk_t* awk)
|
||||
|
@ -85,9 +85,9 @@ struct qse_awk_tree_t
|
||||
|
||||
struct qse_awk_t
|
||||
{
|
||||
qse_mmgr_t* mmgr;
|
||||
qse_ccls_t* ccls;
|
||||
qse_awk_prmfns_t* prmfns;
|
||||
qse_mmgr_t* mmgr;
|
||||
qse_ccls_t* ccls;
|
||||
qse_awk_prm_t* prm;
|
||||
|
||||
void* assoc_data;
|
||||
|
||||
@ -157,7 +157,7 @@ struct qse_awk_t
|
||||
/* source code management */
|
||||
struct
|
||||
{
|
||||
qse_awk_srcios_t ios;
|
||||
qse_awk_sio_t ios;
|
||||
|
||||
struct
|
||||
{
|
||||
@ -376,7 +376,7 @@ struct qse_awk_rtx_t
|
||||
void* data;
|
||||
|
||||
qse_awk_t* awk;
|
||||
qse_awk_runcbs_t* cbs;
|
||||
qse_awk_rcb_t* cbs;
|
||||
};
|
||||
|
||||
|
||||
|
@ -261,8 +261,8 @@ void qse_awk_seterror (
|
||||
switch (argcnt)
|
||||
{
|
||||
case 0:
|
||||
awk->prmfns->sprintf (
|
||||
awk->prmfns->data,
|
||||
awk->prm->sprintf (
|
||||
awk->prm->data,
|
||||
awk->errmsg,
|
||||
QSE_COUNTOF(awk->errmsg),
|
||||
errfmt);
|
||||
@ -289,8 +289,8 @@ void qse_awk_seterror (
|
||||
qse_strxncpy (tmp, QSE_COUNTOF(tmp), errarg[0].ptr, len);
|
||||
}
|
||||
|
||||
awk->prmfns->sprintf (
|
||||
awk->prmfns->data,
|
||||
awk->prm->sprintf (
|
||||
awk->prm->data,
|
||||
awk->errmsg,
|
||||
QSE_COUNTOF(awk->errmsg),
|
||||
errfmt, (int)len, tmp);
|
||||
@ -298,8 +298,8 @@ void qse_awk_seterror (
|
||||
}
|
||||
|
||||
case 2:
|
||||
awk->prmfns->sprintf (
|
||||
awk->prmfns->data,
|
||||
awk->prm->sprintf (
|
||||
awk->prm->data,
|
||||
awk->errmsg,
|
||||
QSE_COUNTOF(awk->errmsg),
|
||||
errfmt,
|
||||
@ -308,8 +308,8 @@ void qse_awk_seterror (
|
||||
return;
|
||||
|
||||
case 3:
|
||||
awk->prmfns->sprintf (
|
||||
awk->prmfns->data,
|
||||
awk->prm->sprintf (
|
||||
awk->prm->data,
|
||||
awk->errmsg,
|
||||
QSE_COUNTOF(awk->errmsg),
|
||||
errfmt,
|
||||
@ -319,8 +319,8 @@ void qse_awk_seterror (
|
||||
return;
|
||||
|
||||
case 4:
|
||||
awk->prmfns->sprintf (
|
||||
awk->prmfns->data,
|
||||
awk->prm->sprintf (
|
||||
awk->prm->data,
|
||||
awk->errmsg,
|
||||
QSE_COUNTOF(awk->errmsg),
|
||||
errfmt,
|
||||
@ -331,8 +331,8 @@ void qse_awk_seterror (
|
||||
return;
|
||||
|
||||
case 5:
|
||||
awk->prmfns->sprintf (
|
||||
awk->prmfns->data,
|
||||
awk->prm->sprintf (
|
||||
awk->prm->data,
|
||||
awk->errmsg,
|
||||
QSE_COUNTOF(awk->errmsg),
|
||||
errfmt,
|
||||
@ -413,8 +413,8 @@ void qse_awk_rtx_seterror (
|
||||
case 0:
|
||||
/* TODO: convert % to %% if the original % is not
|
||||
* the first % of the %% sequence */
|
||||
run->awk->prmfns->sprintf (
|
||||
run->awk->prmfns->data,
|
||||
run->awk->prm->sprintf (
|
||||
run->awk->prm->data,
|
||||
run->errmsg,
|
||||
QSE_COUNTOF(run->errmsg),
|
||||
errfmt);
|
||||
@ -444,8 +444,8 @@ void qse_awk_rtx_seterror (
|
||||
qse_strxncpy (tmp, QSE_COUNTOF(tmp), errarg[0].ptr, len);
|
||||
}
|
||||
|
||||
run->awk->prmfns->sprintf (
|
||||
run->awk->prmfns->data,
|
||||
run->awk->prm->sprintf (
|
||||
run->awk->prm->data,
|
||||
run->errmsg,
|
||||
QSE_COUNTOF(run->errmsg),
|
||||
errfmt, len, tmp);
|
||||
@ -453,8 +453,8 @@ void qse_awk_rtx_seterror (
|
||||
}
|
||||
|
||||
case 2:
|
||||
run->awk->prmfns->sprintf (
|
||||
run->awk->prmfns->data,
|
||||
run->awk->prm->sprintf (
|
||||
run->awk->prm->data,
|
||||
run->errmsg,
|
||||
QSE_COUNTOF(run->errmsg),
|
||||
errfmt,
|
||||
@ -463,8 +463,8 @@ void qse_awk_rtx_seterror (
|
||||
return;
|
||||
|
||||
case 3:
|
||||
run->awk->prmfns->sprintf (
|
||||
run->awk->prmfns->data,
|
||||
run->awk->prm->sprintf (
|
||||
run->awk->prm->data,
|
||||
run->errmsg,
|
||||
QSE_COUNTOF(run->errmsg),
|
||||
errfmt,
|
||||
@ -474,8 +474,8 @@ void qse_awk_rtx_seterror (
|
||||
return;
|
||||
|
||||
case 4:
|
||||
run->awk->prmfns->sprintf (
|
||||
run->awk->prmfns->data,
|
||||
run->awk->prm->sprintf (
|
||||
run->awk->prm->data,
|
||||
run->errmsg,
|
||||
QSE_COUNTOF(run->errmsg),
|
||||
errfmt,
|
||||
@ -486,8 +486,8 @@ void qse_awk_rtx_seterror (
|
||||
return;
|
||||
|
||||
case 5:
|
||||
run->awk->prmfns->sprintf (
|
||||
run->awk->prmfns->data,
|
||||
run->awk->prm->sprintf (
|
||||
run->awk->prm->data,
|
||||
run->errmsg,
|
||||
QSE_COUNTOF(run->errmsg),
|
||||
errfmt,
|
||||
|
@ -465,21 +465,21 @@ qse_cstr_t* qse_awk_getkw (qse_awk_t* awk, int id, qse_cstr_t* s)
|
||||
return s;
|
||||
}
|
||||
|
||||
int qse_awk_parse (qse_awk_t* awk, qse_awk_srcios_t* srcios)
|
||||
int qse_awk_parse (qse_awk_t* awk, qse_awk_sio_t* sio)
|
||||
{
|
||||
int n;
|
||||
|
||||
QSE_ASSERTX (awk->ccls != QSE_NULL, "Call qse_setccls() first");
|
||||
QSE_ASSERTX (awk->prmfns != QSE_NULL, "Call qse_setprmfns() first");
|
||||
QSE_ASSERTX (awk->prm != QSE_NULL, "Call qse_setprm() first");
|
||||
|
||||
QSE_ASSERTX (
|
||||
srcios != QSE_NULL && srcios->in != QSE_NULL,
|
||||
sio != QSE_NULL && sio->in != QSE_NULL,
|
||||
"the source code input stream must be provided at least");
|
||||
QSE_ASSERT (awk->parse.depth.cur.loop == 0);
|
||||
QSE_ASSERT (awk->parse.depth.cur.expr == 0);
|
||||
|
||||
qse_awk_clear (awk);
|
||||
QSE_MEMCPY (&awk->src.ios, srcios, QSE_SIZEOF(awk->src.ios));
|
||||
QSE_MEMCPY (&awk->src.ios, sio, QSE_SIZEOF(awk->src.ios));
|
||||
|
||||
n = parse (awk);
|
||||
|
||||
|
1234
qse/lib/awk/run.c
1234
qse/lib/awk/run.c
File diff suppressed because it is too large
Load Diff
@ -29,7 +29,7 @@
|
||||
|
||||
typedef struct xtn_t
|
||||
{
|
||||
qse_awk_prmfns_t prmfns;
|
||||
qse_awk_prm_t prm;
|
||||
} xtn_t;
|
||||
|
||||
typedef struct rxtn_t
|
||||
@ -76,10 +76,10 @@ qse_awk_t* qse_awk_opensimple (qse_size_t xtnsize)
|
||||
|
||||
xtn = (xtn_t*)((qse_byte_t*)qse_awk_getxtn(awk) + xtnsize);
|
||||
|
||||
xtn->prmfns.pow = custom_awk_pow;
|
||||
xtn->prmfns.sprintf = custom_awk_sprintf;
|
||||
xtn->prmfns.data = QSE_NULL;
|
||||
qse_awk_setprmfns (awk, &xtn->prmfns);
|
||||
xtn->prm.pow = custom_awk_pow;
|
||||
xtn->prm.sprintf = custom_awk_sprintf;
|
||||
xtn->prm.data = QSE_NULL;
|
||||
qse_awk_setprm (awk, &xtn->prm);
|
||||
|
||||
qse_awk_setoption (awk,
|
||||
QSE_AWK_IMPLICIT | QSE_AWK_EIO | QSE_AWK_NEWLINE |
|
||||
@ -288,7 +288,7 @@ int qse_awk_parsesimple (
|
||||
qse_awk_t* awk, const void* isp, int ist, const qse_char_t* osf)
|
||||
{
|
||||
sf_t sf;
|
||||
qse_awk_srcios_t sio;
|
||||
qse_awk_sio_t sio;
|
||||
|
||||
if (isp == QSE_NULL)
|
||||
{
|
||||
@ -738,12 +738,14 @@ static qse_ssize_t awk_eio_console (
|
||||
return -1;
|
||||
}
|
||||
|
||||
int qse_awk_runsimple (qse_awk_t* awk, qse_char_t** icf, qse_awk_runcbs_t* cbs)
|
||||
int qse_awk_runsimple (qse_awk_t* awk, qse_char_t** icf, qse_awk_rcb_t* rcb)
|
||||
{
|
||||
qse_awk_runios_t ios;
|
||||
qse_awk_rtx_t* rtx;
|
||||
qse_awk_rio_t ios;
|
||||
runio_data_t rd;
|
||||
rxtn_t rxtn;
|
||||
qse_ntime_t now;
|
||||
int n;
|
||||
|
||||
rd.ic.files = icf;
|
||||
rd.ic.index = 0;
|
||||
@ -757,14 +759,62 @@ int qse_awk_runsimple (qse_awk_t* awk, qse_char_t** icf, qse_awk_runcbs_t* cbs)
|
||||
else rxtn.seed = (unsigned int)now;
|
||||
srand (rxtn.seed);
|
||||
|
||||
return qse_awk_run (
|
||||
rtx = qse_awk_rtx_open (
|
||||
awk,
|
||||
QSE_NULL/*mfn*/,
|
||||
&ios,
|
||||
cbs,
|
||||
rcb,
|
||||
QSE_NULL/*runarg*/,
|
||||
&rxtn/*QSE_NULL*/
|
||||
);
|
||||
if (rtx == QSE_NULL) return -1;
|
||||
|
||||
/* execute the start callback if it exists */
|
||||
if (rcb != QSE_NULL && rcb->on_start != QSE_NULL)
|
||||
{
|
||||
n = rcb->on_start (rtx, rcb->data);
|
||||
if (n <= -1) n = -1; // TODO: something doesn't make send here...
|
||||
}
|
||||
|
||||
n = qse_awk_rtx_loop (rtx);
|
||||
if (n == -1)
|
||||
{
|
||||
/* if no callback is specified, awk's error number
|
||||
* is updated with the run's error number */
|
||||
if (rcb == QSE_NULL)
|
||||
{
|
||||
awk->errnum = rtx->errnum;
|
||||
awk->errlin = rtx->errlin;
|
||||
|
||||
qse_strxcpy (
|
||||
awk->errmsg, QSE_COUNTOF(awk->errmsg),
|
||||
rtx->errmsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
qse_awk_seterrnum (awk, QSE_AWK_ERUNTIME);
|
||||
}
|
||||
}
|
||||
|
||||
/* the run loop ended. execute the end callback if it exists */
|
||||
if (rcb != QSE_NULL && rcb->on_end != QSE_NULL)
|
||||
{
|
||||
if (n == 0)
|
||||
{
|
||||
/* clear error if run is successful just in case */
|
||||
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR);
|
||||
}
|
||||
|
||||
rcb->on_end (rtx,
|
||||
((n == -1)? rtx->errnum: QSE_AWK_ENOERR),
|
||||
rcb->data);
|
||||
|
||||
/* when using callbacks, this function always returns 0
|
||||
* after the start callbacks has been triggered */
|
||||
n = 0;
|
||||
}
|
||||
|
||||
qse_awk_rtx_close (rtx);
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
|
@ -277,13 +277,13 @@ static int print_expression (qse_awk_t* awk, qse_awk_nde_t* nde)
|
||||
if (((qse_awk_nde_real_t*)nde)->str == QSE_NULL)
|
||||
{
|
||||
#if (QSE_SIZEOF_LONG_DOUBLE != 0) && !defined(__MINGW32__)
|
||||
awk->prmfns->sprintf (
|
||||
awk->prmfns->data,
|
||||
awk->prm->sprintf (
|
||||
awk->prm->data,
|
||||
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt), QSE_T("%Lf"),
|
||||
(long double)((qse_awk_nde_real_t*)nde)->val);
|
||||
#elif (QSE_SIZEOF_DOUBLE != 0)
|
||||
awk->prmfns->sprintf (
|
||||
awk->prmfns->data,
|
||||
awk->prm->sprintf (
|
||||
awk->prm->data,
|
||||
awk->tmp.fmt, QSE_COUNTOF(awk->tmp.fmt), QSE_T("%f"),
|
||||
(double)((qse_awk_nde_real_t*)nde)->val);
|
||||
#else
|
||||
|
Loading…
x
Reference in New Issue
Block a user