From e8bc9d0ddcf365fc9dede528b4084d56321ae0cb Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Mon, 15 Jul 2019 22:26:09 +0200 Subject: [PATCH] add docstrings --- desktop/sources/scripts/lisp.js | 24 ++++++++++++++++++------ examples/benchmark.lisp | 2 ++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/desktop/sources/scripts/lisp.js b/desktop/sources/scripts/lisp.js index a7c35af..c71c789 100644 --- a/desktop/sources/scripts/lisp.js +++ b/desktop/sources/scripts/lisp.js @@ -35,18 +35,30 @@ function Lisp (input, lib) { return interpret(input[2], letContext) }, def: function (input, context) { - context.scope[input[1].value] = interpret(input[2], context) - return input[2] + 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) + } + context.scope[identifier] = interpret(value, context) + return value }, defn: function(input, context) { - context.scope[input[1].value] = function() { + 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] = function() { const lambdaArguments = arguments - const lambdaScope = input[2].reduce(function (acc, x, i) { + const lambdaScope = argumentNames.reduce(function (acc, x, i) { acc[x.value] = lambdaArguments[i] return acc }, {}) - - return interpret(input[3], new Context(lambdaScope, context)) + return interpret(functionBody, new Context(lambdaScope, context)) } }, lambda: function (input, context) { diff --git a/examples/benchmark.lisp b/examples/benchmark.lisp index ba1265c..cfe1d21 100644 --- a/examples/benchmark.lisp +++ b/examples/benchmark.lisp @@ -48,6 +48,8 @@ (test "def - func" (addOne 4) 5) (defn addTwo (a) (add 2 a)) (test "defn" (addTwo 4) 6) + (defn mulTwo "multiplies by two" (a) (mul 2 a)) + (test "docstring" (mulTwo 4) 8) ; Generics