*** empty log message ***

This commit is contained in:
hyung-hwan 2006-01-14 16:09:58 +00:00
parent cb8c7535bc
commit 0865bdec30
4 changed files with 160 additions and 66 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.h,v 1.11 2006-01-13 14:16:56 bacon Exp $ * $Id: awk.h,v 1.12 2006-01-14 16:09:57 bacon Exp $
*/ */
#ifndef _XP_AWK_AWK_H_ #ifndef _XP_AWK_AWK_H_
@ -23,6 +23,7 @@ enum
XP_AWK_EENDSRC, /* unexpected end of source */ XP_AWK_EENDSRC, /* unexpected end of source */
XP_AWK_ELBRACE, /* left brace expected */ XP_AWK_ELBRACE, /* left brace expected */
XP_AWK_ELPAREN, /* left parenthesis expected */
XP_AWK_ERPAREN, /* right parenthesis expected */ XP_AWK_ERPAREN, /* right parenthesis expected */
XP_AWK_ECOMMA, /* comma expected */ XP_AWK_ECOMMA, /* comma expected */
XP_AWK_ESEMICOLON, /* semicolon expected */ XP_AWK_ESEMICOLON, /* semicolon expected */

View File

@ -1,5 +1,5 @@
/* /*
* $Id: parse.c,v 1.19 2006-01-14 14:08:57 bacon Exp $ * $Id: parse.c,v 1.20 2006-01-14 16:09:57 bacon Exp $
*/ */
#include <xp/awk/awk.h> #include <xp/awk/awk.h>
@ -46,6 +46,7 @@ enum
TOKEN_END, TOKEN_END,
TOKEN_FUNCTION, TOKEN_FUNCTION,
TOKEN_IF, TOKEN_IF,
TOKEN_ELSE,
TOKEN_WHILE, TOKEN_WHILE,
TOKEN_FOR, TOKEN_FOR,
TOKEN_DO, TOKEN_DO,
@ -71,6 +72,7 @@ static xp_awk_node_t* __parse_funcdcl (xp_awk_t* awk);
static xp_awk_node_t* __parse_patnact (xp_awk_t* awk); static xp_awk_node_t* __parse_patnact (xp_awk_t* awk);
static xp_awk_node_t* __parse_block (xp_awk_t* awk); static xp_awk_node_t* __parse_block (xp_awk_t* awk);
static xp_awk_node_t* __parse_statement (xp_awk_t* awk); static xp_awk_node_t* __parse_statement (xp_awk_t* awk);
static xp_awk_node_t* __parse_statement_nb (xp_awk_t* awk);
static xp_awk_node_t* __parse_expression (xp_awk_t* awk); static xp_awk_node_t* __parse_expression (xp_awk_t* awk);
static xp_awk_node_t* __parse_assignment (xp_awk_t* awk, xp_char_t* ident); static xp_awk_node_t* __parse_assignment (xp_awk_t* awk, xp_char_t* ident);
static xp_awk_node_t* __parse_basic_expr (xp_awk_t* awk, xp_char_t* ident); static xp_awk_node_t* __parse_basic_expr (xp_awk_t* awk, xp_char_t* ident);
@ -107,6 +109,7 @@ static struct __kwent __kwtab[] =
{ XP_TEXT("END"), TOKEN_END }, { XP_TEXT("END"), TOKEN_END },
{ XP_TEXT("function"), TOKEN_FUNCTION }, { XP_TEXT("function"), TOKEN_FUNCTION },
{ XP_TEXT("if"), TOKEN_IF }, { XP_TEXT("if"), TOKEN_IF },
{ XP_TEXT("else"), TOKEN_ELSE },
{ XP_TEXT("while"), TOKEN_WHILE }, { XP_TEXT("while"), TOKEN_WHILE },
{ XP_TEXT("for"), TOKEN_FOR }, { XP_TEXT("for"), TOKEN_FOR },
{ XP_TEXT("do"), TOKEN_DO }, { XP_TEXT("do"), TOKEN_DO },
@ -245,13 +248,13 @@ static xp_awk_node_t* __parse_block (xp_awk_t* awk)
while (1) { while (1) {
if (MATCH(awk,TOKEN_EOF)) { if (MATCH(awk,TOKEN_EOF)) {
if (curr != XP_NULL) xp_awk_clrpt (curr); if (head != XP_NULL) xp_awk_clrpt (head);
PANIC (awk, XP_AWK_EENDSRC); PANIC (awk, XP_AWK_EENDSRC);
} }
if (MATCH(awk,TOKEN_RBRACE)) { if (MATCH(awk,TOKEN_RBRACE)) {
if (__get_token(awk) == -1) { if (__get_token(awk) == -1) {
if (curr != XP_NULL) xp_awk_clrpt (curr); if (head != XP_NULL) xp_awk_clrpt (head);
return XP_NULL; return XP_NULL;
} }
break; break;
@ -260,24 +263,15 @@ static xp_awk_node_t* __parse_block (xp_awk_t* awk)
if (MATCH(awk,TOKEN_SEMICOLON)) { if (MATCH(awk,TOKEN_SEMICOLON)) {
/* null statement */ /* null statement */
if (__get_token(awk) == -1) { if (__get_token(awk) == -1) {
if (curr != XP_NULL) xp_awk_clrpt (curr); if (head != XP_NULL) xp_awk_clrpt (head);
return XP_NULL; return XP_NULL;
} }
continue; continue;
} }
if (MATCH(awk,TOKEN_LBRACE)) { node = __parse_statement (awk);
/* nested block */
if (__get_token(awk) == -1) {
if (curr != XP_NULL) xp_awk_clrpt (curr);
return XP_NULL;
}
node = __parse_block (awk);
}
else node = __parse_statement (awk);
if (node == XP_NULL) { if (node == XP_NULL) {
if (curr != XP_NULL) xp_awk_clrpt (curr); if (head != XP_NULL) xp_awk_clrpt (head);
return XP_NULL; return XP_NULL;
} }
@ -303,6 +297,19 @@ static xp_awk_node_t* __parse_statement (xp_awk_t* awk)
{ {
xp_awk_node_t* node; xp_awk_node_t* node;
if (MATCH(awk,TOKEN_LBRACE)) {
if (__get_token(awk) == -1) return XP_NULL;
node = __parse_block (awk);
}
else node = __parse_statement_nb (awk);
return node;
}
static xp_awk_node_t* __parse_statement_nb (xp_awk_t* awk)
{
xp_awk_node_t* node;
if (MATCH(awk,TOKEN_IF)) { if (MATCH(awk,TOKEN_IF)) {
if (__get_token(awk) == -1) return XP_NULL; if (__get_token(awk) == -1) return XP_NULL;
node = __parse_if(awk); node = __parse_if(awk);
@ -351,7 +358,9 @@ TODO:
} }
*/ */
else { else {
xp_printf (XP_TEXT("%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"));
node = __parse_expression(awk); node = __parse_expression(awk);
xp_printf (XP_TEXT("$$$$$$$$$$$$$$$$$$$$$$$$$\n"));
} }
if (node == XP_NULL) return XP_NULL; if (node == XP_NULL) return XP_NULL;
@ -362,11 +371,13 @@ TODO:
PANIC (awk, XP_AWK_ESEMICOLON); PANIC (awk, XP_AWK_ESEMICOLON);
} }
xp_printf (XP_TEXT("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n"));
/* eat up the semicolon and read in the next token */ /* eat up the semicolon and read in the next token */
if (__get_token(awk) == -1) { if (__get_token(awk) == -1) {
if (node != XP_NULL) xp_awk_clrpt (node); if (node != XP_NULL) xp_awk_clrpt (node);
return XP_NULL; return XP_NULL;
} }
xp_printf (XP_TEXT("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"));
return node; return node;
} }
@ -671,71 +682,126 @@ static xp_awk_node_t* __parse_primary (xp_awk_t* awk, xp_char_t* ident)
static xp_awk_node_t* __parse_funcall (xp_awk_t* awk, xp_char_t* name) static xp_awk_node_t* __parse_funcall (xp_awk_t* awk, xp_char_t* name)
{ {
xp_awk_node_t* head, * curr, * tmp; xp_awk_node_t* head, * curr, * node;
xp_awk_node_call_t* node; xp_awk_node_call_t* call;
if (__get_token(awk) == -1) return XP_NULL; if (__get_token(awk) == -1) return XP_NULL;
if (MATCH(awk,TOKEN_RPAREN)) {
node = (xp_awk_node_call_t*)xp_malloc (xp_sizeof(xp_awk_node_call_t));
if (node == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
node->type = XP_AWK_NODE_CALL;
node->next = XP_NULL;
node->name = name;
node->args = XP_NULL;
if (__get_token(awk) == -1) return XP_NULL;
return (xp_awk_node_t*)node;
}
head = curr = XP_NULL; head = curr = XP_NULL;
if (!MATCH(awk,TOKEN_RPAREN)) {
while (1) { while (1) {
tmp = __parse_expression (awk); node = __parse_expression (awk);
if (tmp == XP_NULL) { if (node == XP_NULL) {
if (cur != XP_NULL) xp_awk_clrpt (cur); if (head != XP_NULL) xp_awk_clrpt (head);
xp_free (node);
return XP_NULL; return XP_NULL;
} }
if (head == XP_NULL) head = tmp; if (head == XP_NULL) head = node;
else curr->next = tmp; else curr->next = node;
curr = tmp; curr = node;
if (MATCH(awk,TOKEN_RPAREN)) { if (MATCH(awk,TOKEN_RPAREN)) {
if (__get_token(awk) == -1) { if (__get_token(awk) == -1) {
// TODO cleanup. if (head != XP_NULL) xp_awk_clrpt (head);
return XP_NULL; return XP_NULL;
} }
break; break;
} }
if (!MATCH(awk,TOKEN_COMMA)) { if (!MATCH(awk,TOKEN_COMMA)) {
// TODO: free all other nodes associated into node->args if (head != XP_NULL) xp_awk_clrpt (head);
xp_free (node);
PANIC (awk, XP_AWK_ECOMMA); PANIC (awk, XP_AWK_ECOMMA);
} }
if (__get_token(awk) == -1) { if (__get_token(awk) == -1) {
// TODO cleanup. if (head != XP_NULL) xp_awk_clrpt (head);
return XP_NULL; return XP_NULL;
} }
} }
node = (xp_awk_node_call_t*)xp_malloc (xp_sizeof(xp_awk_node_call_t)); }
if (node == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
node->type = XP_AWK_NODE_CALL; call = (xp_awk_node_call_t*)xp_malloc (xp_sizeof(xp_awk_node_call_t));
node->next = XP_NULL; if (call == XP_NULL) {
node->name = name; if (head != XP_NULL) xp_awk_clrpt (head);
node->args = head; PANIC (awk, XP_AWK_ENOMEM);
}
return (xp_awk_node_t*)node; call->type = XP_AWK_NODE_CALL;
call->next = XP_NULL;
call->name = name;
call->args = head;
return (xp_awk_node_t*)call;
} }
static xp_awk_node_t* __parse_if (xp_awk_t* awk) static xp_awk_node_t* __parse_if (xp_awk_t* awk)
{ {
xp_awk_node_t* test;
xp_awk_node_t* then_part;
xp_awk_node_t* else_part;
xp_awk_node_if_t* node;
if (!MATCH(awk,TOKEN_LPAREN)) PANIC (awk, XP_AWK_ELPAREN);
if (__get_token(awk) == -1) return XP_NULL;
test = __parse_expression (awk);
if (test == XP_NULL) return XP_NULL;
xp_printf (XP_TEXT("11111111111111111111111111111111\n"));
if (!MATCH(awk,TOKEN_RPAREN)) {
xp_awk_clrpt (test);
PANIC (awk, XP_AWK_ERPAREN);
}
if (__get_token(awk) == -1) {
xp_awk_clrpt (test);
return XP_NULL; return XP_NULL;
}
xp_printf (XP_TEXT("jjjjjjjjjjjjjjjjjjjjjjjj\n"));
then_part = __parse_statement (awk);
if (then_part == XP_NULL) {
xp_awk_clrpt (test);
return XP_NULL;
}
xp_printf (XP_TEXT("aaaaaaaaaaaaaaaaaaaaaaaaa\n"));
if (MATCH(awk,TOKEN_ELSE)) {
if (__get_token(awk) == -1) {
xp_awk_clrpt (then_part);
xp_awk_clrpt (test);
return XP_NULL;
}
else_part = __parse_statement (awk);
if (else_part == XP_NULL) {
xp_awk_clrpt (then_part);
xp_awk_clrpt (test);
return XP_NULL;
}
}
else else_part = XP_NULL;
node = (xp_awk_node_if_t*) xp_malloc (xp_sizeof(xp_awk_node_if_t));
if (node == XP_NULL) {
xp_awk_clrpt (else_part);
xp_awk_clrpt (then_part);
xp_awk_clrpt (test);
PANIC (awk, XP_AWK_ENOMEM);
}
node->type = XP_AWK_NODE_IF;
node->next = XP_NULL;
node->test = test;
node->then_part = then_part;
node->else_part = else_part;
xp_printf (XP_TEXT("####################################################\n"));
return (xp_awk_node_t*)node;
} }
static xp_awk_node_t* __parse_while (xp_awk_t* awk) static xp_awk_node_t* __parse_while (xp_awk_t* awk)

View File

@ -1,5 +1,5 @@
/* /*
* $Id: tree.c,v 1.1 2006-01-14 14:09:52 bacon Exp $ * $Id: tree.c,v 1.2 2006-01-14 16:09:57 bacon Exp $
*/ */
#include <xp/awk/awk.h> #include <xp/awk/awk.h>
@ -104,6 +104,23 @@ static void __print_statements (xp_awk_node_t* tree, int depth)
xp_printf (XP_TEXT("}\n")); xp_printf (XP_TEXT("}\n"));
break; break;
case XP_AWK_NODE_IF:
__print_tabs (depth);
xp_printf (XP_TEXT("if ("));
__print_expr_node (((xp_awk_node_if_t*)p)->test);
xp_printf (XP_TEXT(") "));
if (((xp_awk_node_if_t*)p)->then_part == XP_NULL)
xp_printf (XP_TEXT(";\n"));
else __print_statements (((xp_awk_node_if_t*)p)->then_part, 0);
if (((xp_awk_node_if_t*)p)->else_part != XP_NULL) {
__print_tabs (depth);
xp_printf (XP_TEXT("else "));
__print_statements (((xp_awk_node_if_t*)p)->else_part, 0);
}
break;
case XP_AWK_NODE_BREAK: case XP_AWK_NODE_BREAK:
__print_tabs (depth); __print_tabs (depth);
xp_printf (XP_TEXT("break;\n")); xp_printf (XP_TEXT("break;\n"));
@ -173,6 +190,15 @@ void xp_awk_clrpt (xp_awk_node_t* tree)
xp_free (p); xp_free (p);
break; break;
case XP_AWK_NODE_IF:
xp_awk_clrpt (((xp_awk_node_if_t*)p)->test);
if (((xp_awk_node_if_t*)p)->then_part != XP_NULL)
xp_awk_clrpt (((xp_awk_node_if_t*)p)->then_part);
if (((xp_awk_node_if_t*)p)->else_part != XP_NULL)
xp_awk_clrpt (((xp_awk_node_if_t*)p)->else_part);
xp_free (p);
break;
case XP_AWK_NODE_BREAK: case XP_AWK_NODE_BREAK:
case XP_AWK_NODE_CONTINUE: case XP_AWK_NODE_CONTINUE:
xp_free (p); xp_free (p);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: tree.h,v 1.8 2006-01-14 14:08:58 bacon Exp $ * $Id: tree.h,v 1.9 2006-01-14 16:09:58 bacon Exp $
*/ */
#ifndef _XP_AWK_TREE_H_ #ifndef _XP_AWK_TREE_H_
@ -18,7 +18,8 @@ enum
XP_AWK_NODE_STR, XP_AWK_NODE_STR,
XP_AWK_NODE_NUM, XP_AWK_NODE_NUM,
XP_AWK_NODE_VAR, XP_AWK_NODE_VAR,
XP_AWK_NODE_CALL XP_AWK_NODE_CALL,
XP_AWK_NODE_IF
}; };
typedef struct xp_awk_node_t xp_awk_node_t; typedef struct xp_awk_node_t xp_awk_node_t;