JSPM

  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q31648F
  • License MIT

A Lisp for Node.js

Package Exports

  • node-lisper
  • node-lisper/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-lisper) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Node Lisper

A Lisp for Node

(defvar hello "Hello World")
(log (concatenate hello "!!!"))
(defun fibonacci n
  (if (< n 2)
      n
      (+ (fibonacci (- n 1))
         (fibonacci (- n 2)))))

(fibonacci 10)
; 55
; Define reusable modules
(defun binary-search
        array target (do
  (loop defun search
        arr target start end (do
    (when (<= start end) (do
        (defvar index (floor (* (+ start end) 0.5)))
        (defvar current (get arr index))
        (if (= target current) target
          (if (> current target)
            (search arr target start (- index 1))
            (search arr target (+ index 1) end)))))))
   (search array target 0 (length array))))
(defvar is-odd (lambda x i o (= (mod x 2) 1)))
(defvar mult_2 (lambda x i o (* x 2)))
(defvar sum (lambda a x i o (+ a x)))
; Pipe the first to a series of composed functions
; (arg (arg .. ) (arg .. ) (ar . . . . ))
(go
  (Array 1 2 3 4 5 6 7 101)
  (remove is-odd)
  (map mult_2)
  (reduce sum 0))
(import std "range" "push" "factorial" "product-array" "reduce")
(defun factorial n
  (go
    (range 1 n)
    (product-array)))
(factorial 10)

Simple CLI usage - create main.js

import lisp from 'node-lisper'
lisp.cli()
"type": "module",
"scripts": {
  "lisp": "node index.js"
}

interpred

yarn

yarn lisp -file <filepath> -r

npm

npm run lisp -- -file <filepath> -r

or compile

yarn

yarn lisp -s <filepath lisp> -d <filepath js> -c

npm

npm run lisp -- -s <filepath lisp> -d <filepath js> -c

show help

yarn

yarn lisp -help

npm

npm run lisp -- -help

-------------------------------------
-help
-------------------------------------
-std              list std functions
-------------------------------------
-import           log import for std
-------------------------------------
-bake:std         bake std to an AST
-------------------------------------
-s                    prepare a file
-------------------------------------
-d               file to compile js
-------------------------------------
-c                    compile to js
-------------------------------------
-r                  interpret & run
-------------------------------------
-p      interpret & run with 0 deps
-------------------------------------
-repl    start Read Eval Print Loop
-------------------------------------

Parse, Interpred & Compile

import lisp from 'node-lisper'
lisp.parse('(+ 1 2)') // [[{  type: 'apply', value: '+' }, { type: 'atom', value: 1 }, { type: 'atom', value: 2 }]]
lisp.interpred('(+ 1 2)') // 3
lisp.compile('(+ 1 2)') // 3 but faster!
lisp.js(lisp.parse('(+ 1 2)')).program // (1 + 2); as js

Compiles to JavaScript

(import std "remove" "map" "reduce")
(go
  (Array 1 2 3 4 5 6 7 101)
  (remove (lambda x . . (= (mod x 2) 1)))
  (map (lambda x . . (* x 2)))
  (reduce (lambda a x . . (+ a x)) 0))
reduce(
  map(
    remove([1, 2, 3, 4, 5, 6, 7, 101], (x, _1, _2) => {
      return +(x % 2 === 1)
    }),
    (x, _1, _2) => {
      return x * 2
    }
  ),
  (a, x, _2, _3) => {
    return a + x
  },
  0
)
; Tail Call Optimization
(loop defun sum-below number sum (do
(if (= number 0) sum (sum-below (- number 1) (+ sum number)))))
(log (sum-below 10000 0))
var log = (msg) => { console.log(msg) return msg },
    tco = (fn) => (...args) => {
      let result = fn(...args)
      while (typeof result === 'function') result = result()
      return result
    }
var sumBelow, rec_32721849989891052
;(sumBelow = tco(
  (rec_32721849989891052 = (number, sum) => {
    return +(number === 0)
      ? sum
      : () => rec_32721849989891052(number - 1, sum + number)
  }),
  rec_32721849989891052
)),
  sumBelow
log(sumBelow(10000, 0))