*** empty log message ***

This commit is contained in:
hyung-hwan 2006-01-25 04:27:01 +00:00
parent f77ca39b83
commit 377b10e8f9
3 changed files with 103 additions and 4 deletions

39
ase/awk/awk.txt Normal file
View File

@ -0,0 +1,39 @@
Variables
global variables (enabled when awk->opt & XP_AWK_OPT_VARDCL)
global x;
global x, y;
local variables (enabled when awk->opt & XP_AWK_OPT_VARDCL)
local x;
local x, y;
function arguments (enabled always)
function funca (x, y)
local variables in function declaration (enabled when awk->opt & XP_AWK_OPT_FUNCLOCAL)
function funca (x, y, v1, v2)
variables without any declarations (enabled when awk->opt & XP_AWK_OPT_NAMEDVAR)
x = 10; // x is put into the global hash table.
Optimization
constant folding
2 * 10 => 20
loop
remove while (0) { ... }
if
remove if (0) {}
use else_part only

View File

@ -1,5 +1,5 @@
/* /*
* $Id: parse.c,v 1.30 2006-01-24 16:14:28 bacon Exp $ * $Id: parse.c,v 1.31 2006-01-25 04:27:01 bacon Exp $
*/ */
#include <xp/awk/awk.h> #include <xp/awk/awk.h>
@ -106,7 +106,8 @@ static int __skip_spaces (xp_awk_t* awk);
static int __skip_comment (xp_awk_t* awk); static int __skip_comment (xp_awk_t* awk);
static int __classfy_ident (const xp_char_t* ident); static int __classfy_ident (const xp_char_t* ident);
static int __find_func_arg (xp_awk_t* awk, const xp_char_t* name); static xp_size_t __find_func_arg (xp_awk_t* awk, const xp_char_t* name);
static xp_long_t __str_to_long (const xp_char_t* name);
struct __kwent struct __kwent
{ {
@ -237,6 +238,7 @@ static xp_awk_node_t* __parse_progunit (xp_awk_t* awk)
// TODO: weave the action block into awk->tree.actions... // TODO: weave the action block into awk->tree.actions...
} }
xp_awk_prnpt(node);
return node; return node;
} }
@ -722,6 +724,8 @@ static xp_awk_node_t* __parse_additive (xp_awk_t* awk, xp_char_t* ident)
return XP_NULL; return XP_NULL;
} }
// TODO: constant folding -> in other parts of the program also...
node = (xp_awk_node_expr_t*)xp_malloc(xp_sizeof(xp_awk_node_expr_t)); node = (xp_awk_node_expr_t*)xp_malloc(xp_sizeof(xp_awk_node_expr_t));
if (node == XP_NULL) { if (node == XP_NULL) {
xp_awk_clrpt (right); xp_awk_clrpt (right);
@ -767,6 +771,42 @@ static xp_awk_node_t* __parse_multiplicative (xp_awk_t* awk, xp_char_t* ident)
return XP_NULL; return XP_NULL;
} }
/* TODO: enhance constant folding. do it in a better way */
/* TODO: differentiate different types of numbers ... */
if (left->type == XP_AWK_NODE_NUM &&
right->type == XP_AWK_NODE_NUM) {
xp_long_t l, r;
xp_awk_node_term_t* tmp;
xp_char_t buf[256];
l = __str_to_long (((xp_awk_node_term_t*)left)->value);
r = __str_to_long (((xp_awk_node_term_t*)right)->value);
xp_awk_clrpt (left);
xp_awk_clrpt (right);
if (opcode == BINOP_MUL) l *= r;
else if (opcode == BINOP_DIV) l /= r;
else if (opcode == BINOP_MOD) l %= r;
xp_sprintf (buf, xp_countof(buf), XP_TEXT("%lld"), (long long)l);
tmp = (xp_awk_node_term_t*) xp_malloc (xp_sizeof(xp_awk_node_term_t));
if (tmp == XP_NULL) PANIC (awk, XP_AWK_ENOMEM);
tmp->type = XP_AWK_NODE_NUM;
tmp->next = XP_NULL;
tmp->value = xp_strdup (buf);
if (tmp->value == XP_NULL) {
xp_free (tmp);
PANIC (awk, XP_AWK_ENOMEM);
}
left = (xp_awk_node_t*) tmp;
continue;
}
node = (xp_awk_node_expr_t*)xp_malloc(xp_sizeof(xp_awk_node_expr_t)); node = (xp_awk_node_expr_t*)xp_malloc(xp_sizeof(xp_awk_node_expr_t));
if (node == XP_NULL) { if (node == XP_NULL) {
xp_awk_clrpt (right); xp_awk_clrpt (right);
@ -1625,10 +1665,24 @@ static int __classfy_ident (const xp_char_t* ident)
static xp_size_t __find_func_arg (xp_awk_t* awk, const xp_char_t* name) static xp_size_t __find_func_arg (xp_awk_t* awk, const xp_char_t* name)
{ {
/*
if (awk->curfunc != XP_NULL) { if (awk->curfunc != XP_NULL) {
// TODO: finish this.... // TODO: finish this....
} }
*/
return (xp_size_t)-1; return (xp_size_t)-1;
} }
static xp_long_t __str_to_long (const xp_char_t* name)
{
xp_long_t n = 0;
while (xp_isdigit(*name)) {
n = n * 10 + (*name - XP_CHAR('0'));
name++;
}
return n;
}

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sa.h,v 1.6 2006-01-20 17:00:36 bacon Exp $ * $Id: sa.h,v 1.7 2006-01-25 04:27:01 bacon Exp $
*/ */
#ifndef _XP_AWK_SA_H_ #ifndef _XP_AWK_SA_H_
@ -61,6 +61,12 @@ typedef long xp_ssize_t;
typedef ssize_t xp_ssize_t; typedef ssize_t xp_ssize_t;
#endif #endif
#if defined(_WIN32)
typedef __int64 xp_long_t;
#else
typedef long long xp_long_t;
#endif
#define XP_STR_LEN(x) ((x)->size) #define XP_STR_LEN(x) ((x)->size)
#define XP_STR_SIZE(x) ((x)->size + 1) #define XP_STR_SIZE(x) ((x)->size + 1)
#define XP_STR_CAPA(x) ((x)->capa) #define XP_STR_CAPA(x) ((x)->capa)
@ -84,7 +90,7 @@ xp_char_t* xp_strdup (const xp_char_t* str);
int xp_printf (const xp_char_t* fmt, ...); int xp_printf (const xp_char_t* fmt, ...);
int xp_vprintf (const xp_char_t* fmt, xp_va_list ap); int xp_vprintf (const xp_char_t* fmt, xp_va_list ap);
int xp_sprint (xp_char_t* buf, xp_size_t size, const xp_char_t* fmt, ...); int xp_sprintf (xp_char_t* buf, xp_size_t size, const xp_char_t* fmt, ...);
int xp_vsprintf ( int xp_vsprintf (
xp_char_t* buf, xp_size_t size, const xp_char_t* fmt, xp_va_list ap); xp_char_t* buf, xp_size_t size, const xp_char_t* fmt, xp_va_list ap);