Skip to content

cicada-lang/sexp

Repository files navigation

S-expression

Syntax without representation is tyranny.

-- Sussman's mantra

An implementation of S-expression in JavaScript.

Installation

npm i @cicada-lang/sexp

Examples

Parsing lambda calculus expressions:

<exp> = <variable>
      | (lambda (<name> ...) <exp>)
      | (<exp> <exp> ...)

Example from from @cicada-lang/lambda.sexp:

import { cons, match, matchList, matchSymbol, Sexp, v } from "@cicada-lang/sexp"
import { Exp } from "../exp"
import * as Exps from "../exps"

export function matchExp(sexp: Sexp): Exp {
  return match<Exp>(sexp, [
    [
      ["lambda", v("names"), v("exp")],
      ({ names, exp }) =>
        matchList(names, matchSymbol).reduceRight(
          (fn, name) => new Exps.Fn(name, fn),
          matchExp(exp),
        ),
    ],
    [
      cons(v("target"), v("args")),
      ({ target, args }) =>
        matchList(args, matchExp).reduce(
          (result, arg) => new Exps.Ap(result, arg),
          matchExp(target),
        ),
    ],
    [v("name"), ({ name }) => new Exps.Var(matchSymbol(name))],
  ])
}

Development

npm install      # Install dependencies
npm run build    # Compile `src/` to `lib/`
npm run format   # Format the code
npm run test     # Run test

Contributions

To make a contribution, fork this project and create a pull request.

Please read the STYLE-GUIDE.md before you change the code.

Remember to add yourself to AUTHORS. Your line belongs to you, you can write a little introduction to yourself but not too long.

License

GPLv3