Skip to content

Commit 357de7f

Browse files
author
Max Maischein
committed
Initial attempt at feature 'try'
* Add feature, experimental warning, keyword * Basic parsing * Basic implementation as optree fragment See also Perl#18504 # Conflicts: # ext/Opcode/Opcode.pm
1 parent a80efdc commit 357de7f

34 files changed

+2342
-1885
lines changed

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5961,6 +5961,7 @@ t/op/time.t See if time functions work
59615961
t/op/time_loop.t Test that very large values don't hang gmtime and localtime.
59625962
t/op/tr.t See if tr works
59635963
t/op/tr_latin1.t See if tr works, but file isn't encoded in UTF-8
5964+
t/op/try.t See if try works
59645965
t/op/undef.t See if undef works
59655966
t/op/universal.t See if UNIVERSAL class works
59665967
t/op/unlink.t See if unlink works

cop.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ struct context {
940940
or plain block { ...; } */
941941
#define CXt_SUB 9
942942
#define CXt_FORMAT 10
943-
#define CXt_EVAL 11
943+
#define CXt_EVAL 11 /* eval'', eval{}, try{} */
944944
#define CXt_SUBST 12
945945
/* SUBST doesn't feature in all switch statements. */
946946

@@ -954,6 +954,7 @@ struct context {
954954
/* private flags for CXt_EVAL */
955955
#define CXp_REAL 0x20 /* truly eval'', not a lookalike */
956956
#define CXp_EVALBLOCK 0x40 /* eval{}, not eval'' or similar */
957+
#define CXp_TRY 0x80 /* try {} block */
957958

958959
/* private flags for CXt_LOOP */
959960

@@ -977,6 +978,8 @@ struct context {
977978
== (CXt_EVAL|CXp_REAL))
978979
#define CxEVALBLOCK(c) (((c)->cx_type & (CXTYPEMASK|CXp_EVALBLOCK)) \
979980
== (CXt_EVAL|CXp_EVALBLOCK))
981+
#define CxTRY(c) (((c)->cx_type & (CXTYPEMASK|CXp_TRY)) \
982+
== (CXt_EVAL|CXp_TRY))
980983
#define CxFOREACH(c) ( CxTYPE(cx) >= CXt_LOOP_ARY \
981984
&& CxTYPE(cx) <= CXt_LOOP_LIST)
982985

ext/Opcode/Opcode.pm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use strict;
66

77
our @EXPORT_OK;
88

9-
our $VERSION = "1.50";
9+
$VERSION = "1.50";
1010

1111
use Carp;
1212
use Exporter 'import';
@@ -343,7 +343,7 @@ invert_opset function.
343343
344344
cond_expr flip flop andassign orassign dorassign and or dor xor
345345
346-
warn die lineseq nextstate scope enter leave
346+
warn die lineseq nextstate scope enter leave catch
347347
348348
rv2cv anoncode prototype coreargs avhvswitch anonconst
349349
@@ -561,7 +561,7 @@ SystemV Interprocess Communications:
561561
This tag holds opcodes related to loading modules and getting information
562562
about calling environment and args.
563563
564-
require dofile
564+
require dofile
565565
caller runcv
566566
567567
=item :still_to_be_decided

feature.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
#define FEATURE_SIGNATURES_BIT 0x1000
2828
#define FEATURE_STATE_BIT 0x2000
2929
#define FEATURE_SWITCH_BIT 0x4000
30-
#define FEATURE_UNIEVAL_BIT 0x8000
31-
#define FEATURE_UNICODE_BIT 0x10000
30+
#define FEATURE_TRY_BIT 0x8000
31+
#define FEATURE_UNIEVAL_BIT 0x10000
32+
#define FEATURE_UNICODE_BIT 0x20000
3233

3334
#define FEATURE_BUNDLE_DEFAULT 0
3435
#define FEATURE_BUNDLE_510 1
@@ -72,6 +73,12 @@
7273
FEATURE_IS_ENABLED_MASK(FEATURE_SAY_BIT)) \
7374
)
7475

76+
#define FEATURE_TRY_IS_ENABLED \
77+
( \
78+
CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
79+
FEATURE_IS_ENABLED_MASK(FEATURE_TRY_BIT) \
80+
)
81+
7582
#define FEATURE_STATE_IS_ENABLED \
7683
( \
7784
(CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_510 && \
@@ -337,6 +344,14 @@ S_magic_sethint_feature(pTHX_ SV *keysv, const char *keypv, STRLEN keylen,
337344
}
338345
return;
339346

347+
case 't':
348+
if (keylen == sizeof("feature_try")-1
349+
&& memcmp(subf+1, "ry", keylen - sizeof("feature_")) == 0) {
350+
mask = FEATURE_TRY_BIT;
351+
break;
352+
}
353+
return;
354+
340355
case 'u':
341356
if (keylen == sizeof("feature_unicode")-1
342357
&& memcmp(subf+1, "nicode", keylen - sizeof("feature_")) == 0) {

gv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ S_maybe_add_coresub(pTHX_ HV * const stash, GV *gv,
544544
/* no support for \&CORE::infix;
545545
no support for funcs that do not parse like funcs */
546546
case KEY___DATA__: case KEY___END__: case KEY_and: case KEY_AUTOLOAD:
547-
case KEY_BEGIN : case KEY_CHECK : case KEY_cmp:
547+
case KEY_BEGIN : case KEY_CHECK : case KEY_catch : case KEY_cmp:
548548
case KEY_default : case KEY_DESTROY:
549549
case KEY_do : case KEY_dump : case KEY_else : case KEY_elsif :
550550
case KEY_END : case KEY_eq : case KEY_eval :
@@ -559,7 +559,7 @@ S_maybe_add_coresub(pTHX_ HV * const stash, GV *gv,
559559
case KEY_qx : case KEY_redo : case KEY_require: case KEY_return:
560560
case KEY_s : case KEY_say : case KEY_sort :
561561
case KEY_state: case KEY_sub :
562-
case KEY_tr : case KEY_UNITCHECK: case KEY_unless:
562+
case KEY_tr : case KEY_try : case KEY_UNITCHECK: case KEY_unless:
563563
case KEY_until: case KEY_use : case KEY_when : case KEY_while :
564564
case KEY_x : case KEY_xor : case KEY_y :
565565
return NULL;

keywords.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ Perl_keyword (pTHX_ const char *name, I32 len, bool all_keywords)
203203
goto unknown;
204204
}
205205

206-
case 3: /* 29 tokens of length 3 */
206+
case 3: /* 30 tokens of length 3 */
207207
switch (name[0])
208208
{
209209
case 'E':
@@ -463,13 +463,27 @@ Perl_keyword (pTHX_ const char *name, I32 len, bool all_keywords)
463463
}
464464

465465
case 't':
466-
if (name[1] == 'i' &&
467-
name[2] == 'e')
468-
{ /* tie */
469-
return -KEY_tie;
470-
}
466+
switch (name[1])
467+
{
468+
case 'i':
469+
if (name[2] == 'e')
470+
{ /* tie */
471+
return -KEY_tie;
472+
}
471473

472-
goto unknown;
474+
goto unknown;
475+
476+
case 'r':
477+
if (name[2] == 'y')
478+
{ /* try */
479+
return (all_keywords || FEATURE_TRY_IS_ENABLED ? KEY_try : 0);
480+
}
481+
482+
goto unknown;
483+
484+
default:
485+
goto unknown;
486+
}
473487

474488
case 'u':
475489
if (name[1] == 's' &&
@@ -964,7 +978,7 @@ Perl_keyword (pTHX_ const char *name, I32 len, bool all_keywords)
964978
goto unknown;
965979
}
966980

967-
case 5: /* 39 tokens of length 5 */
981+
case 5: /* 40 tokens of length 5 */
968982
switch (name[0])
969983
{
970984
case 'B':
@@ -1046,6 +1060,16 @@ Perl_keyword (pTHX_ const char *name, I32 len, bool all_keywords)
10461060
case 'c':
10471061
switch (name[1])
10481062
{
1063+
case 'a':
1064+
if (name[2] == 't' &&
1065+
name[3] == 'c' &&
1066+
name[4] == 'h')
1067+
{ /* catch */
1068+
return (all_keywords || FEATURE_TRY_IS_ENABLED ? KEY_catch : 0);
1069+
}
1070+
1071+
goto unknown;
1072+
10491073
case 'h':
10501074
switch (name[2])
10511075
{
@@ -3451,5 +3475,5 @@ Perl_keyword (pTHX_ const char *name, I32 len, bool all_keywords)
34513475
}
34523476

34533477
/* Generated from:
3454-
* dd384f0c948716414a93d758d89a38e3c8116acfdc48eb7e34fa6737887097d5 regen/keywords.pl
3478+
* 3a4f2004642b00b871c01cbdc018f6ca5ead6b4e0b2b184120c60b0b62a229dd regen/keywords.pl
34553479
* ex: set ro: */

0 commit comments

Comments
 (0)