add '() lambda shorthand

This commit is contained in:
Quentin Leonetti 2019-07-20 16:52:50 +02:00
parent 9e8c65ea13
commit b87e2cf965
2 changed files with 28 additions and 1 deletions

View File

@ -60,6 +60,16 @@ function Lisp (input, lib) {
return interpret(input[2], new Context(lambdaScope, context))
}
},
fn: function (input, context) {
return async function () {
const lambdaArguments = arguments
const lambdaScope = [].reduce(function (acc, x, i) {
acc[x.value] = lambdaArguments[i]
return acc
}, {})
return interpret(input.slice(1), new Context(lambdaScope, context))
}
},
if: async function (input, context) {
if (await interpret(input[1], context)) {
return interpret(input[2], context)
@ -110,6 +120,10 @@ function Lisp (input, lib) {
const token = input.shift()
if (token === undefined) {
return list.pop()
} else if (token === '\'(') {
input.unshift('fn')
list.push(parenthesize(input, []))
return parenthesize(input, list)
} else if (token === '(') {
list.push(parenthesize(input, []))
return parenthesize(input, list)
@ -121,7 +135,14 @@ function Lisp (input, lib) {
}
const tokenize = function (input) {
return input.replace(/^\;.*\n?/gm, '').split('"').map(function (x, i) { return i % 2 === 0 ? x.replace(/\(/g, ' ( ').replace(/\)/g, ' ) ') : x.replace(/ /g, '!whitespace!') }).join('"').trim().split(/\s+/).map(function (x) { return x.replace(/!whitespace!/g, ' ') })
const i = input.replace(/^\;.*\n?/gm, '').split('"')
return i.map(function (x, i) {
return i % 2 === 0 ?
x.replace(/\(/g, ' ( ').replace(/\)/g, ' ) ').replace(/' \( /g, ' \'( ')
: x.replace(/ /g, '!whitespace!')
})
.join('"').trim().split(/\s+/)
.map(function (x) { return x.replace(/!whitespace!/g, ' ') })
}
this.parse = function (input) {

6
examples/lambda.lisp Normal file
View File

@ -0,0 +1,6 @@
(
(echo (map '(echo 1 2 3) (4 5 6))
)