Dec 2020 Tweet
Prototype of a textual dataflow programming language
Many programmers want to design a programming language at some point. This is one of such attempts. Is my first try, and it might be uninformed and naive, but have to start somewhere :)
I was also inspired by visual dataflow environments like Pure Data, NoFlo, and Nodes. The thing that I don't like much about those, is using graphical elements to design programs. It works well in some cases, but I still like text editing more.
I wanted to explore an alternative, text based interface for dataflow programs. Such languages exist, for example:
I wanted to make something similar, but interactive (more like Observable), and with more lightweight and minimal syntax.
The main requirement for the design is simple and minimal syntax. Another requirement is easy JS interop, so that for any features that are not supported by the language, it's possible to just use inline JS code, or call JS functions.
Syntax is simple, and has only a few building blocks:
- top-level expressions, that are are a list of statements separated with an arrow (→). Literal, or output of a function is assigned to a variable that follows after an arrow.
- a statement can be:
- repeat(10), or repeat(10hz) — triggers execution of the expression number of times or periodically
- literals (numbers and strings are supported)
- builtin functions: log, push, filter, clone
- a function call (functions defined with fn keyword, or JS functions)
- a variable, local variables are just alphanumeric identifiers, global variables are wrapped in brackets
- inline JS code
- function definitions start with "fn" keyword and function name, and are followed by a list of statements
- JS functions definitions. can be placed inline, with similar to JS syntax
Simple example, "hello world"
Following interactive example shows usage of literals, variables and the log function. Here, a literal is assigned to a variable, and then log function is used for output.
Another simple example
Next example shows assigning a number to a variable, and repeating expressions.
Mult-agent simulation (flocking) example
For even more complex example, I selected multi-agent flocking simulation. Explanations of the "boids" flocking algorithm can be found on pages by Craig Reynolds and Conrad Parker. It is often used as a demo for interactive graphical applications and agent-based frameworks, for example:
- Agents.js (Julia) https://juliadynamics.github.io/Agents.jl/stable/examples/flock/
- Mesa (Python) https://github.com/projectmesa/mesa/tree/master/examples/boid_flockers
Compared to other implementations, code is seems to be more compact (~50 LOC vs >100 LOC). Another feature is that the main "gist" of the algorithm is outlined within first few dozen lines, and the rest of the code is within a set of utility functions.
I think that this style promotes describing structure of the algorithm in short concise way. Also, it encourages using short functional bits of code. But for now it's just a hypothesis, and to confirm it have to write more code based on the finished language implementation.