This is a custom toy language that I put together as an exercise. The interpreter uses a recursive descent parser and runs in Node.js.
The language is entirely contained in interpreter.js
. You can interpret a file by running something like this: node interpreter.js path/to/file.s
The only construct the language supports is the function call. It uses postfix notation. Arguments are sandwiched between :
and ;
. If a function can take multiple arguments they are separated with a ,
.
this:
: 'Hello, World!' ; print
writes this to standard out:
Hello, World!
Since I wrote the interpreter in JavaScript, the language inherits the little typing it has from JavaScript. Admittedly, the typing is next to non-existant, but the laguage has at least these three types: 'number', 'string', and 'function'. I also added a 'list' type, which is a little one off. The list type is represented internally as a JavaScript array. These types are enforced through cryptic runtime error messages emanating from the interpreter's JavaScript internals.
- Functional
- Curried Functions
- Functional Composition
- First Class functions
- Reverse Polish Notation
- Has
cons
- Side effect free, not used in production anywhere.
- Gluten free*
+
- Adds two numbers - binary, curried*
- Multplies two numbers - binary, curried/
- (interger) Divides two numbers - binary, curried-
- Subtracts two numbers - binary, curried%
- Mods two numbers - binary, curried=
- Tests if two numbers are equal. Returns 1 if they are else returns 0. - binary, curriedprint
- Prints one arg - unarycompose
- Composes two functions - binary, uncurriedconcat
- Contactenates two args that it converts to strings - binary, curriedcons
- Pass an item to return a list. Pass a list and call the resulting function with an item to add it to the list - binary or unary, curriedwrap
- Takes the function to wrap and the list of args. Usecons
to build the list. binary, curriedif
- 0 counts as false, everything else counts as true.if
function takesfunction called on true
,function called on false
,number to test
. Usewrap
to create the "true" and "false" functions. - ternary, curriedlist
- Cheaters way to make a list. Takes unlimited arguments. Returns its arguments as a list. - ?multinary, uncurriedrange
- creates a list of numbers within a given range between the two arguments. - binary, curriedreduce
- iterates through a list accumulating a result. First argument is a function that takes the previous (accumulator) element, and the current element to iterate. Second argument is the list to reduce - binary, curriedmap
- iterates through a list and returns a new list where the elements are transformed by a function.map
's first argument is the function to do the transformation (which is called on each element), and the second argument is the list to map over - binary, curried
Clone the repo and then run:
npm install
After that you can run programs like this: node interpreter.js path/to/file.s
Examples are stored under examples
you can run the hello world example like this:
node interpreter.js examples/01_hello_world.s
examples/01_hello_world.s
examples/02_add_and_concat.s
examples/03_math.s
examples/04_compose.s
examples/05_cons.s
examples/06_wrap.s
examples/07_if.s
examples/08_list.s
* Tests below FDA maximum threshold of 20ppm for gluten free labeling.