Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Perlito5 - parser: "C-style" loop

  • Loading branch information...
commit db64ad636ed3b1fb4d5cb8c3fc2d4e6297c09abe 1 parent d9b0cb6
@fglock authored
View
3  TODO-perlito5
@@ -70,6 +70,9 @@ TODO list for Perlito5
* Javascript backend
+-- bug: some declarations are not "seen":
+ for ( my $i = 0; $i < 10 ; $i++ ) { print "$i\n" } # compiles to global $i
+
-- bug: don't emit throw() in term position; javascript requires a function() wrapper because throw() is a statement
-- initialize @INC from $ENV{PERL5LIB}
View
104 html/perlito5.js
@@ -3001,6 +3001,13 @@ var p5100 = p5pkg['main'];
(v_self = (p5pkg["Perlito5::AST::For"].shift([List__])));
var v_level = null;
(v_level = (p5pkg["Perlito5::AST::For"].shift([List__])));
+ if ( (p5str(p5pkg["Perlito5::AST::For"].ref([(v_self || (v_self = new p5HashRef({})))._hash_['cond']], 0)) == 'ARRAY') ) {
+ (function () {
+ var v_body = null;
+ (v_body = (p5call(p5pkg["Perlito5::Javascript::LexicalBlock"], "new", p5list_to_a('block', p5call((v_self || (v_self = new p5HashRef({})))._hash_['body'], "stmts", [], 1), 'needs_return', 0, 'create_context', 1), 0)));
+ throw(p5context([('for ( ' + p5str(( p5bool(((v_self || (v_self = new p5HashRef({})))._hash_['cond'] || ((v_self || (v_self = new p5HashRef({})))._hash_['cond'] = new p5ArrayRef([])))._array_[p5idx(((v_self || (v_self = new p5HashRef({})))._hash_['cond'] || ((v_self || (v_self = new p5HashRef({})))._hash_['cond'] = new p5ArrayRef([])))._array_,0)]) ? (p5str(p5call(((v_self || (v_self = new p5HashRef({})))._hash_['cond'] || ((v_self || (v_self = new p5HashRef({})))._hash_['cond'] = new p5ArrayRef([])))._array_[p5idx(((v_self || (v_self = new p5HashRef({})))._hash_['cond'] || ((v_self || (v_self = new p5HashRef({})))._hash_['cond'] = new p5ArrayRef([])))._array_,0)], "emit_javascript", [(p5num(v_level) + 1)], 0)) + '; ') : '; ')) + p5str(( p5bool(((v_self || (v_self = new p5HashRef({})))._hash_['cond'] || ((v_self || (v_self = new p5HashRef({})))._hash_['cond'] = new p5ArrayRef([])))._array_[p5idx(((v_self || (v_self = new p5HashRef({})))._hash_['cond'] || ((v_self || (v_self = new p5HashRef({})))._hash_['cond'] = new p5ArrayRef([])))._array_,1)]) ? (p5str(p5call(((v_self || (v_self = new p5HashRef({})))._hash_['cond'] || ((v_self || (v_self = new p5HashRef({})))._hash_['cond'] = new p5ArrayRef([])))._array_[p5idx(((v_self || (v_self = new p5HashRef({})))._hash_['cond'] || ((v_self || (v_self = new p5HashRef({})))._hash_['cond'] = new p5ArrayRef([])))._array_,1)], "emit_javascript", [(p5num(v_level) + 1)], 0)) + '; ') : '; ')) + p5str(( p5bool(((v_self || (v_self = new p5HashRef({})))._hash_['cond'] || ((v_self || (v_self = new p5HashRef({})))._hash_['cond'] = new p5ArrayRef([])))._array_[p5idx(((v_self || (v_self = new p5HashRef({})))._hash_['cond'] || ((v_self || (v_self = new p5HashRef({})))._hash_['cond'] = new p5ArrayRef([])))._array_,2)]) ? (p5str(p5call(((v_self || (v_self = new p5HashRef({})))._hash_['cond'] || ((v_self || (v_self = new p5HashRef({})))._hash_['cond'] = new p5ArrayRef([])))._array_[p5idx(((v_self || (v_self = new p5HashRef({})))._hash_['cond'] || ((v_self || (v_self = new p5HashRef({})))._hash_['cond'] = new p5ArrayRef([])))._array_,2)], "emit_javascript", [(p5num(v_level) + 1)], 0)) + ' ') : ' ')) + ') {' + String.fromCharCode(10) + p5str(p5call(v_body, "emit_javascript", [(p5num(v_level) + 1)], 0)) + String.fromCharCode(10) + p5str(p5pkg["Perlito5::Javascript"].tab([v_level], 0)) + '}')], p5want));
+ })();
+ };
var v_cond = null;
(v_cond = (p5pkg["Perlito5::Javascript"].to_list(p5list_to_a((new p5ArrayRef(p5list_to_a((v_self || (v_self = new p5HashRef({})))._hash_['cond']))), (p5num(v_level) + 1)), 0)));
if ( p5bool(p5call((v_self || (v_self = new p5HashRef({})))._hash_['body'], "sig", [], 0)) ) {
@@ -6427,7 +6434,7 @@ var p5100 = p5pkg['main'];
})()], p5want) }));
})()], p5want), function () { return p5context([(function () {
((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = v_pos1);
- return (p5context([p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5context([(function () {
+ return (p5context([p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5and(p5context([(function () {
var v_m2 = null;
(v_m2 = (p5call(v_grammar, "opt_ws", p5list_to_a(v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']), 0)));
if ( p5bool(v_m2) ) {
@@ -6440,16 +6447,94 @@ var p5100 = p5pkg['main'];
}
})()], p5want), function () { return p5context([p5and(('(' == p5pkg["Perlito5::Grammar"].substr([v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'], 1], 0)), function () { return p5context([((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = (1 + p5num((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'])))], p5want) })], p5want) }), function () { return p5context([(function () {
var v_m2 = null;
- (v_m2 = (p5call(p5pkg["Perlito5::Expression"], "paren_parse", p5list_to_a(v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']), 0)));
+ (v_m2 = (p5call(p5pkg["Perlito5::Expression"], "exp_parse", p5list_to_a(v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']), 0)));
if ( p5bool(v_m2) ) {
((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = (v_m2 || (v_m2 = new p5HashRef({})))._hash_['to']);
- ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Expression.paren_parse'] = v_m2);
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Expression.exp_parse'] = v_m2);
return (p5context([1], p5want));
}
else {
return (p5context([0], p5want));
}
+ })()], p5want) }), function () { return p5context([(function () {
+ var v_pos1 = null;
+ (v_pos1 = ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']));
+ return (p5or(p5context([(function () {
+ return (p5and(p5and(p5and(p5and(p5context([p5and((';' == p5pkg["Perlito5::Grammar"].substr([v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'], 1], 0)), function () { return p5context([((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = (1 + p5num((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'])))], p5want) })], p5want), function () { return p5context([(function () {
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['c_style_for'] = 1);
+ return (p5context([1], p5want));
+ })()], p5want) }), function () { return p5context([(function () {
+ var v_pos1 = null;
+ (v_pos1 = ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']));
+ return (p5or(p5or(p5context([(function () {
+ var v_m2 = null;
+ (v_m2 = (p5call(p5pkg["Perlito5::Grammar"], "exp", p5list_to_a(v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']), 0)));
+ if ( p5bool(v_m2) ) {
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = (v_m2 || (v_m2 = new p5HashRef({})))._hash_['to']);
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp'] = v_m2);
+ return (p5context([1], p5want));
+ }
+
+ else {
+ return (p5context([0], p5want));
+ }
+ })()], p5want), function () { return p5context([(function () {
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = v_pos1);
+ return (p5context([], p5want));
+ })()], p5want) }), function () { return p5context([(function () {
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = v_pos1);
+ return (p5context([p5context([(function () {
+ var v_m2 = null;
+ (v_m2 = (p5call(v_grammar, "opt_ws", p5list_to_a(v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']), 0)));
+ if ( p5bool(v_m2) ) {
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = (v_m2 || (v_m2 = new p5HashRef({})))._hash_['to']);
+ return (p5context([1], p5want));
+ }
+
+ else {
+ return (p5context([0], p5want));
+ }
+ })()], p5want)], p5want));
+ })()], p5want) }));
+ })()], p5want) }), function () { return p5context([p5and((';' == p5pkg["Perlito5::Grammar"].substr([v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'], 1], 0)), function () { return p5context([((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = (1 + p5num((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'])))], p5want) })], p5want) }), function () { return p5context([(function () {
+ var v_pos1 = null;
+ (v_pos1 = ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']));
+ return (p5or(p5or(p5context([(function () {
+ var v_m2 = null;
+ (v_m2 = (p5call(p5pkg["Perlito5::Grammar"], "exp2", p5list_to_a(v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']), 0)));
+ if ( p5bool(v_m2) ) {
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = (v_m2 || (v_m2 = new p5HashRef({})))._hash_['to']);
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp2'] = v_m2);
+ return (p5context([1], p5want));
+ }
+
+ else {
+ return (p5context([0], p5want));
+ }
+ })()], p5want), function () { return p5context([(function () {
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = v_pos1);
+ return (p5context([], p5want));
+ })()], p5want) }), function () { return p5context([(function () {
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = v_pos1);
+ return (p5context([p5context([(function () {
+ var v_m2 = null;
+ (v_m2 = (p5call(v_grammar, "opt_ws", p5list_to_a(v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']), 0)));
+ if ( p5bool(v_m2) ) {
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = (v_m2 || (v_m2 = new p5HashRef({})))._hash_['to']);
+ return (p5context([1], p5want));
+ }
+
+ else {
+ return (p5context([0], p5want));
+ }
+ })()], p5want)], p5want));
+ })()], p5want) }));
+ })()], p5want) }));
+ })()], p5want), function () { return p5context([(function () {
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = v_pos1);
+ return (p5context([1], p5want));
+ })()], p5want) }));
})()], p5want) }), function () { return p5context([p5and((')' == p5pkg["Perlito5::Grammar"].substr([v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'], 1], 0)), function () { return p5context([((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = (1 + p5num((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'])))], p5want) })], p5want) }), function () { return p5context([(function () {
var v_m2 = null;
(v_m2 = (p5call(v_grammar, "opt_ws", p5list_to_a(v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']), 0)));
@@ -6474,10 +6559,10 @@ var p5100 = p5pkg['main'];
}
})()], p5want) }), function () { return p5context([(function () {
var v_m2 = null;
- (v_m2 = (p5call(p5pkg["Perlito5::Grammar"], "exp_stmts", p5list_to_a(v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']), 0)));
+ (v_m2 = (p5call(p5pkg["Perlito5::Grammar"], "exp_stmts2", p5list_to_a(v_str, (v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to']), 0)));
if ( p5bool(v_m2) ) {
((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['to'] = (v_m2 || (v_m2 = new p5HashRef({})))._hash_['to']);
- ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp_stmts'] = v_m2);
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp_stmts2'] = v_m2);
return (p5context([1], p5want));
}
@@ -6519,7 +6604,14 @@ var p5100 = p5pkg['main'];
return (p5context([0], p5want));
}
})()], p5want) }), function () { return p5context([(function () {
- ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['capture'] = p5call(p5pkg["Perlito5::AST::For"], "new", p5list_to_a('cond', p5pkg["Perlito5::Match"].flat(p5list_to_a((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Expression.paren_parse']), 1), 'topic', null, 'body', p5call(p5pkg["Perlito5::AST::Lit::Block"], "new", p5list_to_a('stmts', p5pkg["Perlito5::Match"].flat(p5list_to_a((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp_stmts']), 1), 'sig', null), 1), 'continue', ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['opt_continue_block'] || ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['opt_continue_block'] = new p5HashRef({})))._hash_['capture']), p5want));
+ var v_header = null;
+ if ( p5bool((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['c_style_for']) ) {
+ (v_header = ((new p5ArrayRef(p5list_to_a((((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Expression.exp_parse'] || ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Expression.exp_parse'] = new p5HashRef({})))._hash_['capture'] || (((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Expression.exp_parse'] || ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Expression.exp_parse'] = new p5HashRef({})))._hash_['capture'] = new p5HashRef({})))._hash_['exp'], (((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp'] || ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp'] = new p5HashRef({})))._hash_['capture'] || (((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp'] || ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp'] = new p5HashRef({})))._hash_['capture'] = new p5HashRef({})))._hash_['exp'], (((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp2'] || ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp2'] = new p5HashRef({})))._hash_['capture'] || (((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp2'] || ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp2'] = new p5HashRef({})))._hash_['capture'] = new p5HashRef({})))._hash_['exp'])))));
+ }
+ else {
+ (v_header = ((((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Expression.exp_parse'] || ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Expression.exp_parse'] = new p5HashRef({})))._hash_['capture'] || (((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Expression.exp_parse'] || ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Expression.exp_parse'] = new p5HashRef({})))._hash_['capture'] = new p5HashRef({})))._hash_['exp']));
+ };
+ ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['capture'] = p5call(p5pkg["Perlito5::AST::For"], "new", p5list_to_a('cond', v_header, 'topic', null, 'body', p5call(p5pkg["Perlito5::AST::Lit::Block"], "new", p5list_to_a('stmts', p5pkg["Perlito5::Match"].flat(p5list_to_a((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['Perlito5::Grammar.exp_stmts2']), 1), 'sig', null), 1), 'continue', ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['opt_continue_block'] || ((v_MATCH || (v_MATCH = new p5HashRef({})))._hash_['opt_continue_block'] = new p5HashRef({})))._hash_['capture']), p5want));
return (p5context([1], p5want));
})()], p5want) })], p5want));
})()], p5want) }));
View
86 lib5/Perlito5/Grammar/Control.pm
@@ -438,7 +438,7 @@ sub Perlito5::Grammar::for {
})))
})) || ((do {
($MATCH->{'to'} = $pos1);
- (((((((((((((((do {
+ ((((((((((((((((do {
((my $m2) = $grammar->opt_ws($str, $MATCH->{'to'}));
if ($m2) {
($MATCH->{'to'} = $m2->{'to'});
@@ -448,15 +448,82 @@ sub Perlito5::Grammar::for {
0
}
})) && ((('(' eq substr($str, $MATCH->{'to'}, 1)) && (($MATCH->{'to'} = (1 + $MATCH->{'to'})))))) && ((do {
- ((my $m2) = Perlito5::Expression->paren_parse($str, $MATCH->{'to'}));
+ ((my $m2) = Perlito5::Expression->exp_parse($str, $MATCH->{'to'}));
+ if ($m2) {
+ ($MATCH->{'to'} = $m2->{'to'});
+ ($MATCH->{'Perlito5::Expression.exp_parse'} = $m2);
+ 1
+ }
+ else {
+ 0
+ }
+}))) && ((do {
+ ((my $pos1) = $MATCH->{'to'});
+ (((do {
+ (((((((';' eq substr($str, $MATCH->{'to'}, 1)) && (($MATCH->{'to'} = (1 + $MATCH->{'to'}))))) && ((do {
+ ($MATCH->{'c_style_for'} = 1);
+ 1
+}))) && ((do {
+ ((my $pos1) = $MATCH->{'to'});
+ ((((do {
+ ((my $m2) = Perlito5::Grammar->exp($str, $MATCH->{'to'}));
+ if ($m2) {
+ ($MATCH->{'to'} = $m2->{'to'});
+ ($MATCH->{'Perlito5::Grammar.exp'} = $m2);
+ 1
+ }
+ else {
+ 0
+ }
+})) || ((do {
+ ($MATCH->{'to'} = $pos1);
+ ()
+}))) || ((do {
+ ($MATCH->{'to'} = $pos1);
+ (((do {
+ ((my $m2) = $grammar->opt_ws($str, $MATCH->{'to'}));
+ if ($m2) {
+ ($MATCH->{'to'} = $m2->{'to'});
+ 1
+ }
+ else {
+ 0
+ }
+})))
+})))
+}))) && (((';' eq substr($str, $MATCH->{'to'}, 1)) && (($MATCH->{'to'} = (1 + $MATCH->{'to'})))))) && ((do {
+ ((my $pos1) = $MATCH->{'to'});
+ ((((do {
+ ((my $m2) = Perlito5::Grammar->exp2($str, $MATCH->{'to'}));
+ if ($m2) {
+ ($MATCH->{'to'} = $m2->{'to'});
+ ($MATCH->{'Perlito5::Grammar.exp2'} = $m2);
+ 1
+ }
+ else {
+ 0
+ }
+})) || ((do {
+ ($MATCH->{'to'} = $pos1);
+ ()
+}))) || ((do {
+ ($MATCH->{'to'} = $pos1);
+ (((do {
+ ((my $m2) = $grammar->opt_ws($str, $MATCH->{'to'}));
if ($m2) {
($MATCH->{'to'} = $m2->{'to'});
- ($MATCH->{'Perlito5::Expression.paren_parse'} = $m2);
1
}
else {
0
}
+})))
+})))
+})))
+})) || ((do {
+ ($MATCH->{'to'} = $pos1);
+ (1)
+})))
}))) && (((')' eq substr($str, $MATCH->{'to'}, 1)) && (($MATCH->{'to'} = (1 + $MATCH->{'to'})))))) && ((do {
((my $m2) = $grammar->opt_ws($str, $MATCH->{'to'}));
if ($m2) {
@@ -476,10 +543,10 @@ sub Perlito5::Grammar::for {
0
}
}))) && ((do {
- ((my $m2) = Perlito5::Grammar->exp_stmts($str, $MATCH->{'to'}));
+ ((my $m2) = Perlito5::Grammar->exp_stmts2($str, $MATCH->{'to'}));
if ($m2) {
($MATCH->{'to'} = $m2->{'to'});
- ($MATCH->{'Perlito5::Grammar.exp_stmts'} = $m2);
+ ($MATCH->{'Perlito5::Grammar.exp_stmts2'} = $m2);
1
}
else {
@@ -514,7 +581,14 @@ sub Perlito5::Grammar::for {
0
}
}))) && ((do {
- ($MATCH->{'capture'} = Perlito5::AST::For->new('cond', Perlito5::Match::flat($MATCH->{'Perlito5::Expression.paren_parse'}), 'topic', undef(), 'body', Perlito5::AST::Lit::Block->new('stmts', Perlito5::Match::flat($MATCH->{'Perlito5::Grammar.exp_stmts'}), 'sig', undef()), 'continue', $MATCH->{'opt_continue_block'}->{'capture'}));
+ (my $header);
+ if ($MATCH->{'c_style_for'}) {
+ ($header = [$MATCH->{'Perlito5::Expression.exp_parse'}->{'capture'}->{'exp'}, $MATCH->{'Perlito5::Grammar.exp'}->{'capture'}->{'exp'}, $MATCH->{'Perlito5::Grammar.exp2'}->{'capture'}->{'exp'}])
+ }
+ else {
+ ($header = $MATCH->{'Perlito5::Expression.exp_parse'}->{'capture'}->{'exp'})
+ };
+ ($MATCH->{'capture'} = Perlito5::AST::For->new('cond', $header, 'topic', undef(), 'body', Perlito5::AST::Lit::Block->new('stmts', Perlito5::Match::flat($MATCH->{'Perlito5::Grammar.exp_stmts2'}), 'sig', undef()), 'continue', $MATCH->{'opt_continue_block'}->{'capture'}));
1
}))))
})))
View
4 lib5/Perlito5/Javascript/Emitter.pm
@@ -1129,6 +1129,10 @@ do { for ($_) {
sub Perlito5::AST::For::emit_javascript {
((my $self) = shift());
((my $level) = shift());
+ if ((ref($self->{'cond'}) eq 'ARRAY')) {
+ ((my $body) = Perlito5::Javascript::LexicalBlock->new('block', $self->{'body'}->stmts(), 'needs_return', 0, 'create_context', 1));
+ return (('for ( ' . (($self->{'cond'}->[0] ? ($self->{'cond'}->[0]->emit_javascript(($level + 1)) . '; ') : '; ')) . (($self->{'cond'}->[1] ? ($self->{'cond'}->[1]->emit_javascript(($level + 1)) . '; ') : '; ')) . (($self->{'cond'}->[2] ? ($self->{'cond'}->[2]->emit_javascript(($level + 1)) . ' ') : ' ')) . ') {' . chr(10) . $body->emit_javascript(($level + 1)) . chr(10) . Perlito5::Javascript::tab($level) . '}'))
+ };
((my $cond) = Perlito5::Javascript::to_list([$self->{'cond'}], ($level + 1)));
if ($self->{'body'}->sig()) {
((my $v) = $self->{'body'}->sig());
View
10 lib5/Perlito5/Perl5/Emitter.pm
@@ -342,12 +342,18 @@ do { for ($_) {
sub Perlito5::AST::For::emit_perl5 {
((my $self) = $_[0]);
((my $level) = $_[1]);
- ((my $cond) = $self->{'cond'});
+ (my $cond);
+ if ((ref($self->{'cond'}) eq 'ARRAY')) {
+ ($cond = ((($self->{'cond'}->[0] ? ($self->{'cond'}->[0]->emit_javascript(($level + 1)) . '; ') : '; ')) . (($self->{'cond'}->[1] ? ($self->{'cond'}->[1]->emit_javascript(($level + 1)) . '; ') : '; ')) . (($self->{'cond'}->[2] ? ($self->{'cond'}->[2]->emit_javascript(($level + 1)) . ' ') : ' '))))
+ }
+ else {
+ ($cond = $self->{'cond'}->emit_perl5())
+ };
(my $sig);
if ($self->{'body'}->sig()) {
($sig = ('my ' . $self->{'body'}->sig()->emit_perl5() . ' '))
};
- return ((Perlito5::Perl5::tab($level) . 'for ' . $sig . '(' . $cond->emit_perl5() . ') {' . chr(10) . join(';' . chr(10), map($_->emit_perl5(($level + 1)), @{$self->{'body'}->stmts()})) . chr(10) . Perlito5::Perl5::tab($level) . '}'))
+ return ((Perlito5::Perl5::tab($level) . 'for ' . $sig . '(' . $cond . ') {' . chr(10) . join(';' . chr(10), map($_->emit_perl5(($level + 1)), @{$self->{'body'}->stmts()})) . chr(10) . Perlito5::Perl5::tab($level) . '}'))
}
}};
package Perlito5::AST::Decl;
View
31 src5/lib/Perlito5/Grammar/Control.pm
@@ -121,17 +121,40 @@ token for {
}
|
<.opt_ws>
- '(' <Perlito5::Expression.paren_parse> ')' <.opt_ws>
+
+ # (@x) (my $i = 0; $i < 10; $i++)
+
+ '('
+ <Perlito5::Expression.exp_parse>
+ [ ';'
+ { $MATCH->{c_style_for} = 1 }
+ [ <Perlito5::Grammar.exp> || <.opt_ws> ]
+ ';' [ <Perlito5::Grammar.exp2> || <.opt_ws> ]
+ | ''
+ ]
+ ')' <.opt_ws>
'{' <.opt_ws>
- <Perlito5::Grammar.exp_stmts>
+ <Perlito5::Grammar.exp_stmts2>
<.opt_ws>
'}' <.opt_ws>
<opt_continue_block>
{
+ my $header;
+ if ($MATCH->{c_style_for}) {
+ $header = [
+ $MATCH->{"Perlito5::Expression.exp_parse"}{capture}{exp},
+ $MATCH->{"Perlito5::Grammar.exp"}{capture}{exp},
+ $MATCH->{"Perlito5::Grammar.exp2"}{capture}{exp},
+ ];
+ }
+ else {
+ $header = $MATCH->{"Perlito5::Expression.exp_parse"}{capture}{exp};
+ }
+
$MATCH->{capture} = Perlito5::AST::For->new(
- cond => Perlito5::Match::flat($MATCH->{"Perlito5::Expression.paren_parse"}),
+ cond => $header,
topic => undef,
- body => Perlito5::AST::Lit::Block->new( stmts => Perlito5::Match::flat($MATCH->{"Perlito5::Grammar.exp_stmts"}), sig => undef ),
+ body => Perlito5::AST::Lit::Block->new( stmts => Perlito5::Match::flat($MATCH->{"Perlito5::Grammar.exp_stmts2"}), sig => undef ),
continue => $MATCH->{opt_continue_block}{capture}
)
}
View
13 src5/lib/Perlito5/Javascript/Emitter.pm
@@ -1766,6 +1766,19 @@ package Perlito5::AST::For;
my $self = shift;
my $level = shift;
+ if (ref($self->{cond}) eq 'ARRAY') {
+ # C-style for
+ my $body = Perlito5::Javascript::LexicalBlock->new( block => $self->{body}->stmts, needs_return => 0, create_context => 1 );
+ return
+ 'for ( '
+ . ( $self->{cond}[0] ? $self->{cond}[0]->emit_javascript($level + 1) . '; ' : '; ' )
+ . ( $self->{cond}[1] ? $self->{cond}[1]->emit_javascript($level + 1) . '; ' : '; ' )
+ . ( $self->{cond}[2] ? $self->{cond}[2]->emit_javascript($level + 1) . ' ' : ' ' )
+ . ') {' . "\n"
+ . $body->emit_javascript( $level + 1 ) . "\n"
+ . Perlito5::Javascript::tab($level) . '}'
+ }
+
my $cond = Perlito5::Javascript::to_list([$self->{cond}], $level + 1);
if ($self->{body}->sig()) {
# XXX - cleanup: "for" parser throws away the variable declaration, so we need to create it again
View
17 src5/lib/Perlito5/Perl5/Emitter.pm
@@ -513,7 +513,7 @@ package Perlito5::AST::While;
my $cond = $self->{cond};
Perlito5::Perl5::tab($level) . 'for ( '
. ( $self->{init} ? $self->{init}->emit_perl5() . '; ' : '; ' )
- . ( $cond ? $cond->emit_perl5() . '; ' : '; ' )
+ . ( $cond ? $cond->emit_perl5() . '; ' : '; ' )
. ( $self->{continue} ? $self->{continue}->emit_perl5() . ' ' : ' ' )
. ') {' . "\n"
. join(";\n",
@@ -529,12 +529,23 @@ package Perlito5::AST::For;
my $self = $_[0];
my $level = $_[1];
- my $cond = $self->{cond};
+ my $cond;
+ if (ref($self->{cond}) eq 'ARRAY') {
+ # C-style for
+ $cond =
+ ( $self->{cond}[0] ? $self->{cond}[0]->emit_javascript($level + 1) . '; ' : '; ' )
+ . ( $self->{cond}[1] ? $self->{cond}[1]->emit_javascript($level + 1) . '; ' : '; ' )
+ . ( $self->{cond}[2] ? $self->{cond}[2]->emit_javascript($level + 1) . ' ' : ' ' )
+ }
+ else {
+ $cond = $self->{cond}->emit_perl5()
+ }
+
my $sig;
if ($self->{body}->sig()) {
$sig = 'my ' . $self->{body}->sig->emit_perl5() . ' ';
}
- return Perlito5::Perl5::tab($level) . 'for ' . $sig . '(' . $cond->emit_perl5() . ') {' . "\n"
+ return Perlito5::Perl5::tab($level) . 'for ' . $sig . '(' . $cond . ') {' . "\n"
. join(";\n", map( $_->emit_perl5( $level + 1 ), @{ $self->{body}->stmts } )) . "\n"
. Perlito5::Perl5::tab($level) . "}"
}
Please sign in to comment.
Something went wrong with that request. Please try again.