qse/ase/awk/tree.c

603 lines
14 KiB
C
Raw Normal View History

2006-01-14 14:09:52 +00:00
/*
2006-04-04 16:50:36 +00:00
* $Id: tree.c,v 1.32 2006-04-04 16:50:36 bacon Exp $
2006-01-14 14:09:52 +00:00
*/
2006-03-31 16:35:37 +00:00
#include <xp/awk/awk_i.h>
2006-01-18 15:16:01 +00:00
#ifndef __STAND_ALONE
2006-01-14 14:09:52 +00:00
#include <xp/bas/memory.h>
#include <xp/bas/assert.h>
#include <xp/bas/stdio.h>
2006-01-18 15:16:01 +00:00
#endif
2006-01-14 14:09:52 +00:00
2006-02-08 16:14:31 +00:00
static const xp_char_t* __binop_str[] =
2006-01-14 14:09:52 +00:00
{
2006-03-30 16:31:50 +00:00
XP_TEXT("||"),
XP_TEXT("&&"),
XP_TEXT("|"),
XP_TEXT("^"),
XP_TEXT("&"),
2006-03-31 12:04:14 +00:00
2006-02-08 16:14:31 +00:00
XP_TEXT("=="),
XP_TEXT("!="),
XP_TEXT(">"),
XP_TEXT(">="),
XP_TEXT("<"),
2006-03-31 12:04:14 +00:00
XP_TEXT("<="),
XP_TEXT("<<"),
XP_TEXT(">>"),
XP_TEXT("+"),
XP_TEXT("-"),
XP_TEXT("*"),
XP_TEXT("/"),
XP_TEXT("%")
};
2006-04-02 12:45:04 +00:00
static const xp_char_t* __unrop_str[] =
2006-03-31 12:04:14 +00:00
{
XP_TEXT("+"),
XP_TEXT("-"),
XP_TEXT("!"),
XP_TEXT("~")
2006-01-14 14:09:52 +00:00
};
2006-04-02 12:45:04 +00:00
static const xp_char_t* __incop_str[] =
{
XP_TEXT("++"),
XP_TEXT("--"),
XP_TEXT("++"),
XP_TEXT("--")
};
2006-01-14 14:09:52 +00:00
static void __print_tabs (int depth);
2006-03-04 15:54:37 +00:00
static int __print_expression (xp_awk_nde_t* nde);
static int __print_expression_list (xp_awk_nde_t* tree);
2006-03-03 11:45:45 +00:00
static void __print_statements (xp_awk_nde_t* tree, int depth);
2006-01-14 14:09:52 +00:00
static void __print_tabs (int depth)
{
int i;
for (i = 0; i < depth; i++) xp_printf (XP_TEXT("\t"));
}
2006-03-04 15:54:37 +00:00
static int __print_expression (xp_awk_nde_t* nde)
2006-01-14 14:09:52 +00:00
{
2006-03-03 11:45:45 +00:00
switch (nde->type) {
case XP_AWK_NDE_ASS:
2006-03-04 15:54:37 +00:00
if (__print_expression (((xp_awk_nde_ass_t*)nde)->left) == -1) return -1;
2006-01-20 07:33:46 +00:00
xp_printf (XP_TEXT(" = "));
2006-03-04 15:54:37 +00:00
if (__print_expression (((xp_awk_nde_ass_t*)nde)->right) == -1) return -1;
2006-03-03 11:45:45 +00:00
xp_assert ((((xp_awk_nde_ass_t*)nde)->right)->next == XP_NULL);
2006-01-14 14:09:52 +00:00
break;
2006-03-04 15:54:37 +00:00
case XP_AWK_NDE_EXP_BIN:
2006-01-14 14:09:52 +00:00
xp_printf (XP_TEXT("("));
2006-04-02 12:45:04 +00:00
if (__print_expression(((xp_awk_nde_exp_t*)nde)->left) == -1)
return -1;
2006-03-04 15:54:37 +00:00
xp_assert ((((xp_awk_nde_exp_t*)nde)->left)->next == XP_NULL);
2006-04-02 12:45:04 +00:00
xp_printf (XP_TEXT(" %s "),
__binop_str[((xp_awk_nde_exp_t*)nde)->opcode]);
if (((xp_awk_nde_exp_t*)nde)->right->type == XP_AWK_NDE_ASS)
xp_printf (XP_TEXT("("));
if (__print_expression (((xp_awk_nde_exp_t*)nde)->right) == -1)
return -1;
if (((xp_awk_nde_exp_t*)nde)->right->type == XP_AWK_NDE_ASS)
xp_printf (XP_TEXT(")"));
2006-03-04 15:54:37 +00:00
xp_assert ((((xp_awk_nde_exp_t*)nde)->right)->next == XP_NULL);
2006-01-14 14:09:52 +00:00
xp_printf (XP_TEXT(")"));
break;
2006-03-04 15:54:37 +00:00
case XP_AWK_NDE_EXP_UNR:
2006-04-02 12:45:04 +00:00
xp_assert (((xp_awk_nde_exp_t*)nde)->right == XP_NULL);
xp_printf (XP_TEXT("%s("),
__unrop_str[((xp_awk_nde_exp_t*)nde)->opcode]);
if (__print_expression (((xp_awk_nde_exp_t*)nde)->left) == -1)
return -1;
xp_printf (XP_TEXT(")"));
break;
case XP_AWK_NDE_EXP_INCPRE:
xp_assert (((xp_awk_nde_exp_t*)nde)->right == XP_NULL);
xp_printf (XP_TEXT("%s("),
__incop_str[((xp_awk_nde_exp_t*)nde)->opcode]);
if (__print_expression (((xp_awk_nde_exp_t*)nde)->left) == -1)
return -1;
xp_printf (XP_TEXT(")"));
break;
case XP_AWK_NDE_EXP_INCPST:
xp_assert (((xp_awk_nde_exp_t*)nde)->right == XP_NULL);
xp_printf (XP_TEXT("("));
if (__print_expression (((xp_awk_nde_exp_t*)nde)->left) == -1)
return -1;
xp_printf (XP_TEXT(")%s"),
__incop_str[((xp_awk_nde_exp_t*)nde)->opcode]);
2006-01-14 14:09:52 +00:00
break;
2006-03-05 17:07:33 +00:00
case XP_AWK_NDE_INT:
2006-03-15 15:34:59 +00:00
#if defined(__LCC__)
xp_printf (XP_TEXT("%lld"), (long long)((xp_awk_nde_int_t*)nde)->val);
#elif defined(__BORLANDC__) || defined(_MSC_VER)
2006-03-14 16:40:00 +00:00
xp_printf (XP_TEXT("%I64d"), (__int64)((xp_awk_nde_int_t*)nde)->val);
2006-03-15 15:34:59 +00:00
#elif defined(vax) || defined(__vax)
xp_printf (XP_TEXT("%ld"), (long)((xp_awk_nde_int_t*)nde)->val);
2006-03-05 17:07:33 +00:00
#else
xp_printf (XP_TEXT("%lld"), (long long)((xp_awk_nde_int_t*)nde)->val);
#endif
2006-01-14 14:09:52 +00:00
break;
2006-03-28 16:33:09 +00:00
case XP_AWK_NDE_REAL:
2006-04-04 16:50:36 +00:00
xp_printf (XP_TEXT("%Lf"), (long double)((xp_awk_nde_real_t*)nde)->val);
2006-03-28 16:33:09 +00:00
break;
2006-03-05 17:07:33 +00:00
case XP_AWK_NDE_STR:
// TODO: buf, len
xp_printf (XP_TEXT("\"%s\""), ((xp_awk_nde_str_t*)nde)->buf);
2006-01-14 14:09:52 +00:00
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_ARG:
xp_assert (((xp_awk_nde_var_t*)nde)->id.idxa != (xp_size_t)-1);
2006-02-05 13:45:59 +00:00
xp_printf (XP_TEXT("__arg%lu"),
2006-03-03 11:45:45 +00:00
(unsigned long)((xp_awk_nde_var_t*)nde)->id.idxa);
2006-01-31 16:57:45 +00:00
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_ARGIDX:
xp_assert (((xp_awk_nde_var_t*)nde)->id.idxa != (xp_size_t)-1);
2006-02-05 13:45:59 +00:00
xp_printf (XP_TEXT("__arg%lu["),
2006-03-03 11:45:45 +00:00
(unsigned long)((xp_awk_nde_idx_t*)nde)->id.idxa);
2006-03-04 15:54:37 +00:00
__print_expression (((xp_awk_nde_idx_t*)nde)->idx);
2006-01-31 16:57:45 +00:00
xp_printf (XP_TEXT("]"));
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_NAMED:
xp_assert (((xp_awk_nde_var_t*)nde)->id.idxa == (xp_size_t)-1);
xp_printf (XP_TEXT("%s"), ((xp_awk_nde_var_t*)nde)->id.name);
2006-02-05 16:00:33 +00:00
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_NAMEDIDX:
xp_assert (((xp_awk_nde_idx_t*)nde)->id.idxa == (xp_size_t)-1);
xp_printf (XP_TEXT("%s["), ((xp_awk_nde_idx_t*)nde)->id.name);
2006-03-04 15:54:37 +00:00
__print_expression (((xp_awk_nde_idx_t*)nde)->idx);
2006-02-05 16:00:33 +00:00
xp_printf (XP_TEXT("]"));
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_GLOBAL:
2006-03-04 15:54:37 +00:00
if (((xp_awk_nde_var_t*)nde)->id.idxa != (xp_size_t)-1)
{
2006-02-05 16:00:33 +00:00
xp_printf (XP_TEXT("__global%lu"),
2006-03-03 11:45:45 +00:00
(unsigned long)((xp_awk_nde_var_t*)nde)->id.idxa);
2006-02-05 16:00:33 +00:00
}
2006-03-04 15:54:37 +00:00
else
{
2006-03-03 11:45:45 +00:00
xp_printf (XP_TEXT("%s"), ((xp_awk_nde_var_t*)nde)->id.name);
2006-02-05 16:00:33 +00:00
}
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_GLOBALIDX:
2006-03-04 15:54:37 +00:00
if (((xp_awk_nde_idx_t*)nde)->id.idxa != (xp_size_t)-1)
{
2006-02-05 16:00:33 +00:00
xp_printf (XP_TEXT("__global%lu["),
2006-03-03 11:45:45 +00:00
(unsigned long)((xp_awk_nde_idx_t*)nde)->id.idxa);
2006-02-05 16:00:33 +00:00
}
2006-03-04 15:54:37 +00:00
else
{
2006-03-03 11:45:45 +00:00
xp_printf (XP_TEXT("%s["), ((xp_awk_nde_idx_t*)nde)->id.name);
2006-02-05 16:00:33 +00:00
}
2006-03-04 15:54:37 +00:00
__print_expression (((xp_awk_nde_idx_t*)nde)->idx);
2006-02-05 16:00:33 +00:00
xp_printf (XP_TEXT("]"));
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_LOCAL:
2006-03-04 15:54:37 +00:00
if (((xp_awk_nde_var_t*)nde)->id.idxa != (xp_size_t)-1)
{
2006-02-05 13:45:59 +00:00
xp_printf (XP_TEXT("__local%lu"),
2006-03-03 11:45:45 +00:00
(unsigned long)((xp_awk_nde_var_t*)nde)->id.idxa);
2006-02-05 13:45:59 +00:00
}
2006-03-04 15:54:37 +00:00
else
{
2006-03-03 11:45:45 +00:00
xp_printf (XP_TEXT("%s"), ((xp_awk_nde_var_t*)nde)->id.name);
2006-02-05 13:45:59 +00:00
}
2006-01-14 14:09:52 +00:00
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_LOCALIDX:
2006-03-04 15:54:37 +00:00
if (((xp_awk_nde_idx_t*)nde)->id.idxa != (xp_size_t)-1)
{
2006-02-05 13:45:59 +00:00
xp_printf (XP_TEXT("__local%lu["),
2006-03-03 11:45:45 +00:00
(unsigned long)((xp_awk_nde_idx_t*)nde)->id.idxa);
2006-02-05 13:45:59 +00:00
}
2006-03-04 15:54:37 +00:00
else
{
2006-03-03 11:45:45 +00:00
xp_printf (XP_TEXT("%s["), ((xp_awk_nde_idx_t*)nde)->id.name);
2006-02-05 13:45:59 +00:00
}
2006-03-04 15:54:37 +00:00
__print_expression (((xp_awk_nde_idx_t*)nde)->idx);
2006-01-19 16:28:21 +00:00
xp_printf (XP_TEXT("]"));
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_POS:
2006-01-25 14:50:57 +00:00
xp_printf (XP_TEXT("$"));
2006-03-05 17:07:33 +00:00
__print_expression (((xp_awk_nde_pos_t*)nde)->val);
2006-01-25 14:50:57 +00:00
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_CALL:
xp_printf (XP_TEXT("%s ("), ((xp_awk_nde_call_t*)nde)->name);
2006-03-04 15:54:37 +00:00
if (__print_expression_list (((xp_awk_nde_call_t*)nde)->args) == -1) return -1;
2006-01-14 14:09:52 +00:00
xp_printf (XP_TEXT(")"));
break;
default:
return -1;
}
return 0;
}
2006-03-04 15:54:37 +00:00
static int __print_expression_list (xp_awk_nde_t* tree)
2006-01-14 14:09:52 +00:00
{
2006-03-03 11:45:45 +00:00
xp_awk_nde_t* p = tree;
2006-01-14 14:09:52 +00:00
2006-03-04 15:54:37 +00:00
while (p != XP_NULL)
{
if (__print_expression(p) == -1) return -1;
2006-01-14 14:09:52 +00:00
p = p->next;
if (p != XP_NULL) xp_printf (XP_TEXT(","));
}
return 0;
}
2006-03-03 11:45:45 +00:00
static void __print_statements (xp_awk_nde_t* tree, int depth)
2006-01-14 14:09:52 +00:00
{
2006-03-03 11:45:45 +00:00
xp_awk_nde_t* p = tree;
2006-02-05 06:10:43 +00:00
xp_size_t i;
2006-01-14 14:09:52 +00:00
2006-03-04 15:54:37 +00:00
while (p != XP_NULL)
{
2006-01-14 14:09:52 +00:00
2006-03-04 15:54:37 +00:00
switch (p->type)
{
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_NULL:
2006-01-18 16:12:59 +00:00
__print_tabs (depth);
xp_printf (XP_TEXT(";\n"));
break;
2006-03-05 17:07:33 +00:00
case XP_AWK_NDE_BLK:
2006-01-14 14:09:52 +00:00
__print_tabs (depth);
xp_printf (XP_TEXT("{\n"));
2006-02-05 06:10:43 +00:00
2006-03-05 17:07:33 +00:00
if (((xp_awk_nde_blk_t*)p)->nlocals > 0) {
2006-02-04 19:31:51 +00:00
__print_tabs (depth + 1);
2006-02-05 06:10:43 +00:00
xp_printf (XP_TEXT("local "));
2006-03-05 17:07:33 +00:00
for (i = 0; i < ((xp_awk_nde_blk_t*)p)->nlocals - 1; i++) {
2006-02-05 13:45:59 +00:00
xp_printf (XP_TEXT("__local%lu, "), (unsigned long)i);
2006-02-05 06:10:43 +00:00
}
2006-02-05 13:45:59 +00:00
xp_printf (XP_TEXT("__local%lu;\n"), (unsigned long)i);
2006-02-04 19:31:51 +00:00
}
2006-02-05 06:10:43 +00:00
2006-03-05 17:07:33 +00:00
__print_statements (((xp_awk_nde_blk_t*)p)->body, depth + 1);
2006-01-14 14:09:52 +00:00
__print_tabs (depth);
xp_printf (XP_TEXT("}\n"));
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_IF:
2006-01-14 16:09:58 +00:00
__print_tabs (depth);
xp_printf (XP_TEXT("if ("));
2006-03-04 15:54:37 +00:00
__print_expression (((xp_awk_nde_if_t*)p)->test);
2006-01-15 06:51:35 +00:00
xp_printf (XP_TEXT(")\n"));
2006-01-14 16:09:58 +00:00
2006-03-03 11:45:45 +00:00
xp_assert (((xp_awk_nde_if_t*)p)->then_part != XP_NULL);
2006-03-05 17:07:33 +00:00
if (((xp_awk_nde_if_t*)p)->then_part->type == XP_AWK_NDE_BLK)
2006-03-03 11:45:45 +00:00
__print_statements (((xp_awk_nde_if_t*)p)->then_part, depth);
2006-01-19 10:56:35 +00:00
else
2006-03-03 11:45:45 +00:00
__print_statements (((xp_awk_nde_if_t*)p)->then_part, depth + 1);
2006-01-14 16:09:58 +00:00
2006-03-04 15:54:37 +00:00
if (((xp_awk_nde_if_t*)p)->else_part != XP_NULL)
{
2006-01-14 16:09:58 +00:00
__print_tabs (depth);
2006-01-15 06:51:35 +00:00
xp_printf (XP_TEXT("else\n"));
2006-03-05 17:07:33 +00:00
if (((xp_awk_nde_if_t*)p)->else_part->type == XP_AWK_NDE_BLK)
2006-03-03 11:45:45 +00:00
__print_statements (((xp_awk_nde_if_t*)p)->else_part, depth);
2006-01-19 10:56:35 +00:00
else
2006-03-03 11:45:45 +00:00
__print_statements (((xp_awk_nde_if_t*)p)->else_part, depth + 1);
2006-01-14 16:09:58 +00:00
}
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_WHILE:
2006-01-18 16:12:59 +00:00
__print_tabs (depth);
xp_printf (XP_TEXT("while ("));
2006-03-04 15:54:37 +00:00
__print_expression (((xp_awk_nde_while_t*)p)->test);
2006-01-18 16:12:59 +00:00
xp_printf (XP_TEXT(")\n"));
2006-03-05 17:07:33 +00:00
if (((xp_awk_nde_while_t*)p)->body->type == XP_AWK_NDE_BLK)
2006-03-04 15:54:37 +00:00
{
2006-03-03 11:45:45 +00:00
__print_statements (((xp_awk_nde_while_t*)p)->body, depth);
2006-01-19 10:56:35 +00:00
}
2006-03-04 15:54:37 +00:00
else
{
2006-03-03 11:45:45 +00:00
__print_statements (((xp_awk_nde_while_t*)p)->body, depth + 1);
2006-01-19 10:56:35 +00:00
}
break;
2006-01-18 16:12:59 +00:00
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_DOWHILE:
2006-01-19 10:56:35 +00:00
__print_tabs (depth);
xp_printf (XP_TEXT("do\n"));
2006-03-05 17:07:33 +00:00
if (((xp_awk_nde_while_t*)p)->body->type == XP_AWK_NDE_BLK)
2006-03-04 15:54:37 +00:00
{
2006-03-03 11:45:45 +00:00
__print_statements (((xp_awk_nde_while_t*)p)->body, depth);
2006-01-19 10:56:35 +00:00
}
2006-03-04 15:54:37 +00:00
else
{
2006-03-03 11:45:45 +00:00
__print_statements (((xp_awk_nde_while_t*)p)->body, depth + 1);
2006-01-19 10:56:35 +00:00
}
__print_tabs (depth);
xp_printf (XP_TEXT("while ("));
2006-03-04 15:54:37 +00:00
__print_expression (((xp_awk_nde_while_t*)p)->test);
2006-01-19 10:56:35 +00:00
xp_printf (XP_TEXT(");\n"));
2006-01-18 16:12:59 +00:00
break;
2006-01-14 16:09:58 +00:00
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_FOR:
2006-01-19 13:28:29 +00:00
__print_tabs (depth);
xp_printf (XP_TEXT("for ("));
2006-03-04 15:54:37 +00:00
if (((xp_awk_nde_for_t*)p)->init != XP_NULL)
{
__print_expression (((xp_awk_nde_for_t*)p)->init);
2006-01-19 13:28:29 +00:00
}
xp_printf (XP_TEXT("; "));
2006-03-04 15:54:37 +00:00
if (((xp_awk_nde_for_t*)p)->test != XP_NULL)
{
__print_expression (((xp_awk_nde_for_t*)p)->test);
2006-01-19 13:28:29 +00:00
}
xp_printf (XP_TEXT("; "));
2006-03-04 15:54:37 +00:00
if (((xp_awk_nde_for_t*)p)->incr != XP_NULL)
{
__print_expression (((xp_awk_nde_for_t*)p)->incr);
2006-01-19 13:28:29 +00:00
}
xp_printf (XP_TEXT(")\n"));
2006-03-05 17:07:33 +00:00
if (((xp_awk_nde_for_t*)p)->body->type == XP_AWK_NDE_BLK)
2006-03-04 15:54:37 +00:00
{
2006-03-03 11:45:45 +00:00
__print_statements (((xp_awk_nde_for_t*)p)->body, depth);
2006-01-19 13:28:29 +00:00
}
2006-03-04 15:54:37 +00:00
else
{
2006-03-03 11:45:45 +00:00
__print_statements (((xp_awk_nde_for_t*)p)->body, depth + 1);
2006-01-19 13:28:29 +00:00
}
2006-03-23 15:36:20 +00:00
break;
2006-01-19 13:28:29 +00:00
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_BREAK:
2006-01-14 14:09:52 +00:00
__print_tabs (depth);
xp_printf (XP_TEXT("break;\n"));
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_CONTINUE:
2006-01-14 14:09:52 +00:00
__print_tabs (depth);
xp_printf (XP_TEXT("continue;\n"));
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_RETURN:
2006-01-14 14:09:52 +00:00
__print_tabs (depth);
2006-03-05 17:07:33 +00:00
if (((xp_awk_nde_return_t*)p)->val == XP_NULL)
2006-03-04 15:54:37 +00:00
{
2006-01-28 06:38:01 +00:00
xp_printf (XP_TEXT("return;\n"));
2006-01-14 14:09:52 +00:00
}
2006-03-04 15:54:37 +00:00
else
{
2006-01-28 06:38:01 +00:00
xp_printf (XP_TEXT("return "));
2006-03-05 17:07:33 +00:00
xp_assert (((xp_awk_nde_return_t*)p)->val->next == XP_NULL);
if (__print_expression(((xp_awk_nde_return_t*)p)->val) == 0) {
2006-01-28 06:38:01 +00:00
xp_printf (XP_TEXT(";\n"));
}
2006-03-04 15:54:37 +00:00
else
{
xp_awk_nde_return_t* x = (xp_awk_nde_return_t*)p;
2006-03-03 11:45:45 +00:00
xp_printf (XP_TEXT("***INTERNAL ERROR: unknown nde type - %d\n"), x->type);
2006-01-28 06:38:01 +00:00
}
2006-01-14 14:09:52 +00:00
}
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_EXIT:
2006-01-14 14:09:52 +00:00
__print_tabs (depth);
2006-01-28 06:38:01 +00:00
2006-03-05 17:07:33 +00:00
if (((xp_awk_nde_exit_t*)p)->val == XP_NULL)
2006-03-04 15:54:37 +00:00
{
2006-01-28 06:38:01 +00:00
xp_printf (XP_TEXT("exit;\n"));
2006-01-14 14:09:52 +00:00
}
2006-03-04 15:54:37 +00:00
else
{
2006-01-28 06:38:01 +00:00
xp_printf (XP_TEXT("exit "));
2006-03-05 17:07:33 +00:00
xp_assert (((xp_awk_nde_exit_t*)p)->val->next == XP_NULL);
if (__print_expression(((xp_awk_nde_exit_t*)p)->val) == 0)
2006-03-04 15:54:37 +00:00
{
2006-01-28 06:38:01 +00:00
xp_printf (XP_TEXT(";\n"));
}
2006-03-04 15:54:37 +00:00
else
{
xp_awk_nde_exit_t* x = (xp_awk_nde_exit_t*)p;
2006-03-03 11:45:45 +00:00
xp_printf (XP_TEXT("***INTERNAL ERROR: unknown nde type - %d\n"), x->type);
2006-01-28 06:38:01 +00:00
}
2006-01-14 14:09:52 +00:00
}
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_NEXT:
2006-01-20 07:33:46 +00:00
__print_tabs (depth);
xp_printf (XP_TEXT("next;\n"));
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_NEXTFILE:
2006-01-20 07:33:46 +00:00
__print_tabs (depth);
xp_printf (XP_TEXT("nextfile;\n"));
break;
2006-01-14 14:09:52 +00:00
default:
__print_tabs (depth);
2006-03-04 15:54:37 +00:00
if (__print_expression(p) == 0)
{
2006-01-14 14:09:52 +00:00
xp_printf (XP_TEXT(";\n"));
}
2006-03-04 15:54:37 +00:00
else
{
2006-01-14 14:09:52 +00:00
xp_printf (XP_TEXT("***INTERNAL ERROR: unknown type - %d\n"), p->type);
}
}
p = p->next;
}
}
2006-03-03 11:45:45 +00:00
void xp_awk_prnpt (xp_awk_nde_t* tree)
2006-01-14 14:09:52 +00:00
{
__print_statements (tree, 0);
}
2006-03-03 11:45:45 +00:00
void xp_awk_clrpt (xp_awk_nde_t* tree)
2006-01-14 14:09:52 +00:00
{
2006-03-03 11:45:45 +00:00
xp_awk_nde_t* p = tree;
xp_awk_nde_t* next;
2006-01-14 14:09:52 +00:00
2006-03-04 15:54:37 +00:00
while (p != XP_NULL)
{
2006-01-14 14:09:52 +00:00
next = p->next;
2006-03-04 15:54:37 +00:00
switch (p->type)
{
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_NULL:
2006-01-18 16:12:59 +00:00
xp_free (p);
break;
2006-03-05 17:07:33 +00:00
case XP_AWK_NDE_BLK:
xp_awk_clrpt (((xp_awk_nde_blk_t*)p)->body);
2006-01-14 14:09:52 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_IF:
xp_awk_clrpt (((xp_awk_nde_if_t*)p)->test);
xp_awk_clrpt (((xp_awk_nde_if_t*)p)->then_part);
2006-01-19 13:28:29 +00:00
2006-03-03 11:45:45 +00:00
if (((xp_awk_nde_if_t*)p)->else_part != XP_NULL)
xp_awk_clrpt (((xp_awk_nde_if_t*)p)->else_part);
2006-01-14 16:09:58 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_WHILE:
case XP_AWK_NDE_DOWHILE:
xp_awk_clrpt (((xp_awk_nde_while_t*)p)->test);
xp_awk_clrpt (((xp_awk_nde_while_t*)p)->body);
2006-01-19 13:28:29 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_FOR:
if (((xp_awk_nde_for_t*)p)->init != XP_NULL)
xp_awk_clrpt (((xp_awk_nde_for_t*)p)->init);
if (((xp_awk_nde_for_t*)p)->test != XP_NULL)
xp_awk_clrpt (((xp_awk_nde_for_t*)p)->test);
if (((xp_awk_nde_for_t*)p)->incr != XP_NULL)
xp_awk_clrpt (((xp_awk_nde_for_t*)p)->incr);
xp_awk_clrpt (((xp_awk_nde_for_t*)p)->body);
2006-01-18 16:12:59 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_BREAK:
2006-03-04 15:54:37 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_CONTINUE:
2006-03-04 15:54:37 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_NEXT:
case XP_AWK_NDE_NEXTFILE:
2006-01-14 14:09:52 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_RETURN:
2006-03-05 17:07:33 +00:00
if (((xp_awk_nde_return_t*)p)->val != XP_NULL)
xp_awk_clrpt (((xp_awk_nde_return_t*)p)->val);
2006-03-04 15:54:37 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_EXIT:
2006-03-05 17:07:33 +00:00
if (((xp_awk_nde_exit_t*)p)->val != XP_NULL)
xp_awk_clrpt (((xp_awk_nde_exit_t*)p)->val);
2006-01-14 14:09:52 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_ASS:
xp_awk_clrpt (((xp_awk_nde_ass_t*)p)->left);
xp_awk_clrpt (((xp_awk_nde_ass_t*)p)->right);
2006-01-14 14:09:52 +00:00
xp_free (p);
break;
2006-03-04 15:54:37 +00:00
case XP_AWK_NDE_EXP_BIN:
xp_assert ((((xp_awk_nde_exp_t*)p)->left)->next == XP_NULL);
xp_assert ((((xp_awk_nde_exp_t*)p)->right)->next == XP_NULL);
2006-01-14 14:09:52 +00:00
2006-03-04 15:54:37 +00:00
xp_awk_clrpt (((xp_awk_nde_exp_t*)p)->left);
xp_awk_clrpt (((xp_awk_nde_exp_t*)p)->right);
2006-01-14 14:09:52 +00:00
xp_free (p);
break;
2006-03-04 15:54:37 +00:00
case XP_AWK_NDE_EXP_UNR:
2006-04-02 12:45:04 +00:00
case XP_AWK_NDE_EXP_INCPRE:
case XP_AWK_NDE_EXP_INCPST:
xp_assert (((xp_awk_nde_exp_t*)p)->right == XP_NULL);
xp_awk_clrpt (((xp_awk_nde_exp_t*)p)->left);
2006-01-14 14:09:52 +00:00
xp_free (p);
break;
2006-03-05 17:07:33 +00:00
case XP_AWK_NDE_INT:
xp_free (p);
break;
2006-03-28 16:33:09 +00:00
case XP_AWK_NDE_REAL:
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_STR:
2006-03-05 17:07:33 +00:00
xp_free (((xp_awk_nde_str_t*)p)->buf);
2006-01-14 14:09:52 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_NAMED:
xp_assert (((xp_awk_nde_idx_t*)p)->id.name != XP_NULL);
case XP_AWK_NDE_GLOBAL:
case XP_AWK_NDE_LOCAL:
case XP_AWK_NDE_ARG:
if (((xp_awk_nde_var_t*)p)->id.name != XP_NULL)
xp_free (((xp_awk_nde_var_t*)p)->id.name);
2006-01-31 16:57:45 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_NAMEDIDX:
xp_assert (((xp_awk_nde_idx_t*)p)->id.name != XP_NULL);
case XP_AWK_NDE_GLOBALIDX:
case XP_AWK_NDE_LOCALIDX:
case XP_AWK_NDE_ARGIDX:
xp_awk_clrpt (((xp_awk_nde_idx_t*)p)->idx);
if (((xp_awk_nde_idx_t*)p)->id.name != XP_NULL)
xp_free (((xp_awk_nde_idx_t*)p)->id.name);
2006-01-31 16:57:45 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_POS:
2006-03-05 17:07:33 +00:00
xp_assert (((xp_awk_nde_pos_t*)p)->val != XP_NULL);
xp_awk_clrpt (((xp_awk_nde_pos_t*)p)->val);
2006-01-25 14:50:57 +00:00
xp_free (p);
break;
2006-03-03 11:45:45 +00:00
case XP_AWK_NDE_CALL:
xp_free (((xp_awk_nde_call_t*)p)->name);
xp_awk_clrpt (((xp_awk_nde_call_t*)p)->args);
2006-01-14 14:09:52 +00:00
xp_free (p);
break;
default:
xp_assert (XP_TEXT("shoud not happen") == XP_TEXT(" here"));
}
p = next;
}
}