*** empty log message ***

This commit is contained in:
hyung-hwan 2006-03-30 16:24:10 +00:00
parent 7e20e755e2
commit 52d92a7d08
2 changed files with 105 additions and 146 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: parse.c,v 1.62 2006-03-29 16:37:31 bacon Exp $ * $Id: parse.c,v 1.63 2006-03-30 16:24:10 bacon Exp $
*/ */
#include <xp/awk/awk.h> #include <xp/awk/awk.h>
@ -38,6 +38,7 @@ enum
TOKEN_LOR, TOKEN_LOR,
TOKEN_LAND, TOKEN_LAND,
TOKEN_BOR, TOKEN_BOR,
TOKEN_BXOR,
TOKEN_BAND, TOKEN_BAND,
TOKEN_LPAREN, TOKEN_LPAREN,
@ -84,6 +85,13 @@ enum
#define INLINE inline #define INLINE inline
#endif #endif
typedef struct __binmap_t __binmap_t;
struct __binmap_t
{
int token;
int binop;
};
static xp_awk_t* __parse_progunit (xp_awk_t* awk); static xp_awk_t* __parse_progunit (xp_awk_t* awk);
static xp_awk_t* __collect_globals (xp_awk_t* awk); static xp_awk_t* __collect_globals (xp_awk_t* awk);
@ -99,8 +107,12 @@ static xp_awk_nde_t* __parse_block (xp_awk_t* awk, xp_bool_t is_top);
static xp_awk_nde_t* __parse_statement (xp_awk_t* awk); static xp_awk_nde_t* __parse_statement (xp_awk_t* awk);
static xp_awk_nde_t* __parse_statement_nb (xp_awk_t* awk); static xp_awk_nde_t* __parse_statement_nb (xp_awk_t* awk);
static xp_awk_nde_t* __parse_expression (xp_awk_t* awk); static xp_awk_nde_t* __parse_expression (xp_awk_t* awk);
static xp_awk_nde_t* __parse_binary_expr (
xp_awk_t* awk, const __binmap_t* binmap,
xp_awk_nde_t*(*next_level_func)(xp_awk_t*));
static xp_awk_nde_t* __parse_basic_expr (xp_awk_t* awk); static xp_awk_nde_t* __parse_basic_expr (xp_awk_t* awk);
static xp_awk_nde_t* __parse_conditional (xp_awk_t* awk);
static xp_awk_nde_t* __parse_logical_or (xp_awk_t* awk); static xp_awk_nde_t* __parse_logical_or (xp_awk_t* awk);
static xp_awk_nde_t* __parse_logical_and (xp_awk_t* awk); static xp_awk_nde_t* __parse_logical_and (xp_awk_t* awk);
static xp_awk_nde_t* __parse_bitwise_or (xp_awk_t* awk); static xp_awk_nde_t* __parse_bitwise_or (xp_awk_t* awk);
@ -1030,176 +1042,116 @@ static xp_awk_nde_t* __parse_expression (xp_awk_t* awk)
return (xp_awk_nde_t*)nde; return (xp_awk_nde_t*)nde;
} }
static xp_awk_nde_t* __parse_basic_expr (xp_awk_t* awk) static xp_awk_nde_t* __parse_binary_expr (
xp_awk_t* awk, const __binmap_t* binmap,
xp_awk_nde_t*(*next_level_func)(xp_awk_t*))
{ {
/* <basic expression> ::= <additive> xp_awk_nde_exp_t* nde;
* <additive> ::= <multiplicative> [additiveOp <multiplicative>]* xp_awk_nde_t* left, * right;
* <multiplicative> ::= <unary> [multiplicativeOp <unary>]* int opcode;
* <unary> ::= [unaryOp]* <term>
* <term> ::= <function call> | variable | literals
* <function call> ::= <identifier> <lparen> <basic expression list>* <rparen>
* <basic expression list> ::= <basic expression> [comma <basic expression>]*
*/
return __parse_conditional (awk); left = next_level_func (awk);
if (left == XP_NULL) return XP_NULL;
while (1)
{
const __binmap_t* p = binmap;
xp_bool_t matched = xp_false;
while (p->token != TOKEN_EOF)
{
if (MATCH(awk,p->token))
{
opcode = p->binop;
matched = xp_true;
break;
}
p++;
}
if (!matched) break;
if (__get_token(awk) == -1)
{
xp_awk_clrpt (left);
return XP_NULL;
}
right = next_level_func (awk);
if (right == XP_NULL)
{
xp_awk_clrpt (left);
return XP_NULL;
}
// TODO: constant folding -> in other parts of the program also...
nde = (xp_awk_nde_exp_t*)xp_malloc(xp_sizeof(xp_awk_nde_exp_t));
if (nde == XP_NULL)
{
xp_awk_clrpt (right);
xp_awk_clrpt (left);
PANIC (awk, XP_AWK_ENOMEM);
}
nde->type = XP_AWK_NDE_EXP_BIN;
nde->next = XP_NULL;
nde->opcode = opcode;
nde->left = left;
nde->right = right;
left = (xp_awk_nde_t*)nde;
}
return left;
} }
static xp_awk_nde_t* __parse_conditional (xp_awk_t* awk) static xp_awk_nde_t* __parse_basic_expr (xp_awk_t* awk)
{ {
return __parse_logical_or (awk); return __parse_logical_or (awk);
} }
static xp_awk_nde_t* __parse_logical_or (xp_awk_t* awk) static xp_awk_nde_t* __parse_logical_or (xp_awk_t* awk)
{ {
xp_awk_nde_exp_t* nde; __binmap_t map[] =
xp_awk_nde_t* left, * right;
int opcode;
left = __parse_logical_and (awk);
if (left == XP_NULL) return XP_NULL;
while (1)
{ {
if (MATCH(awk,TOKEN_LOR)) opcode = XP_AWK_BINOP_LOR; { TOKEN_LOR, XP_AWK_BINOP_LOR },
else break; { TOKEN_EOF, 0 }
};
if (__get_token(awk) == -1) return __parse_binary_expr (awk, __parse_logical_and, map);
{
xp_awk_clrpt (left);
return XP_NULL;
}
right = __parse_logical_and (awk);
if (right == XP_NULL)
{
xp_awk_clrpt (left);
return XP_NULL;
}
// TODO: constant folding -> in other parts of the program also...
nde = (xp_awk_nde_exp_t*)xp_malloc(xp_sizeof(xp_awk_nde_exp_t));
if (nde == XP_NULL)
{
xp_awk_clrpt (right);
xp_awk_clrpt (left);
PANIC (awk, XP_AWK_ENOMEM);
}
nde->type = XP_AWK_NDE_EXP_BIN;
nde->next = XP_NULL;
nde->opcode = opcode;
nde->left = left;
nde->right = right;
left = (xp_awk_nde_t*)nde;
}
return left;
} }
static xp_awk_nde_t* __parse_logical_and (xp_awk_t* awk) static xp_awk_nde_t* __parse_logical_and (xp_awk_t* awk)
{ {
xp_awk_nde_exp_t* nde; __binmap_t map[] =
xp_awk_nde_t* left, * right;
int opcode;
left = __parse_bitwise_or (awk);
if (left == XP_NULL) return XP_NULL;
while (1)
{ {
if (MATCH(awk,TOKEN_LAND)) opcode = XP_AWK_BINOP_LAND; { TOKEN_LAND, XP_AWK_BINOP_LAND },
else break; { TOKEN_EOF, 0 }
};
if (__get_token(awk) == -1) return __parse_binary_expr (awk, __parse_bitwise_or, map);
{
xp_awk_clrpt (left);
return XP_NULL;
}
right = __parse_bitwise_or (awk);
if (right == XP_NULL)
{
xp_awk_clrpt (left);
return XP_NULL;
}
// TODO: constant folding -> in other parts of the program also...
nde = (xp_awk_nde_exp_t*)xp_malloc(xp_sizeof(xp_awk_nde_exp_t));
if (nde == XP_NULL)
{
xp_awk_clrpt (right);
xp_awk_clrpt (left);
PANIC (awk, XP_AWK_ENOMEM);
}
nde->type = XP_AWK_NDE_EXP_BIN;
nde->next = XP_NULL;
nde->opcode = opcode;
nde->left = left;
nde->right = right;
left = (xp_awk_nde_t*)nde;
}
return left;
} }
static xp_awk_nde_t* __parse_bitwise_or (xp_awk_t* awk) static xp_awk_nde_t* __parse_bitwise_or (xp_awk_t* awk)
{ {
xp_awk_nde_exp_t* nde; __binmap_t map[] =
xp_awk_nde_t* left, * right;
int opcode;
left = __parse_bitwise_xor (awk);
if (left == XP_NULL) return XP_NULL;
while (1)
{ {
if (MATCH(awk,TOKEN_BOR)) opcode = XP_AWK_BINOP_BOR; { TOKEN_BOR, XP_AWK_BINOP_BOR },
else break; { TOKEN_EOF, 0 }
};
if (__get_token(awk) == -1) return __parse_binary_expr (awk, __parse_bitwise_xor, map);
{
xp_awk_clrpt (left);
return XP_NULL;
}
right = __parse_bitwise_xor (awk);
if (right == XP_NULL)
{
xp_awk_clrpt (left);
return XP_NULL;
}
// TODO: constant folding -> in other parts of the program also...
nde = (xp_awk_nde_exp_t*)xp_malloc(xp_sizeof(xp_awk_nde_exp_t));
if (nde == XP_NULL)
{
xp_awk_clrpt (right);
xp_awk_clrpt (left);
PANIC (awk, XP_AWK_ENOMEM);
}
nde->type = XP_AWK_NDE_EXP_BIN;
nde->next = XP_NULL;
nde->opcode = opcode;
nde->left = left;
nde->right = right;
left = (xp_awk_nde_t*)nde;
}
return left;
} }
static xp_awk_nde_t* __parse_bitwise_xor (xp_awk_t* awk) static xp_awk_nde_t* __parse_bitwise_xor (xp_awk_t* awk)
{ {
// TODO: __binmap_t map[] =
return XP_NULL; {
{ TOKEN_BXOR, XP_AWK_BINOP_BXOR },
{ TOKEN_EOF, 0 }
};
return __parse_binary_expr (awk, __parse_equality, map);
} }
static xp_awk_nde_t* __parse_equality (xp_awk_t* awk) static xp_awk_nde_t* __parse_equality (xp_awk_t* awk)
@ -2389,10 +2341,16 @@ static int __get_token (xp_awk_t* awk)
ADD_TOKEN_STR (awk, XP_TEXT("|")); ADD_TOKEN_STR (awk, XP_TEXT("|"));
} }
} }
else if (c == XP_CHAR('^'))
{
SET_TOKEN_TYPE (awk, TOKEN_BXOR);
ADD_TOKEN_CHAR (awk, c);
GET_CHAR_TO (awk, c);
}
else if (c == XP_CHAR('&')) else if (c == XP_CHAR('&'))
{ {
GET_CHAR_TO (awk, c); GET_CHAR_TO (awk, c);
if (c == XP_CHAR('&&')) if (c == XP_CHAR('&'))
{ {
SET_TOKEN_TYPE (awk, TOKEN_LAND); SET_TOKEN_TYPE (awk, TOKEN_LAND);
ADD_TOKEN_STR (awk, XP_TEXT("&&")); ADD_TOKEN_STR (awk, XP_TEXT("&&"));

View File

@ -1,5 +1,5 @@
/* /*
* $Id: run.h,v 1.2 2006-03-29 16:37:31 bacon Exp $ * $Id: run.h,v 1.3 2006-03-30 16:24:10 bacon Exp $
*/ */
#ifndef _XP_AWK_RUN_H_ #ifndef _XP_AWK_RUN_H_
@ -24,6 +24,7 @@ enum
XP_AWK_BINOP_LOR, XP_AWK_BINOP_LOR,
XP_AWK_BINOP_LAND, XP_AWK_BINOP_LAND,
XP_AWK_BINOP_BOR, XP_AWK_BINOP_BOR,
XP_AWK_BINOP_BXOR,
XP_AWK_BINOP_BAND, XP_AWK_BINOP_BAND,
XP_AWK_BINOP_PLUS, XP_AWK_BINOP_PLUS,
XP_AWK_BINOP_MINUS, XP_AWK_BINOP_MINUS,