added lisp.awk as a test program

This commit is contained in:
2007-12-05 08:13:38 +00:00
parent 41b11d8f8b
commit 2d9523245c
6 changed files with 856 additions and 34 deletions

View File

@ -55,7 +55,7 @@ int StdAwk::open ()
ADD_FUNC (ASE_T("sqrt"), 1, 1, &StdAwk::sqrt);
ADD_FUNC (ASE_T("int"), 1, 1, &StdAwk::fnint);
ADD_FUNC (ASE_T("rand"), 0, 0, &StdAwk::rand);
ADD_FUNC (ASE_T("srand"), 1, 1, &StdAwk::srand);
ADD_FUNC (ASE_T("srand"), 0, 1, &StdAwk::srand);
ADD_FUNC (ASE_T("systime"), 0, 0, &StdAwk::systime);
ADD_FUNC (ASE_T("strftime"), 0, 2, &StdAwk::strftime);
ADD_FUNC (ASE_T("strfgmtime"), 0, 2, &StdAwk::strfgmtime);
@ -128,7 +128,10 @@ int StdAwk::srand (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len)
{
unsigned int prevSeed = seed;
seed = (unsigned int)args[0].toInt();
seed = (nargs == 0)?
(unsigned int)::time(NULL):
(unsigned int)args[0].toInt();
::srand (seed);
return ret.set ((long_t)prevSeed);
}

View File

@ -139,7 +139,9 @@ ase_awk_t* ase_awk_open (const ase_awk_prmfns_t* prmfns, void* custom_data)
awk->tree.nglobals = 0;
awk->tree.nbglobals = 0;
awk->tree.begin = ASE_NULL;
awk->tree.begin_tail = ASE_NULL;
awk->tree.end = ASE_NULL;
awk->tree.end_tail = ASE_NULL;
awk->tree.chain = ASE_NULL;
awk->tree.chain_tail = ASE_NULL;
awk->tree.chain_size = 0;
@ -295,16 +297,19 @@ int ase_awk_clear (ase_awk_t* awk)
if (awk->tree.begin != ASE_NULL)
{
ASE_ASSERT (awk->tree.begin->next == ASE_NULL);
ase_awk_nde_t* next = awk->tree.begin->next;
/*ASE_ASSERT (awk->tree.begin->next == ASE_NULL);*/
ase_awk_clrpt (awk, awk->tree.begin);
awk->tree.begin = ASE_NULL;
awk->tree.begin_tail = ASE_NULL;
}
if (awk->tree.end != ASE_NULL)
{
ASE_ASSERT (awk->tree.end->next == ASE_NULL);
/*ASE_ASSERT (awk->tree.end->next == ASE_NULL);*/
ase_awk_clrpt (awk, awk->tree.end);
awk->tree.end = ASE_NULL;
awk->tree.end_tail = ASE_NULL;
}
while (awk->tree.chain != ASE_NULL)

View File

@ -58,11 +58,17 @@ struct ase_awk_tree_t
ase_size_t nbglobals; /* number of intrinsic globals */
ase_cstr_t cur_afn;
ase_awk_map_t* afns; /* awk function map */
ase_awk_nde_t* begin;
ase_awk_nde_t* begin_tail;
ase_awk_nde_t* end;
ase_awk_nde_t* end_tail;
ase_awk_chain_t* chain;
ase_awk_chain_t* chain_tail;
ase_size_t chain_size; /* number of nodes in the chain */
int ok;
};

View File

@ -593,11 +593,13 @@ static ase_awk_t* parse_progunit (ase_awk_t* awk)
return ASE_NULL;
}
/*
if (awk->tree.begin != ASE_NULL)
{
SETERRLIN (awk, ASE_AWK_EDUPBEG, awk->token.prev.line);
return ASE_NULL;
}
*/
awk->parse.id.block = PARSE_BEGIN;
if (get_token(awk) == -1) return ASE_NULL;
@ -628,11 +630,13 @@ static ase_awk_t* parse_progunit (ase_awk_t* awk)
return ASE_NULL;
}
/*
if (awk->tree.end != ASE_NULL)
{
SETERRLIN (awk, ASE_AWK_EDUPEND, awk->token.prev.line);
return ASE_NULL;
}
*/
awk->parse.id.block = PARSE_END;
if (get_token(awk) == -1) return ASE_NULL;
@ -1068,7 +1072,17 @@ static ase_awk_nde_t* parse_begin (ase_awk_t* awk)
nde = awk->parse.parse_block (awk, awk->token.prev.line, ase_true);
if (nde == ASE_NULL) return ASE_NULL;
awk->tree.begin = nde;
if (awk->tree.begin == ASE_NULL)
{
awk->tree.begin = nde;
awk->tree.begin_tail = nde;
}
else
{
awk->tree.begin_tail->next = nde;
awk->tree.begin_tail = nde;
}
return nde;
}
@ -1082,7 +1096,16 @@ static ase_awk_nde_t* parse_end (ase_awk_t* awk)
nde = awk->parse.parse_block (awk, awk->token.prev.line, ase_true);
if (nde == ASE_NULL) return ASE_NULL;
awk->tree.end = nde;
if (awk->tree.end == ASE_NULL)
{
awk->tree.end = nde;
awk->tree.end_tail = nde;
}
else
{
awk->tree.end_tail->next = nde;
awk->tree.end_tail = nde;
}
return nde;
}
@ -5168,6 +5191,7 @@ struct deparse_func_t
static int deparse (ase_awk_t* awk)
{
ase_awk_nde_t* nde;
ase_awk_chain_t* chain;
ase_char_t tmp[ASE_SIZEOF(ase_size_t)*8 + 32];
struct deparse_func_t df;
@ -5284,23 +5308,16 @@ static int deparse (ase_awk_t* awk)
EXIT_DEPARSE ();
}
if (awk->tree.begin != ASE_NULL)
for (nde = awk->tree.begin; nde != ASE_NULL; nde = nde->next)
{
if (ase_awk_putsrcstr(awk,ase_awk_getkw(awk,ASE_T("BEGIN"))) == -1)
{
EXIT_DEPARSE ();
}
if (ase_awk_putsrcstr (awk, ASE_T(" ")) == -1)
{
EXIT_DEPARSE ();
}
if (ase_awk_prnpt (awk, awk->tree.begin) == -1) EXIT_DEPARSE ();
const ase_char_t* kw = ase_awk_getkw(awk,ASE_T("BEGIN"));
if (ase_awk_putsrcstr(awk,kw) == -1) EXIT_DEPARSE ();
if (ase_awk_putsrcstr (awk, ASE_T(" ")) == -1) EXIT_DEPARSE ();
if (ase_awk_prnpt (awk, nde) == -1) EXIT_DEPARSE ();
if (awk->option & ASE_AWK_CRLF)
{
if (put_char (awk, ASE_T('\r')) == -1)
EXIT_DEPARSE ();
if (put_char (awk, ASE_T('\r')) == -1) EXIT_DEPARSE ();
}
if (put_char (awk, ASE_T('\n')) == -1) EXIT_DEPARSE ();
@ -5350,14 +5367,19 @@ static int deparse (ase_awk_t* awk)
chain = chain->next;
}
if (awk->tree.end != ASE_NULL)
for (nde = awk->tree.end; nde != ASE_NULL; nde = nde->next)
{
if (ase_awk_putsrcstr(awk,ase_awk_getkw(awk,ASE_T("END"))) == -1)
EXIT_DEPARSE ();
if (ase_awk_putsrcstr (awk, ASE_T(" ")) == -1)
EXIT_DEPARSE ();
if (ase_awk_prnpt (awk, awk->tree.end) == -1)
EXIT_DEPARSE ();
const ase_char_t* kw = ase_awk_getkw(awk,ASE_T("END"));
if (ase_awk_putsrcstr(awk,kw) == -1) EXIT_DEPARSE ();
if (ase_awk_putsrcstr (awk, ASE_T(" ")) == -1) EXIT_DEPARSE ();
if (ase_awk_prnpt (awk, nde) == -1) EXIT_DEPARSE ();
if (awk->option & ASE_AWK_CRLF)
{
if (put_char (awk, ASE_T('\r')) == -1) EXIT_DEPARSE ();
}
if (put_char (awk, ASE_T('\n')) == -1) EXIT_DEPARSE ();
}
if (flush_out (awk) == -1) EXIT_DEPARSE ();

View File

@ -1322,6 +1322,8 @@ static int run_main (
}
else if (n == 0)
{
ase_awk_nde_t* nde;
/* no main function is specified.
* run the normal patter blocks including BEGIN and END */
saved_stack_top = run->stack_top;
@ -1377,13 +1379,13 @@ static int run_main (
STACK_NARGS(run) = (void*)nargs;
/* stack set up properly. ready to exeucte statement blocks */
if (n == 0 &&
run->awk->tree.begin != ASE_NULL &&
run->exit_level != EXIT_ABORT)
for (nde = run->awk->tree.begin;
n == 0 && nde != ASE_NULL && run->exit_level != EXIT_ABORT;
nde = nde->next)
{
ase_awk_nde_blk_t* blk;
blk = (ase_awk_nde_blk_t*)run->awk->tree.begin;
blk = (ase_awk_nde_blk_t*)nde;
ASE_ASSERT (blk->type == ASE_AWK_NDE_BLK);
run->active_block = blk;
@ -1399,13 +1401,13 @@ static int run_main (
if (run_pattern_blocks (run) == -1) n = -1;
}
if (n == 0 &&
run->awk->tree.end != ASE_NULL &&
run->exit_level != EXIT_ABORT)
for (nde = run->awk->tree.end;
n == 0 && nde != ASE_NULL && run->exit_level != EXIT_ABORT;
nde = nde->next)
{
ase_awk_nde_blk_t* blk;
blk = (ase_awk_nde_blk_t*)run->awk->tree.end;
blk = (ase_awk_nde_blk_t*)nde;
ASE_ASSERT (blk->type == ASE_AWK_NDE_BLK);
run->active_block = blk;