add docstrings

This commit is contained in:
Quentin Leonetti 2019-07-15 22:26:09 +02:00
parent 2e4dcf08d8
commit e8bc9d0ddc
2 changed files with 20 additions and 6 deletions

View File

@ -35,18 +35,30 @@ function Lisp (input, lib) {
return interpret(input[2], letContext) return interpret(input[2], letContext)
}, },
def: function (input, context) { def: function (input, context) {
context.scope[input[1].value] = interpret(input[2], context) const identifier = input[1].value
return input[2] 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) { 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 lambdaArguments = arguments
const lambdaScope = input[2].reduce(function (acc, x, i) { const lambdaScope = argumentNames.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(input[3], new Context(lambdaScope, context))
} }
}, },
lambda: function (input, context) { lambda: function (input, context) {

View File

@ -48,6 +48,8 @@
(test "def - func" (addOne 4) 5) (test "def - func" (addOne 4) 5)
(defn addTwo (a) (add 2 a)) (defn addTwo (a) (add 2 a))
(test "defn" (addTwo 4) 6) (test "defn" (addTwo 4) 6)
(defn mulTwo "multiplies by two" (a) (mul 2 a))
(test "docstring" (mulTwo 4) 8)
; Generics ; Generics