Skip to content

Commit

Permalink
jest setup
Browse files Browse the repository at this point in the history
  • Loading branch information
keyz committed Apr 14, 2016
1 parent 61a57b1 commit 2d0f4a3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
34 changes: 17 additions & 17 deletions src/Interp.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
/*
Welcome to the interpreter!
It only supports the following expressions and statements:
- numbers
- booleans
- variables
- if statements
- function expression
- function calls
- +, -, *, /
*/
* Welcome to the interpreter!
* It only supports the following expressions and statements:
* - numbers
* - booleans
* - variables
* - if statements
* - function expression
* - function calls
* - `+`, `-`, `*`, `/`
*/

import { parse } from './Parser';
import { readFile } from './Reader';
import {
emptyEnv,
lookupEnv,
Expand All @@ -23,9 +22,6 @@ import {
applyClosure,
} from './Closure';

const code = readFile('../samples/adder.js');
const ast = parse(code);

const expInterp = (exp, env) => {
switch (exp.type) {
case 'ArrowFunctionExpression': {
Expand All @@ -50,6 +46,10 @@ const expInterp = (exp, env) => {
const { value: val } = exp;
return { val, env };
}
case 'BooleanLiteral': {
const { value: val } = exp;
return { val, env };
}
case 'Identifier': {
const { name } = exp;
const val = lookupEnv(name, env);
Expand Down Expand Up @@ -106,7 +106,7 @@ const blockStatementInterp = (exp, env) => {
}
};

const programInterp = (exp, env) => {
const programInterp = (exp, env = emptyEnv) => {
switch (exp.type) {
case 'Program': {
const { val } = exp.body.reduce(
Expand All @@ -121,4 +121,4 @@ const programInterp = (exp, env) => {
}
};

console.log(programInterp(ast, emptyEnv));
export { programInterp };
2 changes: 1 addition & 1 deletion src/__tests__/Environment-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jest.unmock('../Environment').unmock('immutable');
jest.disableAutomock();

const {
emptyEnv,
Expand Down
18 changes: 18 additions & 0 deletions src/__tests__/Interp-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
jest.disableAutomock();

const { programInterp } = require('../Interp');
const { parse } = require('../Parser');

const parseAndEval = (code) => programInterp(parse(code));

describe('Interp', () => {
it('should support trivial prim types', () => {
expect(parseAndEval('3;')).toBe(3);
expect(parseAndEval('true;')).toBe(true);
});

it('should support arrow function expressions', () => {
expect(parseAndEval('const foo = (x, y) => 3; foo(100, 200)')).toBe(3);
expect(parseAndEval('const foo = (x) => (y) => x + y; foo(12)(24)')).toBe(36);
});
});

0 comments on commit 2d0f4a3

Please sign in to comment.