diff --git a/README.md b/README.md index ab88dbe..eceac8b 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,8 @@ ``` print_range = λ(a, b) if a <= b { - print(a); + println(a); if a + 1 <= b { - print(", "); print_range(a + 1, b); } else @@ -18,7 +17,16 @@ print_range(1, 10); ``` output: ``` -1, 2, 3, 4, 5, 6, 7, 8, 9, 10 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 ***Result: false ``` diff --git a/bin/lambda b/bin/lambda new file mode 100644 index 0000000..9cd6a46 --- /dev/null +++ b/bin/lambda @@ -0,0 +1,22 @@ +#!/usr/bin/env node + +let InputStream = require('../src/InputStream') +let TokenStream = require('../src/TokenStream') +let parser = require('../src/parser') +let Environment = require('../src/Environment') + +const fs = require('fs') + +let args = process.argv +code = fs.readFileSync(args[2]).toString() + +let globalEnv = new Environment() +ast = parser(TokenStream(InputStream(code))) + +globalEnv.def('println', (val) => { + console.log(val) +}) + +// globalEnv.def('readFileSync', ()) + +Environment.evaluate(ast, globalEnv) \ No newline at end of file diff --git a/demo b/demo new file mode 100644 index 0000000..9e3bcd5 --- /dev/null +++ b/demo @@ -0,0 +1,21 @@ +print_range = λ(a, b) if a <= b { + println(a); + if a + 1 <= b { + print_range(a + 1, b); + } + else + println(""); + }; +print_range(1, 10); + +cons = λ(a, b) λ(f) f(a, b); +car = λ(cell) cell(λ(a, b) a); +cdr = λ(cell) cell(λ(a, b) b); +NIL = λ(f) f(NIL, NIL); + +x = cons(1, cons(2, cons(3, cons(4, cons(5, NIL))))); +println(car(x)); # 1 +println(car(cdr(x))); # 2 +println(car(cdr(cdr(x)))); # 3 +println(car(cdr(cdr(cdr(x))))); # 4 +println(car(cdr(cdr(cdr(cdr(x)))))); # 5 \ No newline at end of file diff --git a/package.json b/package.json index eada0af..ac5823e 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "implement-a-pl", - "version": "0.0.1", + "version": "0.1.0", "description": "compiler", "main": "index.js", "scripts": { - "test": "test" + "test": "node ./bin/lambda ./demo" }, "keywords": [ "compiler" diff --git a/src/Environment.js b/src/Environment.js index 6ae3122..b383092 100644 --- a/src/Environment.js +++ b/src/Environment.js @@ -96,6 +96,7 @@ class Environment { } } +Environment.evaluate = evaluate @@ -292,41 +293,5 @@ function makeLambda(expr, env) { } } -// Primitive functions - -// let code = 'sum = λ(x, y) x + y; println(sum(2, 3.5));' -let code = -` -# demo1 -print_range = λ(a, b) if a <= b { - println(a); - if a + 1 <= b { - print_range(a + 1, b); - } - else - println(""); - }; -print_range(1, 100); - -# demo2 -cons = λ(a, b) λ(f) f(a, b); -car = λ(cell) cell(λ(a, b) a); -cdr = λ(cell) cell(λ(a, b) b); -NIL = λ(f) f(NIL, NIL); - -x = cons(1, cons(2, cons(3, cons(4, cons(5, NIL))))); -println(car(x)); # 1 -println(car(cdr(x))); # 2 -println(car(cdr(cdr(x)))); # 3 -println(car(cdr(cdr(cdr(x))))); # 4 -println(car(cdr(cdr(cdr(cdr(x)))))); # 5 -` -let ast = parser(TokenStream(InputStream(code))) - -let globalEnv = new Environment() - -globalEnv.def('println', (val) => { - console.log(val) -}) - -evaluate(ast, globalEnv) \ No newline at end of file +module.exports = Environment +// Primitive functions \ No newline at end of file