add (:symbol {obj}) syntax
This commit is contained in:
parent
df72684510
commit
7c7feb9fb8
@ -4,7 +4,7 @@ function Lisp (lib = {}, includes = []) {
|
|||||||
const path = require('path')
|
const path = require('path')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
|
|
||||||
const TYPES = { identifier: 0, number: 1, string: 2, bool: 3 }
|
const TYPES = { identifier: 0, number: 1, string: 2, bool: 3, symbol: 4 }
|
||||||
|
|
||||||
const Context = function (scope, parent) {
|
const Context = function (scope, parent) {
|
||||||
this.scope = scope
|
this.scope = scope
|
||||||
@ -95,7 +95,11 @@ function Lisp (lib = {}, includes = []) {
|
|||||||
}
|
}
|
||||||
const list = []
|
const list = []
|
||||||
for (let i = 0; i < input.length; i++) {
|
for (let i = 0; i < input.length; i++) {
|
||||||
list.push(await interpret(input[i], context))
|
if (i === 0 && input[i].type === TYPES.symbol && input.length === 2) {
|
||||||
|
list.push(obj => obj[input[i].value])
|
||||||
|
} else {
|
||||||
|
list.push(await interpret(input[i], context))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return list[0] instanceof Function ? list[0].apply(undefined, list.slice(1)) : list
|
return list[0] instanceof Function ? list[0].apply(undefined, list.slice(1)) : list
|
||||||
}
|
}
|
||||||
@ -109,7 +113,10 @@ function Lisp (lib = {}, includes = []) {
|
|||||||
return interpretList(input, context)
|
return interpretList(input, context)
|
||||||
} else if (input.type === TYPES.identifier) {
|
} else if (input.type === TYPES.identifier) {
|
||||||
return context.get(input.value)
|
return context.get(input.value)
|
||||||
} else if (input.type === TYPES.number || input.type === TYPES.string || input.type === TYPES.bool) {
|
} else if (
|
||||||
|
input.type === TYPES.number || input.type === TYPES.symbol
|
||||||
|
|| input.type === TYPES.string || input.type === TYPES.bool
|
||||||
|
) {
|
||||||
return input.value
|
return input.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,7 +127,7 @@ function Lisp (lib = {}, includes = []) {
|
|||||||
} else if (input[0] === '"' && input.slice(-1) === '"') {
|
} else if (input[0] === '"' && input.slice(-1) === '"') {
|
||||||
return { type: TYPES.string, value: input.slice(1, -1) }
|
return { type: TYPES.string, value: input.slice(1, -1) }
|
||||||
} else if (input[0] === ':') {
|
} else if (input[0] === ':') {
|
||||||
return { type: TYPES.string, value: input.slice(1) }
|
return { type: TYPES.symbol, value: input.slice(1) }
|
||||||
} else if (input === 'true' || input === 'false') {
|
} else if (input === 'true' || input === 'false') {
|
||||||
return { type: TYPES.bool, value: input === 'true' }
|
return { type: TYPES.bool, value: input === 'true' }
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user