r/ProgrammingLanguages • u/Innorbio • Oct 19 '24
I’m building my own custom programming language—anyone else gone down this rabbit hole?
Hey Devs,
I’ve just started my own programming language, and it’s been both incredibly rewarding and challenging. One of the hardest parts has been balancing simplicity with functionality—trying to decide what features to include, how to structure the syntax, and where to draw the line between power and ease of use. But I think I have finally found that balance with an incredible feature that is going to change how we write code.
I’d love to know if anyone else here has tackled building their own language, or even thought about it. What were the biggest challenges you faced? How did you handle things like parsing, compiling, or interpreting? For those who’ve done this before, what do you wish you knew when you started?
Also, I’ve been documenting my progress in a series of dev logs (happy to share in the comments if anyone’s interested), but I’m mainly here to start a discussion and learn from others who’ve ventured down this path.
Looking forward to hearing your thoughts and experiences!
51
u/stylewarning Oct 19 '24
It's not a challenge to make a new language. That's possible in an afternoon.
The challenge is to make one that is:
Novel. One where you didn't just choose a new way to write an if-statement or for-loop. Genuinely new programming concepts, or existing concepts combined in a new way.
Useful. Programming languages are insanely good these days. Big libraries, good editor support, good tooling, etc. Even the stuff that people look down on as being meh took a lot of effort to build.
Lasting. Building a programming language that doesn't just scratch an itch requires sustained effort. If you keep an issue tracker, it'll fill up faster than you can empty it. Programming languages (+ libraries + tooling) have so many corners to them, and when things don't work so well it sticks out like a sore thumb.
Complete. A programming language is one thing. What about a complete library for doing math, or FFI, or web stuff, or ...? Building something that really covers the bases, even in a limited domain, requires planning and effort. Often, people get bored or frustrated once they get 80% of the way there in a library or API.
Programming languages are extremely fun, doubly so if you have a friend to work on it with, triply so if you have users, quadruply so if you get paid for it.
Happy hacking, and I look forward to your creations!
1
u/Innorbio Oct 20 '24
I 100% agree with you but right now I'm stuck with the less important but still exciting tasks of creating the syntax features so I cannot wait to start working on my ideas that will actually make a difference but for now I need to do my chores of getting the concepts implemented.
0
Oct 20 '24
[deleted]
4
u/stylewarning Oct 20 '24
Depends on what you care about. I build programming languages for money, and companies and their programmers don't like half-assed stuff. :)
0
Oct 25 '24
[deleted]
2
u/stylewarning Oct 25 '24
I build Coalton, primarily (but not exclusively) for the purpose of building quantum compilers.
20
u/Inconstant_Moo 🧿 Pipefish Oct 20 '24
The biggest challenge can be expressed as a law: for any two orthogonal features there's a corner case, be it syntactic, semantic, or conceptual.
(For example in my language I've been spending the last few weeks mainly doing multiple major refactorings and redesigning just to make sure that the type system and multiple dispatch plays nice with the modules. I didn't think modules through sufficiently in advance, I thought they'd be easy. Be warned.)
To cope with this:
Write good instrumentation. Make sure you can pretty-print your internal types. Then what I have is a set of constants looking like this, which can be used to turn on various forms of permanent commentary in the program.
SHOW_LEXER = false
SHOW_RELEXER = false
SHOW_PARSER = false
SHOW_VMM = false
SHOW_COMPILER = false
SHOW_COMPILER_COMMENTS = false
SHOW_RUNTIME = false
SHOW_RUNTIME_VALUES = false
SHOW_XCALLS = falseA debugger doesn't cut it. Turning this stuff on and off gives me relevant color-coded prettyprinted explanations of what the program is doing and why.
Also, make sure that every error your language returns to the user contains, for your benefit, an identifier saying where in your source code the error comes from. (So for example I have errors with tags parse/sig/a, parse/sig/b and parse/sig/c, all of which have the same error message for the user. But the tags tell me where the errors came from in my code.) This won't help the users much but it'll be very useful to you in debugging. (Also sometimes I've been able to add a feature very easily by assuming it was already there, putting it into a piece of Pipefish code, trying to compile it, and then following the trail of errors through the lexer, parser, and compiler to guide me to the exact lines I needed to change.)
Refactor early, refactor often.
Have a good test suite. Because of the law of orthogonal features, that new thing you added will have broken three other things that were working just fine. You need to know about this immediately.
You can't develop a language without developing in the language. Write stuff in your language. Start now, writing code on paper. When you get bits of it working, write things with those bits. This doesn't just test it, it finds where the rough unergonomic edges are, and the features you put in but don't really need, and the features you didn't put in but find you can't live without.
Final piece of advice: this is going to take longer than you think.
I too would like to know about your incredible feature. And do please share your dev logs and any other relevant material.
1
u/Innorbio Oct 20 '24
That is a very interesting way to look at it and has definitely inspired me to see things in a different context I agree 100% and my YouTube channel is innorbio
9
u/Ratstail91 The Toy Programming Language Oct 20 '24
...did you not see what this sub is about?
jump into the discord, we're a friemdly bunch in there.
3
19
35
u/new_old_trash Oct 19 '24
with an incredible feature that is going to change how we write code
buried the lede
9
5
u/Artistic_Speech_1965 Oct 19 '24 edited Oct 20 '24
Hi mate, happy you discovered this passion. Like everyone else in this subreddit I am actually building my language
The bigest challenges where about balancing functionalities (some of them can be incompatible) and find/use the right tool. I am more interested in building type systems tbh but building a cool language is really rewarding
9
u/Cyber_Encephalon Oct 20 '24
Nope, nobody ever created their own programming language. I'm waiting for the first one to come out. It's gonna be awesome.
(This reddit post was made possible by 3500 perforated cards ordered in sequence).
4
u/UVRaveFairy 🦋8Bitch Faceless Witch - Roll my own IDEs / poly IDE user /ACE Oct 19 '24
Yes, been going down those rabbit holes since the late 80's.
The kinds of things it teaches you about coding are diverse and challenging.
Still do, one in progress.
4
u/oscarryz Yz Oct 19 '24
It might be useful (for you) to share your progress and get early feedback.
Good luck and have fun
4
u/onequbit Oct 20 '24
Sooner or later, anyone with sufficient interest in programming can get bitten by the "create-a-programming-language" bug. It's a good exercise and worth undertaking, even if only to self-evaluate your skill by challenging yourself, and that is a valuable mindset for continual growth
3
u/david-1-1 Oct 20 '24
"I think I have finally found that balance with an incredible feature that is going to change how we write code."
I'm skeptical. What is this incredible feature?
I've written the lexer and parser for most of my compiler front ends. Compilers and software tools were my main subject for most of my 38 years as a software engineer. I now write tiny languages, mostly in JSON, as part of Web programs, rather than full programming languages.
3
u/RJLaxgang Jento Dev Oct 20 '24
I think the simple way of looking at it from the beginning of your journey as such:
Rule 1. DO NOT fall in the trap of forcing your first programming language(s) to be perfect syntactically. Esoteric language developers understand this quite well, albeit making it very difficult for us “Normies”.
Build your first language(s) with a single core focus and the functionality will become apparent by your constraints as a developer, or the constraints of that single core focus.
Some core focuses for your first programming language(s) might be:
Building the language as a virtual machine
Implementing Type Checking
Asynchrony as a core value
Maximizing the number of “First Class” data structures or features of the language
(as fore mentioned) Esoteric Syntax Focus
Reimplementing another language, a subset of the language, or superset of a language
Runs on Web? Low end machines? High end machines?
And directly from the craniums of the massive crowd of 12 people out there: “Let me see how many days before I quit trying to understand blockchain to create a contract language and get rich!”
The aspects you should be primarily concerned with on your promotional advance to “Programming Language Rear Admiral” are Audience, Performance, and Implementation. Your subordinates will love you for your charisma in determining how you can maximize your languages potential in those key areas… and then maybe after that your bosses will hate you for tossing the crew some syntactic sugar to top it off!
The sea of possibilities is endless. It’s your ship to stir or sink. Your challenges will take you through an odyssey containing syntax sirens, debugging storms, and occasionally having your productivity/sanity turn to stone.
Just stay away from Brainf*ck
2
u/kahdeg Oct 20 '24
It's fun to envision some kind of usecase for your new language, be it automated scripting or magic-like script for some kind of rpg game, i'm currently making a generic language for controlling embedded led strip with arduino or esp32 runtime and it's fun to design usecase for that
2
u/LiquidGermanium Oct 20 '24
I made one with ANTLR that compiled to LVVM. It was.my favorite project of all time. It's this project that allowed me to understand programming properly and removed the "magic". It was an OOP language with plenty of cool things . I made some fun little terminal game with it.
2
u/PurpleUpbeat2820 Oct 20 '24
What were the biggest challenges you faced?
Overcoming my fear of things I'd been told are scary.
How did you handle things like parsing, compiling, or interpreting?
I've always aimed to bootstrap my language so I rolled my own lexer and parser built atop my own combinators.
For those who’ve done this before, what do you wish you knew when you started?
Everything is easy. Don't be scared.
2
u/gofl-zimbard-37 Oct 21 '24
I've done several, for both work and personal use. The hardest thing for me was editing it down to keep things simple. Feature creep is real, and if you have users it's very hard or impossible to remove features.
1
2
u/dokushin Oct 20 '24
Programming language design had nothing to do with lexers and parsers. (Well, okay, it does need to be parseable.) Usually you want the language figured out before you write the parser for it.
Parsing a toy language is a fine way to learn lexers and parsers,ind; but if your goal is novel languages the time spent on building a lexer from scratch isn't contributing to that goal.
1
u/umlcat Oct 20 '24
Learn to describe tokens using Regular Expressions or Automaton Diagrams.
Leard to describe your P.L. syntax using Regular Expressions or Syntax Railroad Diagrams.
1
u/csharpboy97 Oct 20 '24
The most challanging part was the documentation and vscode support with kind of intelligent code completion. the parsing and compiling, in my case c# and generating msil was not hard
1
1
1
u/P-39_Airacobra Oct 21 '24
But I think I have finally found that balance with an incredible feature that is going to change how we write code.
That's certainly a very bold claim. I'm not doubting you, but you've definitely piqued my interest and raised my expectations. Are you able to go into any detail? We all love hearing about design/implementation details, that's what this sub is here for after all.
1
u/mnbjhu2 Oct 21 '24 edited Oct 21 '24
I've been working on a language for about a year now (although somehow I've only just found this sub).
My language at its base is similar to rust but is garbage collected, making the it easier and the syntax cleaner. My focus has been on the users experience while writing the language and specifically I've been taking the lsp-first approach. I have some ideas about replacing a lot of usecases for macros/codegen/compiler plugins in other languages with comptime functions (like zig) but keeping the type info at type-check time (unlike zig).
The main challenge for me has been building an architecture for the language which lets me easily/safely add in language features and get the tooling features (diagnostics, hover, gotodef, completions, etc) with little effort. I only overcame this a week or so ago, but now it feels like I can paint the language I want and see it in 3d (this has saved me from burnout).
I had to solve many problems to get to this point but the 3 main ones are:
writing a good parser: Parsing valid code is easy but when you're writing ide support (and for high-quality compilers) you want the parser to be able to report mistakes and to be able to recover. E.g. you don't want to stop highlighting a whole file because of one error.
Type system: My language has types similar to rust (enums, structs, traits, hindley-milner) but also has generic type variance and traits can implement other traits... This is still causing me some headaches but I'm mostly there.
Incremental compilation: I don't want to recheck in entire project on every change/save so some incremental compilation is needed. I went down loads of rabbit holes here (rust + async = pain). Eventually I wrote an implementation of language ignoring LSP and inc compilation and found that I could port the model I used to use salsa (an incremental computation library for rust). There still a lot that I need to work on here but for now I'm enjoying adding features to the language.
Edit: very much WIP but heres the repo if anyone is interested: https://github.com/mnbjhu/giblang
-1
u/Ronin-s_Spirit Oct 20 '24
No, but I did go down the rabbit hole of trying to fuck up the language I'm using before it fucks me up. Javascript is a beast in good and bad ways.
-1
u/Ronin-s_Spirit Oct 20 '24
No, but I did go down the rabbit hole of trying to fuck up the language I'm using before it fucks me up. Javascript is a beast in good and bad ways.
2
Oct 20 '24
[removed] — view removed comment
2
u/Tasty_Replacement_29 Bau Oct 20 '24
There is "The RISC-V Compressed Instruction Set" which is 16 bit, how does yours compare to that?
1
u/PurpleUpbeat2820 Oct 20 '24 edited Oct 20 '24
Nice challenge! My quick hack:
16x 32-bit registers
r0 = 0
r14 is link register
r15 is program counter
No static calls. Just preserve old lr, set lr to instruction after the call, load the destination either as an absolute address or added to the pc and jump to register. Return is
add pc, 0, lr.16-bit encoding is:
0000 dddd aaaa bbbb d = a+b 0001 dddd aaaa bbbb d = a-b 0010 dddd aaaa bbbb d = a*b 0011 dddd aaaa bbbb d = sdiv(a,b) 0100 dddd aaaa bbbb d = udiv(a,b) 0101 dddd aaaa bbbb d = a & b 0110 dddd aaaa bbbb d = a | b 0111 dddd aaaa bbbb d = a ^ b 1000 dddd aaaa bbbb if (a<b) pc=d 1001 dddd aaaa bbbb if (a≤b) pc=d 1010 dddd aaaa bbbb if (a=b) pc=d 1011 dddd aaaa bbbb if (a≠b) pc=d 1100 dddd iiii iiii d = i 1101 dddd iiii iiii d |= i<<8 1110 dddd aaaa bbbb *(a+b) 1111 dddd aaaa bbbb *(a+b) = dSo 8 for int ops, 4 for comparisons and jumps, two for loading immediates, one load and one store.
Useful aliases:
mov rd, raisadd rd, 0, ra
neg rd, raissub rd, 0, ra1
Oct 20 '24
[removed] — view removed comment
1
u/PurpleUpbeat2820 Oct 20 '24
Not bad how do you get constants from the code, going to assume use the load.
Actually I put the
d = iandd |= i<<8there to load 16-bit constants in two instructions but you could just load all constants from memory and have two more ops.I am working with Harvard style so program and memory are different spaces
Ok.
1
Oct 20 '24
[removed] — view removed comment
1
u/PurpleUpbeat2820 Oct 20 '24
Sure, NP. I just invented it on-the-fly. Been playing with Aarch64 and RiscV asm a lot recently...
122
u/thedeemon Oct 19 '24
In this part of reddit, pretty much everyone is doing it.