enhanced and fixed a few functions.
- qse_awk_rtx_call() - qse_awk_parsesimple() - qse_awk_parsesimple_in_t - qse_awk_parsesimple_out_t
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/include
|
||||
|
||||
bin_PROGRAMS = awk01 awk02 awk03
|
||||
bin_PROGRAMS = awk01 awk02 awk03 awk04
|
||||
|
||||
LDFLAGS = -L../../lib/cmn -L../../lib/utl -L../../lib/awk
|
||||
LDADD = -lqseawk -lqseutl -lqsecmn $(LIBM)
|
||||
@ -8,3 +8,4 @@ LDADD = -lqseawk -lqseutl -lqsecmn $(LIBM)
|
||||
awk01_SOURCES = awk01.c
|
||||
awk02_SOURCES = awk02.c
|
||||
awk03_SOURCES = awk03.c
|
||||
awk04_SOURCES = awk04.c
|
||||
|
@ -32,7 +32,8 @@ PRE_UNINSTALL = :
|
||||
POST_UNINSTALL = :
|
||||
build_triplet = @build@
|
||||
host_triplet = @host@
|
||||
bin_PROGRAMS = awk01$(EXEEXT) awk02$(EXEEXT) awk03$(EXEEXT)
|
||||
bin_PROGRAMS = awk01$(EXEEXT) awk02$(EXEEXT) awk03$(EXEEXT) \
|
||||
awk04$(EXEEXT)
|
||||
subdir = test/awk
|
||||
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
|
||||
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
|
||||
@ -58,6 +59,10 @@ am_awk03_OBJECTS = awk03.$(OBJEXT)
|
||||
awk03_OBJECTS = $(am_awk03_OBJECTS)
|
||||
awk03_LDADD = $(LDADD)
|
||||
awk03_DEPENDENCIES = $(am__DEPENDENCIES_1)
|
||||
am_awk04_OBJECTS = awk04.$(OBJEXT)
|
||||
awk04_OBJECTS = $(am_awk04_OBJECTS)
|
||||
awk04_LDADD = $(LDADD)
|
||||
awk04_DEPENDENCIES = $(am__DEPENDENCIES_1)
|
||||
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include/qse
|
||||
depcomp = $(SHELL) $(top_srcdir)/autoconf/depcomp
|
||||
am__depfiles_maybe = depfiles
|
||||
@ -70,8 +75,10 @@ CCLD = $(CC)
|
||||
LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
|
||||
--mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \
|
||||
$(LDFLAGS) -o $@
|
||||
SOURCES = $(awk01_SOURCES) $(awk02_SOURCES) $(awk03_SOURCES)
|
||||
DIST_SOURCES = $(awk01_SOURCES) $(awk02_SOURCES) $(awk03_SOURCES)
|
||||
SOURCES = $(awk01_SOURCES) $(awk02_SOURCES) $(awk03_SOURCES) \
|
||||
$(awk04_SOURCES)
|
||||
DIST_SOURCES = $(awk01_SOURCES) $(awk02_SOURCES) $(awk03_SOURCES) \
|
||||
$(awk04_SOURCES)
|
||||
ETAGS = etags
|
||||
CTAGS = ctags
|
||||
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
|
||||
@ -201,6 +208,7 @@ LDADD = -lqseawk -lqseutl -lqsecmn $(LIBM)
|
||||
awk01_SOURCES = awk01.c
|
||||
awk02_SOURCES = awk02.c
|
||||
awk03_SOURCES = awk03.c
|
||||
awk04_SOURCES = awk04.c
|
||||
all: all-am
|
||||
|
||||
.SUFFIXES:
|
||||
@ -271,6 +279,9 @@ awk02$(EXEEXT): $(awk02_OBJECTS) $(awk02_DEPENDENCIES)
|
||||
awk03$(EXEEXT): $(awk03_OBJECTS) $(awk03_DEPENDENCIES)
|
||||
@rm -f awk03$(EXEEXT)
|
||||
$(LINK) $(awk03_OBJECTS) $(awk03_LDADD) $(LIBS)
|
||||
awk04$(EXEEXT): $(awk04_OBJECTS) $(awk04_DEPENDENCIES)
|
||||
@rm -f awk04$(EXEEXT)
|
||||
$(LINK) $(awk04_OBJECTS) $(awk04_LDADD) $(LIBS)
|
||||
|
||||
mostlyclean-compile:
|
||||
-rm -f *.$(OBJEXT)
|
||||
@ -281,6 +292,7 @@ distclean-compile:
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/awk01.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/awk02.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/awk03.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/awk04.Po@am__quote@
|
||||
|
||||
.c.o:
|
||||
@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
|
||||
|
@ -23,6 +23,7 @@
|
||||
*/
|
||||
|
||||
#include <qse/awk/awk.h>
|
||||
#include <qse/awk/std.h>
|
||||
#include <qse/utl/stdio.h>
|
||||
|
||||
const qse_char_t* src = QSE_T(
|
||||
@ -40,20 +41,20 @@ int main ()
|
||||
{
|
||||
qse_awk_t* awk = QSE_NULL;
|
||||
qse_awk_rtx_t* rtx = QSE_NULL;
|
||||
qse_awk_parsesimple_in_t psin;
|
||||
qse_awk_parsestd_in_t psin;
|
||||
int ret;
|
||||
|
||||
awk = qse_awk_opensimple ();
|
||||
awk = qse_awk_openstd ();
|
||||
if (awk == QSE_NULL)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: cannot open awk\n"));
|
||||
goto oops;
|
||||
}
|
||||
|
||||
psin.type = QSE_AWK_PARSESIMPLE_STR;
|
||||
psin.u.str = src;
|
||||
psin.type = QSE_AWK_PARSESTD_CP;
|
||||
psin.u.cp = src;
|
||||
|
||||
ret = qse_awk_parsesimple (awk, &psin, QSE_NULL);
|
||||
ret = qse_awk_parsestd (awk, &psin, QSE_NULL);
|
||||
if (ret == -1)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
@ -61,10 +62,10 @@ int main ()
|
||||
goto oops;
|
||||
}
|
||||
|
||||
rtx = qse_awk_rtx_opensimple (
|
||||
rtx = qse_awk_rtx_openstd (
|
||||
awk,
|
||||
QSE_NULL, /* no console input */
|
||||
QSE_AWK_RTX_OPENSIMPLE_STDIO /* stdout for console output */
|
||||
QSE_NULL, /* no console input */
|
||||
QSE_AWK_RTX_OPENSTD_STDIO /* stdout for console output */
|
||||
);
|
||||
if (rtx == QSE_NULL)
|
||||
{
|
||||
|
@ -23,6 +23,7 @@
|
||||
*/
|
||||
|
||||
#include <qse/awk/awk.h>
|
||||
#include <qse/awk/std.h>
|
||||
#include <qse/cmn/mem.h>
|
||||
#include <qse/utl/stdio.h>
|
||||
|
||||
@ -44,12 +45,12 @@ int main ()
|
||||
qse_awk_t* awk = QSE_NULL;
|
||||
qse_awk_rtx_t* rtx = QSE_NULL;
|
||||
|
||||
qse_awk_parsesimple_in_t psin;
|
||||
qse_awk_parsesimple_out_t psout;
|
||||
qse_awk_parsestd_in_t psin;
|
||||
qse_awk_parsestd_out_t psout;
|
||||
|
||||
int ret;
|
||||
|
||||
awk = qse_awk_opensimple ();
|
||||
awk = qse_awk_openstd ();
|
||||
if (awk == QSE_NULL)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: cannot open awk\n"));
|
||||
@ -59,12 +60,12 @@ int main ()
|
||||
qse_memset (srcout, QSE_T(' '), QSE_COUNTOF(srcout)-1);
|
||||
srcout[QSE_COUNTOF(srcout)-1] = QSE_T('\0');
|
||||
|
||||
psin.type = QSE_AWK_PARSESIMPLE_STR;
|
||||
psin.u.str = src;
|
||||
psout.type = QSE_AWK_PARSESIMPLE_STR;
|
||||
psout.u.str = srcout;
|
||||
psin.type = QSE_AWK_PARSESTD_CP;
|
||||
psin.u.cp = src;
|
||||
psout.type = QSE_AWK_PARSESTD_CP;
|
||||
psout.u.cp = srcout;
|
||||
|
||||
ret = qse_awk_parsesimple (awk, &psin, &psout);
|
||||
ret = qse_awk_parsestd (awk, &psin, &psout);
|
||||
if (ret == -1)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
@ -76,10 +77,10 @@ int main ()
|
||||
qse_printf (QSE_T("=================================\n"));
|
||||
qse_fflush (QSE_STDOUT);
|
||||
|
||||
rtx = qse_awk_rtx_opensimple (
|
||||
rtx = qse_awk_rtx_openstd (
|
||||
awk,
|
||||
QSE_NULL, /* no console input */
|
||||
QSE_AWK_RTX_OPENSIMPLE_STDIO /* stdout for console output */
|
||||
QSE_NULL, /* no console input */
|
||||
QSE_AWK_RTX_OPENSTD_STDIO /* stdout for console output */
|
||||
);
|
||||
if (rtx == QSE_NULL)
|
||||
{
|
||||
|
@ -25,12 +25,13 @@
|
||||
*/
|
||||
|
||||
#include <qse/awk/awk.h>
|
||||
#include <qse/awk/std.h>
|
||||
#include <qse/utl/stdio.h>
|
||||
|
||||
static const qse_char_t* src = QSE_T(
|
||||
"function init() { a = 20; }"
|
||||
"function main() { a++; }"
|
||||
"function fini() { print a; }"
|
||||
"function init() { a = 20; return a; }"
|
||||
"function main() { return ++a; }"
|
||||
"function fini() { print ++a; return a; }"
|
||||
);
|
||||
|
||||
static const qse_char_t* fnc[] =
|
||||
@ -48,12 +49,12 @@ int main ()
|
||||
qse_awk_t* awk = QSE_NULL;
|
||||
qse_awk_rtx_t* rtx = QSE_NULL;
|
||||
|
||||
qse_awk_parsesimple_in_t psin;
|
||||
qse_awk_parsestd_in_t psin;
|
||||
|
||||
int ret, i;
|
||||
|
||||
/* create a main processor */
|
||||
awk = qse_awk_opensimple ();
|
||||
awk = qse_awk_openstd ();
|
||||
if (awk == QSE_NULL)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: cannot open awk\n"));
|
||||
@ -63,10 +64,10 @@ int main ()
|
||||
/* don't allow BEGIN, END, pattern-action blocks */
|
||||
qse_awk_setoption (awk, qse_awk_getoption(awk) & ~QSE_AWK_PABLOCK);
|
||||
|
||||
psin.type = QSE_AWK_PARSESIMPLE_STR;
|
||||
psin.u.str = src;
|
||||
psin.type = QSE_AWK_PARSESTD_CP;
|
||||
psin.u.cp = src;
|
||||
|
||||
ret = qse_awk_parsesimple (awk, &psin, QSE_NULL);
|
||||
ret = qse_awk_parsestd (awk, &psin, QSE_NULL);
|
||||
if (ret == -1)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
@ -75,10 +76,10 @@ int main ()
|
||||
}
|
||||
|
||||
/* create a runtime context */
|
||||
rtx = qse_awk_rtx_opensimple (
|
||||
rtx = qse_awk_rtx_openstd (
|
||||
awk,
|
||||
QSE_NULL, /* no console input */
|
||||
QSE_AWK_RTX_OPENSIMPLE_STDIO /* stdout for console output */
|
||||
QSE_NULL, /* no console input */
|
||||
QSE_AWK_RTX_OPENSTD_STDIO /* stdout for console output */
|
||||
);
|
||||
if (rtx == QSE_NULL)
|
||||
{
|
||||
@ -90,13 +91,25 @@ int main ()
|
||||
/* invoke functions as indicated in the array fnc */
|
||||
for (i = 0; i < QSE_COUNTOF(fnc); i++)
|
||||
{
|
||||
ret = qse_awk_rtx_call (rtx, fnc[i], QSE_NULL, 0);
|
||||
if (ret == -1)
|
||||
qse_awk_val_t* v;
|
||||
qse_char_t buf[1000];
|
||||
qse_size_t bufsize;
|
||||
|
||||
v = qse_awk_rtx_call (rtx, fnc[i], QSE_NULL, 0);
|
||||
if (v == QSE_NULL)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
qse_awk_rtx_geterrmsg(rtx));
|
||||
goto oops;
|
||||
ret = -1; goto oops;
|
||||
}
|
||||
|
||||
bufsize = QSE_COUNTOF(buf);
|
||||
qse_awk_rtx_valtostr (rtx, v,
|
||||
QSE_AWK_RTX_VALTOSTR_FIXED, buf, &bufsize);
|
||||
qse_printf (QSE_T("return: [%.*s]\n"), (int)bufsize, buf);
|
||||
|
||||
/* clear the return value */
|
||||
qse_awk_rtx_refdownval (rtx, v);
|
||||
}
|
||||
|
||||
oops:
|
||||
|
130
qse/test/awk/awk04.c
Normal file
130
qse/test/awk/awk04.c
Normal file
@ -0,0 +1,130 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
Copyright 2006-2009 Chung, Hyung-Hwan.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
/****S* AWK/Calling Functions
|
||||
* DESCRIPTION
|
||||
* This program demonstrates how to use qse_awk_rtx_call().
|
||||
* It parses the program stored in the string src and calls the functions
|
||||
* stated in the array fnc. If no errors occur, it should print 24.
|
||||
* SOURCE
|
||||
*/
|
||||
|
||||
#include <qse/awk/awk.h>
|
||||
#include <qse/awk/std.h>
|
||||
#include <qse/utl/stdio.h>
|
||||
|
||||
static const qse_char_t* src = QSE_T(
|
||||
"function pow(x,y) { return x ** y; }"
|
||||
);
|
||||
|
||||
int main ()
|
||||
{
|
||||
qse_awk_t* awk = QSE_NULL;
|
||||
qse_awk_rtx_t* rtx = QSE_NULL;
|
||||
qse_awk_parsestd_in_t psin;
|
||||
qse_char_t buf[1000];
|
||||
qse_size_t bufsize;
|
||||
qse_awk_val_t* v;
|
||||
qse_awk_val_t* arg[2] = { QSE_NULL, QSE_NULL };
|
||||
int ret, i;
|
||||
|
||||
/* create a main processor */
|
||||
awk = qse_awk_openstd ();
|
||||
if (awk == QSE_NULL)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: cannot open awk\n"));
|
||||
ret = -1; goto oops;
|
||||
}
|
||||
|
||||
/* don't allow BEGIN, END, pattern-action blocks */
|
||||
qse_awk_setoption (awk, qse_awk_getoption(awk) & ~QSE_AWK_PABLOCK);
|
||||
|
||||
psin.type = QSE_AWK_PARSESTD_CP;
|
||||
psin.u.cp = src;
|
||||
|
||||
ret = qse_awk_parsestd (awk, &psin, QSE_NULL);
|
||||
if (ret == -1)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
qse_awk_geterrmsg(awk));
|
||||
goto oops;
|
||||
}
|
||||
|
||||
/* create a runtime context */
|
||||
rtx = qse_awk_rtx_openstd (
|
||||
awk,
|
||||
QSE_NULL, /* no console input */
|
||||
QSE_AWK_RTX_OPENSTD_STDIO /* stdout for console output */
|
||||
);
|
||||
if (rtx == QSE_NULL)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
qse_awk_geterrmsg(awk));
|
||||
ret = -1; goto oops;
|
||||
}
|
||||
|
||||
/* invoke the pow function */
|
||||
arg[0] = qse_awk_rtx_makeintval (rtx, 50);
|
||||
if (arg[0] == QSE_NULL)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
qse_awk_rtx_geterrmsg(rtx));
|
||||
ret = -1; goto oops;
|
||||
}
|
||||
qse_awk_rtx_refupval (rtx, arg[0]);
|
||||
|
||||
arg[1] = qse_awk_rtx_makeintval (rtx, 3);
|
||||
if (arg[1] == QSE_NULL)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
qse_awk_rtx_geterrmsg(rtx));
|
||||
ret = -1; goto oops;
|
||||
}
|
||||
qse_awk_rtx_refupval (rtx, arg[1]);
|
||||
|
||||
v = qse_awk_rtx_call (rtx, QSE_T("pow"), arg, 2);
|
||||
if (v == QSE_NULL)
|
||||
{
|
||||
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
||||
qse_awk_rtx_geterrmsg(rtx));
|
||||
ret = -1; goto oops;
|
||||
}
|
||||
|
||||
bufsize = QSE_COUNTOF(buf);
|
||||
qse_awk_rtx_valtostr (rtx, v,
|
||||
QSE_AWK_RTX_VALTOSTR_FIXED, buf, &bufsize);
|
||||
qse_printf (QSE_T("[%.*s]\n"), (int)bufsize, buf);
|
||||
|
||||
/* clear the return value */
|
||||
qse_awk_rtx_refdownval (rtx, v);
|
||||
|
||||
oops:
|
||||
/* dereference all arguments */
|
||||
for (i = 0; i < QSE_COUNTOF(arg); i++)
|
||||
{
|
||||
if (arg[i] != QSE_NULL)
|
||||
qse_awk_rtx_refdownval (rtx, arg[i]);
|
||||
}
|
||||
|
||||
/* destroy a runtime context */
|
||||
if (rtx != QSE_NULL) qse_awk_rtx_close (rtx);
|
||||
/* destroy the processor */
|
||||
if (awk != QSE_NULL) qse_awk_close (awk);
|
||||
return ret;
|
||||
}
|
||||
/******/
|
Reference in New Issue
Block a user