Improved helpers
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
function Lisp (lib = {}) {
|
||||
// In the real world, it didn’t matter if I was there or not.
|
||||
// When I realized that, I was no longer afraid of losing my body.
|
||||
|
||||
function Lain (lib = {}) {
|
||||
const TYPES = { identifier: 0, number: 1, string: 2, bool: 3, symbol: 4 }
|
||||
|
||||
const Context = function (scope, parent) {
|
||||
@@ -24,16 +27,22 @@ function Lisp (lib = {}) {
|
||||
return interpret(input[2], letContext)
|
||||
},
|
||||
def: function (input, context) {
|
||||
const identifier = input[1].value
|
||||
const value = input[2].type === TYPES.string && input[3] ? input[3] : input[2]
|
||||
context.scope[identifier] = interpret(value, context)
|
||||
return value
|
||||
if (input.length !== 3) { console.warn('Lain', 'Invalid definition.'); return }
|
||||
const identifier = input[1].host ? input[1].host : input[1].value
|
||||
if (input[1].host) {
|
||||
if (!context.scope[identifier]) { context.scope[identifier] = {} }
|
||||
context.scope[identifier][input[1].value] = interpret(input[2], context)
|
||||
return context.scope[identifier][input[1].value]
|
||||
}
|
||||
context.scope[identifier] = interpret(input[2], context)
|
||||
return context.scope[identifier]
|
||||
},
|
||||
defn: function (input, context) {
|
||||
const fnName = input[1].value
|
||||
const identifier = input[1].value
|
||||
if (context.scope[identifier]) { console.warn('Lain', `Redefining function: ${identifier}`) }
|
||||
const fnParams = input[2].type === TYPES.string && input[3] ? input[3] : input[2]
|
||||
const fnBody = input[2].type === TYPES.string && input[4] ? input[4] : input[3]
|
||||
context.scope[fnName] = async function () {
|
||||
context.scope[identifier] = function () {
|
||||
const lambdaArguments = arguments
|
||||
const lambdaScope = fnParams.reduce(function (acc, x, i) {
|
||||
acc[x.value] = lambdaArguments[i]
|
||||
@@ -42,8 +51,8 @@ function Lisp (lib = {}) {
|
||||
return interpret(fnBody, new Context(lambdaScope, context))
|
||||
}
|
||||
},
|
||||
lambda: function (input, context) {
|
||||
return async function () {
|
||||
λ: function (input, context) {
|
||||
return function () {
|
||||
const lambdaArguments = arguments
|
||||
const lambdaScope = input[1].reduce(function (acc, x, i) {
|
||||
acc[x.value] = lambdaArguments[i]
|
||||
@@ -52,36 +61,12 @@ function Lisp (lib = {}) {
|
||||
return interpret(input[2], new Context(lambdaScope, context))
|
||||
}
|
||||
},
|
||||
if: async function (input, context) {
|
||||
if (await interpret(input[1], context)) {
|
||||
return interpret(input[2], context)
|
||||
}
|
||||
return input[3] ? interpret(input[3], context) : []
|
||||
},
|
||||
__fn: function (input, context) {
|
||||
return async function () {
|
||||
const lambdaArguments = arguments
|
||||
const keys = [...new Set(input.slice(2).flat(100).filter(i =>
|
||||
i.type === TYPES.identifier &&
|
||||
i.value[0] === '%'
|
||||
).map(x => x.value).sort())]
|
||||
const lambdaScope = keys.reduce(function (acc, x, i) {
|
||||
acc[x] = lambdaArguments[i]
|
||||
return acc
|
||||
}, {})
|
||||
return interpret(input.slice(1), new Context(lambdaScope, context))
|
||||
}
|
||||
},
|
||||
__obj: async function (input, context) {
|
||||
const obj = {}
|
||||
for (let i = 1; i < input.length; i += 2) {
|
||||
obj[await interpret(input[i], context)] = await interpret(input[i + 1], context)
|
||||
}
|
||||
return obj
|
||||
if: function (input, context) {
|
||||
return interpret(input[1], context) ? interpret(input[2], context) : input[3] ? interpret(input[3], context) : []
|
||||
}
|
||||
}
|
||||
|
||||
const interpretList = async function (input, context) {
|
||||
const interpretList = function (input, context) {
|
||||
if (input.length > 0 && input[0].value in special) {
|
||||
return special[input[0].value](input, context)
|
||||
}
|
||||
@@ -89,7 +74,7 @@ function Lisp (lib = {}) {
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
if (input[i].type === TYPES.symbol) {
|
||||
if (input[i].host) {
|
||||
const host = await context.get(input[i].host)
|
||||
const host = context.get(input[i].host)
|
||||
if (host) {
|
||||
list.push(host[input[i].value])
|
||||
}
|
||||
@@ -97,14 +82,14 @@ function Lisp (lib = {}) {
|
||||
list.push(obj => obj[input[i].value])
|
||||
}
|
||||
} else {
|
||||
list.push(await interpret(input[i], context))
|
||||
list.push(interpret(input[i], context))
|
||||
}
|
||||
}
|
||||
return list[0] instanceof Function ? list[0].apply(undefined, list.slice(1)) : list
|
||||
}
|
||||
|
||||
const interpret = async function (input, context) {
|
||||
if (!input) { console.warn('Lisp', 'error', context.scope); return null }
|
||||
const interpret = function (input, context) {
|
||||
if (!input) { console.warn('Lain', context.scope); return null }
|
||||
if (context === undefined) {
|
||||
return interpret(input, new Context(lib))
|
||||
} else if (input instanceof Array) {
|
||||
@@ -137,18 +122,10 @@ function Lisp (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 === '{') {
|
||||
input.unshift('__obj')
|
||||
list.push(parenthesize(input, []))
|
||||
return parenthesize(input, list)
|
||||
} else if (token === '(') {
|
||||
list.push(parenthesize(input, []))
|
||||
return parenthesize(input, list)
|
||||
} else if (token === ')' || token === '}') {
|
||||
} else if (token === ')') {
|
||||
return list
|
||||
} else {
|
||||
return parenthesize(input, list.concat(categorize(token)))
|
||||
@@ -156,25 +133,13 @@ function Lisp (lib = {}) {
|
||||
}
|
||||
|
||||
const tokenize = function (input) {
|
||||
const i = input.replace(/^;.*\n?/gm, '').replace(/λ /g, 'lambda ').split('"')
|
||||
const i = input.replace(/^[\s]*;.*\n?/gm, '').split('"')
|
||||
return i.map(function (x, i) {
|
||||
return i % 2 === 0
|
||||
? x.replace(/\(/g, ' ( ')
|
||||
.replace(/\)/g, ' ) ')
|
||||
.replace(/' \( /g, ' \'( ') // '()
|
||||
.replace(/\{/g, ' { ') // {}
|
||||
.replace(/\}/g, ' } ') // {}
|
||||
: x.replace(/ /g, '!whitespace!')
|
||||
})
|
||||
.join('"').trim().split(/\s+/)
|
||||
.map(function (x) { return x.replace(/!whitespace!/g, ' ') })
|
||||
return i % 2 === 0 ? x.replace(/\(/g, ' ( ').replace(/\)/g, ' ) ') : x.replace(/ /g, '!ws!')
|
||||
}).join('"').trim().split(/\s+/).map(function (x) { return x.replace(/!ws!/g, ' ') })
|
||||
}
|
||||
|
||||
this.parse = function (input) {
|
||||
return parenthesize(tokenize(input))
|
||||
}
|
||||
|
||||
this.run = async function (input) {
|
||||
return interpret(this.parse(`((def theme (get-theme))(def frame (get-frame))(${input}))`))
|
||||
this.run = (input) => {
|
||||
return interpret(parenthesize(tokenize(input)))
|
||||
}
|
||||
}
|
||||
@@ -86,7 +86,7 @@ function Theme (client) {
|
||||
this.active[key] = hex
|
||||
}
|
||||
|
||||
this.read = (key) => {
|
||||
this.get = (key) => {
|
||||
return this.active[key]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user