Skip to content

Commit

Permalink
Fix parser arrow functions (#108)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yyc authored and remo5000 committed Nov 1, 2018
1 parent 4dfa00f commit 4edca2d
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/__tests__/__snapshots__/index.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,20 @@ exports[`Inifinite recursion with list args represents CallExpression well 1`] =
f([1, [2, []]]).. f([1, [2, []]]).. f([1, [2, []]]).."
`;

exports[`Metacircular Interpreter parses Arrow Function Assignments properly 1`] = `
Object {
"status": "finished",
"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},[]]",
}
`;

exports[`Metacircular Interpreter parses Arrow Function Expressions properly 1`] = `
Object {
"status": "finished",
"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}},[]]",
}
`;

exports[`Simple inifinite recursion represents CallExpression well 1`] = `
"Line 1: Infinite recursion
x(x => x(x)).. x(x => x(x)).. x(x => x(x)).."
Expand Down
102 changes: 102 additions & 0 deletions src/__tests__/__snapshots__/mci-parser.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Parse an arrow function 1`] = `
Array [
Object {
"body": Object {
"expression": Object {
"line": 0,
"operands": Array [
Object {
"line": 0,
"name": "x",
"tag": "name",
},
Array [
1,
Array [],
],
],
"operator": Object {
"line": 0,
"name": "+",
"tag": "name",
},
"tag": "application",
},
"line": 0,
"tag": "return_statement",
},
"line": 0,
"location": Object {
"end_col": 0,
"end_line": 1,
"start_col": 0,
"start_line": 1,
},
"parameters": Array [
Object {
"line": 0,
"name": "x",
"tag": "name",
},
Array [],
],
"tag": "function_definition",
},
Array [],
]
`;

exports[`Parse arrow function assignment 1`] = `
Array [
Object {
"line": 0,
"name": "y",
"tag": "constant_declaration",
"value": Object {
"body": Object {
"expression": Object {
"line": 0,
"operands": Array [
Object {
"line": 0,
"name": "x",
"tag": "name",
},
Array [
1,
Array [],
],
],
"operator": Object {
"line": 0,
"name": "+",
"tag": "name",
},
"tag": "application",
},
"line": 0,
"tag": "return_statement",
},
"line": 0,
"location": Object {
"end_col": 10,
"end_line": 1,
"start_col": 10,
"start_line": 1,
},
"parameters": Array [
Object {
"line": 0,
"name": "x",
"tag": "name",
},
Array [],
],
"tag": "function_definition",
},
},
Array [],
]
`;
14 changes: 14 additions & 0 deletions src/__tests__/__snapshots__/parser.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,17 @@ Node {
"type": "Program",
}
`;

exports[`Parse an arrow function assignment in js-slang 1`] = `
Object {
"status": "finished",
"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},[]]",
}
`;

exports[`Parse an arrow function expression in js-slang 1`] = `
Object {
"status": "finished",
"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}},[]]",
}
`;
20 changes: 20 additions & 0 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,23 @@ test(
},
30000
)

test('Metacircular Interpreter parses Arrow Function Expressions properly', () => {
const code = 'stringify(parse("x => x + 1;"));'
const context = mockContext(4)
const promise = runInContext(code, context, { scheduler: 'preemptive' })
return promise.then(obj => {
expect(obj).toMatchSnapshot()
expect(obj.status).toBe('finished')
})
})

test('Metacircular Interpreter parses Arrow Function Assignments properly', () => {
const code = 'stringify(parse("const y = x => x + 1;"));'
const context = mockContext(4)
const promise = runInContext(code, context, { scheduler: 'preemptive' })
return promise.then(obj => {
expect(obj).toMatchSnapshot()
expect(obj.status).toBe('finished')
})
})
18 changes: 18 additions & 0 deletions src/__tests__/mci-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { mockContext } from '../mocks/context'
//@ts-ignore
import { Parser } from '../stdlib/parser.js'

const parser = new Parser();

test('Parse an arrow function', () => {
const context = mockContext()
const program = parser.parse('x => x + 1;', context)
expect(program).toMatchSnapshot()
})

test('Parse arrow function assignment', () => {
const context = mockContext()
const program = parser.parse('const y = x => x + 1;', context)
expect(program).toMatchSnapshot()
})

21 changes: 21 additions & 0 deletions src/__tests__/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { mockContext } from '../mocks/context'
import { parse } from '../parser'
import { runInContext } from '../index'

test('Empty parse returns empty Program Node', () => {
const context = mockContext()
Expand All @@ -24,3 +25,23 @@ test('Parse an arrow function', () => {
const program = parse('x => x + 1;', context)
expect(program).toMatchSnapshot()
})

test('Parse an arrow function expression in js-slang', () => {
const code = 'stringify(parse("x => x + 1;"));'
const context = mockContext(4)
const promise = runInContext(code, context, { scheduler: 'preemptive' })
return promise.then(obj => {
expect(obj).toMatchSnapshot()
expect(obj.status).toBe('finished')
})
})

test('Parse an arrow function assignment in js-slang', () => {
const code = 'stringify(parse("const y = x => x + 1;"));'
const context = mockContext(4)
const promise = runInContext(code, context, { scheduler: 'preemptive' })
return promise.then(obj => {
expect(obj).toMatchSnapshot()
expect(obj.status).toBe('finished')
})
})
8 changes: 4 additions & 4 deletions src/stdlib/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4edca2d

Please sign in to comment.