Skip to content

Commit 4edca2d

Browse files
yycremo5000
authored andcommitted
Fix parser arrow functions (#108)
* arrow syntax fixed * quote added * Fix syntax error * fix imports and add arrow function test * bump version * Move MCI parser tests to new file * fix reference issues with parser * silence typescript whining * Add tests for parse in js-slang * Add Metacircular interpreter test within context
1 parent 4dfa00f commit 4edca2d

File tree

7 files changed

+193
-4
lines changed

7 files changed

+193
-4
lines changed

src/__tests__/__snapshots__/index.ts.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,20 @@ exports[`Inifinite recursion with list args represents CallExpression well 1`] =
237237
f([1, [2, []]]).. f([1, [2, []]]).. f([1, [2, []]]).."
238238
`;
239239

240+
exports[`Metacircular Interpreter parses Arrow Function Assignments properly 1`] = `
241+
Object {
242+
"status": "finished",
243+
"value": "[{\\"tag\\":\\"constant_declaration\\",\\"name\\":\\"y\\",\\"value\\":{\\"tag\\":\\"function_definition\\",\\"parameters\\":[{\\"tag\\":\\"name\\",\\"name\\":\\"x\\",\\"line\\":0},[]],\\"body\\":{\\"tag\\":\\"return_statement\\",\\"expression\\":{\\"tag\\":\\"application\\",\\"operator\\":{\\"tag\\":\\"name\\",\\"name\\":\\"+\\",\\"line\\":0},\\"operands\\":[{\\"tag\\":\\"name\\",\\"name\\":\\"x\\",\\"line\\":0},[1,[]]],\\"line\\":0},\\"line\\":0},\\"line\\":0,\\"location\\":{\\"start_line\\":1,\\"start_col\\":10,\\"end_line\\":1,\\"end_col\\":10}},\\"line\\":0},[]]",
244+
}
245+
`;
246+
247+
exports[`Metacircular Interpreter parses Arrow Function Expressions properly 1`] = `
248+
Object {
249+
"status": "finished",
250+
"value": "[{\\"tag\\":\\"function_definition\\",\\"parameters\\":[{\\"tag\\":\\"name\\",\\"name\\":\\"x\\",\\"line\\":0},[]],\\"body\\":{\\"tag\\":\\"return_statement\\",\\"expression\\":{\\"tag\\":\\"application\\",\\"operator\\":{\\"tag\\":\\"name\\",\\"name\\":\\"+\\",\\"line\\":0},\\"operands\\":[{\\"tag\\":\\"name\\",\\"name\\":\\"x\\",\\"line\\":0},[1,[]]],\\"line\\":0},\\"line\\":0},\\"line\\":0,\\"location\\":{\\"start_line\\":1,\\"start_col\\":0,\\"end_line\\":1,\\"end_col\\":0}},[]]",
251+
}
252+
`;
253+
240254
exports[`Simple inifinite recursion represents CallExpression well 1`] = `
241255
"Line 1: Infinite recursion
242256
x(x => x(x)).. x(x => x(x)).. x(x => x(x)).."
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Parse an arrow function 1`] = `
4+
Array [
5+
Object {
6+
"body": Object {
7+
"expression": Object {
8+
"line": 0,
9+
"operands": Array [
10+
Object {
11+
"line": 0,
12+
"name": "x",
13+
"tag": "name",
14+
},
15+
Array [
16+
1,
17+
Array [],
18+
],
19+
],
20+
"operator": Object {
21+
"line": 0,
22+
"name": "+",
23+
"tag": "name",
24+
},
25+
"tag": "application",
26+
},
27+
"line": 0,
28+
"tag": "return_statement",
29+
},
30+
"line": 0,
31+
"location": Object {
32+
"end_col": 0,
33+
"end_line": 1,
34+
"start_col": 0,
35+
"start_line": 1,
36+
},
37+
"parameters": Array [
38+
Object {
39+
"line": 0,
40+
"name": "x",
41+
"tag": "name",
42+
},
43+
Array [],
44+
],
45+
"tag": "function_definition",
46+
},
47+
Array [],
48+
]
49+
`;
50+
51+
exports[`Parse arrow function assignment 1`] = `
52+
Array [
53+
Object {
54+
"line": 0,
55+
"name": "y",
56+
"tag": "constant_declaration",
57+
"value": Object {
58+
"body": Object {
59+
"expression": Object {
60+
"line": 0,
61+
"operands": Array [
62+
Object {
63+
"line": 0,
64+
"name": "x",
65+
"tag": "name",
66+
},
67+
Array [
68+
1,
69+
Array [],
70+
],
71+
],
72+
"operator": Object {
73+
"line": 0,
74+
"name": "+",
75+
"tag": "name",
76+
},
77+
"tag": "application",
78+
},
79+
"line": 0,
80+
"tag": "return_statement",
81+
},
82+
"line": 0,
83+
"location": Object {
84+
"end_col": 10,
85+
"end_line": 1,
86+
"start_col": 10,
87+
"start_line": 1,
88+
},
89+
"parameters": Array [
90+
Object {
91+
"line": 0,
92+
"name": "x",
93+
"tag": "name",
94+
},
95+
Array [],
96+
],
97+
"tag": "function_definition",
98+
},
99+
},
100+
Array [],
101+
]
102+
`;

src/__tests__/__snapshots__/parser.ts.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,17 @@ Node {
260260
"type": "Program",
261261
}
262262
`;
263+
264+
exports[`Parse an arrow function assignment in js-slang 1`] = `
265+
Object {
266+
"status": "finished",
267+
"value": "[{\\"tag\\":\\"constant_declaration\\",\\"name\\":\\"y\\",\\"value\\":{\\"tag\\":\\"function_definition\\",\\"parameters\\":[{\\"tag\\":\\"name\\",\\"name\\":\\"x\\",\\"line\\":0},[]],\\"body\\":{\\"tag\\":\\"return_statement\\",\\"expression\\":{\\"tag\\":\\"application\\",\\"operator\\":{\\"tag\\":\\"name\\",\\"name\\":\\"+\\",\\"line\\":0},\\"operands\\":[{\\"tag\\":\\"name\\",\\"name\\":\\"x\\",\\"line\\":0},[1,[]]],\\"line\\":0},\\"line\\":0},\\"line\\":0,\\"location\\":{\\"start_line\\":1,\\"start_col\\":10,\\"end_line\\":1,\\"end_col\\":10}},\\"line\\":0},[]]",
268+
}
269+
`;
270+
271+
exports[`Parse an arrow function expression in js-slang 1`] = `
272+
Object {
273+
"status": "finished",
274+
"value": "[{\\"tag\\":\\"function_definition\\",\\"parameters\\":[{\\"tag\\":\\"name\\",\\"name\\":\\"x\\",\\"line\\":0},[]],\\"body\\":{\\"tag\\":\\"return_statement\\",\\"expression\\":{\\"tag\\":\\"application\\",\\"operator\\":{\\"tag\\":\\"name\\",\\"name\\":\\"+\\",\\"line\\":0},\\"operands\\":[{\\"tag\\":\\"name\\",\\"name\\":\\"x\\",\\"line\\":0},[1,[]]],\\"line\\":0},\\"line\\":0},\\"line\\":0,\\"location\\":{\\"start_line\\":1,\\"start_col\\":0,\\"end_line\\":1,\\"end_col\\":0}},[]]",
275+
}
276+
`;

src/__tests__/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,23 @@ test(
188188
},
189189
30000
190190
)
191+
192+
test('Metacircular Interpreter parses Arrow Function Expressions properly', () => {
193+
const code = 'stringify(parse("x => x + 1;"));'
194+
const context = mockContext(4)
195+
const promise = runInContext(code, context, { scheduler: 'preemptive' })
196+
return promise.then(obj => {
197+
expect(obj).toMatchSnapshot()
198+
expect(obj.status).toBe('finished')
199+
})
200+
})
201+
202+
test('Metacircular Interpreter parses Arrow Function Assignments properly', () => {
203+
const code = 'stringify(parse("const y = x => x + 1;"));'
204+
const context = mockContext(4)
205+
const promise = runInContext(code, context, { scheduler: 'preemptive' })
206+
return promise.then(obj => {
207+
expect(obj).toMatchSnapshot()
208+
expect(obj.status).toBe('finished')
209+
})
210+
})

src/__tests__/mci-parser.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { mockContext } from '../mocks/context'
2+
//@ts-ignore
3+
import { Parser } from '../stdlib/parser.js'
4+
5+
const parser = new Parser();
6+
7+
test('Parse an arrow function', () => {
8+
const context = mockContext()
9+
const program = parser.parse('x => x + 1;', context)
10+
expect(program).toMatchSnapshot()
11+
})
12+
13+
test('Parse arrow function assignment', () => {
14+
const context = mockContext()
15+
const program = parser.parse('const y = x => x + 1;', context)
16+
expect(program).toMatchSnapshot()
17+
})
18+

src/__tests__/parser.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { mockContext } from '../mocks/context'
22
import { parse } from '../parser'
3+
import { runInContext } from '../index'
34

45
test('Empty parse returns empty Program Node', () => {
56
const context = mockContext()
@@ -24,3 +25,23 @@ test('Parse an arrow function', () => {
2425
const program = parse('x => x + 1;', context)
2526
expect(program).toMatchSnapshot()
2627
})
28+
29+
test('Parse an arrow function expression in js-slang', () => {
30+
const code = 'stringify(parse("x => x + 1;"));'
31+
const context = mockContext(4)
32+
const promise = runInContext(code, context, { scheduler: 'preemptive' })
33+
return promise.then(obj => {
34+
expect(obj).toMatchSnapshot()
35+
expect(obj.status).toBe('finished')
36+
})
37+
})
38+
39+
test('Parse an arrow function assignment in js-slang', () => {
40+
const code = 'stringify(parse("const y = x => x + 1;"));'
41+
const context = mockContext(4)
42+
const promise = runInContext(code, context, { scheduler: 'preemptive' })
43+
return promise.then(obj => {
44+
expect(obj).toMatchSnapshot()
45+
expect(obj.status).toBe('finished')
46+
})
47+
})

src/stdlib/parser.js

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

0 commit comments

Comments
 (0)