Skip to content

Commit

Permalink
Add initial support for const declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
jsm174 committed Aug 19, 2019
1 parent a11200c commit b51b03a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
26 changes: 25 additions & 1 deletion lib/scripting/estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

import { Program, VariableDeclaration, VariableDeclarator } from 'estree';
import { Literal, Program, VariableDeclaration, VariableDeclarator } from 'estree';

/**
* Returns the root node.
Expand Down Expand Up @@ -49,3 +49,27 @@ export function variableDeclarator(name: string): VariableDeclarator {
id: { type: 'Identifier', name },
};
}

/**
* Returns a constant declaration.
*/
export function constDecl(data: any): VariableDeclaration {
const name: string = data[2][0];

return {
type: 'VariableDeclaration',
kind: 'const',
declarations: [ {
type: 'VariableDeclarator',
id: { type: 'Identifier', name },
init: literal(data[2][4]),
}],
};
}

export function literal(data: any): Literal {
return {
type: 'Literal',
value: data,
};
}
6 changes: 6 additions & 0 deletions lib/scripting/transpile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ describe('The VBScript transpiler', () => {
const js = vbsToJs(vbs);
expect(js).to.equal('let test1, test2, test3;\n');
});

it('should transpile a const declaration', () => {
const vbs = `Const pi = 3.14\n`;
const js = vbsToJs(vbs);
expect(js).to.equal('const pi = 3.14;\n');
});
});
28 changes: 26 additions & 2 deletions lib/scripting/vbscript.ne
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@builtin "whitespace.ne"
@builtin "number.ne"

@{%
const estree = require('./estree');
Expand All @@ -7,9 +8,9 @@ const estree = require('./estree');
Program -> NLOpt GlobalStmt:* {% data => estree.program(data[1]) %}

GlobalStmt -> OptionExplicit
| ConstDecl {% data => data[0] %}
| BlockStmt {% data => data[0] %}


OptionExplicit -> "Option" __ "Explicit" NL

BlockStmt -> VarDecl {% data => data[0] %}
Expand All @@ -33,9 +34,30 @@ SafeKeywordID -> "Default"
ID -> Letter IDTail {% data => data[0] + data[1] %}
| "[" IDNameChar:* "]"

ArrayRankList -> IntLiteral "," ArrayRankList
ArrayRankList -> IntLiteral _ "," _ ArrayRankList
| IntLiteral

ConstDecl -> AccessModifierOpt __ "Const" __ ConstList NL
| "Const" __ ConstList NL {% estree.constDecl %}

ConstList -> ExtendedID _ "=" _ ConstExprDef _ "," _ ConstList
| ExtendedID _ "=" _ ConstExprDef

ConstExprDef -> "(" _ ConstExprDef _ ")"
| "-" _ ConstExprDef
| "+" _ ConstExprDef
| ConstExpr {% data => data[0] %}

AccessModifierOpt -> "Public"
| "Private"

ConstExpr -> FloatLiteral {% data => data[0] %}
| Nothing {% data => data[0] %}

Nothing -> "Nothing"
| "Null"
| "Empty"

NLOpt -> NL:*

NL -> NewLine NL
Expand All @@ -50,6 +72,8 @@ IntLiteral -> DecDigit:+
| HexLiteral
| OctLiteral

FloatLiteral -> decimal {% data => data[0] %} # DecDigit:* "." DecDigit:+ ( "E" [+-]:? DecDigit:+ ):?

HexLiteral -> "&H" HexDigit:+ "&":?
OctLiteral -> "&" OctDigit:+ "&":?

Expand Down

0 comments on commit b51b03a

Please sign in to comment.