diff --git a/hawk/README.md b/hawk/README.md index 443c5af5..3e2d2e1a 100644 --- a/hawk/README.md +++ b/hawk/README.md @@ -13,14 +13,14 @@ Hawk implements most of the AWK programming language elements with extensions. You may change the entry point of your script by setting a function name with @pragma entry. -
+```
 @pragma entry main
 
 function main ()
 {
 	print "hello, world";
 }
-
+``` ### Value @@ -41,22 +41,24 @@ function main () In AWK, the caller can pass an uninitialized variable as a function parameter and get a changed value if the callled function sets it to an array. -
+```
 function q(a) {a[1]=20; a[2]=30;}
 BEGIN { q(x); for (i in x) print i, x[i]; }
-
+``` In Hawk, you can prefix the pramater name with & to indicate call-by-reference for the same effect. -
+
+```
 function q(&a) {a[1]=20; a[2]=30;}
 BEGIN { q(x); for (i in x) print i, x[i]; }
-
+``` Alternatively, you may form an array before passing it to a function. -
+
+```
 function q(a) {a[1]=20; a[2]=30;}
 BEGIN { x[3]=99; q(x); for (i in x) print i, x[i]; }'
-
+``` ## Basic Modules @@ -68,3 +70,80 @@ BEGIN { x[3]=99; q(x); for (i in x) print i, x[i]; }' ### mysql ## Embedding Guide + + +``` +#include +#include +#include + +static const hawk_bch_t* src = + "BEGIN {" + " for (i=2;i<=9;i++)" + " {" + " for (j=1;j<=9;j++)" + " print i \"*\" j \"=\" i * j;" + " print \"---------------------\";" + " }" + "}"; + +int main () +{ + hawk_t* hawk = HAWK_NULL; + hawk_rtx_t* rtx = HAWK_NULL; + hawk_val_t* retv; + + hawk_parsestd_t psin[2]; + + int ret; + + hawk = hawk_openstd(0, HAWK_NULL); + if (!hawk) + { + fprintf (stderr, "ERROR: cannot open hawk\n"); + ret = -1; goto oops; + } + + memset (&psin, 0, HAWK_SIZEOF(psin)); + psin[0].type = HAWK_PARSESTD_BCS; + psin[0].u.bcs.ptr = (hawk_bch_t*)src; + psin[0].u.bcs.len = hawk_count_bcstr(src); + psin[1].type = HAWK_PARSESTD_NULL; + + ret = hawk_parsestd(hawk, psin, HAWK_NULL); + if (ret <= -1) + { + hawk_logbfmt (hawk, HAWK_LOG_STDERR, "ERROR(parse): %js\n", hawk_geterrmsg(hawk)); + ret = -1; goto oops; + } + + rtx = hawk_rtx_openstd ( + hawk, + 0, + HAWK_T("hawk02"), + HAWK_NULL, /* stdin */ + HAWK_NULL, /* stdout */ + HAWK_NULL /* default cmgr */ + ); + if (!rtx) + { + hawk_logbfmt (hawk, HAWK_LOG_STDERR, "ERROR(rtx_open): %js\n", hawk_geterrmsg(hawk)); + ret = -1; goto oops; + } + + retv = hawk_rtx_loop(rtx); + if (!retv) + { + hawk_logbfmt (hawk, HAWK_LOG_STDERR, "ERROR(rtx_loop): %js\n", hawk_geterrmsg(hawk)); + ret = -1; goto oops; + } + + hawk_rtx_refdownval (rtx, retv); + ret = 0; + +oops: + if (rtx) hawk_rtx_close (rtx); + if (hawk) hawk_close (hawk); + return -1; +} +``` diff --git a/hawk/samples/Makefile.am b/hawk/samples/Makefile.am index 8920ed1d..07a05c78 100644 --- a/hawk/samples/Makefile.am +++ b/hawk/samples/Makefile.am @@ -19,7 +19,13 @@ bin_PROGRAMS = if ENABLE_CXX -bin_PROGRAMS += hawk51 +bin_PROGRAMS += hawk02 hawk51 + +hawk02_SOURCES = hawk02.c +hawk02_CPPFLAGS = $(CPPFLAGS_ALL_COMMON) +hawk02_LDFLAGS = $(LDFLAGS_ALL_COMMON) +hawk02_LDADD = -lhawk $(LIBADD_BIN_COMMON) $(SOCKET_LIBS) +hawk02_DEPENDENCIES = ../lib/libhawk.la hawk51_SOURCES = hawk51.cpp hawk51_CPPFLAGS = $(CPPFLAGS_ALL_COMMON) diff --git a/hawk/samples/Makefile.in b/hawk/samples/Makefile.in index b3cf7fed..8497cf9b 100644 --- a/hawk/samples/Makefile.in +++ b/hawk/samples/Makefile.in @@ -89,7 +89,7 @@ POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ bin_PROGRAMS = $(am__EXEEXT_1) -@ENABLE_CXX_TRUE@am__append_1 = hawk51 +@ENABLE_CXX_TRUE@am__append_1 = hawk02 hawk51 subdir = samples ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/m4/ax_check_sign.m4 \ @@ -107,18 +107,24 @@ mkinstalldirs = $(install_sh) -d CONFIG_HEADER = $(top_builddir)/lib/hawk-cfg.h CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = -@ENABLE_CXX_TRUE@am__EXEEXT_1 = hawk51$(EXEEXT) +@ENABLE_CXX_TRUE@am__EXEEXT_1 = hawk02$(EXEEXT) hawk51$(EXEEXT) am__installdirs = "$(DESTDIR)$(bindir)" PROGRAMS = $(bin_PROGRAMS) -am__hawk51_SOURCES_DIST = hawk51.cpp -@ENABLE_CXX_TRUE@am_hawk51_OBJECTS = hawk51-hawk51.$(OBJEXT) -hawk51_OBJECTS = $(am_hawk51_OBJECTS) +am__hawk02_SOURCES_DIST = hawk02.c +@ENABLE_CXX_TRUE@am_hawk02_OBJECTS = hawk02-hawk02.$(OBJEXT) +hawk02_OBJECTS = $(am_hawk02_OBJECTS) am__DEPENDENCIES_1 = am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) am__v_lt_0 = --silent am__v_lt_1 = +hawk02_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(hawk02_LDFLAGS) $(LDFLAGS) -o $@ +am__hawk51_SOURCES_DIST = hawk51.cpp +@ENABLE_CXX_TRUE@am_hawk51_OBJECTS = hawk51-hawk51.$(OBJEXT) +hawk51_OBJECTS = $(am_hawk51_OBJECTS) hawk51_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \ $(LIBTOOLFLAGS) --mode=link $(CXXLD) $(AM_CXXFLAGS) \ $(CXXFLAGS) $(hawk51_LDFLAGS) $(LDFLAGS) -o $@ @@ -137,8 +143,27 @@ am__v_at_1 = DEFAULT_INCLUDES = depcomp = $(SHELL) $(top_srcdir)/ac/depcomp am__maybe_remake_depfiles = depfiles -am__depfiles_remade = ./$(DEPDIR)/hawk51-hawk51.Po +am__depfiles_remade = ./$(DEPDIR)/hawk02-hawk02.Po \ + ./$(DEPDIR)/hawk51-hawk51.Po am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CFLAGS) $(CFLAGS) +AM_V_CC = $(am__v_CC_@AM_V@) +am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@) +am__v_CC_0 = @echo " CC " $@; +am__v_CC_1 = +CCLD = $(CC) +LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CCLD = $(am__v_CCLD_@AM_V@) +am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) +am__v_CCLD_0 = @echo " CCLD " $@; +am__v_CCLD_1 = CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) LTCXXCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \ @@ -157,8 +182,8 @@ AM_V_CXXLD = $(am__v_CXXLD_@AM_V@) am__v_CXXLD_ = $(am__v_CXXLD_@AM_DEFAULT_V@) am__v_CXXLD_0 = @echo " CXXLD " $@; am__v_CXXLD_1 = -SOURCES = $(hawk51_SOURCES) -DIST_SOURCES = $(am__hawk51_SOURCES_DIST) +SOURCES = $(hawk02_SOURCES) $(hawk51_SOURCES) +DIST_SOURCES = $(am__hawk02_SOURCES_DIST) $(am__hawk51_SOURCES_DIST) am__can_run_installinfo = \ case $$AM_UPDATE_INFO_DIR in \ n|no|NO) false;; \ @@ -356,6 +381,11 @@ LDFLAGS_ALL_COMMON = -L$(abs_builddir) -L$(abs_builddir)/../lib -L$(libdir) CPPFLAGS_BIN_COMMON = $(CPPFLAGS_ALL_COMMON) LDFLAGS_BIN_COMMON = $(LDFLAGS_ALL_COMMON) -no-undefined LIBADD_BIN_COMMON = $(LIBM) +@ENABLE_CXX_TRUE@hawk02_SOURCES = hawk02.c +@ENABLE_CXX_TRUE@hawk02_CPPFLAGS = $(CPPFLAGS_ALL_COMMON) +@ENABLE_CXX_TRUE@hawk02_LDFLAGS = $(LDFLAGS_ALL_COMMON) +@ENABLE_CXX_TRUE@hawk02_LDADD = -lhawk $(LIBADD_BIN_COMMON) $(SOCKET_LIBS) +@ENABLE_CXX_TRUE@hawk02_DEPENDENCIES = ../lib/libhawk.la @ENABLE_CXX_TRUE@hawk51_SOURCES = hawk51.cpp @ENABLE_CXX_TRUE@hawk51_CPPFLAGS = $(CPPFLAGS_ALL_COMMON) @ENABLE_CXX_TRUE@hawk51_LDFLAGS = $(LDFLAGS_ALL_COMMON) @@ -364,7 +394,7 @@ LIBADD_BIN_COMMON = $(LIBM) all: all-am .SUFFIXES: -.SUFFIXES: .cpp .lo .o .obj +.SUFFIXES: .c .cpp .lo .o .obj $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) @for dep in $?; do \ case '$(am__configure_deps)' in \ @@ -444,6 +474,10 @@ clean-binPROGRAMS: echo " rm -f" $$list; \ rm -f $$list +hawk02$(EXEEXT): $(hawk02_OBJECTS) $(hawk02_DEPENDENCIES) $(EXTRA_hawk02_DEPENDENCIES) + @rm -f hawk02$(EXEEXT) + $(AM_V_CCLD)$(hawk02_LINK) $(hawk02_OBJECTS) $(hawk02_LDADD) $(LIBS) + hawk51$(EXEEXT): $(hawk51_OBJECTS) $(hawk51_DEPENDENCIES) $(EXTRA_hawk51_DEPENDENCIES) @rm -f hawk51$(EXEEXT) $(AM_V_CXXLD)$(hawk51_LINK) $(hawk51_OBJECTS) $(hawk51_LDADD) $(LIBS) @@ -454,6 +488,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hawk02-hawk02.Po@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hawk51-hawk51.Po@am__quote@ # am--include-marker $(am__depfiles_remade): @@ -462,6 +497,44 @@ $(am__depfiles_remade): am--depfiles: $(am__depfiles_remade) +.c.o: +@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.o$$||'`;\ +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ +@am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ $< + +.c.obj: +@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.obj$$||'`;\ +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ `$(CYGPATH_W) '$<'` &&\ +@am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +.c.lo: +@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.lo$$||'`;\ +@am__fastdepCC_TRUE@ $(LTCOMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ +@am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $< + +hawk02-hawk02.o: hawk02.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk02_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT hawk02-hawk02.o -MD -MP -MF $(DEPDIR)/hawk02-hawk02.Tpo -c -o hawk02-hawk02.o `test -f 'hawk02.c' || echo '$(srcdir)/'`hawk02.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/hawk02-hawk02.Tpo $(DEPDIR)/hawk02-hawk02.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='hawk02.c' object='hawk02-hawk02.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk02_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o hawk02-hawk02.o `test -f 'hawk02.c' || echo '$(srcdir)/'`hawk02.c + +hawk02-hawk02.obj: hawk02.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk02_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT hawk02-hawk02.obj -MD -MP -MF $(DEPDIR)/hawk02-hawk02.Tpo -c -o hawk02-hawk02.obj `if test -f 'hawk02.c'; then $(CYGPATH_W) 'hawk02.c'; else $(CYGPATH_W) '$(srcdir)/hawk02.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/hawk02-hawk02.Tpo $(DEPDIR)/hawk02-hawk02.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='hawk02.c' object='hawk02-hawk02.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(hawk02_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o hawk02-hawk02.obj `if test -f 'hawk02.c'; then $(CYGPATH_W) 'hawk02.c'; else $(CYGPATH_W) '$(srcdir)/hawk02.c'; fi` + .cpp.o: @am__fastdepCXX_TRUE@ $(AM_V_CXX)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.o$$||'`;\ @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ @@ -633,7 +706,8 @@ clean: clean-am clean-am: clean-binPROGRAMS clean-generic clean-libtool mostlyclean-am distclean: distclean-am - -rm -f ./$(DEPDIR)/hawk51-hawk51.Po + -rm -f ./$(DEPDIR)/hawk02-hawk02.Po + -rm -f ./$(DEPDIR)/hawk51-hawk51.Po -rm -f Makefile distclean-am: clean-am distclean-compile distclean-generic \ distclean-tags @@ -679,7 +753,8 @@ install-ps-am: installcheck-am: maintainer-clean: maintainer-clean-am - -rm -f ./$(DEPDIR)/hawk51-hawk51.Po + -rm -f ./$(DEPDIR)/hawk02-hawk02.Po + -rm -f ./$(DEPDIR)/hawk51-hawk51.Po -rm -f Makefile maintainer-clean-am: distclean-am maintainer-clean-generic diff --git a/hawk/samples/hawk02.c b/hawk/samples/hawk02.c new file mode 100644 index 00000000..6005582f --- /dev/null +++ b/hawk/samples/hawk02.c @@ -0,0 +1,74 @@ +#include +#include +#include + +static const hawk_bch_t* src = + "BEGIN {" + " for (i=2;i<=9;i++)" + " {" + " for (j=1;j<=9;j++)" + " print i \"*\" j \"=\" i * j;" + " print \"---------------------\";" + " }" + "}"; + +int main () +{ + hawk_t* hawk = HAWK_NULL; + hawk_rtx_t* rtx = HAWK_NULL; + hawk_val_t* retv; + + hawk_parsestd_t psin[2]; + + int ret; + + hawk = hawk_openstd(0, HAWK_NULL); + if (!hawk) + { + fprintf (stderr, "ERROR: cannot open hawk\n"); + ret = -1; goto oops; + } + + memset (&psin, 0, HAWK_SIZEOF(psin)); + psin[0].type = HAWK_PARSESTD_BCS; + psin[0].u.bcs.ptr = (hawk_bch_t*)src; + psin[0].u.bcs.len = hawk_count_bcstr(src); + psin[1].type = HAWK_PARSESTD_NULL; + + ret = hawk_parsestd(hawk, psin, HAWK_NULL); + if (ret <= -1) + { + hawk_logbfmt (hawk, HAWK_LOG_STDERR, "ERROR(parse): %js\n", hawk_geterrmsg(hawk)); + ret = -1; goto oops; + } + + rtx = hawk_rtx_openstd ( + hawk, + 0, + HAWK_T("hawk02"), + HAWK_NULL, /* stdin */ + HAWK_NULL, /* stdout */ + HAWK_NULL /* default cmgr */ + ); + if (!rtx) + { + hawk_logbfmt (hawk, HAWK_LOG_STDERR, "ERROR(rtx_open): %js\n", hawk_geterrmsg(hawk)); + ret = -1; goto oops; + } + + retv = hawk_rtx_loop(rtx); + if (!retv) + { + hawk_logbfmt (hawk, HAWK_LOG_STDERR, "ERROR(rtx_loop): %js\n", hawk_geterrmsg(hawk)); + ret = -1; goto oops; + } + + hawk_rtx_refdownval (rtx, retv); + ret = 0; + +oops: + if (rtx) hawk_rtx_close (rtx); + if (hawk) hawk_close (hawk); + return -1; +} + diff --git a/hawk/lib/generrcode.awk b/hawk/scripts/generrcode.awk similarity index 100% rename from hawk/lib/generrcode.awk rename to hawk/scripts/generrcode.awk diff --git a/hawk/lib/genoptcode.awk b/hawk/scripts/genoptcode.awk similarity index 94% rename from hawk/lib/genoptcode.awk rename to hawk/scripts/genoptcode.awk index 8463aa27..a460c4d4 100644 --- a/hawk/lib/genoptcode.awk +++ b/hawk/scripts/genoptcode.awk @@ -1,7 +1,7 @@ # # genoptcode.awk # -# hawk -f generror.awk hawk.h +# hawk -f genoptcode.awk hawk.h # BEGIN {