Skip to content

Commit

Permalink
Merge pull request #12 from NightlyCommit/issue_10
Browse files Browse the repository at this point in the history
Fix issue #10
  • Loading branch information
ericmorand committed May 12, 2020
2 parents 9376180 + 7fccda3 commit ba7a951
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 15 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "npm run build:cjs",
"build:cjs": "tsc --project . --module commonjs --outDir dist --declaration true",
"build:doc": "typedoc --mode file --out docs --name twig-to-phtml --excludePrivate --excludeNotExported src",
"test": "ts-node node_modules/tape/bin/tape 'test/**/test.ts' | tap-bail | tap-spec",
"test": "ts-node node_modules/tape/bin/tape 'test/**/test.ts' | tap-spec",
"cover": "rimraf .nyc_output coverage && nyc npm t",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
Expand All @@ -37,7 +37,6 @@
"nyc": "^14.1.1",
"rimraf": "^2.6.3",
"tap": "^14.2.2",
"tap-bail": "^1.0.0",
"tap-spec": "^5.0.0",
"tape": "^4.10.2",
"ts-node": "^8.2.0",
Expand Down
144 changes: 136 additions & 8 deletions src/lib/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,37 @@ import {
TwingNodeExpression,
TwingNodeExpressionArray,
TwingNodeExpressionAssignName,
TwingNodeExpressionBinary, TwingNodeExpressionConditional,
TwingNodeExpressionConstant, TwingNodeExpressionFunction,
TwingNodeExpressionBinary,
TwingNodeExpressionBinaryAdd,
TwingNodeExpressionBinaryAnd,
TwingNodeExpressionBinaryBitwiseAnd,
TwingNodeExpressionBinaryBitwiseOr,
TwingNodeExpressionBinaryBitwiseXor,
TwingNodeExpressionBinaryConcat,
TwingNodeExpressionBinaryDiv,
TwingNodeExpressionBinaryEqual,
TwingNodeExpressionBinaryFloorDiv,
TwingNodeExpressionBinaryGreater,
TwingNodeExpressionBinaryGreaterEqual,
TwingNodeExpressionBinaryLess,
TwingNodeExpressionBinaryLessEqual,
TwingNodeExpressionBinaryMod,
TwingNodeExpressionBinaryMul,
TwingNodeExpressionBinaryNotEqual,
TwingNodeExpressionBinaryOr, TwingNodeExpressionBinaryPower, TwingNodeExpressionBinarySub,
TwingNodeExpressionConditional,
TwingNodeExpressionConstant,
TwingNodeExpressionFunction,
TwingNodeExpressionGetAttr,
TwingNodeExpressionName,
TwingNodeExpressionUnary,
TwingNodeExpressionUnaryNeg,
TwingNodeExpressionUnaryNot,
TwingNodeExpressionUnaryPos,
TwingNodeFor,
TwingNodeIf,
TwingNodePrint, TwingNodeSet,
TwingNodePrint,
TwingNodeSet,
TwingNodeText,
TwingNodeType,
TwingSource,
Expand Down Expand Up @@ -52,7 +76,107 @@ export class Transpiler {
}

private transpileExpressionBinaryNode(node: TwingNodeExpressionBinary): string {
return this.transpileNode(node.getNode('left')) + '==' + this.transpileNode(node.getNode('right'));
let prefix: string = '';
let suffix: string = '';
let operator: string;

if (node instanceof TwingNodeExpressionBinaryAdd) {
operator = '+';
}

if (node instanceof TwingNodeExpressionBinaryAnd) {
operator = '&&';
}

if (node instanceof TwingNodeExpressionBinaryBitwiseAnd) {
operator = '&';
}

if (node instanceof TwingNodeExpressionBinaryBitwiseOr) {
operator = '|';
}

if (node instanceof TwingNodeExpressionBinaryBitwiseXor) {
operator = '^';
}

if (node instanceof TwingNodeExpressionBinaryConcat) {
operator = '.';
}

if (node instanceof TwingNodeExpressionBinaryDiv) {
operator = '/';
}

if (node instanceof TwingNodeExpressionBinaryEqual) {
operator = '==';
}

if (node instanceof TwingNodeExpressionBinaryFloorDiv) {
prefix = 'floor(';
operator = '/';
suffix = ')'
}

if (node instanceof TwingNodeExpressionBinaryGreater) {
operator = '>';
}

if (node instanceof TwingNodeExpressionBinaryGreaterEqual) {
operator = '>=';
}

if (node instanceof TwingNodeExpressionBinaryLess) {
operator = '<';
}

if (node instanceof TwingNodeExpressionBinaryLessEqual) {
operator = '<=';
}

if (node instanceof TwingNodeExpressionBinaryMod) {
operator = '%';
}

if (node instanceof TwingNodeExpressionBinaryMul) {
operator = '*';
}

if (node instanceof TwingNodeExpressionBinaryNotEqual) {
operator = '!=';
}

if (node instanceof TwingNodeExpressionBinaryOr) {
operator = '||';
}

if (node instanceof TwingNodeExpressionBinaryPower) {
operator = '**';
}

if (node instanceof TwingNodeExpressionBinarySub) {
operator = '-';
}

return `${prefix}(${this.transpileNode(node.getNode('left'))})${operator}(${this.transpileNode(node.getNode('right'))})${suffix}`;
}

private transpileExpressionUnaryNode(node: TwingNodeExpressionUnary): string {
let operator: string;

if (node instanceof TwingNodeExpressionUnaryNeg) {
operator = '-';
}

if (node instanceof TwingNodeExpressionUnaryNot) {
operator = '!';
}

if (node instanceof TwingNodeExpressionUnaryPos) {
operator = '+';
}

return `${operator}(${this.transpileNode(node.getNode('node'))})`;
}

private transpileExpressionNameNode(node: TwingNodeExpressionName): string {
Expand Down Expand Up @@ -169,7 +293,7 @@ export class Transpiler {
}

if (node.hasNode('else')) {
results.push('<php else: ?>');
results.push('<?php else: ?>');
results.push(this.transpileNode(node.getNode('else'), true));
}

Expand Down Expand Up @@ -254,8 +378,12 @@ export class Transpiler {
return this.transpileExpressionGetAttrNode(node as TwingNodeExpressionGetAttr);
}

if (node.getType() === TwingNodeType.EXPRESSION_BINARY) {
return this.transpileExpressionBinaryNode(node as TwingNodeExpressionBinary);
if (node instanceof TwingNodeExpressionBinary) {
return this.transpileExpressionBinaryNode(node);
}

if (node instanceof TwingNodeExpressionUnary) {
return this.transpileExpressionUnaryNode(node);
}

if (node.getType() === TwingNodeType.COMMENT) {
Expand All @@ -276,7 +404,7 @@ export class Transpiler {

let results = [];

for (let [name, child] of node.getNodes()) {
for (let child of node.getNodes().values()) {
results.push(this.transpileNode(child));
}

Expand Down
37 changes: 32 additions & 5 deletions test/lib/Transpiler/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ bar*/ ?>`);
test.test('supports if', (test) => {
test.same(transpiler.transpile(`{% if (foo == 5) %}
TRUE
{% endif %}`), `<?php if ($foo==5): ?> TRUE
{% endif %}`), `<?php if (($foo)==(5)): ?> TRUE
<?php endif; ?>`, 'if only');

test.same(transpiler.transpile(`{% if (foo == 5) %}
TRUE
{% else %}
FALSE
{% endif %}`), `<?php if ($foo==5): ?> TRUE
<php else: ?> FALSE
{% endif %}`), `<?php if (($foo)==(5)): ?> TRUE
<?php else: ?> FALSE
<?php endif; ?>`, 'if and else');

test.same(transpiler.transpile(`{% if (foo == 5) %}
TRUE
{% elseif (foo == 4) %}
FALSE
{% endif %}`), `<?php if ($foo==5): ?> TRUE
<?php elseif ($foo==4): ?> FALSE
{% endif %}`), `<?php if (($foo)==(5)): ?> TRUE
<?php elseif (($foo)==(4)): ?> FALSE
<?php endif; ?>`, 'if and elseif');

test.end();
Expand Down Expand Up @@ -101,6 +101,33 @@ bar*/ ?>`);
test.end();
});

test.test('supports unary and binary expressions', (test) => {
test.same(transpiler.transpile('{{ 1 + 2 }}'), '<?=(1)+(2)?>');
test.same(transpiler.transpile('{{ 1 and 2 }}'), '<?=(1)&&(2)?>');
test.same(transpiler.transpile('{{ 1 b-and 2 }}'), '<?=(1)&(2)?>');
test.same(transpiler.transpile('{{ 1 b-or 2 }}'), '<?=(1)|(2)?>');
test.same(transpiler.transpile('{{ 1 b-xor 2 }}'), '<?=(1)^(2)?>');
test.same(transpiler.transpile('{{ 1 ~ 2 }}'), '<?=(1).(2)?>');
test.same(transpiler.transpile('{{ 1 / 2 }}'), '<?=(1)/(2)?>');
test.same(transpiler.transpile('{{ 1 == 2 }}'), '<?=(1)==(2)?>');
test.same(transpiler.transpile('{{ 1 // 2 }}'), '<?=floor((1)/(2))?>');
test.same(transpiler.transpile('{{ 1 > 2 }}'), '<?=(1)>(2)?>');
test.same(transpiler.transpile('{{ 1 >= 2 }}'), '<?=(1)>=(2)?>');
test.same(transpiler.transpile('{{ 1 < 2 }}'), '<?=(1)<(2)?>');
test.same(transpiler.transpile('{{ 1 <= 2 }}'), '<?=(1)<=(2)?>');
test.same(transpiler.transpile('{{ 1 % 2 }}'), '<?=(1)%(2)?>');
test.same(transpiler.transpile('{{ 1 * 2 }}'), '<?=(1)*(2)?>');
test.same(transpiler.transpile('{{ 1 != 2 }}'), '<?=(1)!=(2)?>');
test.same(transpiler.transpile('{{ 1 or 2 }}'), '<?=(1)||(2)?>');
test.same(transpiler.transpile('{{ 1 ** 2 }}'), '<?=(1)**(2)?>');
test.same(transpiler.transpile('{{ 1 - 2 }}'), '<?=(1)-(2)?>');
test.same(transpiler.transpile('{{ -1 }}'), '<?=-(1)?>');
test.same(transpiler.transpile('{{ not 1 }}'), '<?=!(1)?>');
test.same(transpiler.transpile('{{ +1 }}'), '<?=+(1)?>');

test.end();
});

test.end();
});

Expand Down

0 comments on commit ba7a951

Please sign in to comment.