*** empty log message ***

This commit is contained in:
hyung-hwan 2006-03-04 15:54:37 +00:00
parent 4ad38025b8
commit 105878c6cf
9 changed files with 412 additions and 211 deletions

View File

@ -1,4 +1,4 @@
SRCS = awk.c err.c tree.c tab.c map.c parse.c run.c sa.c
SRCS = awk.c err.c tree.c tab.c map.c parse.c run.c sa.c val.c
OBJS = $(SRCS:.c=.obj)
OUT = xpawk.lib

View File

@ -1,5 +1,5 @@
/*
* $Id: err.h,v 1.1 2006-03-04 10:08:13 bacon Exp $
* $Id: err.h,v 1.2 2006-03-04 15:54:37 bacon Exp $
*/
#ifndef _XP_AWK_ERR_H_
@ -28,10 +28,10 @@ enum
XP_AWK_ERBRACK, /* right bracket expected */
XP_AWK_ECOMMA, /* comma expected */
XP_AWK_ESEMICOLON, /* semicolon expected */
XP_AWK_EEXPR, /* expression expected */
XP_AWK_EEXPRESSION, /* expression expected */
XP_AWK_EWHILE, /* keyword 'while' is expected */
XP_AWK_EASSSTM, /* assignment statement expected */
XP_AWK_EASSIGNMENT, /* assignment statement expected */
XP_AWK_EIDENT, /* identifier expected */
XP_AWK_EDUPBEGIN, /* duplicate BEGIN */
XP_AWK_EDUPEND, /* duplicate END */

View File

@ -1,4 +1,4 @@
SRCS = awk.c err.c tree.c tab.c map.c parse.c run.c sa.c
SRCS = awk.c err.c tree.c tab.c map.c parse.c run.c sa.c val.c
OBJS = $(SRCS:.c=.o)
OUT = libxpawk.a

View File

@ -1,5 +1,5 @@
/*
* $Id: parse.c,v 1.56 2006-03-04 10:06:49 bacon Exp $
* $Id: parse.c,v 1.57 2006-03-04 15:54:37 bacon Exp $
*/
#include <xp/awk/awk.h>
@ -1006,7 +1006,7 @@ static xp_awk_nde_t* __parse_expression (xp_awk_t* awk)
x->type != XP_AWK_NDE_POS)
{
xp_awk_clrpt (x);
PANIC (awk, XP_AWK_EASSSTM);
PANIC (awk, XP_AWK_EASSIGNMENT);
}
if (__get_token(awk) == -1)
@ -1054,7 +1054,7 @@ static xp_awk_nde_t* __parse_basic_expr (xp_awk_t* awk)
static xp_awk_nde_t* __parse_equality (xp_awk_t* awk)
{
xp_awk_nde_expr_t* nde;
xp_awk_nde_exp_t* nde;
xp_awk_nde_t* left, * right;
int opcode;
@ -1082,7 +1082,7 @@ static xp_awk_nde_t* __parse_equality (xp_awk_t* awk)
// TODO: constant folding -> in other parts of the program also...
nde = (xp_awk_nde_expr_t*)xp_malloc(xp_sizeof(xp_awk_nde_expr_t));
nde = (xp_awk_nde_exp_t*)xp_malloc(xp_sizeof(xp_awk_nde_exp_t));
if (nde == XP_NULL)
{
xp_awk_clrpt (right);
@ -1090,7 +1090,7 @@ static xp_awk_nde_t* __parse_equality (xp_awk_t* awk)
PANIC (awk, XP_AWK_ENOMEM);
}
nde->type = XP_AWK_NDE_BINARY;
nde->type = XP_AWK_NDE_EXP_BIN;
nde->next = XP_NULL;
nde->opcode = opcode;
nde->left = left;
@ -1104,7 +1104,7 @@ static xp_awk_nde_t* __parse_equality (xp_awk_t* awk)
static xp_awk_nde_t* __parse_relational (xp_awk_t* awk)
{
xp_awk_nde_expr_t* nde;
xp_awk_nde_exp_t* nde;
xp_awk_nde_t* left, * right;
int opcode;
@ -1134,7 +1134,7 @@ static xp_awk_nde_t* __parse_relational (xp_awk_t* awk)
// TODO: constant folding -> in other parts of the program also...
nde = (xp_awk_nde_expr_t*)xp_malloc(xp_sizeof(xp_awk_nde_expr_t));
nde = (xp_awk_nde_exp_t*)xp_malloc(xp_sizeof(xp_awk_nde_exp_t));
if (nde == XP_NULL)
{
xp_awk_clrpt (right);
@ -1142,7 +1142,7 @@ static xp_awk_nde_t* __parse_relational (xp_awk_t* awk)
PANIC (awk, XP_AWK_ENOMEM);
}
nde->type = XP_AWK_NDE_BINARY;
nde->type = XP_AWK_NDE_EXP_BIN;
nde->next = XP_NULL;
nde->opcode = opcode;
nde->left = left;
@ -1156,7 +1156,7 @@ static xp_awk_nde_t* __parse_relational (xp_awk_t* awk)
static xp_awk_nde_t* __parse_shift (xp_awk_t* awk)
{
xp_awk_nde_expr_t* nde;
xp_awk_nde_exp_t* nde;
xp_awk_nde_t* left, * right;
int opcode;
@ -1184,7 +1184,7 @@ static xp_awk_nde_t* __parse_shift (xp_awk_t* awk)
// TODO: constant folding -> in other parts of the program also...
nde = (xp_awk_nde_expr_t*)xp_malloc(xp_sizeof(xp_awk_nde_expr_t));
nde = (xp_awk_nde_exp_t*)xp_malloc(xp_sizeof(xp_awk_nde_exp_t));
if (nde == XP_NULL)
{
xp_awk_clrpt (right);
@ -1192,7 +1192,7 @@ static xp_awk_nde_t* __parse_shift (xp_awk_t* awk)
PANIC (awk, XP_AWK_ENOMEM);
}
nde->type = XP_AWK_NDE_BINARY;
nde->type = XP_AWK_NDE_EXP_BIN;
nde->next = XP_NULL;
nde->opcode = opcode;
nde->left = left;
@ -1206,7 +1206,7 @@ static xp_awk_nde_t* __parse_shift (xp_awk_t* awk)
static xp_awk_nde_t* __parse_additive (xp_awk_t* awk)
{
xp_awk_nde_expr_t* nde;
xp_awk_nde_exp_t* nde;
xp_awk_nde_t* left, * right;
int opcode;
@ -1234,7 +1234,7 @@ static xp_awk_nde_t* __parse_additive (xp_awk_t* awk)
// TODO: constant folding -> in other parts of the program also...
nde = (xp_awk_nde_expr_t*)xp_malloc(xp_sizeof(xp_awk_nde_expr_t));
nde = (xp_awk_nde_exp_t*)xp_malloc(xp_sizeof(xp_awk_nde_exp_t));
if (nde == XP_NULL)
{
xp_awk_clrpt (right);
@ -1242,7 +1242,7 @@ static xp_awk_nde_t* __parse_additive (xp_awk_t* awk)
PANIC (awk, XP_AWK_ENOMEM);
}
nde->type = XP_AWK_NDE_BINARY;
nde->type = XP_AWK_NDE_EXP_BIN;
nde->next = XP_NULL;
nde->opcode = opcode;
nde->left = left;
@ -1256,7 +1256,7 @@ static xp_awk_nde_t* __parse_additive (xp_awk_t* awk)
static xp_awk_nde_t* __parse_multiplicative (xp_awk_t* awk)
{
xp_awk_nde_expr_t* nde;
xp_awk_nde_exp_t* nde;
xp_awk_nde_t* left, * right;
int opcode;
@ -1289,11 +1289,11 @@ static xp_awk_nde_t* __parse_multiplicative (xp_awk_t* awk)
right->type == XP_AWK_NDE_NUM)
{
xp_long_t l, r;
xp_awk_nde_term_t* tmp;
xp_awk_nde_trm_t* tmp;
xp_char_t buf[256];
l = __str_to_long (((xp_awk_nde_term_t*)left)->value);
r = __str_to_long (((xp_awk_nde_term_t*)right)->value);
l = __str_to_long (((xp_awk_nde_trm_t*)left)->value);
r = __str_to_long (((xp_awk_nde_trm_t*)right)->value);
xp_awk_clrpt (left);
xp_awk_clrpt (right);
@ -1308,7 +1308,7 @@ static xp_awk_nde_t* __parse_multiplicative (xp_awk_t* awk)
xp_sprintf (buf, xp_countof(buf), XP_TEXT("%lld"), (long long)l);
#endif
tmp = (xp_awk_nde_term_t*) xp_malloc (xp_sizeof(xp_awk_nde_term_t));
tmp = (xp_awk_nde_trm_t*) xp_malloc (xp_sizeof(xp_awk_nde_trm_t));
if (tmp == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
tmp->type = XP_AWK_NDE_NUM;
@ -1325,7 +1325,7 @@ static xp_awk_nde_t* __parse_multiplicative (xp_awk_t* awk)
continue;
}
nde = (xp_awk_nde_expr_t*)xp_malloc(xp_sizeof(xp_awk_nde_expr_t));
nde = (xp_awk_nde_exp_t*)xp_malloc(xp_sizeof(xp_awk_nde_exp_t));
if (nde == XP_NULL)
{
xp_awk_clrpt (right);
@ -1333,7 +1333,7 @@ static xp_awk_nde_t* __parse_multiplicative (xp_awk_t* awk)
PANIC (awk, XP_AWK_ENOMEM);
}
nde->type = XP_AWK_NDE_BINARY;
nde->type = XP_AWK_NDE_EXP_BIN;
nde->next = XP_NULL;
nde->opcode = opcode;
nde->left = left;
@ -1449,9 +1449,9 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk)
}
else if (MATCH(awk,TOKEN_INTEGER))
{
xp_awk_nde_term_t* nde;
xp_awk_nde_trm_t* nde;
nde = (xp_awk_nde_term_t*)xp_malloc(xp_sizeof(xp_awk_nde_term_t));
nde = (xp_awk_nde_trm_t*)xp_malloc(xp_sizeof(xp_awk_nde_trm_t));
if (nde == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
nde->type = XP_AWK_NDE_NUM;
@ -1473,9 +1473,9 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk)
return (xp_awk_nde_t*)nde;
}
else if (MATCH(awk,TOKEN_STRING)) {
xp_awk_nde_term_t* nde;
xp_awk_nde_trm_t* nde;
nde = (xp_awk_nde_term_t*)xp_malloc(xp_sizeof(xp_awk_nde_term_t));
nde = (xp_awk_nde_trm_t*)xp_malloc(xp_sizeof(xp_awk_nde_trm_t));
if (nde == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
nde->type = XP_AWK_NDE_STR;
@ -1498,7 +1498,7 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk)
}
else if (MATCH(awk,TOKEN_DOLLAR))
{
xp_awk_nde_sgv_t* nde;
xp_awk_nde_pos_t* nde;
xp_awk_nde_t* prim;
if (__get_token(awk)) return XP_NULL;
@ -1506,7 +1506,7 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk)
prim = __parse_primary (awk);
if (prim == XP_NULL) return XP_NULL;
nde = (xp_awk_nde_sgv_t*) xp_malloc (xp_sizeof(xp_awk_nde_sgv_t));
nde = (xp_awk_nde_pos_t*) xp_malloc (xp_sizeof(xp_awk_nde_pos_t));
if (nde == XP_NULL)
{
xp_awk_clrpt (prim);
@ -1547,7 +1547,7 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk)
}
/* valid expression introducer is expected */
PANIC (awk, XP_AWK_EEXPR);
PANIC (awk, XP_AWK_EEXPRESSION);
}
static xp_awk_nde_t* __parse_hashidx (xp_awk_t* awk, xp_char_t* name)
@ -2004,34 +2004,34 @@ static xp_awk_nde_t* __parse_dowhile (xp_awk_t* awk)
static xp_awk_nde_t* __parse_break (xp_awk_t* awk)
{
xp_awk_nde_t* nde;
xp_awk_nde_break_t* nde;
nde = (xp_awk_nde_t*) xp_malloc (xp_sizeof(xp_awk_nde_t));
nde = (xp_awk_nde_break_t*) xp_malloc (xp_sizeof(xp_awk_nde_break_t));
if (nde == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
nde->type = XP_AWK_NDE_BREAK;
nde->next = XP_NULL;
return nde;
return (xp_awk_nde_t*)nde;
}
static xp_awk_nde_t* __parse_continue (xp_awk_t* awk)
{
xp_awk_nde_t* nde;
xp_awk_nde_continue_t* nde;
nde = (xp_awk_nde_t*) xp_malloc (xp_sizeof(xp_awk_nde_t));
nde = (xp_awk_nde_continue_t*) xp_malloc (xp_sizeof(xp_awk_nde_continue_t));
if (nde == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
nde->type = XP_AWK_NDE_CONTINUE;
nde->next = XP_NULL;
return nde;
return (xp_awk_nde_t*)nde;
}
static xp_awk_nde_t* __parse_return (xp_awk_t* awk)
{
xp_awk_nde_sgv_t* nde;
xp_awk_nde_return_t* nde;
xp_awk_nde_t* val;
nde = (xp_awk_nde_sgv_t*) xp_malloc (xp_sizeof(xp_awk_nde_sgv_t));
nde = (xp_awk_nde_return_t*) xp_malloc (xp_sizeof(xp_awk_nde_return_t));
if (nde == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
nde->type = XP_AWK_NDE_RETURN;
nde->next = XP_NULL;
@ -2043,7 +2043,6 @@ static xp_awk_nde_t* __parse_return (xp_awk_t* awk)
}
else
{
val = __parse_expression (awk);
if (val == XP_NULL)
{
@ -2058,10 +2057,10 @@ static xp_awk_nde_t* __parse_return (xp_awk_t* awk)
static xp_awk_nde_t* __parse_exit (xp_awk_t* awk)
{
xp_awk_nde_sgv_t* nde;
xp_awk_nde_exit_t* nde;
xp_awk_nde_t* val;
nde = (xp_awk_nde_sgv_t*) xp_malloc (xp_sizeof(xp_awk_nde_sgv_t));
nde = (xp_awk_nde_exit_t*) xp_malloc (xp_sizeof(xp_awk_nde_exit_t));
if (nde == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
nde->type = XP_AWK_NDE_EXIT;
nde->next = XP_NULL;

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c,v 1.5 2006-03-03 11:45:45 bacon Exp $
* $Id: run.c,v 1.6 2006-03-04 15:54:37 bacon Exp $
*/
#include <xp/awk/awk.h>
@ -10,9 +10,9 @@
static int __run_block (xp_awk_t* awk, xp_awk_nde_t* nde);
static int __run_statement (xp_awk_t* awk, xp_awk_nde_t* nde);
static int __run_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde);
static xp_awk_val_t* __eval_expr (xp_awk_t* awk, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_expression (xp_awk_t* awk, xp_awk_nde_t* nde);
static xp_awk_val_t* __eval_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde);
int xp_awk_run (xp_awk_t* awk)
{
@ -58,7 +58,6 @@ static int __run_statement (xp_awk_t* awk, xp_awk_nde_t* nde)
if (__run_block(awk, nde) == -1) return -1;
break;
#if 0
case XP_AWK_NDE_IF:
break;
case XP_AWK_NDE_WHILE:
@ -84,43 +83,96 @@ static int __run_statement (xp_awk_t* awk, xp_awk_nde_t* nde)
case XP_AWK_NDE_NEXTFILE:
break;
#endif
case XP_AWK_NDE_ASS:
if (__run_assignment (
awk, (xp_awk_nde_ass_t*)nde) == -1) return -1;
break;
#if 0
case XP_AWK_NDE_NUM:
break;
#endif
default:
/* this should never be reached */
// TODO: set errnum ....
return -1;
if (__eval_expression(awk,nde) == XP_NULL) return -1;
break;
}
return 0;
}
static int __run_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde)
static xp_awk_val_t* __eval_expression (xp_awk_t* awk, xp_awk_nde_t* nde)
{
xp_awk_val_t* val;
switch (nde->type) {
case XP_AWK_NDE_ASS:
val = __eval_assignment(awk,(xp_awk_nde_ass_t*)nde);
break;
case XP_AWK_NDE_EXP_BIN:
case XP_AWK_NDE_EXP_UNR:
case XP_AWK_NDE_STR:
break;
case XP_AWK_NDE_NUM:
// TODO: int, real...
val = xp_awk_makeintval();
break;
case XP_AWK_NDE_ARG:
case XP_AWK_NDE_ARGIDX:
case XP_AWK_NDE_NAMED:
case XP_AWK_NDE_NAMEDIDX:
case XP_AWK_NDE_GLOBAL:
case XP_AWK_NDE_GLOBALIDX:
case XP_AWK_NDE_LOCAL:
case XP_AWK_NDE_LOCALIDX:
case XP_AWK_NDE_POS:
case XP_AWK_NDE_CALL:
break;
default:
/* somthing wrong */
return XP_NULL;
}
return val;
}
static xp_awk_val_t* __eval_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde)
{
xp_awk_val_t* v;
if (nde->type == XP_AWK_NDE_NAMED)
{
xp_awk_nde_var_t* left = (xp_awk_nde_var_t*)nde->left;
xp_awk_val_t* right = __eval_expr (awk, nde->right);
xp_awk_nde_var_t* tgt;
xp_awk_val_t* old, * new;
xp_assert (left != XP_NULL);
if (right == XP_NULL) return -1;
tgt = (xp_awk_nde_var_t*)nde->left;
new = __eval_expression (awk, nde->right);
xp_assert (tgt != XP_NULL);
if (new == XP_NULL) return XP_NULL;
old = (xp_awk_val_t*) xp_awk_map_get (&awk->run.named, tgt->id.name);
if (xp_awk_map_put (
&awk->run.named, left->id.name, right) == XP_NULL)
&awk->run.named, tgt->id.name, new) == XP_NULL)
{
xp_awk_freeval (new);
awk->errnum = XP_AWK_ENOMEM;
return -1;
return XP_NULL;
}
else if (old != XP_NULL)
{
/* free the old value that has been assigned to the variable */
xp_awk_freeval (old);
}
v = new;
}
else if (nde->type == XP_AWK_NDE_GLOBAL)
{
@ -146,15 +198,10 @@ static int __run_assignment (xp_awk_t* awk, xp_awk_nde_ass_t* nde)
}
else
{
/* this should never be reached */
/* this should never be reached. something wrong */
// TODO: set errnum ....
return -1;
}
return 0;
}
static xp_awk_val_t* __eval_expr (xp_awk_t* awk, xp_awk_nde_t* nde)
{
return XP_NULL;
}
return v;
}

View File

@ -1,5 +1,5 @@
/*
* $Id: tree.c,v 1.21 2006-03-03 11:45:45 bacon Exp $
* $Id: tree.c,v 1.22 2006-03-04 15:54:37 bacon Exp $
*/
#include <xp/awk/awk.h>
@ -28,8 +28,8 @@ static const xp_char_t* __binop_str[] =
};
static void __print_tabs (int depth);
static int __print_expr_nde (xp_awk_nde_t* nde);
static int __print_expr_nde_list (xp_awk_nde_t* tree);
static int __print_expression (xp_awk_nde_t* nde);
static int __print_expression_list (xp_awk_nde_t* tree);
static void __print_statements (xp_awk_nde_t* tree, int depth);
static void __print_tabs (int depth)
@ -38,39 +38,39 @@ static void __print_tabs (int depth)
for (i = 0; i < depth; i++) xp_printf (XP_TEXT("\t"));
}
static int __print_expr_nde (xp_awk_nde_t* nde)
static int __print_expression (xp_awk_nde_t* nde)
{
switch (nde->type) {
case XP_AWK_NDE_ASS:
if (__print_expr_nde (((xp_awk_nde_ass_t*)nde)->left) == -1) return -1;
if (__print_expression (((xp_awk_nde_ass_t*)nde)->left) == -1) return -1;
xp_printf (XP_TEXT(" = "));
if (__print_expr_nde (((xp_awk_nde_ass_t*)nde)->right) == -1) return -1;
if (__print_expression (((xp_awk_nde_ass_t*)nde)->right) == -1) return -1;
xp_assert ((((xp_awk_nde_ass_t*)nde)->right)->next == XP_NULL);
break;
case XP_AWK_NDE_BINARY:
case XP_AWK_NDE_EXP_BIN:
xp_printf (XP_TEXT("("));
if (__print_expr_nde (((xp_awk_nde_expr_t*)nde)->left) == -1) return -1;
xp_assert ((((xp_awk_nde_expr_t*)nde)->left)->next == XP_NULL);
xp_printf (XP_TEXT(" %s "), __binop_str[((xp_awk_nde_expr_t*)nde)->opcode]);
if (((xp_awk_nde_expr_t*)nde)->right->type == XP_AWK_NDE_ASS) xp_printf (XP_TEXT("("));
if (__print_expr_nde (((xp_awk_nde_expr_t*)nde)->right) == -1) return -1;
if (((xp_awk_nde_expr_t*)nde)->right->type == XP_AWK_NDE_ASS) xp_printf (XP_TEXT(")"));
xp_assert ((((xp_awk_nde_expr_t*)nde)->right)->next == XP_NULL);
if (__print_expression (((xp_awk_nde_exp_t*)nde)->left) == -1) return -1;
xp_assert ((((xp_awk_nde_exp_t*)nde)->left)->next == XP_NULL);
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(")"));
xp_assert ((((xp_awk_nde_exp_t*)nde)->right)->next == XP_NULL);
xp_printf (XP_TEXT(")"));
break;
case XP_AWK_NDE_UNARY:
case XP_AWK_NDE_EXP_UNR:
// TODO:
xp_printf (XP_TEXT("unary basic expression\n"));
break;
case XP_AWK_NDE_STR:
xp_printf (XP_TEXT("\"%s\""), ((xp_awk_nde_term_t*)nde)->value);
xp_printf (XP_TEXT("\"%s\""), ((xp_awk_nde_trm_t*)nde)->value);
break;
case XP_AWK_NDE_NUM:
xp_printf (XP_TEXT("%s"), ((xp_awk_nde_term_t*)nde)->value);
xp_printf (XP_TEXT("%s"), ((xp_awk_nde_trm_t*)nde)->value);
break;
case XP_AWK_NDE_ARG:
@ -83,7 +83,7 @@ static int __print_expr_nde (xp_awk_nde_t* nde)
xp_assert (((xp_awk_nde_var_t*)nde)->id.idxa != (xp_size_t)-1);
xp_printf (XP_TEXT("__arg%lu["),
(unsigned long)((xp_awk_nde_idx_t*)nde)->id.idxa);
__print_expr_nde (((xp_awk_nde_idx_t*)nde)->idx);
__print_expression (((xp_awk_nde_idx_t*)nde)->idx);
xp_printf (XP_TEXT("]"));
break;
@ -95,62 +95,70 @@ static int __print_expr_nde (xp_awk_nde_t* nde)
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);
__print_expr_nde (((xp_awk_nde_idx_t*)nde)->idx);
__print_expression (((xp_awk_nde_idx_t*)nde)->idx);
xp_printf (XP_TEXT("]"));
break;
case XP_AWK_NDE_GLOBAL:
if (((xp_awk_nde_var_t*)nde)->id.idxa != (xp_size_t)-1) {
if (((xp_awk_nde_var_t*)nde)->id.idxa != (xp_size_t)-1)
{
xp_printf (XP_TEXT("__global%lu"),
(unsigned long)((xp_awk_nde_var_t*)nde)->id.idxa);
}
else {
else
{
xp_printf (XP_TEXT("%s"), ((xp_awk_nde_var_t*)nde)->id.name);
}
break;
case XP_AWK_NDE_GLOBALIDX:
if (((xp_awk_nde_idx_t*)nde)->id.idxa != (xp_size_t)-1) {
if (((xp_awk_nde_idx_t*)nde)->id.idxa != (xp_size_t)-1)
{
xp_printf (XP_TEXT("__global%lu["),
(unsigned long)((xp_awk_nde_idx_t*)nde)->id.idxa);
}
else {
else
{
xp_printf (XP_TEXT("%s["), ((xp_awk_nde_idx_t*)nde)->id.name);
}
__print_expr_nde (((xp_awk_nde_idx_t*)nde)->idx);
__print_expression (((xp_awk_nde_idx_t*)nde)->idx);
xp_printf (XP_TEXT("]"));
break;
case XP_AWK_NDE_LOCAL:
if (((xp_awk_nde_var_t*)nde)->id.idxa != (xp_size_t)-1) {
if (((xp_awk_nde_var_t*)nde)->id.idxa != (xp_size_t)-1)
{
xp_printf (XP_TEXT("__local%lu"),
(unsigned long)((xp_awk_nde_var_t*)nde)->id.idxa);
}
else {
else
{
xp_printf (XP_TEXT("%s"), ((xp_awk_nde_var_t*)nde)->id.name);
}
break;
case XP_AWK_NDE_LOCALIDX:
if (((xp_awk_nde_idx_t*)nde)->id.idxa != (xp_size_t)-1) {
if (((xp_awk_nde_idx_t*)nde)->id.idxa != (xp_size_t)-1)
{
xp_printf (XP_TEXT("__local%lu["),
(unsigned long)((xp_awk_nde_idx_t*)nde)->id.idxa);
}
else {
else
{
xp_printf (XP_TEXT("%s["), ((xp_awk_nde_idx_t*)nde)->id.name);
}
__print_expr_nde (((xp_awk_nde_idx_t*)nde)->idx);
__print_expression (((xp_awk_nde_idx_t*)nde)->idx);
xp_printf (XP_TEXT("]"));
break;
case XP_AWK_NDE_POS:
xp_printf (XP_TEXT("$"));
__print_expr_nde (((xp_awk_nde_sgv_t*)nde)->value);
__print_expression (((xp_awk_nde_pos_t*)nde)->value);
break;
case XP_AWK_NDE_CALL:
xp_printf (XP_TEXT("%s ("), ((xp_awk_nde_call_t*)nde)->name);
if (__print_expr_nde_list (((xp_awk_nde_call_t*)nde)->args) == -1) return -1;
if (__print_expression_list (((xp_awk_nde_call_t*)nde)->args) == -1) return -1;
xp_printf (XP_TEXT(")"));
break;
@ -161,12 +169,13 @@ static int __print_expr_nde (xp_awk_nde_t* nde)
return 0;
}
static int __print_expr_nde_list (xp_awk_nde_t* tree)
static int __print_expression_list (xp_awk_nde_t* tree)
{
xp_awk_nde_t* p = tree;
while (p != XP_NULL) {
if (__print_expr_nde (p) == -1) return -1;
while (p != XP_NULL)
{
if (__print_expression(p) == -1) return -1;
p = p->next;
if (p != XP_NULL) xp_printf (XP_TEXT(","));
}
@ -179,9 +188,11 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
xp_awk_nde_t* p = tree;
xp_size_t i;
while (p != XP_NULL) {
while (p != XP_NULL)
{
switch (p->type) {
switch (p->type)
{
case XP_AWK_NDE_NULL:
__print_tabs (depth);
xp_printf (XP_TEXT(";\n"));
@ -209,7 +220,7 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
case XP_AWK_NDE_IF:
__print_tabs (depth);
xp_printf (XP_TEXT("if ("));
__print_expr_nde (((xp_awk_nde_if_t*)p)->test);
__print_expression (((xp_awk_nde_if_t*)p)->test);
xp_printf (XP_TEXT(")\n"));
xp_assert (((xp_awk_nde_if_t*)p)->then_part != XP_NULL);
@ -218,7 +229,8 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
else
__print_statements (((xp_awk_nde_if_t*)p)->then_part, depth + 1);
if (((xp_awk_nde_if_t*)p)->else_part != XP_NULL) {
if (((xp_awk_nde_if_t*)p)->else_part != XP_NULL)
{
__print_tabs (depth);
xp_printf (XP_TEXT("else\n"));
if (((xp_awk_nde_if_t*)p)->else_part->type == XP_AWK_NDE_BLOCK)
@ -230,12 +242,14 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
case XP_AWK_NDE_WHILE:
__print_tabs (depth);
xp_printf (XP_TEXT("while ("));
__print_expr_nde (((xp_awk_nde_while_t*)p)->test);
__print_expression (((xp_awk_nde_while_t*)p)->test);
xp_printf (XP_TEXT(")\n"));
if (((xp_awk_nde_while_t*)p)->body->type == XP_AWK_NDE_BLOCK) {
if (((xp_awk_nde_while_t*)p)->body->type == XP_AWK_NDE_BLOCK)
{
__print_statements (((xp_awk_nde_while_t*)p)->body, depth);
}
else {
else
{
__print_statements (((xp_awk_nde_while_t*)p)->body, depth + 1);
}
break;
@ -243,39 +257,46 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
case XP_AWK_NDE_DOWHILE:
__print_tabs (depth);
xp_printf (XP_TEXT("do\n"));
if (((xp_awk_nde_while_t*)p)->body->type == XP_AWK_NDE_BLOCK) {
if (((xp_awk_nde_while_t*)p)->body->type == XP_AWK_NDE_BLOCK)
{
__print_statements (((xp_awk_nde_while_t*)p)->body, depth);
}
else {
else
{
__print_statements (((xp_awk_nde_while_t*)p)->body, depth + 1);
}
__print_tabs (depth);
xp_printf (XP_TEXT("while ("));
__print_expr_nde (((xp_awk_nde_while_t*)p)->test);
__print_expression (((xp_awk_nde_while_t*)p)->test);
xp_printf (XP_TEXT(");\n"));
break;
case XP_AWK_NDE_FOR:
__print_tabs (depth);
xp_printf (XP_TEXT("for ("));
if (((xp_awk_nde_for_t*)p)->init != XP_NULL) {
__print_expr_nde (((xp_awk_nde_for_t*)p)->init);
if (((xp_awk_nde_for_t*)p)->init != XP_NULL)
{
__print_expression (((xp_awk_nde_for_t*)p)->init);
}
xp_printf (XP_TEXT("; "));
if (((xp_awk_nde_for_t*)p)->test != XP_NULL) {
__print_expr_nde (((xp_awk_nde_for_t*)p)->test);
if (((xp_awk_nde_for_t*)p)->test != XP_NULL)
{
__print_expression (((xp_awk_nde_for_t*)p)->test);
}
xp_printf (XP_TEXT("; "));
if (((xp_awk_nde_for_t*)p)->incr != XP_NULL) {
__print_expr_nde (((xp_awk_nde_for_t*)p)->incr);
if (((xp_awk_nde_for_t*)p)->incr != XP_NULL)
{
__print_expression (((xp_awk_nde_for_t*)p)->incr);
}
xp_printf (XP_TEXT(")\n"));
if (((xp_awk_nde_for_t*)p)->body->type == XP_AWK_NDE_BLOCK) {
if (((xp_awk_nde_for_t*)p)->body->type == XP_AWK_NDE_BLOCK)
{
__print_statements (((xp_awk_nde_for_t*)p)->body, depth);
}
else {
else
{
__print_statements (((xp_awk_nde_for_t*)p)->body, depth + 1);
}
@ -291,17 +312,20 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
case XP_AWK_NDE_RETURN:
__print_tabs (depth);
if (((xp_awk_nde_sgv_t*)p)->value == XP_NULL) {
if (((xp_awk_nde_return_t*)p)->value == XP_NULL)
{
xp_printf (XP_TEXT("return;\n"));
}
else {
else
{
xp_printf (XP_TEXT("return "));
xp_assert (((xp_awk_nde_sgv_t*)p)->value->next == XP_NULL);
if (__print_expr_nde(((xp_awk_nde_sgv_t*)p)->value) == 0) {
xp_assert (((xp_awk_nde_return_t*)p)->value->next == XP_NULL);
if (__print_expression(((xp_awk_nde_return_t*)p)->value) == 0) {
xp_printf (XP_TEXT(";\n"));
}
else {
xp_awk_nde_sgv_t* x = (xp_awk_nde_sgv_t*)p;
else
{
xp_awk_nde_return_t* x = (xp_awk_nde_return_t*)p;
xp_printf (XP_TEXT("***INTERNAL ERROR: unknown nde type - %d\n"), x->type);
}
}
@ -310,17 +334,21 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
case XP_AWK_NDE_EXIT:
__print_tabs (depth);
if (((xp_awk_nde_sgv_t*)p)->value == XP_NULL) {
if (((xp_awk_nde_exit_t*)p)->value == XP_NULL)
{
xp_printf (XP_TEXT("exit;\n"));
}
else {
else
{
xp_printf (XP_TEXT("exit "));
xp_assert (((xp_awk_nde_sgv_t*)p)->value->next == XP_NULL);
if (__print_expr_nde(((xp_awk_nde_sgv_t*)p)->value) == 0) {
xp_assert (((xp_awk_nde_exit_t*)p)->value->next == XP_NULL);
if (__print_expression(((xp_awk_nde_exit_t*)p)->value) == 0)
{
xp_printf (XP_TEXT(";\n"));
}
else {
xp_awk_nde_sgv_t* x = (xp_awk_nde_sgv_t*)p;
else
{
xp_awk_nde_exit_t* x = (xp_awk_nde_exit_t*)p;
xp_printf (XP_TEXT("***INTERNAL ERROR: unknown nde type - %d\n"), x->type);
}
}
@ -338,10 +366,12 @@ static void __print_statements (xp_awk_nde_t* tree, int depth)
default:
__print_tabs (depth);
if (__print_expr_nde(p) == 0) {
if (__print_expression(p) == 0)
{
xp_printf (XP_TEXT(";\n"));
}
else {
else
{
xp_printf (XP_TEXT("***INTERNAL ERROR: unknown type - %d\n"), p->type);
}
}
@ -360,10 +390,12 @@ void xp_awk_clrpt (xp_awk_nde_t* tree)
xp_awk_nde_t* p = tree;
xp_awk_nde_t* next;
while (p != XP_NULL) {
while (p != XP_NULL)
{
next = p->next;
switch (p->type) {
switch (p->type)
{
case XP_AWK_NDE_NULL:
xp_free (p);
break;
@ -401,16 +433,26 @@ void xp_awk_clrpt (xp_awk_nde_t* tree)
break;
case XP_AWK_NDE_BREAK:
xp_free (p);
break;
case XP_AWK_NDE_CONTINUE:
xp_free (p);
break;
case XP_AWK_NDE_NEXT:
case XP_AWK_NDE_NEXTFILE:
xp_free (p);
break;
case XP_AWK_NDE_RETURN:
if (((xp_awk_nde_return_t*)p)->value != XP_NULL)
xp_awk_clrpt (((xp_awk_nde_return_t*)p)->value);
xp_free (p);
break;
case XP_AWK_NDE_EXIT:
if (((xp_awk_nde_sgv_t*)p)->value != XP_NULL)
xp_awk_clrpt (((xp_awk_nde_sgv_t*)p)->value);
if (((xp_awk_nde_exit_t*)p)->value != XP_NULL)
xp_awk_clrpt (((xp_awk_nde_exit_t*)p)->value);
xp_free (p);
break;
@ -420,23 +462,23 @@ void xp_awk_clrpt (xp_awk_nde_t* tree)
xp_free (p);
break;
case XP_AWK_NDE_BINARY:
xp_assert ((((xp_awk_nde_expr_t*)p)->left)->next == XP_NULL);
xp_assert ((((xp_awk_nde_expr_t*)p)->right)->next == XP_NULL);
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);
xp_awk_clrpt (((xp_awk_nde_expr_t*)p)->left);
xp_awk_clrpt (((xp_awk_nde_expr_t*)p)->right);
xp_awk_clrpt (((xp_awk_nde_exp_t*)p)->left);
xp_awk_clrpt (((xp_awk_nde_exp_t*)p)->right);
xp_free (p);
break;
case XP_AWK_NDE_UNARY:
case XP_AWK_NDE_EXP_UNR:
// TODO: clear unary expression...
xp_free (p);
break;
case XP_AWK_NDE_STR:
case XP_AWK_NDE_NUM:
xp_free (((xp_awk_nde_term_t*)p)->value);
xp_free (((xp_awk_nde_trm_t*)p)->value);
xp_free (p);
break;
@ -462,8 +504,8 @@ void xp_awk_clrpt (xp_awk_nde_t* tree)
break;
case XP_AWK_NDE_POS:
xp_assert (((xp_awk_nde_sgv_t*)p)->value != XP_NULL);
xp_awk_clrpt (((xp_awk_nde_sgv_t*)p)->value);
xp_assert (((xp_awk_nde_pos_t*)p)->value != XP_NULL);
xp_awk_clrpt (((xp_awk_nde_pos_t*)p)->value);
xp_free (p);
break;

View File

@ -1,5 +1,5 @@
/*
* $Id: tree.h,v 1.24 2006-03-03 11:51:48 bacon Exp $
* $Id: tree.h,v 1.25 2006-03-04 15:54:37 bacon Exp $
*/
#ifndef _XP_AWK_TREE_H_
@ -13,15 +13,10 @@ enum
{
XP_AWK_NDE_NULL,
XP_AWK_NDE_BLOCK,
XP_AWK_NDE_BREAK,
XP_AWK_NDE_CONTINUE,
XP_AWK_NDE_RETURN,
XP_AWK_NDE_EXIT,
XP_AWK_NDE_NEXT,
XP_AWK_NDE_NEXTFILE,
XP_AWK_NDE_ASS,
XP_AWK_NDE_BINARY,
XP_AWK_NDE_UNARY,
XP_AWK_NDE_EXP_BIN,
XP_AWK_NDE_EXP_UNR,
XP_AWK_NDE_STR,
XP_AWK_NDE_NUM,
XP_AWK_NDE_NAMED,
@ -34,27 +29,41 @@ enum
XP_AWK_NDE_ARGIDX,
XP_AWK_NDE_POS,
XP_AWK_NDE_CALL,
XP_AWK_NDE_IF,
XP_AWK_NDE_WHILE,
XP_AWK_NDE_DOWHILE,
XP_AWK_NDE_FOR
XP_AWK_NDE_FOR,
XP_AWK_NDE_BREAK,
XP_AWK_NDE_CONTINUE,
XP_AWK_NDE_RETURN,
XP_AWK_NDE_EXIT,
XP_AWK_NDE_NEXT,
XP_AWK_NDE_NEXTFILE
};
typedef struct xp_awk_func_t xp_awk_func_t;
typedef struct xp_awk_nde_t xp_awk_nde_t;
typedef struct xp_awk_nde_sgv_t xp_awk_nde_sgv_t;
typedef struct xp_awk_nde_pos_t xp_awk_nde_pos_t;
typedef struct xp_awk_nde_block_t xp_awk_nde_block_t;
typedef struct xp_awk_nde_ass_t xp_awk_nde_ass_t;
typedef struct xp_awk_nde_expr_t xp_awk_nde_expr_t;
typedef struct xp_awk_nde_term_t xp_awk_nde_term_t;
typedef struct xp_awk_nde_exp_t xp_awk_nde_exp_t;
typedef struct xp_awk_nde_trm_t xp_awk_nde_trm_t;
typedef struct xp_awk_nde_var_t xp_awk_nde_var_t;
typedef struct xp_awk_nde_idx_t xp_awk_nde_idx_t;
typedef struct xp_awk_nde_pos_t xp_awk_nde_pos_t;
typedef struct xp_awk_nde_call_t xp_awk_nde_call_t;
typedef struct xp_awk_nde_if_t xp_awk_nde_if_t;
typedef struct xp_awk_nde_while_t xp_awk_nde_while_t;
typedef struct xp_awk_nde_for_t xp_awk_nde_for_t;
typedef struct xp_awk_nde_break_t xp_awk_nde_break_t;
typedef struct xp_awk_nde_continue_t xp_awk_nde_continue_t;
typedef struct xp_awk_nde_return_t xp_awk_nde_return_t;
typedef struct xp_awk_nde_exit_t xp_awk_nde_exit_t;
struct xp_awk_func_t
{
@ -72,8 +81,8 @@ struct xp_awk_nde_t
XP_AWK_NDE_HDR;
};
/* XP_AWK_NDE_RETURN, XP_AWK_NDE_EXIT, XP_AWK_NDE_POS */
struct xp_awk_nde_sgv_t
/* XP_AWK_NDE_POS - positional - $1, $2, $x, etc */
struct xp_awk_nde_pos_t
{
XP_AWK_NDE_HDR;
xp_awk_nde_t* value;
@ -86,6 +95,7 @@ struct xp_awk_nde_block_t
xp_awk_nde_t* body;
};
/* XP_AWK_NDE_ASS - assignment */
struct xp_awk_nde_ass_t
{
XP_AWK_NDE_HDR;
@ -93,15 +103,17 @@ struct xp_awk_nde_ass_t
xp_awk_nde_t* right;
};
struct xp_awk_nde_expr_t
/* XP_AWK_NDE_EXP_BIN, XP_AWK_NDE_EXP_UNR */
struct xp_awk_nde_exp_t
{
XP_AWK_NDE_HDR;
int opcode;
xp_awk_nde_t* left;
xp_awk_nde_t* right;
xp_awk_nde_t* right; /* optional for XP_AWK_NDE_UNR */
};
struct xp_awk_nde_term_t
/* XP_AWK_NDE_STR, XP_AWK_NDE_NUM */
struct xp_awk_nde_trm_t
{
XP_AWK_NDE_HDR;
xp_char_t* value;
@ -128,38 +140,67 @@ struct xp_awk_nde_idx_t
xp_awk_nde_t* idx;
};
/* XP_AWK_NDE_CALL */
struct xp_awk_nde_call_t
{
XP_AWK_NDE_HDR; /* XP_AWK_NDE_CALL */
XP_AWK_NDE_HDR;
xp_char_t* name;
xp_awk_nde_t* args;
};
/* XP_AWK_NDE_IF */
struct xp_awk_nde_if_t
{
XP_AWK_NDE_HDR; /* XP_AWK_NDE_IF */
XP_AWK_NDE_HDR;
xp_awk_nde_t* test;
xp_awk_nde_t* then_part;
xp_awk_nde_t* else_part; /* optional */
};
/* XP_AWK_NDE_WHILE, XP_AWK_NDE_DOWHILE */
struct xp_awk_nde_while_t
{
XP_AWK_NDE_HDR; /* XP_AWK_NDE_WHILE, XP_AWK_NDE_DOWHILE */
XP_AWK_NDE_HDR;
xp_awk_nde_t* test;
xp_awk_nde_t* body;
};
/* XP_AWK_NDE_FOR */
struct xp_awk_nde_for_t
{
XP_AWK_NDE_HDR; /* XP_AWK_NDE_FOR */
XP_AWK_NDE_HDR;
xp_awk_nde_t* init; /* optional */
xp_awk_nde_t* test; /* optional */
xp_awk_nde_t* incr; /* optional */
xp_awk_nde_t* body;
};
/* XP_AWK_NDE_BREAK */
struct xp_awk_nde_break_t
{
XP_AWK_NDE_HDR;
};
/* XP_AWK_NDE_CONTINUE */
struct xp_awk_nde_continue_t
{
XP_AWK_NDE_HDR;
};
/* XP_AWK_NDE_RETURN */
struct xp_awk_nde_return_t
{
XP_AWK_NDE_HDR;
xp_awk_nde_t* value; /* optional (no return code if XP_NULL) */
};
/* XP_AWK_NDE_EXIT */
struct xp_awk_nde_exit_t
{
XP_AWK_NDE_HDR;
xp_awk_nde_t* value; /* optional (no exit code if XP_NULL) */
};
#ifdef __cplusplus
extern "C" {
#endif

51
ase/awk/val.c Normal file
View File

@ -0,0 +1,51 @@
/*
* $Id: val.c,v 1.1 2006-03-04 15:54:37 bacon Exp $
*/
#include <xp/awk/awk.h>
xp_awk_val_t* xp_awk_makeintval (xp_long_t v)
{
xp_awk_val_int_t* val;
val = xp_malloc (xp_sizeof(xp_awk_val_int_t));
if (val == XP_NULL) return XP_NULL;
val->val = v;
return (xp_awk_val_t*)val;
}
void xp_awk_freeval (xp_awk_val_t* val)
{
switch (val->type)
{
case XP_AWK_VAL_STR:
xp_free (((xp_awk_val_str_t*)val)->buf);
default:
xp_free (val);
}
}
void xp_awk_printval (xp_awk_val_t* val)
{
// TODO: better value printing......................
switch (val->type)
{
case XP_AWK_VAL_INT:
xp_printf (XP_TEXT("%lld"),
(long long)((xp_awk_val_int_t*)val)->val);
break;
case XP_AWK_VAL_REAL:
xp_printf (XP_TEXT("%lf"),
(long double)((xp_awk_val_real_t*)val)->val);
break;
case XP_AWK_VAL_STR:
xp_printf (XP_TEXT("%s"), ((xp_awk_val_str_t*)val)->buf);
break;
default:
xp_printf (XP_TEXT("**** INTERNAL ERROR - UNKNOWN VALUE TYPE ****\n"));
break;
}
}

View File

@ -1,5 +1,5 @@
/*
* $Id: val.h,v 1.1 2006-03-03 11:45:45 bacon Exp $
* $Id: val.h,v 1.2 2006-03-04 15:54:37 bacon Exp $
*/
#ifndef _XP_AWK_VAL_H_
@ -9,6 +9,13 @@
#error Never include this file directly. Include <xp/awk/awk.h> instead
#endif
enum
{
XP_AWK_VAL_INT,
XP_AWK_VAL_REAL,
XP_AWK_VAL_STR
};
typedef struct xp_awk_val_t xp_awk_val_t;
typedef struct xp_awk_val_int_t xp_awk_val_int_t;
typedef struct xp_awk_val_real_t xp_awk_val_real_t;
@ -22,18 +29,21 @@ struct xp_awk_val_t
XP_AWK_VAL_HDR;
};
/* XP_AWK_VAL_INT */
struct xp_awk_val_int_t
{
XP_AWK_VAL_HDR;
xp_long_t val;
};
/* XP_AWK_VAL_REAL */
struct xp_awk_val_real_t
{
XP_AWK_VAL_HDR;
xp_real_t val;
};
/* XP_AWK_VAL_STR */
struct xp_awk_val_str_t
{
XP_AWK_VAL_HDR;
@ -41,4 +51,15 @@ struct xp_awk_val_str_t
xp_size_t len;
};
#ifdef __cplusplus
extern "C" {
#endif
void xp_awk_freeval (xp_awk_val_t* val);
void xp_awk_printval (xp_awk_val_t* val);
#ifdef __cplusplus
}
#endif
#endif