Skip to content

Commit

Permalink
Added support for multiple inline if statements. Updated Or and And t…
Browse files Browse the repository at this point in the history
…o use LogicalExpression
  • Loading branch information
jsm174 authored and freezy committed Oct 26, 2019
1 parent 9311bae commit 7812ce9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
11 changes: 11 additions & 0 deletions lib/scripting/estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
Identifier,
IfStatement,
Literal,
LogicalExpression,
LogicalOperator,
MemberExpression,
NewExpression,
Pattern,
Expand Down Expand Up @@ -179,6 +181,15 @@ export function functionExpression(body: BlockStatement, params: Pattern[]): Fun
};
}

export function logicalExpression(operator: LogicalOperator, left: Expression, right: Expression): LogicalExpression {
return {
type: 'LogicalExpression',
operator,
left,
right,
};
}

export function memberExpression(object: Expression | Super, property: Expression): MemberExpression {
return {
type: 'MemberExpression',
Expand Down
8 changes: 4 additions & 4 deletions lib/scripting/post-process/expr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ describe('The VBScript transpiler - Expressions', () => {
});

it('should transpile a "Or" expression', () => {
const vbs = `EnableBallControl = 10 Or 8\n`;
const vbs = `If test = 5 Or Err Then test = 6\n`;
const js = vbsToJs(vbs);
expect(js).to.equal('EnableBallControl = 10 | 8;');
expect(js).to.equal('if (test == 5 || Err)\n test = 6;');
});

it('should transpile a "And" expression', () => {
const vbs = `EnableBallControl = 10 And 8\n`;
const vbs = `If test = 5 And Err Then test = 6`;
const js = vbsToJs(vbs);
expect(js).to.equal('EnableBallControl = 10 & 8;');
expect(js).to.equal('if (test == 5 && Err)\n test = 6;');
});

it('should transpile a "Not" expression', () => {
Expand Down
4 changes: 2 additions & 2 deletions lib/scripting/post-process/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ export function xor(result: [Expression, null, Token, null, Expression]): Expres
export function or(result: [Expression, null, Token, null, Expression]): Expression {
const leftExpr = result[0];
const rightExpr = result[4];
return estree.binaryExpression('|', leftExpr, rightExpr);
return estree.logicalExpression('||', leftExpr, rightExpr);
}

export function and(result: [Expression, null, Token, null, Expression]): Expression {
const leftExpr = result[0];
const rightExpr = result[4];
return estree.binaryExpression('&', leftExpr, rightExpr);
return estree.logicalExpression('&&', leftExpr, rightExpr);
}

export function not(result: [Token, null, Expression]): Expression {
Expand Down
8 changes: 8 additions & 0 deletions lib/scripting/post-process/if.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ describe('The VBScript transpiler - If', () => {
const js = vbsToJs(vbs);
expect(js).to.equal('if (x == 1) {\n x = 2;\n} else if (x == 3)\n x = 4;\nelse\n x = 5;');
});

it('should transpile multiple inline "If/Then" statement', () => {
const vbs = `If VPMver > "" Then If Controller.Version < VPMver Or Err Then MsgBox "VPinMAME ver " & VPMver & " required."\n`;
const js = vbsToJs(vbs);
expect(js).to.equal(
"if (VPMver > '')\n if (Controller.Version < VPMver || Err)\n MsgBox('VPinMAME ver ' + VPMver + ' required.');",
);
});
});
9 changes: 4 additions & 5 deletions lib/scripting/post-process/if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { BlockStatement, Comment, Expression, IfStatement, Statement } from 'est
import { Token } from 'moo';
import * as estree from '../estree';

export function stmt1(
export function stmt(
result: [Token, null, Expression, null, Token, Comment[], BlockStatement, Statement, Token, null, Token, Comment[]],
): IfStatement {
const test = result[2];
Expand All @@ -32,14 +32,13 @@ export function stmt1(
return estree.ifStatement(test, consequent, alternate, leadingComments, trailingComments);
}

export function stmt2(
result: [Token, null, Expression, null, Token, null, Statement, null, Statement, null, Token, Comment[]],
export function stmtInline(
result: [Token, null, Expression, null, Token, null, Statement, null, Statement, null, Token],
): IfStatement {
const test = result[2];
const consequent = result[6];
const alternate = result[8];
const comments = result[11] || [];
return estree.ifStatement(test, consequent, alternate, [], comments);
return estree.ifStatement(test, consequent, alternate, [], []);
}

export function elseStmt1(
Expand Down
6 changes: 4 additions & 2 deletions lib/scripting/vbscript.ne
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ InlineStmt -> AssignStmt
| SubCallStmt {% id %}
| ErrorStmt {% id %}
| ExitStmt {% id %}
| IfStmtInline {% id %}

GlobalStmtList -> GlobalStmt GlobalStmtList {% ppHelpers.globalStmtList %}
| null
Expand Down Expand Up @@ -322,8 +323,9 @@ RedimDecl -> ExtendedID _ %paren_left _ ExprList _ %paren_right

#========= If Statement

IfStmt -> %kw_if _ Expr _ %kw_then NL BlockStmtList ElseStmtList %kw_end __ %kw_if NL {% ppIf.stmt1 %}
| %kw_if _ Expr _ %kw_then __ InlineStmt _ ElseOpt _ EndIfOpt NL {% ppIf.stmt2 %}
IfStmt -> %kw_if _ Expr _ %kw_then NL BlockStmtList ElseStmtList %kw_end __ %kw_if NL {% ppIf.stmt %}

IfStmtInline -> %kw_if _ Expr _ %kw_then __ InlineStmt _ ElseOpt _ EndIfOpt {% ppIf.stmtInline %}

ElseStmtList -> %kw_elseif _ Expr _ %kw_then NL BlockStmtList ElseStmtList {% ppIf.elseStmt1 %}
| %kw_elseif _ Expr _ %kw_then __ InlineStmt NL ElseStmtList {% ppIf.elseStmt2 %}
Expand Down

0 comments on commit 7812ce9

Please sign in to comment.