improved the naming rule

This commit is contained in:
hyung-hwan 2009-06-29 07:41:47 +00:00
parent 3c63104bf7
commit 14b0bca55c
4 changed files with 60 additions and 21 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.c 207 2009-06-22 13:01:28Z hyunghwan.chung $ * $Id: awk.c 217 2009-06-28 13:41:47Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -345,6 +345,7 @@ struct opttab_t
{ QSE_T("pablock"), QSE_AWK_PABLOCK, QSE_T("enable pattern-action loop") }, { QSE_T("pablock"), QSE_AWK_PABLOCK, QSE_T("enable pattern-action loop") },
{ QSE_T("rexbound"), QSE_AWK_REXBOUND, QSE_T("enable {n,m} in a regular expression") }, { QSE_T("rexbound"), QSE_AWK_REXBOUND, QSE_T("enable {n,m} in a regular expression") },
{ QSE_T("ncmponstr"), QSE_AWK_NCMPONSTR, QSE_T("perform numeric comparsion on numeric strings") }, { QSE_T("ncmponstr"), QSE_AWK_NCMPONSTR, QSE_T("perform numeric comparsion on numeric strings") },
{ QSE_T("strictnaming"), QSE_AWK_STRICTNAMING, QSE_T("enable the strict naming rule") },
{ QSE_NULL, 0 } { QSE_NULL, 0 }
}; };
@ -390,6 +391,7 @@ static int comparg (int argc, qse_char_t* argv[], struct arg_t* arg)
{ QSE_T(":pablock"), QSE_T('\0') }, { QSE_T(":pablock"), QSE_T('\0') },
{ QSE_T(":rexbound"), QSE_T('\0') }, { QSE_T(":rexbound"), QSE_T('\0') },
{ QSE_T(":ncmponstr"), QSE_T('\0') }, { QSE_T(":ncmponstr"), QSE_T('\0') },
{ QSE_T(":strictnaming"), QSE_T('\0') },
{ QSE_T(":call"), QSE_T('c') }, { QSE_T(":call"), QSE_T('c') },
{ QSE_T(":file"), QSE_T('f') }, { QSE_T(":file"), QSE_T('f') },

View File

@ -1,5 +1,5 @@
/* /*
* $Id: awk.h 214 2009-06-27 02:50:54Z hyunghwan.chung $ * $Id: awk.h 217 2009-06-28 13:41:47Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -487,25 +487,25 @@ enum qse_awk_option_t
* " a b c " is split to [a], [b], [c] if #QSE_AWK_STRIPSPACES is on. * " a b c " is split to [a], [b], [c] if #QSE_AWK_STRIPSPACES is on.
* Otherwise, it is split to [], [a], [b], [c], []. * Otherwise, it is split to [], [a], [b], [c], [].
*/ */
QSE_AWK_STRIPSPACES = (1 << 11), QSE_AWK_STRIPSPACES = (1 << 10),
/** enables @b nextofile */ /** enables @b nextofile */
QSE_AWK_NEXTOFILE = (1 << 12), QSE_AWK_NEXTOFILE = (1 << 11),
/** enables @b reset */ /** enables @b reset */
QSE_AWK_RESET = (1 << 13), QSE_AWK_RESET = (1 << 12),
/** CR + LF by default */ /** CR + LF by default */
QSE_AWK_CRLF = (1 << 14), QSE_AWK_CRLF = (1 << 13),
/** allows the assignment of a map value to a variable */ /** allows the assignment of a map value to a variable */
QSE_AWK_MAPTOVAR = (1 << 15), QSE_AWK_MAPTOVAR = (1 << 14),
/** allows @b BEGIN, @b END, pattern-action blocks */ /** allows @b BEGIN, @b END, pattern-action blocks */
QSE_AWK_PABLOCK = (1 << 16), QSE_AWK_PABLOCK = (1 << 15),
/** allows {n,m} in a regular expression. */ /** allows {n,m} in a regular expression. */
QSE_AWK_REXBOUND = (1 << 17), QSE_AWK_REXBOUND = (1 << 16),
/** /**
* performs numeric comparison when a string convertable * performs numeric comparison when a string convertable
@ -515,14 +515,22 @@ enum qse_awk_option_t
* - 9 is greater if #QSE_AWK_NCMPONSTR is off; * - 9 is greater if #QSE_AWK_NCMPONSTR is off;
* - "10.9" is greater if #QSE_AWK_NCMPONSTR is on * - "10.9" is greater if #QSE_AWK_NCMPONSTR is on
*/ */
QSE_AWK_NCMPONSTR = (1 << 18), QSE_AWK_NCMPONSTR = (1 << 17),
/**
* strict naming rule
* - a parameter can not be the same as the owning function name.
* - a local variable can not be the same as the owning function name.
*/
QSE_AWK_STRICTNAMING = (1 << 18),
/** /**
* makes #qse_awk_t to behave as compatibly as classical AWK * makes #qse_awk_t to behave as compatibly as classical AWK
* implementations * implementations
*/ */
QSE_AWK_CLASSIC = QSE_AWK_IMPLICIT | QSE_AWK_RIO | QSE_AWK_CLASSIC = QSE_AWK_IMPLICIT | QSE_AWK_RIO |
QSE_AWK_NEWLINE | QSE_AWK_PABLOCK QSE_AWK_NEWLINE | QSE_AWK_PABLOCK |
QSE_AWK_STRICTNAMING
}; };
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* $Id: parse.c 216 2009-06-27 12:42:53Z hyunghwan.chung $ * $Id: parse.c 217 2009-06-28 13:41:47Z hyunghwan.chung $
* *
Copyright 2006-2009 Chung, Hyung-Hwan. Copyright 2006-2009 Chung, Hyung-Hwan.
@ -947,8 +947,9 @@ static qse_awk_nde_t* parse_function (qse_awk_t* awk)
/* check if a parameter conflicts with the function /* check if a parameter conflicts with the function
* name or other parameters */ * name or other parameters */
if (qse_strxncmp ( if (((awk->option & QSE_AWK_STRICTNAMING) &&
param, param_len, name_dup, name_len) == 0 || qse_strxncmp (
param, param_len, name_dup, name_len) == 0) ||
qse_lda_search (awk->parse.params, qse_lda_search (awk->parse.params,
0, param, param_len) != QSE_LDA_NIL) 0, param, param_len) != QSE_LDA_NIL)
{ {
@ -1732,7 +1733,7 @@ static qse_awk_t* collect_locals (
lcl.len = QSE_STR_LEN(awk->token.name); lcl.len = QSE_STR_LEN(awk->token.name);
/* check if it conflict with a builtin function name /* check if it conflict with a builtin function name
* function f() { lcl length; } */ * function f() { local length; } */
if (qse_awk_getfnc (awk, lcl.ptr, lcl.len) != QSE_NULL) if (qse_awk_getfnc (awk, lcl.ptr, lcl.len) != QSE_NULL)
{ {
SETERRARG ( SETERRARG (
@ -1743,8 +1744,11 @@ static qse_awk_t* collect_locals (
if (istop) if (istop)
{ {
/* check if it conflicts with a paremeter name */ /* check if it conflicts with a parameter name.
n = qse_lda_search (awk->parse.params, 0, lcl.ptr, lcl.len); * the first level declaration is treated as the same
* scope as the parameter list */
n = qse_lda_search (
awk->parse.params, 0, lcl.ptr, lcl.len);
if (n != QSE_LDA_NIL) if (n != QSE_LDA_NIL)
{ {
SETERRARG ( SETERRARG (
@ -1754,6 +1758,25 @@ static qse_awk_t* collect_locals (
} }
} }
if (awk->option & QSE_AWK_STRICTNAMING)
{
/* check if it conflicts with the owning function */
if (awk->tree.cur_fun.ptr != QSE_NULL)
{
if (qse_strxncmp (
lcl.ptr, lcl.len,
awk->tree.cur_fun.ptr,
awk->tree.cur_fun.len) == 0)
{
SETERRARG (
awk, QSE_AWK_EFUNRED,
awk->token.line,
lcl.ptr, lcl.len);
return QSE_NULL;
}
}
}
/* check if it conflicts with other local variable names */ /* check if it conflicts with other local variable names */
n = qse_lda_search ( n = qse_lda_search (
awk->parse.lcls, awk->parse.lcls,
@ -1773,7 +1796,8 @@ static qse_awk_t* collect_locals (
{ {
if (n < awk->tree.ngbls_base) if (n < awk->tree.ngbls_base)
{ {
/* conflicting with a static global variable */ /* it is a conflict only if it is one of a
* static global variable */
SETERRARG ( SETERRARG (
awk, QSE_AWK_EDUPLCL, awk->token.line, awk, QSE_AWK_EDUPLCL, awk->token.line,
lcl.ptr, lcl.len); lcl.ptr, lcl.len);

View File

@ -1,7 +1,12 @@
function a (a) { print a; } function x (y) { print y; }
function a (x) { print x; }
BEGIN { BEGIN {
local a; local a;
a = 20; a = 20;
a (1000); a (1000);
{
local a;
print a;
}
} }