From 14b0bca55cb652f8bbb59c078ccfb3b6cffa6e05 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 29 Jun 2009 07:41:47 +0000 Subject: [PATCH] improved the naming rule --- qse/cmd/awk/awk.c | 4 +++- qse/include/qse/awk/awk.h | 28 ++++++++++++++++--------- qse/lib/awk/parse.c | 40 ++++++++++++++++++++++++++++-------- qse/regress/awk/lang-005.awk | 9 ++++++-- 4 files changed, 60 insertions(+), 21 deletions(-) diff --git a/qse/cmd/awk/awk.c b/qse/cmd/awk/awk.c index a245ecc7..2dd46b0c 100644 --- a/qse/cmd/awk/awk.c +++ b/qse/cmd/awk/awk.c @@ -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. @@ -345,6 +345,7 @@ struct opttab_t { 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("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 } }; @@ -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(":rexbound"), QSE_T('\0') }, { QSE_T(":ncmponstr"), QSE_T('\0') }, + { QSE_T(":strictnaming"), QSE_T('\0') }, { QSE_T(":call"), QSE_T('c') }, { QSE_T(":file"), QSE_T('f') }, diff --git a/qse/include/qse/awk/awk.h b/qse/include/qse/awk/awk.h index a91e3a25..902dfbda 100644 --- a/qse/include/qse/awk/awk.h +++ b/qse/include/qse/awk/awk.h @@ -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. @@ -487,25 +487,25 @@ enum qse_awk_option_t * " a b c " is split to [a], [b], [c] if #QSE_AWK_STRIPSPACES is on. * Otherwise, it is split to [], [a], [b], [c], []. */ - QSE_AWK_STRIPSPACES = (1 << 11), + QSE_AWK_STRIPSPACES = (1 << 10), /** enables @b nextofile */ - QSE_AWK_NEXTOFILE = (1 << 12), + QSE_AWK_NEXTOFILE = (1 << 11), /** enables @b reset */ - QSE_AWK_RESET = (1 << 13), + QSE_AWK_RESET = (1 << 12), /** CR + LF by default */ - QSE_AWK_CRLF = (1 << 14), + QSE_AWK_CRLF = (1 << 13), /** 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 */ - QSE_AWK_PABLOCK = (1 << 16), + QSE_AWK_PABLOCK = (1 << 15), /** allows {n,m} in a regular expression. */ - QSE_AWK_REXBOUND = (1 << 17), + QSE_AWK_REXBOUND = (1 << 16), /** * 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; * - "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 * implementations */ QSE_AWK_CLASSIC = QSE_AWK_IMPLICIT | QSE_AWK_RIO | - QSE_AWK_NEWLINE | QSE_AWK_PABLOCK + QSE_AWK_NEWLINE | QSE_AWK_PABLOCK | + QSE_AWK_STRICTNAMING }; /** diff --git a/qse/lib/awk/parse.c b/qse/lib/awk/parse.c index 409a3ca9..76e7b9ba 100644 --- a/qse/lib/awk/parse.c +++ b/qse/lib/awk/parse.c @@ -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. @@ -947,8 +947,9 @@ static qse_awk_nde_t* parse_function (qse_awk_t* awk) /* check if a parameter conflicts with the function * name or other parameters */ - if (qse_strxncmp ( - param, param_len, name_dup, name_len) == 0 || + if (((awk->option & QSE_AWK_STRICTNAMING) && + qse_strxncmp ( + param, param_len, name_dup, name_len) == 0) || qse_lda_search (awk->parse.params, 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); /* 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) { SETERRARG ( @@ -1743,8 +1744,11 @@ static qse_awk_t* collect_locals ( if (istop) { - /* check if it conflicts with a paremeter name */ - n = qse_lda_search (awk->parse.params, 0, lcl.ptr, lcl.len); + /* check if it conflicts with a parameter name. + * 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) { 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 */ n = qse_lda_search ( awk->parse.lcls, @@ -1771,9 +1794,10 @@ static qse_awk_t* collect_locals ( n = find_global (awk, lcl.ptr, lcl.len); if (n != QSE_LDA_NIL) { - 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 ( awk, QSE_AWK_EDUPLCL, awk->token.line, lcl.ptr, lcl.len); diff --git a/qse/regress/awk/lang-005.awk b/qse/regress/awk/lang-005.awk index 490fa372..36a2a721 100644 --- a/qse/regress/awk/lang-005.awk +++ b/qse/regress/awk/lang-005.awk @@ -1,7 +1,12 @@ -function a (a) { print a; } +function x (y) { print y; } +function a (x) { print x; } BEGIN { local a; a = 20; - a (1000); + a (1000); + { + local a; + print a; + } }