A parser, interpretter, and compiler for a functional programming language with the same grammar as JSON.
Go to file
2024-03-17 10:19:50 -04:00
comb_parse.py Add in IR generation 2024-03-15 20:21:42 -04:00
compile.py Optimization: Remove redundant let expressions 2024-03-16 22:21:02 -04:00
emis_funky_funktions.py Clean up some type errors 2024-03-15 20:21:47 -04:00
example.json Add some more stuff to the example file 2024-03-15 20:21:44 -04:00
fibb.json Don't include fibb_helper in the produced bindings for the fibbonacci example 2024-03-15 20:21:48 -04:00
genir.py Perform match elimination at IR generation rather than codegen 2024-03-16 19:42:09 -04:00
grammar.py Start making a grammar then realize there's a builtin json parser 2024-03-15 20:21:41 -04:00
ir.py Make '$' variables globally unique 2024-03-17 10:12:02 -04:00
lex.py Add parse_, expand some doc tests 2024-03-15 20:21:41 -04:00
main.py Buggy typechecking 2024-03-15 20:21:43 -04:00
match_tree.py Clean up some type errors 2024-03-15 20:21:47 -04:00
opt.py Expand let elimination to work on some simple expression types regardless of use count 2024-03-17 10:19:50 -04:00
parse2.py Implement compilation 2024-03-15 20:21:46 -04:00
pattern.py Implement compilation 2024-03-15 20:21:46 -04:00
patterns.py Clean up some type errors 2024-03-15 20:21:47 -04:00
README.md Hide buggy ReplHole feature from README 2024-03-17 09:50:39 -04:00
silly_thing.py Don't use recursion in evaluate :( 2024-03-15 20:21:44 -04:00
types_.py Make function type rendering prettier 2024-03-15 20:21:44 -04:00

JSON as Functional Progamming Syntax

A parser, interpretter, and compiler for a functional programming language with the same grammar as JSON.

This project started as something of a joke: Could I use JavaScript Object Notation as a way of describing not just standard arrays and objects, but also functions and other data types? Bascially, could I turn JSON into a programming language which compiles to JavaScript?

The answer, it turns out, is yes!

Setup

This project has no dependencies, just Python3! That means all you have to do to run is install Python3 and clone the repository. Once you have the code, you can either run:

python3 main.py # Open a REPL with only the built-ins

OR

python3 main.py example.json # Evaluate some code and either print the results or open a REPL

OR

python3 compile.py fibb.json # Compile some code to JavaScript
python3 compile.py fibb.json | npx uglify-js -c evaluate -m # Compile some code to JavaScript and tidy it up a bit

Syntax Rundown

Functions / Lambdas

A function is defined using a standard JavaScript object with string keys. The simplest function you can write is the identity function:

{"x": "x"}

(equivalent JavaScript code:)

x => x

This function takes in one argument (called "x") and returns it unmodified. The "x" on the left defines the argument, and the "x" on the right is the return value. You'll notice we're also introducing the syntax for variables: Strings! While there are some times that strings don't function as variables (such as that first "x" on the left, which is an argument), most of the strings you'll see are variables. They refer to some previous value, and resolve to that. Here, the "x" on the right refers to the "x" on the left.

If you type this code into the repl, however, you'll see this output:

{"x": "x"}

Unchanged! In order to do something with this function, we'll need...

Function Application

Function application! (Meaning calling functions or passing them arguments). For this, we borrow JSON's array syntax. The first element of the array is the function being applied, and all things to the left are the arguments, which are passed in order from left to right.

Let's try applying our identity function to a number (new syntax! numbers are just JSON numbers!):

[{"x": "x"}, 4]

(equivalent JavaScript code:)

(x => x)(4)

Let's try putting this new expression into the REPL!

4

Now we get just 4! Our code worked! The four was returned unmodified.

This is a little bit boring though. We can start to make things more interesting by applying some of the built-in functions. One simple example is the "S" (or successor) function. Let's write a function which applies the S function twice:

{"x": ["S", ["S", "x"]]}

and now apply it:

[{"x": ["S", ["S", "x"]]}, 4]

(output:)

6

Nice!

Pattern Matching

Much like in other functional languages, all functions can perform case matching. By adding additional key/value pairs to the function (object), we can pattern match on different terms. All keys still need to be strings, but the contents of the string represent the pattern sub-language. For example, this function subtracts one from a number, unless that number is zero:

{"0": 0, "S x": "x"}

Note: The documentation for this section and the following sections are currently incomplete. While I aim to finish eventually, for now this is all there is. I encourage you to play around with the examples in example.json as well as on your own. If you're comfortable with Python, you can also poke around in the source code to find out how things work!

Let Bindings

["x", 3, "y", ["+", "x", 5], ["+", "x", "y"]]

Equivalent JavaScript:

{
	const x = 3;
	const y = x + 5;
	return x + y
}

Bind values in REPL

aka a let binding without an "in" clause

["addTwo", {"x": ["S", ["S", "x"]]}]

Multi-argument functions

{"x": {"y": ["+", "x", "y"]}}

Equivalent JavaScript:

x => y => x + y

(note: produces curried JS)

More language features remain undocumented :(