Reviewers: arv-chromium, slightlylate,
Message:
A lot of State functions return arrays of statements, so I added this to
make integration with PlaceholderParser easier. The other functionality
was easy to implement, so I added it in.
#--cut--
dl_check() {
local f=$(basename $1)
test -e $f || curl -sO $1
openssl sha1 < $f | grep -q "= $2" && return 0
echo sha1 mismatch!
return 1
}
git checkout 60408908bf3c682b # last merge with branch master
git checkout -b issue7649045-placeholder-parser-statement-arrays
dl_check
https://codereview.appspot.com/download/issue7649045_1.diff \
a1de2b3be3fc2dde && git apply issue7649045_1.diff && make test
node demo/repl.js <<"END"
var ps = traceur.codegeneration.parseStatement;
var pe = traceur.codegeneration.parseExpression;
var w = traceur.outputgeneration.TreeWriter.write;
console.log(w(ps`var a = ${[pe`3+4`, pe`5+6`]}`));
console.log(w(ps`if (i < n) ${[ps`i++;`, ps`break;`]}`));
console.log(w(ps`var v = ${
'hello world'.split('').map((c) => c.charCodeAt(0))
}`));
END
#--cut--
Description:
PlaceholderParser: handle arrays of statements, expressions, etc.
- Arrays of statements converted to a block statement.
- Arrays of expressions converted to a comma expression.
- Arrays converted to an array literal.
BUG=None
TEST=None
Please review this at
https://codereview.appspot.com/7649045/
Affected files:
M src/codegeneration/PlaceholderParser.js
Index: src/codegeneration/PlaceholderParser.js
diff --git a/src/codegeneration/PlaceholderParser.js
b/src/codegeneration/PlaceholderParser.js
index
39e959dc341c3ea6af5e90f399f1058884574e13..f1b3f5f736f0bbb7c79d5374279e47023c926c6a
100644
--- a/src/codegeneration/PlaceholderParser.js
+++ b/src/codegeneration/PlaceholderParser.js
@@ -31,8 +31,11 @@ import {
import {SourceFile} from '../syntax/SourceFile.js';
import {IDENTIFIER} from '../syntax/TokenType.js';
import {
+ createArrayLiteralExpression,
createBindingIdentifier,
+ createBlock,
createBooleanLiteral,
+ createCommaExpression,
createExpressionStatement,
createGetAccessor,
createIdentifierExpression,
@@ -40,6 +43,7 @@ import {
createMemberExpression,
createNullLiteral,
createNumberLiteral,
+ createParenExpression,
createSetAccessor,
createStringLiteral,
createVoid0
@@ -180,6 +184,17 @@ function convertValueToExpression(value) {
return value;
if (value instanceof IdentifierToken)
return createIdentifierExpression(value);
+ if (Array.isArray(value)) {
+ if (value[0] instanceof ParseTree) {
+ if (value.length === 1)
+ return value[0];
+ if (value[0].isStatement())
+ return createBlock(value);
+ else
+ return createParenExpression(createCommaExpression(value));
+ }
+ return
createArrayLiteralExpression(value.map(convertValueToExpression));
+ }
if (value === null)
return createNullLiteral();
if (value === undefined)