From 8050f876b197c929b5c3bd5d47bda8390e29acf1 Mon Sep 17 00:00:00 2001 From: Vignesh Date: Wed, 31 Oct 2018 10:18:26 +0800 Subject: [PATCH] Re-enable function expressions (#106) * Revert "Disable function expressions (#96)" This reverts commit 615fb51d4c50e54a00940b441a4e9285a1011005. * Bump version --- package.json | 2 +- .../__snapshots__/disallowed_syntax.ts.snap | 4 --- src/__tests__/disallowed_syntax.ts | 29 ------------------- src/interpreter.ts | 3 ++ src/syntaxTypes.ts | 5 ++-- 5 files changed, 6 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index d81769599..b63b458fa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-slang", - "version": "0.1.13", + "version": "0.1.14", "description": "Javascript-based interpreter for slang, written in Typescript", "author": { "name": "Source Academy", diff --git a/src/__tests__/__snapshots__/disallowed_syntax.ts.snap b/src/__tests__/__snapshots__/disallowed_syntax.ts.snap index 9fedeab82..f1d6c96ec 100644 --- a/src/__tests__/__snapshots__/disallowed_syntax.ts.snap +++ b/src/__tests__/__snapshots__/disallowed_syntax.ts.snap @@ -8,8 +8,4 @@ exports[`Cannot leave blank expressions in for loops 3`] = `"Line 2: For stateme exports[`Cannot leave blank expressions in for loops 4`] = `"Line 2: For statement cannot have empty init,test,update expressions."`; -exports[`Cannot use js function expressions 1`] = `"Line 2: Function expressions are not allowed"`; - exports[`Cannot use update expressions 1`] = `"Line 3: Update expressions are not allowed"`; - -exports[`DEFINITELY CANNOT use named function declarations as expressions 1`] = `"Line 2: Function expressions are not allowed"`; diff --git a/src/__tests__/disallowed_syntax.ts b/src/__tests__/disallowed_syntax.ts index 15550c0ee..c6260d204 100644 --- a/src/__tests__/disallowed_syntax.ts +++ b/src/__tests__/disallowed_syntax.ts @@ -57,32 +57,3 @@ test("Cannot use update expressions", () => { }); }); -test("Cannot use js function expressions", () => { - const code = ` - map(function(x) { - return x + 1; - }, list(1)); - `; - const context = mockContext(3); - const promise = runInContext(code, context, { scheduler: "preemptive" }); - return promise.then(obj => { - expect(obj.status).toBe("error"); - const errors = parseError(context.errors); - expect(errors).toMatchSnapshot(); - }); -}); - -test("DEFINITELY CANNOT use named function declarations as expressions", () => { - const code = ` - map(function a(x) { - return x + 1; - }, list(1)); - `; - const context = mockContext(3); - const promise = runInContext(code, context, { scheduler: "preemptive" }); - return promise.then(obj => { - expect(obj.status).toBe("error"); - const errors = parseError(context.errors); - expect(errors).toMatchSnapshot(); - }); -}); diff --git a/src/interpreter.ts b/src/interpreter.ts index 6a76a2a1b..4df480e33 100644 --- a/src/interpreter.ts +++ b/src/interpreter.ts @@ -175,6 +175,9 @@ export const evaluators: { [nodeType: string]: Evaluator } = { } return res }, + FunctionExpression: function*(node: es.FunctionExpression, context: Context) { + return new Closure(node, currentFrame(context), context) + }, ArrowFunctionExpression: function*(node: es.Function, context: Context) { return new ArrowClosure(node, currentFrame(context), context) }, diff --git a/src/syntaxTypes.ts b/src/syntaxTypes.ts index 357c7861e..134f3e40c 100644 --- a/src/syntaxTypes.ts +++ b/src/syntaxTypes.ts @@ -11,6 +11,7 @@ const syntaxTypes: { [nodeName: string]: number } = { BinaryExpression: 1, LogicalExpression: 1, ConditionalExpression: 1, + FunctionExpression: 1, ArrowFunctionExpression: 1, Identifier: 1, Literal: 1, @@ -44,9 +45,7 @@ const syntaxTypes: { [nodeName: string]: number } = { CatchClause: Infinity, DoWhileStatement: Infinity, ForInStatement: Infinity, - SequenceExpression: Infinity, - FunctionExpression: Infinity, - + SequenceExpression: Infinity } export default syntaxTypes