Skip to content

Repository files navigation

FunctionalScript

NPM Version

FunctionalScript is a safe, purely functional programming language and a strict subset of ECMAScript/JavaScript. It's inspired by

  • JSON and JSON5 as subsets of JavaScript values. JSON is also a subset of FunctionalScript values.
  • asm.JS (a precursor of WebAssembly), as a subset of JavaScript.
  • TypeScript, as a superset of JavaScript.

The FunctionalScript specification describes the language the compiler accepts today; features not implemented yet are in spec/todo/.

Learn more about

This repository is a monorepo and distributed under MIT.

Getting Started

Install FunctionalScript via npm:

npm install -g functionalscript

or run the CLI without installing it, with npx functionalscript <command>.

Compiling a module

A FunctionalScript module is already a valid JavaScript module, so nothing has to be compiled in order to run it. The compiler serves the other direction: it reads a module with every import resolved and writes it out in the language the output name declares. Two of those outputs evaluate the module and emit the data it exports; the rest write the graph it compiles to, which is how a module holding a function has an output at all.

m.f.js:

export default ["text"];

input.f.js:

import c from "./m.f.js";
const a = 1;
export default [a, a, c, { x: c }];

The output file extension picks the language, longest suffix first:

fjs compile input.f.js output.data.js        # DataJS, a JavaScript module
fjs compile input.f.js output.js             # FunctionalScript
fjs compile input.f.js output.json           # JSON
fjs compile input.f.js output.edag.data.js   # the program's EDAG, as DataJS
fjs compile input.f.js output.rs             # a generated nanvm-lib module

output.data.js and output.json are the value the module denotes; the other three are the graph it compiles to, each written a different way.

output.data.js is a DataJS document in normalized form. It preserves the object graph: c is one array referenced twice, so it stays shared and is hoisted into a const:

const $0=["text"];export default [1,1,$0,{"x":$0}];

output.js — any JavaScript name the narrower ones above do not claim — is a FunctionalScript module, written from the linked graph rather than from the value. For the module above, which denotes data, that is the same text. For a module holding a function it is the only output that holds one, a value having none — export default (...a) => a; compiles to:

export default (...$a)=>$a;

output.json is a tree, so the compiler refuses a value JSON cannot spell — a shared value, bigint, undefined, NaN, Infinity — rather than write a file that reads back as something else. For the module above it refuses:

output.json - error: no JSON spelling for a shared node

output.edag.data.js is the program compiled to an EDAG, the graph of what it computes rather than its value, written as a DataJS document with the same sharing kept:

const $0=["[]",["text"]];export default ["[]",[1,1,$0,["{}",[[":","x",$0]]]]];

output.rs is that graph printed against the nanvm-lib API instead — a generated Rust module, fjs/fsc/rust.

An extension naming none of these languages is refused, rather than written in one the name does not declare.

With m.f.js exporting the string "text" instead — a leaf, which is never shared — no output has a const to hoist, and output.json is:

[1,1,"text",{"x":"text"}]

The compiler currently accepts import statements, const declarations, data expressions (objects, arrays, strings, numbers, bigint, booleans, null, undefined), property access on a name — a.b, a[0], an own property and never the prototype chain — and functions: one rest parameter, (...a) => …, whose body reaches nothing outside itself, so a reference to a const, an import or an enclosing function's parameter is refused as a capture; calls, f(a) and o.m(a), whose arguments are the list an array holds — a module with a call in it compiles to output.edag.data.js, the other outputs having no spelling for one yet; unary -, the one operator, which binds looser than a step, so -1 .x is -(1 .x) as JavaScript reads it; and a group, ( … ), which denotes the value in it, so (...a) => ({ x: 1 }) is how a function returns an object. See fjs/fsc/README.md for the compiler, the data language it accepts today, and its roadmap.

The fjs CLI

Command Description Documentation
fjs test Run the FunctionalScript test suite fjs/emergent_testing
fjs compile Compile a FunctionalScript module to JavaScript or JSON fjs/fsc
fjs cas Content-addressable storage (add, get, list) fjs/cas/README.md
fjs mcp MCP server over stdio, exposing the CAS and Evo as tools fjs/mcp/README.md
fjs ci Generate the GitHub Actions CI and npm publishing workflows fjs/ci/README.md
fjs web Serve a directory over HTTP fjs/web/README.md
fjs run Run a FunctionalScript module as a Node program fjs/README.md
fjs help Print the available commands fjs/README.md

Run fjs help to print the available commands, or see fjs/README.md for the full CLI reference. Commands also accept short aliases, which fjs help prints; the documentation spells them out instead.

Vision

We aim to create a safe, cross-platform programming language that can work in any JS platform without any build step. There are thousands of programming languages, and we don't want to create another one that others must learn. Instead, we take the opposite approach: we remove everything that makes the most popular and cross-platform language unsafe, insecure, or less portable.

Applications

FunctionalScript code can be used:

  • safely in any JavaScript/TypeScript application or library;
  • as a JSON with expressions, see DJS;
  • as a query language;
  • as a smart contract programming language in DeFi.

Design Principles

In FunctionalScript:

  • Any module is a valid JavaScript module. No additional build steps are required.
  • Code should not have side-effects. Any JavaScript statement, expression, or function that has a side effect is not allowed in FunctionalScript. There are no exceptions to this rule, such as unsafe code, which can be found in Rust, C#, and other languages.
  • A module can depend only on another FunctionalScript module.
  • It also has no standard library. Only a safe subset of standard JavaScript API can be used without referencing other modules.

Sponsors

Sponsor this project

Used by

Contributors

Languages