X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=perly.y;h=c81cee35ba7e97864674157e24fb915d714bc052;hb=0d863452f5cac86322a90184dc68dbf446006ed7;hp=2e218624c24e99b52e49ba1225b92fb3fb7bb993;hpb=9a9798c21b8a0b65f8e87f35074ebb595dc00f1c;p=p5sagit%2Fp5-mst-13.2.git diff --git a/perly.y b/perly.y index 2e21862..c81cee3 100644 --- a/perly.y +++ b/perly.y @@ -41,15 +41,16 @@ %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 mintro -%type progstart remember mremember '&' savescope +%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 miexpr @@ -57,6 +58,7 @@ %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 +79,7 @@ %nonassoc EQOP %nonassoc RELOP %nonassoc UNIOP UNIOPSUB +%nonassoc REQUIRE %left SHIFTOP %left ADDOP %left MULOP @@ -108,6 +111,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 +152,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 +210,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; } @@ -252,6 +271,15 @@ loop : label WHILE '(' remember texpr ')' mintro mblock cont 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 && @@ -338,7 +366,7 @@ startformsub: /* NULL */ /* start a format subroutine scope */ ; /* Name of a subroutine - must be a bareword, could be special */ -subname : WORD { STRLEN n_a; const char *name = SvPV_const(((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); @@ -566,7 +594,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() */ @@ -664,6 +692,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))); }