Skip to content

Commit 4dfa00f

Browse files
martin-henzyyc
authored andcommitted
C4 rush (#107)
* arrow syntax fixed * quote added * Fix syntax error * fix imports and add arrow function test * bump version
1 parent 8050f87 commit 4dfa00f

File tree

9 files changed

+313
-106
lines changed

9 files changed

+313
-106
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-slang",
3-
"version": "0.1.14",
3+
"version": "0.1.15",
44
"description": "Javascript-based interpreter for slang, written in Typescript",
55
"author": {
66
"name": "Source Academy",

src/__tests__/__snapshots__/parser.ts.snap

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,130 @@ Node {
133133
"type": "Program",
134134
}
135135
`;
136+
137+
exports[`Parse an arrow function 1`] = `
138+
Node {
139+
"__id": "node_13",
140+
"body": Array [
141+
Node {
142+
"__id": "node_12",
143+
"end": 11,
144+
"expression": Node {
145+
"__id": "node_11",
146+
"body": Node {
147+
"__id": "node_10",
148+
"end": 10,
149+
"left": Node {
150+
"__id": "node_8",
151+
"end": 6,
152+
"loc": SourceLocation {
153+
"end": Position {
154+
"column": 6,
155+
"line": 1,
156+
},
157+
"start": Position {
158+
"column": 5,
159+
"line": 1,
160+
},
161+
},
162+
"name": "x",
163+
"start": 5,
164+
"type": "Identifier",
165+
},
166+
"loc": SourceLocation {
167+
"end": Position {
168+
"column": 10,
169+
"line": 1,
170+
},
171+
"start": Position {
172+
"column": 5,
173+
"line": 1,
174+
},
175+
},
176+
"operator": "+",
177+
"right": Node {
178+
"__id": "node_9",
179+
"end": 10,
180+
"loc": SourceLocation {
181+
"end": Position {
182+
"column": 10,
183+
"line": 1,
184+
},
185+
"start": Position {
186+
"column": 9,
187+
"line": 1,
188+
},
189+
},
190+
"raw": "1",
191+
"start": 9,
192+
"type": "Literal",
193+
"value": 1,
194+
},
195+
"start": 5,
196+
"type": "BinaryExpression",
197+
},
198+
"end": 10,
199+
"expression": true,
200+
"generator": false,
201+
"id": null,
202+
"loc": SourceLocation {
203+
"end": Position {
204+
"column": 10,
205+
"line": 1,
206+
},
207+
"start": Position {
208+
"column": 0,
209+
"line": 1,
210+
},
211+
},
212+
"params": Array [
213+
Node {
214+
"end": 1,
215+
"loc": SourceLocation {
216+
"end": Position {
217+
"column": 1,
218+
"line": 1,
219+
},
220+
"start": Position {
221+
"column": 0,
222+
"line": 1,
223+
},
224+
},
225+
"name": "x",
226+
"start": 0,
227+
"type": "Identifier",
228+
},
229+
],
230+
"start": 0,
231+
"type": "ArrowFunctionExpression",
232+
},
233+
"loc": SourceLocation {
234+
"end": Position {
235+
"column": 11,
236+
"line": 1,
237+
},
238+
"start": Position {
239+
"column": 0,
240+
"line": 1,
241+
},
242+
},
243+
"start": 0,
244+
"type": "ExpressionStatement",
245+
},
246+
],
247+
"end": 11,
248+
"loc": SourceLocation {
249+
"end": Position {
250+
"column": 11,
251+
"line": 1,
252+
},
253+
"start": Position {
254+
"column": 0,
255+
"line": 1,
256+
},
257+
},
258+
"sourceType": "script",
259+
"start": 0,
260+
"type": "Program",
261+
}
262+
`;

src/__tests__/parser.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ test('Parse a single number', () => {
1818
const program = parse('42;', context)
1919
expect(program).toMatchSnapshot()
2020
})
21+
22+
test('Parse an arrow function', () => {
23+
const context = mockContext()
24+
const program = parse('x => x + 1;', context)
25+
expect(program).toMatchSnapshot()
26+
})

src/__tests__/tst.notts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// import { mockContext } from "../mocks/context";
2+
// import { parseError, runInContext } from "../index";
3+
// import { Finished } from "../types";
4+
//
5+
// test(
6+
// "Functions are stored correctly in lists",
7+
// () => {
8+
// const code = `
9+
// const f = i => i + 1;
10+
// pair(3, 4);
11+
// const lst = pair(f, f);
12+
// head(lst) === f;
13+
// `;
14+
// const context = mockContext(2);
15+
// const promise = runInContext(code, context, { scheduler: "preemptive" });
16+
// return promise.then(obj => {
17+
// console.log(parseError(context.errors));
18+
//
19+
// expect(obj.status).toBe("finished");
20+
// expect((obj as Finished).value).toBe(true);
21+
// });
22+
// },
23+
// 30000
24+
// );
25+
//
26+
// test(
27+
// "HOFs work",
28+
// () => {
29+
// const code = `
30+
// accumulate((x, y) => x + y, 0, list(1,2,3));
31+
// `;
32+
// const context = mockContext(2);
33+
// const promise = runInContext(code, context, { scheduler: "preemptive" });
34+
// return promise.then(obj => {
35+
// console.log(parseError(context.errors));
36+
// expect(obj.status).toBe("finished");
37+
// expect((obj as Finished).value).toBe(6);
38+
// });
39+
// },
40+
// 30000
41+
// );
42+
//
43+
test("tru", {} => return true);

src/createContext.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { toString } from '.';
55

66
/** Import meta-circular parser */
77
const createParserModule = require('./stdlib/parser')
8-
const createParser = createParserModule.createParser
8+
const Parser = createParserModule.Parser
99

1010
const GLOBAL = typeof window === 'undefined' ? global : window
1111

@@ -29,7 +29,7 @@ export const createEmptyContext = <T>(chapter: number, externalSymbols: string[]
2929
externalContext,
3030
cfg: createEmptyCFG(),
3131
runtime: createEmptyRuntime(),
32-
metaCircularParser: createParser()
32+
metaCircularParser: new Parser()
3333
})
3434

3535
export const ensureGlobalEnvironmentExist = (context: Context) => {
@@ -79,7 +79,7 @@ export const importBuiltins = (context: Context, externalBuiltIns: CustomBuiltIn
7979
alert.__SOURCE__ = externalBuiltIns.alert.__SOURCE__
8080
let visualiseList = (list: any) => externalBuiltIns.visualiseList(list, context.externalContext)
8181
visualiseList.__SOURCE__ = externalBuiltIns.visualiseList.__SOURCE__
82-
82+
8383

8484
if (context.chapter >= 1) {
8585
defineSymbol(context, 'runtime', misc.runtime)
@@ -140,7 +140,7 @@ export const importBuiltins = (context: Context, externalBuiltIns: CustomBuiltIn
140140
if (context.chapter >= 4) {
141141
defineSymbol(context, 'stringify', JSON.stringify)
142142
defineSymbol(context, 'parse',
143-
function () {
143+
function () {
144144
return context.metaCircularParser
145145
.parse.apply(context.metaCircularParser, arguments)
146146
});
@@ -186,7 +186,7 @@ const defaultBuiltIns: CustomBuiltIns = {
186186
throw new Error('List visualizer is not enabled')}
187187
}
188188

189-
const createContext = <T>(chapter = 1, externalSymbols: string[] = [], externalContext?: T,
189+
const createContext = <T>(chapter = 1, externalSymbols: string[] = [], externalContext?: T,
190190
externalBuiltIns: CustomBuiltIns = defaultBuiltIns) => {
191191
const context = createEmptyContext(chapter, externalSymbols, externalContext)
192192

src/stdlib/metacircular-interpreter/lib/interpreter/parser.jison

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -516,21 +516,24 @@ expression
516516
}
517517
};
518518
}}
519-
| identifier '=>' expression
520-
{{
521-
$$ = {
522-
tag: 'function_definition',
523-
parameters: [$1, [] ],
524-
body: { tag: 'return_statement', expression: $3,
525-
line: yylineno },
526-
line: yylineno,
527-
location: {
519+
| expression '=>' expression
520+
{{
521+
if ($1.tag === 'name') {
522+
$$ = {
523+
tag: 'function_definition',
524+
parameters: [$1, [] ],
525+
body: { tag: 'return_statement', expression: $3,
526+
line: yylineno },
527+
line: yylineno,
528+
location: {
528529
start_line: @1.first_line,
529530
start_col: @1.first_column,
530531
end_line: @5.first_line,
531532
end_col: @5.first_column
532-
}
533-
};
533+
};
534+
} else {
535+
error('expecting name before => ' + yylineno + ": " + yytext);
536+
}
534537
}}
535538
536539
| expression '[' expression ']'

src/stdlib/metacircular-interpreter/lib/interpreter/parser.jison.tpl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -516,21 +516,24 @@ expression
516516
}
517517
};
518518
}}
519-
| identifier '=>' expression
520-
{{
521-
$$ = {
522-
tag: 'function_definition',
523-
parameters: [$1, [] ],
524-
body: { tag: 'return_statement', expression: $3,
525-
line: yylineno },
526-
line: yylineno,
527-
location: {
519+
| expression '=>' expression
520+
{{
521+
if ($1.tag === 'name') {
522+
$$ = {
523+
tag: 'function_definition',
524+
parameters: [$1, [] ],
525+
body: { tag: 'return_statement', expression: $3,
526+
line: yylineno },
527+
line: yylineno,
528+
location: {
528529
start_line: @1.first_line,
529530
start_col: @1.first_column,
530531
end_line: @5.first_line,
531532
end_col: @5.first_column
532-
}
533-
};
533+
};
534+
} else {
535+
error('expecting name before => ' + yylineno + ": " + yytext);
536+
}
534537
}}
535538
{{if week|ormore>10}}
536539
| expression '[' expression ']'

src/stdlib/metacircular-interpreter/lib/interpreter/parser.js

Lines changed: 16 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)