npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mojir/lits

v1.2.2-alpha.2

Published

Lits is a Lisp dialect implemented in TypeScript

Downloads

368

Readme

Lits

Lits is a programming language and REPL (Read-Eval-Print Loop) that allows you to evaluate Lisp expressions. It provides a playground for experimenting with Lits and an API for integrating Lits into your own projects.

Lits is a Lisp dialect implemented in TypeScript, drawing heavy inspiration from Clojure. Most core functions have been ported to Lits, ensuring a robust and familiar experience for Clojure users.

  • Dependencies: No third party dependencies.
  • Immutability: All datatypes in Lits are immutable.
  • Pure Functions: Functions are pure by default. Functions with side effects have names ending in an exclamation mark (!), such as write! or rand!
  • Type Mapping: All datatypes in Lits map directly to JavaScript types.
  • Evaluation: Lits does not support lazy evaluation.
  • Macros:Macros are not supported in Lits.
  • Keyword Symbols: There are no keyword symbols. The notation :foo is simply shorthand for the string "foo"
  • Scoping: Lits uses dynamic scoping not lexical scoping

Documentation

You can find the Lits playground here. The playground allows you to interactively write and evaluate Lits expressions.

Installation

To install Lits globally, run the following command:

npm i -g @mojir/lits

Repl usage

Initiate the Lits REPL in a terminal by typing lits. If Lits hasn't been installed globally, you can use npx lits instead.

  • Tab completion
  • History stored on file
$ lits
Type "`help" for more information.
> (+ 7 4)
11
> (let ((day (* 24 60 60 1000))) (* 7 day)) ; Ever wondered how many milliseconds there are in a week?
604800000
$ lits --help
Usage: lits [options]

Options:
  -c, --context=...               Context as a JSON string
  -C, --context-file=...          Context file (.json file)
  -e, --eval=...                  Evaluate Lits expression
  -f, --file=...                  Evaluate .lits file
  -p, --test-pattern=...          Test name pattern, used together with --test
  -t, --test=...                  Test .test.lits file
  --help                          Show this help
  --version                       Print lits version
$ lits -e "(/ 81 9)"
9

API

Install api

npm i @mojir/lits

How to use?

import { Lits } from '@mojir/lits'

const lits = new Lits()
lits.run("(+ 1 2 3 4)"); // returns 10

Tokenization and Parsing

Lits provides two important functions for working with Lisp expressions: lits.tokenize and lits.parse.

lits.tokenize

The lits.tokenize function takes a string as input and returns a TokenStream. Tokens are the individual components of a Lisp expression, such as parentheses, symbols, numbers, and strings. Here's an example usage:

const lits = new Lits()
const expression = "(+ 1 2)";
const tokens = lits.tokenize(expression);
console.log(tokens);
// Output: {
  "tokens": [
    {
      "t": 101,
      "v": "("
    },
    {
      "t": 103,
      "v": "+"
    },
    {
      "t": 102,
      "v": "1"
    },
    {
      "t": 102,
      "v": "2"
    },
    {
      "t": 101,
      "v": ")"
    }
  ]
}

Lits.parse

The lits.parse function accepts a TokenStream as an argument and returns an Abstract Syntax Tree (AST). By compiling Lits expressions into an AST, you can significantly enhance the evaluation speed, achieving approximately a tenfold improvement.

The AST is represented as a JSON document, which facilitates straightforward serialization.

To evaluate an AST, utilize the lits.evaluate function.

const lits = new Lits()
const expression = "(+ 1 2)";
const tokens = lits.tokenize(expression);
const ast = lits.parse(tokens)
console.log(tokens);
// Output: {
  "b": [
    {
      "t": 203,
      "n": "+",
      "p": [
        {
          "t": 201,
          "v": 1
        },
        {
          "t": 201,
          "v": 2
        }
      ]
    }
  ]
}