X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/131b3ad08b25bcfcb31b9933319b3186bdd13248..121ec5ba7a19a219149ea3c8c8a845624b2bf8ea:/perly.y diff --git a/perly.y b/perly.y index 11c1bd3..cbd2f7a 100644 --- a/perly.y +++ b/perly.y @@ -30,6 +30,9 @@ %union { I32 ival; char *pval; +#ifdef PERL_MAD + TOKEN* tkval; +#endif OP *opval; GV *gvval; } @@ -41,22 +44,24 @@ %token LABEL %token FORMAT SUB ANONSUB PACKAGE USE %token WHILE UNTIL IF UNLESS ELSE ELSIF CONTINUE FOR +%token GIVEN WHEN DEFAULT %token LOOPEX DOTDOT %token FUNC0 FUNC1 FUNC UNIOP LSTOP %token RELOP EQOP MULOP ADDOP %token DOLSHARP DO HASHBRACK NOAMP -%token LOCAL MY MYSUB +%token LOCAL MY MYSUB REQUIRE %token COLONATTR -%type prog decl format startsub startanonsub startformsub -%type progstart remember mremember '&' savescope +%type prog decl format startsub startanonsub startformsub mintro +%type progstart remember mremember '&' savescope mydefsv %type block mblock lineseq line loop cond else %type expr term subscripted scalar ary hsh arylen star amper sideff -%type argexpr nexpr texpr iexpr mexpr mnexpr mtexpr miexpr +%type argexpr nexpr texpr iexpr mexpr mnexpr miexpr %type listexpr listexprcom indirob listop method %type formname subname proto subbody cont my_scalar %type subattrlist myattrlist mysubrout myattrterm myterm %type termbinop termunop anonymous termdo +%type switch case %type label %nonassoc PREC_LOW @@ -77,6 +82,7 @@ %nonassoc EQOP %nonassoc RELOP %nonassoc UNIOP UNIOPSUB +%nonassoc REQUIRE %left SHIFTOP %left ADDOP %left MULOP @@ -89,6 +95,8 @@ %left '(' %left '[' '{' +%token PEG + %% /* RULES */ /* The whole program */ @@ -108,6 +116,10 @@ remember: /* NULL */ /* start a full lexical scope */ { $$ = block_start(TRUE); } ; +mydefsv: /* NULL */ /* lexicalize $_ */ + { $$ = (I32) allocmy("$_"); } + ; + progstart: { PL_expect = XSTATE; $$ = block_start(TRUE); @@ -145,6 +157,10 @@ lineseq : /* NULL */ line : label cond { $$ = newSTATEOP(0, $1, $2); } | loop /* loops add their own labels */ + | switch /* ... and so do switches */ + { $$ = $1; } + | label case + { $$ = newSTATEOP(0, $1, $2); } | label ';' { if ($1 != Nullch) { $$ = newSTATEOP(0, $1, newOP(OP_NULL, 0)); @@ -199,6 +215,14 @@ cond : IF '(' remember mexpr ')' mblock else newCONDOP(0, $4, scope($6), $7)); } ; +/* Cases for a switch statement */ +case : WHEN '(' remember mexpr ')' mblock + { $$ = block_end($3, + newWHENOP($4, scope($6))); } + | DEFAULT block + { $$ = newWHENOP(0, scope($2)); } + ; + /* Continue blocks */ cont : /* NULL */ { $$ = Nullop; } @@ -207,18 +231,18 @@ cont : /* NULL */ ; /* Loops: while, until, for, and a bare block */ -loop : label WHILE '(' remember mtexpr ')' mblock cont +loop : label WHILE '(' remember texpr ')' mintro mblock cont { PL_copline = (line_t)$2; $$ = block_end($4, newSTATEOP(0, $1, newWHILEOP(0, 1, (LOOP*)Nullop, - $2, $5, $7, $8))); } - | label UNTIL '(' remember miexpr ')' mblock cont + $2, $5, $8, $9, $7))); } + | label UNTIL '(' remember iexpr ')' mintro mblock cont { PL_copline = (line_t)$2; $$ = block_end($4, newSTATEOP(0, $1, newWHILEOP(0, 1, (LOOP*)Nullop, - $2, $5, $7, $8))); } + $2, $5, $8, $9, $7))); } | label FOR MY remember my_scalar '(' mexpr ')' mblock cont { $$ = block_end($4, newFOROP(0, $1, (line_t)$2, $5, $7, $9, $10)); } @@ -229,14 +253,15 @@ loop : label WHILE '(' remember mtexpr ')' mblock cont | label FOR '(' remember mexpr ')' mblock cont { $$ = block_end($4, newFOROP(0, $1, (line_t)$2, Nullop, $5, $7, $8)); } - | label FOR '(' remember mnexpr ';' mtexpr ';' mnexpr ')' mblock + | label FOR '(' remember mnexpr ';' texpr ';' mintro mnexpr ')' + mblock /* basically fake up an initialize-while lineseq */ { OP *forop; PL_copline = (line_t)$2; forop = newSTATEOP(0, $1, newWHILEOP(0, 1, (LOOP*)Nullop, $2, scalar($7), - $11, $9)); + $12, $10, $9)); if ($5) { forop = append_elem(OP_LINESEQ, newSTATEOP(0, ($1?savepv($1):Nullch), @@ -248,9 +273,24 @@ loop : label WHILE '(' remember mtexpr ')' mblock cont | label block cont /* a block is a loop that happens once */ { $$ = newSTATEOP(0, $1, newWHILEOP(0, 1, (LOOP*)Nullop, - NOLINE, Nullop, $2, $3)); } + NOLINE, Nullop, $2, $3, 0)); } ; +/* Switch blocks */ +switch : label GIVEN '(' remember mydefsv mexpr ')' mblock + { PL_copline = (line_t) $2; + $$ = block_end($4, + newSTATEOP(0, $1, + newGIVENOP($6, scope($8), + (PADOFFSET) $5) )); } + ; + +/* determine whether there are any new my declarations */ +mintro : /* NULL */ + { $$ = (PL_min_intro_pending && + PL_max_intro_pending >= PL_min_intro_pending); + intro_my(); } + /* Normal expression */ nexpr : /* NULL */ { $$ = Nullop; } @@ -277,10 +317,6 @@ mnexpr : nexpr { $$ = $1; intro_my(); } ; -mtexpr : texpr - { $$ = $1; intro_my(); } - ; - miexpr : iexpr { $$ = $1; intro_my(); } ; @@ -335,7 +371,7 @@ startformsub: /* NULL */ /* start a format subroutine scope */ ; /* Name of a subroutine - must be a bareword, could be special */ -subname : WORD { STRLEN n_a; char *name = SvPV(((SVOP*)$1)->op_sv,n_a); +subname : WORD { const char *const name = SvPV_nolen_const(((SVOP*)$1)->op_sv); if (strEQ(name, "BEGIN") || strEQ(name, "END") || strEQ(name, "INIT") || strEQ(name, "CHECK")) CvSPECIAL_on(PL_compcv); @@ -398,7 +434,7 @@ argexpr : argexpr ',' ; /* List operators */ -listop : LSTOP indirob argexpr /* print $fh @args */ +listop : LSTOP indirob argexpr /* map {...} @args or print $fh @args */ { $$ = convert($1, OPf_STACKED, prepend_elem(OP_LIST, newGVREF($1,$2), $3) ); } | FUNC '(' indirob expr ')' /* print ($fh @args */ @@ -427,7 +463,7 @@ listop : LSTOP indirob argexpr /* print $fh @args */ { $$ = convert($1, 0, $2); } | FUNC '(' listexprcom ')' /* print (@args) */ { $$ = convert($1, 0, $3); } - | LSTOPSUB startanonsub block /* map { foo } ... */ + | LSTOPSUB startanonsub block /* sub f(&@); f { foo } ... */ { $3 = newANONATTRSUB($2, 0, Nullop, $3); } listexpr %prec LSTOP /* ... @bar */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, @@ -484,6 +520,10 @@ subscripted: star '{' expr ';' '}' /* *main::{something} */ | subscripted '(' ')' /* $foo->{bar}->() */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, newCVREF(0, scalar($1))); } + | '(' expr ')' '[' expr ']' /* list slice */ + { $$ = newSLICEOP(0, $5, $2); } + | '(' ')' '[' expr ']' /* empty list slice! */ + { $$ = newSLICEOP(0, $4, Nullop); } ; /* Binary operators between terms */ @@ -559,7 +599,7 @@ anonymous: '[' expr ']' /* Things called with "do" */ termdo : DO term %prec UNIOP /* do $filename */ - { $$ = dofile($2); } + { $$ = dofile($2, $1); } | DO block %prec '(' /* do { code */ { $$ = newUNOP(OP_NULL, OPf_SPECIAL, scope($2)); } | DO WORD '(' ')' /* do somesub() */ @@ -619,10 +659,6 @@ term : termbinop { $$ = newUNOP(OP_AV2ARYLEN, 0, ref($1, OP_AV2ARYLEN));} | subscripted { $$ = $1; } - | '(' expr ')' '[' expr ']' /* list slice */ - { $$ = newSLICEOP(0, $5, $2); } - | '(' ')' '[' expr ']' /* empty list slice! */ - { $$ = newSLICEOP(0, $4, Nullop); } | ary '[' expr ']' /* array slice */ { $$ = prepend_elem(OP_ASLICE, newOP(OP_PUSHMARK, 0), @@ -661,6 +697,10 @@ term : termbinop { $$ = newUNOP($1, 0, $2); } | UNIOP term /* Unary op */ { $$ = newUNOP($1, 0, $2); } + | REQUIRE /* require, $_ implied */ + { $$ = newOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0); } + | REQUIRE term /* require Foo */ + { $$ = newUNOP(OP_REQUIRE, $1 ? OPf_SPECIAL : 0, $2); } | UNIOPSUB term /* Sub treated as unop */ { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, append_elem(OP_LIST, $2, scalar($1))); } @@ -672,7 +712,8 @@ term : termbinop { $$ = newUNOP(OP_ENTERSUB, OPf_STACKED, scalar($1)); } | FUNC1 '(' ')' /* not () */ - { $$ = newOP($1, OPf_SPECIAL); } + { $$ = $1 == OP_NOT ? newUNOP($1, 0, newSVOP(OP_CONST, 0, newSViv(0))) + : newOP($1, OPf_SPECIAL); } | FUNC1 '(' expr ')' /* not($foo) */ { $$ = newUNOP($1, 0, $3); } | PMFUNC '(' argexpr ')' /* m//, s///, tr/// */