235 points · by kaycebasques · 2y ago · 133 comments
It starts innocently enough, you just have a JSON that has some basic functionality. Then you decide it would be cool to nest functionality because there's no reason not to, so you build a recursive parser. Then you think it'd be neat to be able to add some arguments to the recursive stuff, because then you can more easily parameterize the JSON. Then you realize it might be nice to assign names to these things as you call them, you implement that, and now you're treating JSON as an AST and you're stuck with maintaining an interpreter, and life is pain.
Switched to dynamically compiled C# eventually.
So glad you finished with this. Right now I’m working with a guy who wants to write an interpreter…
It is an incredibly fun side project though.
A whole lot of the "parse, don't validate" ethos is potentially about handling incoming data with complicated formats by writing a minimalist interpreter that reads the input and outputs either guaranteed-valid domain objects or error messages. Most the time you can get away with just a parser, but one occasionally runs into more complex formats that are easier to handle if you think of them as a declarative language that you handle using general-purpose interpreter implementation patterns. And it's just not a wheel worth reinventing.
The "very clever JSON" example the parent poster gives isn't particularly farfetched. The description reminds me of, for example, every Jetbrains REST API I've ever had to interact with. Also the data file formats for some commercial applications I've had to interact with. I don't like formats like this, but when I'm stuck with them, choosing the most maintainable option from the choices I've been left with is not a code smell; it's just sound engineering.
Stuff like that works out great if you have a static set of features that will never change and you can at one point say "it's done", and won't have to touch it again.
Problem is that features are almost never static across their lifetime, and some poor sucker has to modify it at one point.
This is why I have a separate config language and code language.
My config language is essentially JSON with newline separators and a first-class binary type (base64). I added little else.
When I get the temptation to add code to it, I just pull out my other, general-purpose language instead.
Imo, users asking for stuff like loops is a signal that they want a real language and not a bolted-on DSL. I've seen far too many cases of "extending a DSL until it's a full-flegdged but awkward and horrible language".
Ansible's YAML and Terraform's are my prime examples. Both have grown into basically full languages featuring imports, for loops, etc, and both suck to use because of how awkward reaching for them is. I don't want to have to remember Ansible's bastardized, YAML-encoded for loop syntax, just let me use a Python for loop.
The only question in my mind is whether our common config langs should be Turing-complete (e.g., Nix, Nickel, Jsonnet, Pkl) or not (e.g., HCL, CUE, Starlark, Dhall), which I think can only be determined through experience over the long term.
Data and execution are two separate things and should remain as separate as possible.
The reason is that once you mix data and execution, suddenly you don't know what your data is until you execute it. Then you can only deal with it in the context of whatever tools you write and you can never just look at it straight.
On the other hand now execution depends on lots of data and is no longer modular or general/generic and so becomes a one off solution somewhere.
At some point everyone has the "what if" idea and hopefully it only burns them instead of lots of other people through poor design.
[0]: https://github.com/sharpobject/yisim/blob/master/swogi.json
[1]: https://store.steampowered.com/app/1948800/Yi_Xian_The_Culti...
I played with `eval`, got lost in codegen: O(AB), maybe?
- you write some piece of software
- then you add a straightforward configuration language
- then you add variables because you don't want too much copy/paste
- then you add if statements to allow conditional configuration
- then you add loops because you are sick of seeing configuration that consists of unrolled loops
- ...
At some point your configuration system is Turing complete, so could be considered a programming language. However it was never designed as such, so it is not a horrible one
TeX would be a good example of this. Ansible playbooks are (I believe) Turing complete, and YAML itself has such a huge spec, that if it isn't Turning complete and/or self aware already, I can't imagine it'll be long before it is
Maybe it's true failing though was stopping short of becoming the OS.
Otherwise you could "boot to" any terminal app.
Nano? OS. Cat? OS. Echo? Believe it or not, OS. Good grief.
I've always taken this to heart, but not necessarily with programming languages. Any piece of software that helps run my business that I can reasonably make and maintain myself, I do. I build my own CI/CD app, orchestration/deployment tool, task planner, bug tracker, release note editing & publishing tools, blog editor, logstash/viewer for exceptions, etc.
Does building (and especially maintaining!) all of these tools take up a lot of time, and distract me from the primary business of building the software that I actually sell? Sure, of course it does. But it also keeps me fresh and forces me to expand the scope of ideas that I regularly work with, and keeps me from becoming "that guy who makes that one app" and who isn't capable of moving outside of his comfort zone.
And while that doesn't (yet) extend to building my own tools in my own languages, it certainly does extend to writing my own DSLs for things like configuration management or infrastructure. My tools may be homerolled and second-rate, but they're mine (dammit!) and -- this part is important -- no one can take them away from me or catch me out with a licensing rug-pull.
Make your initial grammar SUPER simple. Like, don't go guns blazing and try to add all these cool features you saw in another language or always thought about. Start stupid, stupid simple, get that working, then build on top of it.
It's simple to lex and parse into an abstract syntax tree, so you can get on with exploring more interesting aspects of programming beyond the mere syntax. (Not to say that there aren't interesting aspects of grammar and innovative syntax, but those can probably be explored later on as macros.)
Last time I created a toy language, I implemented a C-like infix syntax but still used a Lisp evaluator at its core from a previous project.
There are some "cheats" for this, tools like ANTLR etc. that are good at generating parsers from a particular grammar. But of course, I think a beginner should try to do this on their own to get a feel for it.
Personally, for me, I do find writing parsers a little tedious and not the most fun part of making a language.
I would go further and say don't write a parser at first, unless what's novel and interesting about your language is it's syntax. Use the configuration language of your choice (like TOML or YAML) to write your ASTs directly, so you can focus on writing the runtime/backend and playing with the semantics of your language (where the novelty probably is).
When you feel it's the appropriate time, you can circle back to the frontend and implement a parser. But if you're writing a DSL, a config language may well be good enough. An additional bonus is that your config language syntax will make it easier to write certain unit tests.
I've had several experiments in writing a new language, and the first few times I got completely bogged down in parsing. I learned a lot about parsing, which is great, but it made it difficult for me to get at the meat of a project.
Lots of things come to light there. Lots of recursion/fun with stacks, operator precedence, the type system, parameter passing. Pretty much a good solid chunk of language is wrapped up in expressions.
Get expressions working, and the rest starts to readily fall into place.
jank is currently part of a mentorship program, too, so you can join (for sree) and get mentored by me on C++, compiler dev, and Clojure runtime internals.
1. https://jank-lang.org/ 2. https://clojureverse.org/t/announcing-the-scicloj-open-sourc...
I can't find the reference right now though.
Might be a good Clojure 2.0 thing.
It's really fun. Brainfuck specifically is great because there's a lot to optimize with only 6 total operations. (An example, multiplication has to be done as repeated addition in a loop, make a multiply AST node! [1]) and you could knock out a (BF => AST => Anything you want) compiler in an afternoon!
Bonus, there's a lot of really impressive brainfuck scripts out there. Nothing compares to seeing your compiler take in some non-sense ascii, and spit out an application that draws the mandlebrot fractal.
[0] https://esolangs.org/wiki/Brainfuck
[1] https://github.com/graypegg/unfuck/blob/master/src/optimiser...
One thing I'd like to add is that even though you can totally write your own parser, it's an absolute joy to use Tree-sitter:
https://tree-sitter.github.io/tree-sitter
I plug it every time I get a chance. It makes refactoring your grammar incredibly easy, and lets you just focus on your syntax.
It's a language for the domain of writing and verifying formal proofs. Basically, I didn't enjoy the experience of working with the couple of proof assistants I tried, so I'm doing my own thing. My objective is to create a language where I can document "everything I know" about math, if for no other reason than to prove to myself that I know those things, and to return to that knowledge if it ever slips away.
It's so much fun!
This was before async/generators were added to JS and callback hell was quite real. I wanted to shape it in the way I’d learned to program in Visual Basic. Very human readable. The result is no longer useful, but it was a fun goal to have the compiler compile itself.
The metalanguage: https://dogma-lang.org/
It's even got a syntax highlighter: https://marketplace.visualstudio.com/items?itemName=ksteneru...
The binary format I wanted to describe: https://github.com/kstenerud/concise-encoding/blob/master/cb...
A year later (this summer) I used Crumb to land my first job at a pretty cool startup! The payoff was way more than I could have ever expected.
(https://ratfactor.com/forth/the_programming_language_that_wr... is a great read, btw.)
At my current rate I'll know everything and be ready to get started on the day I die.
But my feature wishlist is
First class functions, Garbage collection, Transparent parallelism, Explicit parallelism, Type inference with strong type consistency, Dynamicly typed by annotation, Operator overloading (every language should either have vector/matrix operators or the ability to build them)
And a few more that I can't recall just now. I've made it hard for myself.
Failing that maybe just a hacked JavaScript without implicit type conversion between objects/strings etc. (the source of most "Wat?"), frozen array tuples, operator overloading. Implied "this." on identifiers defined and used inside class definitions.
List making, planning, routines, and pretty much anything that follows someone saying "You just need to ..." Doesn't work for me, or for any of the other ADHD sufferers I know of.
Personally I have accepted a.) most of my projects will be unfinished when I die and b.) if I am just staring at an empty terminal session and nothing is happening, it's not going to help to heap abuse on myself and feel guilty for not being productive. The only path forward will involve accepting that work isn't happening and doing whatever it is I can find a spoon for. On really bad days, that means just going to sleep. The sooner I accept that the sooner I will find a way to the other side, and nothing is getting done before then.
Reading about Taoism and Zen has helped. In terms of productivity, I have appreciated the book The Creative Act: A Way of Being by Rick Rubin. It's an attempt to apply Taoist ideas in a modern context, but where a Taoist text would say "the sage does XYZ," Rubin says "the artist." It's about finding a way to continue being creative when doing so is painful.
Medication and therapy have been a mixed bag for me, but my friends have had to go through multiple medications and therapists, so I remain optimistic. I'm trying to get into the habit of exercising and meditating.
Wishing the best for you both.
This same advice could be applied to most hobbies. It doesn't have to be good, and it certainly doesn't have to make money. It just has to be fun and rewarding. If you learn something, even better.
> Go Forth, make something fun
*golfclap*
For the interested reader:
https://github.com/loscoala/goforth
It was a great experience and I can only recommend trying to develop a programming language yourself.
https://github.com/Podginator/Rattle/tree/master
It used JavaCC, which I found to be a pretty simple way to get up and running.
I also worked a job that used yacc to create their own DSL for a piece of software. Same thing, really. Easy enough to get up and running, and messing around with.
More seriously, I have been very tempted recently to make a programming language specifically for cryptography, but I am holding off until I can no longer stand assembly.
You rarely make actual tools, though. It's unheard of that a woodworking goes on to make their own router, band saw, planer, jointer, chisels, etc. - but you can learn a ton by starting with the absolute bare basics, before investing a ton in expensive tools.
Kind of makes me wonder where this analogy fits (if at all) in the world of software engineering: Some tools are probably either too complex, or don't really make sense making, if you're going to use it to actually build something.
I mean, it is a good intellectual exercised for the curious, and you pick up a bunch of things underway, but at some point it is probably good to ask yourself if your time is better spent on something else.
Turing Complete, Actually performant, and possibly crazy enough to work
[1] https://esolangs.org/wiki/Bitgrid
[2] https://github.com/mikewarot/Bitgrid
And my latest attempt at things, because Pascal isn't popular enough, includes C and a Web emulator
I find it fascinating a language can be purpose-built, like Odin. It's a language that was built for game development. It got me thinking what a language built for web development may look like.
It also got me thinking, we have 'game engines' which are tools built around the same idea. Why don't we have web-CRUD engines? Sure we have frameworks, but we don't have entire purpose built applications for building fullstack applications.
Yes I can use python (other scripting language for that) but this is not cool as writing your own programming language and also you don't always have access to python environment. Bash just runs everywhere (at least for me). It is also my motivation to learn about compilers and how they work.
Every developer should make a language at some point. I want to see everyone's weird, half-baked languages! I want to hear about what other people struggled with, and the tradeoffs they've made!
But one thing I really liked was the "begin end" type of code blocks.
(I cannot testify for usability, the project is alive but it seems to need more contributors to really get going)
Every time, I think it sounds SO fun.
And then do nothing.
Given this time I have the excellent excuse of a new job, I don't expect to break the trend this year.
But one day . . . one day . . .
You're going to need one anyway . . .
Yes, I might gain a better understanding of how things work. I can do that lots of ways; others may be a better use of my limited time. (For that matter, doing other things that don't give me a better understanding of how things work may be a better, more valuable use of my limited time.)
Yes, I could get a language that fits what I need better... in theory. In practice, it would be buggy, inconsistent, and incompletely implemented. It would not actually fit what I need better than existing languages.
It's not hard for a game to touch all sorts of corners of the computer world that routine work will never come near. Games are great because they're easy to focus on, you know pretty much precisely what you want it to do. They also tend to be made of small parts, yet extensible, in terms of adding features if you still have wind in your sails.
And if not, hey, you have a few more data structures under your belt. Who knows when those may come in handy again.
Languages are similar. Get past a few "hard parts" and they can be a fun ride.
For extra fun, put the language into your game. Dog food for everyone.
No, it's probably not the right learning experience for everyone (clearly, it won't be yours), but there's definitely value in it for some people. And if reading the article nudges a few more people to take that learning dive, I think that's a good thing.
Without the freedom to make a mess of things, you live too cautious a life. It's worth digging in to some of these efforts even if they do wind up a mess. It's not because the mess will be meaningful. It's because you will understand better what was good and bad about what you did and how you got there.
Checkmate.
But when you are designing a programming language and writing the interpreter/compiler, well you are on your own :D
So, not checkmate at all.
Well, that is exactly what your app is if it's built on modern "state of the art" tooling - you're probably pulling in a thousand trivial dependencies; every day, something gets updated, and you're rolling dice on whether or not your program will build.
Writing your own language to build your app in creates a lot of big problems, but not those particular problems.