Skip to content

Commit

Permalink
update and add bin
Browse files Browse the repository at this point in the history
  • Loading branch information
yjhmelody committed Aug 4, 2017
1 parent 6c8fa4c commit 2071c0f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 43 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```

Expand Down
22 changes: 22 additions & 0 deletions bin/lambda
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions demo
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
41 changes: 3 additions & 38 deletions src/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class Environment {
}
}

Environment.evaluate = evaluate



Expand Down Expand Up @@ -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)
module.exports = Environment
// Primitive functions

0 comments on commit 2071c0f

Please sign in to comment.