Skip to content

Commit

Permalink
runtime lexical/dynamic toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
keyz committed Apr 20, 2016
1 parent d90df35 commit ec2c63f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/Interp/ExpressionInterp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
applyClosure,
} from '../Closure';

import Options from '../Options';

import { interp as statementInterp } from './StatementInterp';

const interp = (exp, env) => {
Expand Down Expand Up @@ -52,7 +54,7 @@ const interp = (exp, env) => {
const closure = interp(callee, env);
const vals = rawArgs.map((obj) => interp(obj, env));

return applyClosure(interp, closure, vals, env);
return applyClosure(interp, closure, vals, env, Options.isLexical);
}

case 'UnaryExpression': {
Expand Down
38 changes: 35 additions & 3 deletions src/Interp/__tests__/ExpressionInterp-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict';

jest.unmock('../../Parser')
.unmock('../ExpressionInterp').unmock('../StatementInterp')
.unmock('../../Environment').unmock('../../Closure');
.unmock('../ExpressionInterp')
.unmock('../StatementInterp')
.unmock('../../Environment')
.unmock('../../Closure')
.unmock('../../Options');

const { interp } = require('../ExpressionInterp');
const { parse } = require('../../Parser');
Expand All @@ -11,7 +14,8 @@ const {
extendEnv,
} = require('../../Environment');

import { makeClosure } from '../../Closure';
const { makeClosure } = require('../../Closure');
const Options = require('../../Options').default;

const parseAndgetExp = (code) => parse(code).body[0].expression;

Expand Down Expand Up @@ -205,6 +209,34 @@ describe('Interp', () => {
})());
});

it('should support dynamic scope', () => {
Options.isLexical = false;

expect(interpExp(`(() => {
const adder = (x) => (y) => x + y;
const x = 100;
const add3ButActuallyAdd100 = adder(3);
return add3ButActuallyAdd100(5);
})()`)).toBe(100 + 5);

expect(interpExp(`(() => {
const adder = (x) => (y) => x + y;
const x = 100;
const add3ButActuallyAdd100 = adder(3);
return add3ButActuallyAdd100(5);
})()`)).toBe(100 + 5);

expect(interpExp(`(() => {
const x = 100;
const y = 200;
const adder = (x) => (y) => x + y;
const add3 = adder(3);
return add3(39);
})()`)).toBe(100 + 39);

Options.isLexical = true;
});


// it('IfStatement', () => {
// expect(interpExp(`(() => {
Expand Down
9 changes: 9 additions & 0 deletions src/Options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* This guy allows us to toggle the lexical/dynamic behavior at runtime.
*/

const Options = {
isLexical: true,
};

export default Options;

0 comments on commit ec2c63f

Please sign in to comment.