hawk/samples/hawk02.c

76 lines
1.8 KiB
C
Raw Normal View History

#include <hawk-std.h>
#include <stdio.h>
#include <string.h>
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;
2020-03-09 07:10:41 +00:00
hawk = hawk_openstd(0, HAWK_NULL); /* create a hawk instance */
if (!hawk)
{
fprintf (stderr, "ERROR: cannot open hawk\n");
ret = -1; goto oops;
}
2020-03-09 07:10:41 +00:00
/* set up source script file to read in */
memset (&psin, 0, HAWK_SIZEOF(psin));
2020-03-09 07:10:41 +00:00
psin[0].type = HAWK_PARSESTD_BCS; /* specify the first script path */
psin[0].u.bcs.ptr = (hawk_bch_t*)src;
psin[0].u.bcs.len = hawk_count_bcstr(src);
2020-03-09 07:10:41 +00:00
psin[1].type = HAWK_PARSESTD_NULL; /* indicate the no more script to read */
2020-03-09 07:10:41 +00:00
ret = hawk_parsestd(hawk, psin, HAWK_NULL); /* parse the script */
if (ret <= -1)
{
hawk_logbfmt (hawk, HAWK_LOG_STDERR, "ERROR(parse): %js\n", hawk_geterrmsg(hawk));
ret = -1; goto oops;
}
2020-03-09 07:10:41 +00:00
/* create a runtime context needed for execution */
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;
}
2020-03-09 07:10:41 +00:00
/* execute the BEGIN/pattern-action/END blocks */
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;
}
2020-03-09 07:10:41 +00:00
/* lowered the reference count of the returned value */
hawk_rtx_refdownval (rtx, retv);
ret = 0;
oops:
2020-03-09 07:10:41 +00:00
if (rtx) hawk_rtx_close (rtx); /* destroy the runtime context */
if (hawk) hawk_close (hawk); /* destroy the hawk instance */
return -1;
}