Skip to content

Commit

Permalink
added "let"
Browse files Browse the repository at this point in the history
  • Loading branch information
hugodecasta committed Feb 21, 2020
1 parent d974910 commit 32b3a20
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ let methods = {
},
'-':function(args) {
let end_args = args.map(arg=>handle(arg))
if(end_args.length == 1) {
return -end_args[0]
}
return end_args.reduce(function(acc,cur) {
return acc - cur
})
Expand All @@ -75,6 +78,9 @@ let methods = {
},
'/':function(args) {
let end_args = args.map(arg=>handle(arg))
if(end_args.length == 1) {
return 1/end_args[0]
}
return end_args.reduce(function(acc,cur) {
return acc / cur
})
Expand Down Expand Up @@ -186,6 +192,18 @@ let methods = {
return ret
}
},
'let':function(args) {
let params = args[0]
let prog = args[1]
let old_base = JSON.stringify(base)
for(let param of params) {
handle(['define',param[0],param[1]])
}
let ret = handle(prog)
base = JSON.parse(old_base)
return ret

},
'exit':function(args) {
process.exit(0)
}
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@ test('negate', t => {
t.is(scheme("a"), true)
t.is(scheme("b"), false)
t.is(scheme("(not b)"), true)
})

test('let', t => {
scheme('(clear)')
scheme("(define func (lambda (n) (let ((a 5)) (- a n))))")
t.is(scheme("(func 5)"), 0)
t.is(scheme("(func (- 5))"), 10)
})

0 comments on commit 32b3a20

Please sign in to comment.