From 7f479f156b93c86c8f42de9a81ba2f96d2220deb Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 4 May 2020 07:14:32 +0000 Subject: [PATCH] added special parser-level substitution word - @SCRIPTNAME, @SCRIPTLINE --- hawk/lib/parse.c | 46 +++++++++++++++-------------- hawk/t/h-001.hawk | 74 +++++++++++++++++++++++------------------------ 2 files changed, 62 insertions(+), 58 deletions(-) diff --git a/hawk/lib/parse.c b/hawk/lib/parse.c index 83a09f2a..87c9270f 100644 --- a/hawk/lib/parse.c +++ b/hawk/lib/parse.c @@ -136,7 +136,6 @@ enum tok_t TOK_COLON, TOK_DBLCOLON, TOK_QUEST, - /*TOK_DBLAT,*/ /* == begin reserved words == */ /* === extended reserved words === */ @@ -398,7 +397,7 @@ static global_t gtab[] = #define ADD_TOKEN_CHAR(hawk,tok,c) \ do { \ - if (hawk_ooecs_ccat((tok)->name,(c)) == (hawk_oow_t)-1) \ + if (HAWK_UNLIKELY(hawk_ooecs_ccat((tok)->name,(c)) == (hawk_oow_t)-1)) \ { \ ADJERR_LOC (hawk, &(tok)->loc); \ return -1; \ @@ -407,7 +406,7 @@ static global_t gtab[] = #define ADD_TOKEN_STR(hawk,tok,s,l) \ do { \ - if (hawk_ooecs_ncat((tok)->name,(s),(l)) == (hawk_oow_t)-1) \ + if (HAWK_UNLIKELY(hawk_ooecs_ncat((tok)->name,(s),(l)) == (hawk_oow_t)-1)) \ { \ ADJERR_LOC (hawk, &(tok)->loc); \ return -1; \ @@ -504,7 +503,7 @@ static int get_char (hawk_t* hawk) } hawk->sio.inp->b.pos = 0; - hawk->sio.inp->b.len = n; + hawk->sio.inp->b.len = n; } if (hawk->sio.inp->last.c == HAWK_T('\n')) @@ -6642,7 +6641,7 @@ retry: } else if (hawk_is_ooch_digit(c)/*|| c == HAWK_T('.')*/) { - if (get_number (hawk, tok) <= -1) return -1; + if (get_number(hawk, tok) <= -1) return -1; } else if (c == HAWK_T('.')) { @@ -6672,17 +6671,7 @@ retry: ADD_TOKEN_CHAR (hawk, tok, c); GET_CHAR_TO (hawk, c); - /* - if (c == HAWK_T('@')) - { - SET_TOKEN_TYPE (hawk, tok, TOK_DBLAT); - GET_CHAR (hawk); - }*/ - /* other special extended operators here - else if (c == HAWK_T('*')) - { - }*/ - /*else*/ if (c != HAWK_T('_') && !hawk_is_ooch_alpha(c)) + if (c != HAWK_T('_') && !hawk_is_ooch_alpha(c)) { /* this extended keyword is empty, * not followed by a valid word */ @@ -6699,13 +6688,28 @@ retry: } while (c == HAWK_T('_') || hawk_is_ooch_alpha(c) || hawk_is_ooch_digit(c)); - type = classify_ident(hawk, HAWK_OOECS_OOCS(tok->name)); - if (type == TOK_IDENT) + if (hawk_comp_oochars_bcstr(HAWK_OOECS_PTR(tok->name), HAWK_OOECS_LEN(tok->name), "@SCRIPTNAME") == 0) { - hawk_seterrfmt (hawk, &hawk->tok.loc, HAWK_EXKWNR, FMT_EXKWNR, HAWK_OOECS_LEN(hawk->tok.name), HAWK_OOECS_PTR(hawk->tok.name)); - return -1; + /* special parser-level word @SCRIPTNAME. substitute an actual value for it */ + if (HAWK_UNLIKELY(hawk_ooecs_cpy(tok->name, tok->loc.file) == (hawk_oow_t)-1)) return -1; + SET_TOKEN_TYPE (hawk, tok, TOK_STR); + } + else if (hawk_comp_oochars_bcstr(HAWK_OOECS_PTR(tok->name), HAWK_OOECS_LEN(tok->name), "@SCRIPTLINE") == 0) + { + /* special parser-level word @SCRIPTLINE. subsitute an actual value for it */ + if (HAWK_UNLIKELY(hawk_ooecs_fmt(tok->name, HAWK_T("%zu"), tok->loc.line) == (hawk_oow_t)-1)) return -1; + SET_TOKEN_TYPE (hawk, tok, TOK_INT); + } + else + { + type = classify_ident(hawk, HAWK_OOECS_OOCS(tok->name)); + if (type == TOK_IDENT) + { + hawk_seterrfmt (hawk, &hawk->tok.loc, HAWK_EXKWNR, FMT_EXKWNR, HAWK_OOECS_LEN(hawk->tok.name), HAWK_OOECS_PTR(hawk->tok.name)); + return -1; + } + SET_TOKEN_TYPE (hawk, tok, type); } - SET_TOKEN_TYPE (hawk, tok, type); } } else if (c == HAWK_T('B')) diff --git a/hawk/t/h-001.hawk b/hawk/t/h-001.hawk index c4c29ffa..0a657d7f 100644 --- a/hawk/t/h-001.hawk +++ b/hawk/t/h-001.hawk @@ -1,11 +1,11 @@ @pragma entry main -function ensure (a, b, desc) +function ensure (a, b, desc, line) { if (a != b) { - print "FAILURE in", desc; + printf ("FAILURE[%d] %s\n", line, desc); exit (-1); } } @@ -33,13 +33,13 @@ function main() x = 20; y = 90; r = call_by_ref_1(x, y, z); - ensure (r, 1800, SCRIPTNAME); + ensure (r, 1800, @SCRIPTNAME, @SCRIPTLINE); ensure (x, 20); ensure (y, 90); ensure (z, "hello, world"); ## TODO: add a new special word, @FILENAME, @FILELINE, @LINE <--- which are understood by the parser and swapped to the actual value -## SCRIPTNAME doesn't tell the main file. +## @SCRIPTNAME, @SCRIPTLINE doesn't tell the main file. { @local b; @@ -60,9 +60,9 @@ function main() b[1] = 1; r = hawk::call("call_by_ref_2", 99, b); ensure (r, 99); - ensure (length(b), 2, SCRIPTNAME); - ensure (b[1], 99, SCRIPTNAME); - ensure (b[2], "perfect", SCRIPTNAME); + ensure (length(b), 2, @SCRIPTNAME, @SCRIPTLINE); + ensure (b[1], 99, @SCRIPTNAME, @SCRIPTLINE); + ensure (b[2], "perfect", @SCRIPTNAME, @SCRIPTLINE); } @@ -70,11 +70,11 @@ function main() @local a; a[1] = hawk::array(); a[1][2]=20; - ensure (a[1][2], 20, SCRIPTNAME); + ensure (a[1][2], 20, @SCRIPTNAME, @SCRIPTLINE); a[1][2]++; - ensure (a[1][2], 21, SCRIPTNAME); + ensure (a[1][2], 21, @SCRIPTNAME, @SCRIPTLINE); --a[1][2]; - ensure (a[1][2], 20, SCRIPTNAME); + ensure (a[1][2], 20, @SCRIPTNAME, @SCRIPTLINE); } { @@ -83,24 +83,24 @@ function main() a[10] = "good"; a[0][40] = "bye" a[0][0] = "farewell" - ensure (hawk::typename(a), "array", SCRIPTNAME); - ensure (hawk::typename(a[0]), "array", SCRIPTNAME); + ensure (hawk::typename(a), "array", @SCRIPTNAME, @SCRIPTLINE); + ensure (hawk::typename(a[0]), "array", @SCRIPTNAME, @SCRIPTLINE); call_by_ref_3(a[9]); - ensure (a[10], "good", SCRIPTNAME); - ensure (a[9], "hello world", SCRIPTNAME); + ensure (a[10], "good", @SCRIPTNAME, @SCRIPTLINE); + ensure (a[9], "hello world", @SCRIPTNAME, @SCRIPTLINE); call_by_ref_3(a[10]); - ensure (a[10], "hello world", SCRIPTNAME); - ensure (a[9], "hello world", SCRIPTNAME); - ensure (a[0][40], "bye", SCRIPTNAME); + ensure (a[10], "hello world", @SCRIPTNAME, @SCRIPTLINE); + ensure (a[9], "hello world", @SCRIPTNAME, @SCRIPTLINE); + ensure (a[0][40], "bye", @SCRIPTNAME, @SCRIPTLINE); call_by_ref_3(a[0][40]); - ensure (a[0][40], "hello world", SCRIPTNAME); - ensure (length(a[0]), 2, SCRIPTNAME); + ensure (a[0][40], "hello world", @SCRIPTNAME, @SCRIPTLINE); + ensure (length(a[0]), 2, @SCRIPTNAME, @SCRIPTLINE); delete a[0][40]; - ensure (length(a[0]), 1, SCRIPTNAME); - ensure (a[0][0], "farewell", SCRIPTNAME); - ensure (a[0][40], nil, SCRIPTNAME); - #hawk::splice (a[0], 40, 1, SCRIPTNAME); - #ensure (length(a[0]), 40, SCRIPTNAME); + ensure (length(a[0]), 1, @SCRIPTNAME, @SCRIPTLINE); + ensure (a[0][0], "farewell", @SCRIPTNAME, @SCRIPTLINE); + ensure (a[0][40], nil, @SCRIPTNAME, @SCRIPTLINE); + #hawk::splice (a[0], 40, 1, @SCRIPTNAME, @SCRIPTLINE); + #ensure (length(a[0]), 40, @SCRIPTNAME, @SCRIPTLINE); } @@ -108,25 +108,25 @@ function main() @local a, i; a = hawk::array(); - ensure (length(a), 0, SCRIPTNAME); + ensure (length(a), 0, @SCRIPTNAME, @SCRIPTLINE); for (i = 0; i < 10; i++) a[i*i]=i; - ensure (length(a), 10, SCRIPTNAME); + ensure (length(a), 10, @SCRIPTNAME, @SCRIPTLINE); delete a[0]; - ensure (length(a), 9, SCRIPTNAME); + ensure (length(a), 9, @SCRIPTNAME, @SCRIPTLINE); delete a[81]; - ensure (length(a), 8, SCRIPTNAME); + ensure (length(a), 8, @SCRIPTNAME, @SCRIPTLINE); delete a[36]; - ensure (length(a), 7, SCRIPTNAME); + ensure (length(a), 7, @SCRIPTNAME, @SCRIPTLINE); for (i = 0; i < 10; i++) delete a[i*i]; - ensure (length(a), 0, SCRIPTNAME); + ensure (length(a), 0, @SCRIPTNAME, @SCRIPTLINE); delete a; - ensure (length(a), 0, SCRIPTNAME); + ensure (length(a), 0, @SCRIPTNAME, @SCRIPTLINE); } { @@ -134,19 +134,19 @@ function main() a = hawk::array(); a[10][20][30] = 20; b[12][20][30] = 20; - ensure ((10 in a) && (20 in a[10]) && (30 in a[10][20]), 1, SCRIPTNAME); - ensure ((11 in a) && (20 in a[10]) && (30 in a[10][20]), 0, SCRIPTNAME); + ensure ((10 in a) && (20 in a[10]) && (30 in a[10][20]), 1, @SCRIPTNAME, @SCRIPTLINE); + ensure ((11 in a) && (20 in a[10]) && (30 in a[10][20]), 0, @SCRIPTNAME, @SCRIPTLINE); b = hawk::map(); b[10][20][30] = 20; b[12][20][30] = 20; - ensure ((10 in a) && (20 in a[10]) && (30 in a[10][20]), 1, SCRIPTNAME); - ensure ((11 in a) && (20 in a[10]) && (30 in a[10][20]), 0, SCRIPTNAME); + ensure ((10 in a) && (20 in a[10]) && (30 in a[10][20]), 1, @SCRIPTNAME, @SCRIPTLINE); + ensure ((11 in a) && (20 in a[10]) && (30 in a[10][20]), 0, @SCRIPTNAME, @SCRIPTLINE); c = hawk::map(); c[10,20,30] = "heloo"; - ensure (((10,20,30) in c), 1, SCRIPTNAME); - ensure (((10,30,30) in c), 0, SCRIPTNAME); + ensure (((10,20,30) in c), 1, @SCRIPTNAME, @SCRIPTLINE); + ensure (((10,30,30) in c), 0, @SCRIPTNAME, @SCRIPTLINE); } print "SUCCESS"