From 677b03bc28330c172ec17ca5bacc42a3b5fbc666 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 18 Jul 2019 14:05:40 +0900 Subject: [PATCH] Added basics.lisp --- desktop/sources/scripts/lisp.js | 22 +++++++--------------- examples/basics.lisp | 9 +++++++++ 2 files changed, 16 insertions(+), 15 deletions(-) create mode 100644 examples/basics.lisp diff --git a/desktop/sources/scripts/lisp.js b/desktop/sources/scripts/lisp.js index 3a38a83..ddc4b97 100644 --- a/desktop/sources/scripts/lisp.js +++ b/desktop/sources/scripts/lisp.js @@ -33,29 +33,21 @@ function Lisp (input, lib) { }, def: function (input, context) { const identifier = input[1].value - const value = (input[2].type === TYPES.string) ? input[3] : input[2] - if (input[2].type === TYPES.string) { - // docstring - console.log(input[2].value) - } + const value = input[2].type === TYPES.string && input[3] ? input[3] : input[2] context.scope[identifier] = interpret(value, context) return value }, defn: function (input, context) { - const identifier = input[1].value - const argumentNames = (input[2].type === TYPES.string) ? input[3] : input[2] - const functionBody = (input[2].type === TYPES.string) ? input[4] : input[3] - if (input[2].type === TYPES.string) { - // docstring - console.log(input[2].value) - } - context.scope[identifier] = async function () { + const fnName = input[1].value + 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 () { const lambdaArguments = arguments - const lambdaScope = argumentNames.reduce(function (acc, x, i) { + const lambdaScope = fnParams.reduce(function (acc, x, i) { acc[x.value] = lambdaArguments[i] return acc }, {}) - return interpret(functionBody, new Context(lambdaScope, context)) + return interpret(fnBody, new Context(lambdaScope, context)) } }, lambda: function (input, context) { diff --git a/examples/basics.lisp b/examples/basics.lisp new file mode 100644 index 0000000..c1832ea --- /dev/null +++ b/examples/basics.lisp @@ -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))) \ No newline at end of file