add (:symbol {obj}) syntax

This commit is contained in:
Quentin Leonetti 2019-07-25 18:09:41 +02:00
parent df72684510
commit 7c7feb9fb8

View File

@ -4,7 +4,7 @@ function Lisp (lib = {}, includes = []) {
const path = require('path')
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) {
this.scope = scope
@ -95,7 +95,11 @@ function Lisp (lib = {}, includes = []) {
}
const list = []
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
}
@ -109,7 +113,10 @@ function Lisp (lib = {}, includes = []) {
return interpretList(input, context)
} else if (input.type === TYPES.identifier) {
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
}
}
@ -120,7 +127,7 @@ function Lisp (lib = {}, includes = []) {
} else if (input[0] === '"' && input.slice(-1) === '"') {
return { type: TYPES.string, value: input.slice(1, -1) }
} 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') {
return { type: TYPES.bool, value: input === 'true' }
} else {