Skip to content

Commit 241d641

Browse files
Verlon SmithVerlon Smith
authored andcommitted
Dictionaries of arrays complete
1 parent fcd77f8 commit 241d641

16 files changed

+1050
-1253
lines changed

tests/lexerTests.js

Lines changed: 399 additions & 855 deletions
Large diffs are not rendered by default.

tests/parserTestsFirstMilestone.js

Lines changed: 237 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,7 @@ describe('Parser', function() {
7373
"type": "Identifier",
7474
"name": "a"
7575
},
76-
"init": {
77-
"type": "Literal",
78-
"value": 1,
79-
"raw": "1"
80-
}
76+
"init": { "type": "Literal", "value": 1, "raw": "1" }
8177
}
8278
],
8379
"kind": "var"
@@ -87,15 +83,8 @@ describe('Parser', function() {
8783
"expression": {
8884
"type": "AssignmentExpression",
8985
"operator": "=",
90-
"left": {
91-
"type": "Identifier",
92-
"name": "a"
93-
},
94-
"right": {
95-
"type": "Literal",
96-
"value": 2,
97-
"raw": "2"
98-
}
86+
"left": {"type": "Identifier", "name": "a"},
87+
"right": {"type": "Literal", "value": 2, "raw": "2"}
9988
}
10089
}
10190
],
@@ -2769,11 +2758,239 @@ describe('Parser', function() {
27692758
expect(R.equals(parser(input), output)).to.equal(true);
27702759
});
27712760

2772-
2761+
// Test xx - Swift input: 'let firstNum = 1; let secNum = 2; var dict = [firstNum: [[1,2], [3,4]], secNum: [["one", "two"], ["three", "four"]]];'
2762+
// AST Explorer input:
2763+
/*
2764+
var firstNum = 1;
2765+
var secNum = 2;
2766+
var dict = {};
2767+
dict[firstNum] = [[1,2], [3,4]];
2768+
dict[secNum] = [["one", "two"], ["three", "four"]];
2769+
*/
2770+
it('should handle basic dynamic key assignment in dictionary creation', function() {
2771+
input = [
2772+
{ type: "DECLARATION_KEYWORD", value: "var" },
2773+
{ type: "IDENTIFIER", value: "firstNum" },
2774+
{ type: "OPERATOR", value: "=" },
2775+
{ type: "NUMBER", value: "1" },
2776+
{ type: "PUNCTUATION", value: ";" },
2777+
{ type: "DECLARATION_KEYWORD", value: "var" },
2778+
{ type: "IDENTIFIER", value: "secNum" },
2779+
{ type: "OPERATOR", value: "=" },
2780+
{ type: "NUMBER", value: "2" },
2781+
{ type: "PUNCTUATION", value: ";" },
2782+
{ type: "DECLARATION_KEYWORD", value: "var" },
2783+
{ type: "IDENTIFIER", value: "dict" },
2784+
{ type: "OPERATOR", value: "=" },
2785+
{ type: "DICTIONARY_START", value: "[" },
2786+
{ type: "IDENTIFIER", value: "firstNum" },
2787+
{ type: "PUNCTUATION", value: ":" },
2788+
{ type: "ARRAY_START", value: "[" },
2789+
{ type: "ARRAY_START", value: "[" },
2790+
{ type: "NUMBER", value: "1" },
2791+
{ type: "PUNCTUATION", value: "," },
2792+
{ type: "NUMBER", value: "2" },
2793+
{ type: "ARRAY_END", value: "]" },
2794+
{ type: "PUNCTUATION", value: "," },
2795+
{ type: "ARRAY_START", value: "[" },
2796+
{ type: "NUMBER", value: "3" },
2797+
{ type: "PUNCTUATION", value: "," },
2798+
{ type: "NUMBER", value: "4" },
2799+
{ type: "ARRAY_END", value: "]" },
2800+
{ type: "ARRAY_END", value: "]" },
2801+
{ type: "PUNCTUATION", value: "," },
2802+
{ type: "IDENTIFIER", value: "secNum" },
2803+
{ type: "PUNCTUATION", value: ":" },
2804+
{ type: "ARRAY_START", value: "[" },
2805+
{ type: "ARRAY_START", value: "[" },
2806+
{ type: "STRING", value: "one" },
2807+
{ type: "PUNCTUATION", value: "," },
2808+
{ type: "STRING", value: "two" },
2809+
{ type: "ARRAY_END", value: "]" },
2810+
{ type: "PUNCTUATION", value: "," },
2811+
{ type: "ARRAY_START", value: "[" },
2812+
{ type: "STRING", value: "three" },
2813+
{ type: "PUNCTUATION", value: "," },
2814+
{ type: "STRING", value: "four" },
2815+
{ type: "ARRAY_END", value: "]" },
2816+
{ type: "ARRAY_END", value: "]" },
2817+
{ type: "DICTIONARY_END", value: "]" },
2818+
{ type: "PUNCTUATION", value: ";" },
2819+
{ type: "TERMINATOR", value: "EOF" }
2820+
];
2821+
output = {
2822+
"type": "Program",
2823+
"body": [
2824+
{
2825+
"type": "VariableDeclaration",
2826+
"declarations": [
2827+
{
2828+
"type": "VariableDeclarator",
2829+
"id": {
2830+
"type": "Identifier",
2831+
"name": "firstNum"
2832+
},
2833+
"init": {
2834+
"type": "Literal",
2835+
"value": 1,
2836+
"raw": "1"
2837+
}
2838+
}
2839+
],
2840+
"kind": "var"
2841+
},
2842+
{
2843+
"type": "VariableDeclaration",
2844+
"declarations": [
2845+
{
2846+
"type": "VariableDeclarator",
2847+
"id": {
2848+
"type": "Identifier",
2849+
"name": "secNum"
2850+
},
2851+
"init": {
2852+
"type": "Literal",
2853+
"value": 2,
2854+
"raw": "2"
2855+
}
2856+
}
2857+
],
2858+
"kind": "var"
2859+
},
2860+
{
2861+
"type": "VariableDeclaration",
2862+
"declarations": [
2863+
{
2864+
"type": "VariableDeclarator",
2865+
"id": {
2866+
"type": "Identifier",
2867+
"name": "dict"
2868+
},
2869+
"init": {
2870+
"type": "ObjectExpression",
2871+
"properties": []
2872+
}
2873+
}
2874+
],
2875+
"kind": "var"
2876+
},
2877+
{
2878+
"type": "ExpressionStatement",
2879+
"expression": {
2880+
"type": "AssignmentExpression",
2881+
"operator": "=",
2882+
"left": {
2883+
"type": "MemberExpression",
2884+
"computed": true,
2885+
"object": {
2886+
"type": "Identifier",
2887+
"name": "dict"
2888+
},
2889+
"property": {
2890+
"type": "Identifier",
2891+
"name": "firstNum"
2892+
}
2893+
},
2894+
"right": {
2895+
"type": "ArrayExpression",
2896+
"elements": [
2897+
{
2898+
"type": "ArrayExpression",
2899+
"elements": [
2900+
{
2901+
"type": "Literal",
2902+
"value": 1,
2903+
"raw": "1"
2904+
},
2905+
{
2906+
"type": "Literal",
2907+
"value": 2,
2908+
"raw": "2"
2909+
}
2910+
]
2911+
},
2912+
{
2913+
"type": "ArrayExpression",
2914+
"elements": [
2915+
{
2916+
"type": "Literal",
2917+
"value": 3,
2918+
"raw": "3"
2919+
},
2920+
{
2921+
"type": "Literal",
2922+
"value": 4,
2923+
"raw": "4"
2924+
}
2925+
]
2926+
}
2927+
]
2928+
}
2929+
}
2930+
},
2931+
{
2932+
"type": "ExpressionStatement",
2933+
"expression": {
2934+
"type": "AssignmentExpression",
2935+
"operator": "=",
2936+
"left": {
2937+
"type": "MemberExpression",
2938+
"computed": true,
2939+
"object": {
2940+
"type": "Identifier",
2941+
"name": "dict"
2942+
},
2943+
"property": {
2944+
"type": "Identifier",
2945+
"name": "secNum"
2946+
}
2947+
},
2948+
"right": {
2949+
"type": "ArrayExpression",
2950+
"elements": [
2951+
{
2952+
"type": "ArrayExpression",
2953+
"elements": [
2954+
{
2955+
"type": "Literal",
2956+
"value": "one",
2957+
"raw": "\"one\""
2958+
},
2959+
{
2960+
"type": "Literal",
2961+
"value": "two",
2962+
"raw": "\"two\""
2963+
}
2964+
]
2965+
},
2966+
{
2967+
"type": "ArrayExpression",
2968+
"elements": [
2969+
{
2970+
"type": "Literal",
2971+
"value": "three",
2972+
"raw": "\"three\""
2973+
},
2974+
{
2975+
"type": "Literal",
2976+
"value": "four",
2977+
"raw": "\"four\""
2978+
}
2979+
]
2980+
}
2981+
]
2982+
}
2983+
}
2984+
}
2985+
],
2986+
"sourceType": "module"
2987+
};
2988+
expect(R.equals(parser(input), output)).to.equal(true);
2989+
});
27732990

27742991
// Test 20 - Swift input: 'let arr = [1,2]; var v = [arr[0]: [[1,2], [3,4]], arr[1]: [["one", "two"], ["three", "four"]]];'
2775-
// AST Explorer input: 'let arr = [1,2]; var v = {}; v[arr[0]] = [[1,2], [3,4]]; v[arr[1]] = [["one", "two"], ["three", "four"]];'
2776-
xit('should handle arrays of dictionaries', function () {
2992+
// AST Explorer input: 'var arr = [1,2]; var v = {}; v[arr[0]] = [[1,2], [3,4]]; v[arr[1]] = [["one", "two"], ["three", "four"]];'
2993+
it('should handle arrays of dictionaries', function () {
27772994
input = [
27782995
{ type: "DECLARATION_KEYWORD", value: "let" },
27792996
{ type: "IDENTIFIER", value: "arr" },
@@ -2794,6 +3011,7 @@ describe('Parser', function() {
27943011
{ type: "PUNCTUATION", value: "]" },
27953012
{ type: "PUNCTUATION", value: ":" },
27963013
{ type: "ARRAY_START", value: "[" },
3014+
{ type: "ARRAY_START", value: "[" },
27973015
{ type: "NUMBER", value: "1" },
27983016
{ type: "PUNCTUATION", value: "," },
27993017
{ type: "NUMBER", value: "2" },
@@ -2812,6 +3030,7 @@ describe('Parser', function() {
28123030
{ type: "PUNCTUATION", value: "]" },
28133031
{ type: "PUNCTUATION", value: ":" },
28143032
{ type: "ARRAY_START", value: "[" },
3033+
{ type: "ARRAY_START", value: "[" },
28153034
{ type: "STRING", value: "one" },
28163035
{ type: "PUNCTUATION", value: "," },
28173036
{ type: "STRING", value: "two" },
@@ -2856,7 +3075,7 @@ describe('Parser', function() {
28563075
}
28573076
}
28583077
],
2859-
"kind": "let"
3078+
"kind": "var"
28603079
},
28613080
{
28623081
"type": "VariableDeclaration",

tests/parserTestsSecondMilestone.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ describe('Second Milestone Parser', function() {
357357

358358
// Swift input: 'var e = 1; if (e + 1) == 2 {e = 5};'
359359
// AST Explorer input: 'var e = 1; if ((e + 1) == 2) {e = 5};'
360-
it('should handle complex conditionals without an outer parenthetical', function() {
360+
xit('should handle complex conditionals without an outer parenthetical', function() {
361361
input = [
362362
{ type: "DECLARATION_KEYWORD", value: "var" },
363363
{ type: "IDENTIFIER", value: "e" },
@@ -4980,7 +4980,6 @@ describe('Second Milestone Parser', function() {
49804980
{ type: 'TERMINATOR', value: '\\n' },
49814981
{ type: 'TERMINATOR', value: '\\n' },
49824982
{ type: 'TERMINATOR', value: 'EOF' }
4983-
49844983
];
49854984
output = {
49864985
"type": "Program",

transpiler/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ module.exports = {
1414
ast: function(tokens) {
1515
return lexer(parser(tokens));
1616
}
17-
};
17+
}

transpiler/lexer/lexer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ module.exports = function(code) {
143143
last.status = false;
144144
insideInvocation.pop();
145145
advanceAndClear(1);
146-
lexerFunctions.handleEndOfFile(nextCol, tokens);
147146
continue;
148147
}
149148

@@ -254,7 +253,7 @@ module.exports = function(code) {
254253
}) ||
255254
lexerFunctions.checkFor('OPERATOR', chunk, tokens) ||
256255
lexerFunctions.checkFor('TERMINATOR', chunk, tokens) ||
257-
lexerFunctions.checkForIdentifier(chunk, tokens, lastToken, VARIABLE_NAMES, insideFunction) ||
256+
lexerFunctions.checkForIdentifier(chunk, tokens, lastToken, VARIABLE_NAMES) ||
258257
lexerFunctions.checkForLiteral(chunk, tokens);
259258
}
260259

transpiler/lexer/lexerFunctions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ module.exports = {
257257
},
258258

259259
// helper function to check for identifiers
260-
checkForIdentifier: function(chunk, tokens, lastToken, variable_names, insideFunction) {
260+
checkForIdentifier: function(chunk, tokens, lastToken, variable_names) {
261261
if (variable_names[chunk]) {
262262
if (tokens) {
263263
module.exports.makeToken(undefined, chunk, tokens, 'IDENTIFIER', chunk);
@@ -269,7 +269,7 @@ module.exports = {
269269
// special conditions to handle for-in loops that iterate over dictionaries
270270
(lastToken.value === '(' && tokens[tokens.length - 2].value === 'for') ||
271271
(lastToken.value === ',' && tokens[tokens.length - 3].value) === '(' &&
272-
tokens[tokens.length - 4].value === 'for' || (insideFunction.length && insideFunction[insideFunction.length - 1]['insideParams'] === true)) {
272+
tokens[tokens.length - 4].value === 'for') {
273273
if (tokens) {
274274
module.exports.makeToken(undefined, chunk, tokens, 'IDENTIFIER', chunk);
275275
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy