Added basics.lisp

This commit is contained in:
Devine Lu Linvega 2019-07-18 14:05:40 +09:00
parent 8037bddbf7
commit 677b03bc28
2 changed files with 16 additions and 15 deletions

View File

@ -33,29 +33,21 @@ function Lisp (input, lib) {
}, },
def: function (input, context) { def: function (input, context) {
const identifier = input[1].value const identifier = input[1].value
const value = (input[2].type === TYPES.string) ? input[3] : input[2] const value = input[2].type === TYPES.string && input[3] ? input[3] : input[2]
if (input[2].type === TYPES.string) {
// docstring
console.log(input[2].value)
}
context.scope[identifier] = interpret(value, context) context.scope[identifier] = interpret(value, context)
return value return value
}, },
defn: function (input, context) { defn: function (input, context) {
const identifier = input[1].value const fnName = input[1].value
const argumentNames = (input[2].type === TYPES.string) ? input[3] : input[2] const fnParams = input[2].type === TYPES.string && input[3] ? input[3] : input[2]
const functionBody = (input[2].type === TYPES.string) ? input[4] : input[3] const fnBody = input[2].type === TYPES.string && input[4] ? input[4] : input[3]
if (input[2].type === TYPES.string) { context.scope[fnName] = async function () {
// docstring
console.log(input[2].value)
}
context.scope[identifier] = async function () {
const lambdaArguments = arguments const lambdaArguments = arguments
const lambdaScope = argumentNames.reduce(function (acc, x, i) { const lambdaScope = fnParams.reduce(function (acc, x, i) {
acc[x.value] = lambdaArguments[i] acc[x.value] = lambdaArguments[i]
return acc return acc
}, {}) }, {})
return interpret(functionBody, new Context(lambdaScope, context)) return interpret(fnBody, new Context(lambdaScope, context))
} }
}, },
lambda: function (input, context) { lambda: function (input, context) {

9
examples/basics.lisp Normal file
View File

@ -0,0 +1,9 @@
; basics
(
; define a variable
(def a 25)
(echo a)
; define a function
(defn add-two (a) (add 2 a))
(echo (add-two 4)))