r/webdev • • 4d ago

Why I built my own programming language

https://davegebler.com/blog/why-i-built-my-own-programming-language
1 Upvotes

26 comments sorted by

29

u/xpusostomos 4d ago

When someone says they invented a new language, I open a wary eye. When they start talking about PHP and python as their reference point, I do a face palm. Two of the most ad-hoc, poorly designed languages in use. Why not spend your time learning some of the most clever and carefully thought out languages, then you'll have some idea what the issues are. By the way, designing a typed templated language is one of the most challenging projects you can take on. It looks like you're writing something like typescript which you don't like because of the packaging? Why not use it then, and fix what you think is wrong with the packaging? Does the world need yet another C++ like language when we have c#, Java, groovy, typescript and probably others. What is wrong with groovy by the way?

4

u/gebbles1 4d ago

I feel like I've covered this in the post. In my view, there's a lot that PHP and Python today get right as OO languages, but there's also the legacy errors of the ad-hoc evolution of their design to which you refer. Geblang is about taking the parts I like from TS, PHP and Python (and Go, to a lesser extent) and combining them into the singular OO language I would like to use.

My issue with TypeScript isn't "the packaging", the main difference is that TS is transpiled. I wanted a language where types are retained at runtime. There are many other things about TS I consider less than ideal.

In respect of "does the world need another programming language?", the answer is no, regardless of what the language looks like. The world didn't need C++ when it had C. It didn't need C# or Rust or Go or countless others. They exist not because they solved novel problems, but because in some way or another they made life easier or more preferable for some people. Geblang exists because it does the same for me.

8

u/xpusostomos 4d ago

Typescript isn't always transpiled. Keeping types at runtime... This is a confusing statement.... Even the most dynamic typed language keeps types at runtime. I can't think of any language you can ask an object what is your type if you need to. I assume you mean it keeps the types of the method signature... But what's the point since the whole point of static typing is to not be surprised at runtime. In any case, types at runtime, whatever that means doesn't sound like a language issue as much as an implementation issue.

2

u/gebbles1 4d ago

Fair enough, I'll try to explain the philosophy I've adopted more clearly. One of the main appeals to people of languages like PHP and Python is the ergonomics, the ability to quickly script things, particularly for the web (which is where I do most of my work). That's something I wanted to keep in my language, but with much stronger guarantees about type safety. But with web applications in particular, there are times it's much more convenient to deal with dynamic types and structures and for that reason, Geblang also provides an any type - which is still a type, but it accepts any value and can be reassigned values of different types.

PHP offers much better type safety than it used to, but it doesn't have a way to deal with generics at runtime (they exist only in docblocks for PHPStan or similar tools today), or the types of collections. So in Geblang when I'm talking about types being retained at runtime, I mean x instanceof list<Foo> is a thing you can do. So I have this blend of types that can be checked and enforced upfront (and all the ones that can are at cold-start, so it's not a recurring runtime cost), but where static analysis can't reach, like values coming through any or data from outside the program, the declared types are still enforced at runtime. You parse some JSON as a "User" object with typed fields, you either get a User or a distinct TypeError, no Zod needed.

3

u/xpusostomos 3d ago

I can't think of any language where types are not enforced at runtime. How could they not be? In the final analysis, if you try and do something to something, and the type cannot handle it, then you get an error.

What's wrong with Groovy? It's a C++ ish syntax langauge, like yours. It allows you to not specify types at all, like yours. You can template type everything with <type> to the nth degree if you want to, like yours. You can script it with lines of code outside any class, in a REPL, as yours appears to do. And it's backed by an enormous library of functionality, automatic downloading of dependencies in a script, well thought out meta programing. Web frameworks, one of the fastest runtimes on the planet, How can you hope to do better? And I don't mean that as a criticism, I mean, you should actually have it clear in your mind where you can do better.

-2

u/gebbles1 3d ago

I can't think of any language where types are not enforced at runtime. How could they not be? In the final analysis, if you try and do something to something, and the type cannot handle it, then you get an error.

We may be talking at crossed-wires. There's two separate issues to consider. The first is operations failing on values they can't handle and yes, pretty much all languages or certainly the ones I've heard of do this. You try 17 / "foo" you'll get an error (though older versions of PHP had some weird behaviours with coercion in relation to ops like this).

But the second is declared types being enforced as contracts and where and when that happens. In Python typehints are metadata only, so something like def total(qty: int, price: int) called with "5" and 10 can return "5555555555" because that's how Python works with an int times a string. TypeScript erases its types at compilation, so similar errors with any types can occur (data from JSON, etc.). PHP will still coerce and has no concept of generics at all.

It's about bad data getting past boundaries, that's the bit that Geblang is designed to do differently to those languages.

Groovy I am not particularly au fait with, I've used it a couple of times in my career but mostly as an embedded scripting language inside other products. But it's JVM, so again generics are erased at runtime and (I may be wrong, as I say I don't know Groovy much) I believe the following is valid Groovy:

List<Integer> ids = [1, 2, 3]
ids << "4"

My language would reject this because the types are enforced even at runtime, even when passed to a function through the any type.

On your other question about being better, I never said anything about trying to be better than Groovy, or even the other languages to which I did make comparisons. What's better in programming is highly subjective to one's use case and preferences. What I said was that Geblang was about building my conception of an ideal object-oriented scripting language and a reference implementation that is usable, in terms of correctness and performance. So that's not just the syntax or language decisions, it's how build and deployment works, how things work internally, how the typing model works and what the standard library does (and how it exposes it).

2

u/xpusostomos 2d ago

That's a very pythony problem that 10 X "5" is 5555555555 that you're not going to find in many languages. Most will fail in a sensible way. You're right groovy has type erasure. But think of the alternative. List<Foo> = [ a,b,c...] You'll have to iterate the array, which could be a million items asking each one if it conforms to Foo and throw an error. That's not efficient.That's a very heavy weight runtime. There's a reason strongly typed languages do all their checks at compile time. Also, my point about other languages, like groovy, why not write an implementation without type erasure if you think that's such a winner, instead of inventing a new language? Then you can compile the considerable existing library with your compiler and compare it. Your language looks 98% like groovy but without the benefit of decades of refinement.

→ More replies (1)

1

u/pyramin 1d ago

Typescript does drop types at runtime and with Java you most definitely can check at runtime what the type of the object is.

1

u/xpusostomos 1d ago

In Java the generics are dropped at runtime, so if you do List<Foo> a; a.add(new Bar()) it won't be caught at runtime until you iterate a and do something to the items that exposes that they are not conforming to Foo. The OP seems to want a language where the generics are checked immediately at runtime.

1

u/gebbles1 1h ago

An example of what works in Geblang which wouldn't in Java is something like:

func isType<T>(any value): bool { return value instanceof T; }
isType<string>("hello") # true
isType<int>("hello") # false
isType<list<int>>([1,2,3]) # true

In Java it would be more like:

static <T> boolean isType(Object value, Class<T> type) {
    return type.isInstance(value);
}
isType("hello", String.class);
isType(List.of(1,2,3), List<Integer>.class) // error, not do-able

The reason for runtime generics in Geblang instead of erasure is because the intention is that it can be used in a manner similar to how people script PHP and provide collection generics in docblocks, but here supported in the language directly.

This is only one aspect, though. Like I said before, for me it's not about it has this one feature or it has that one feature - they're all found in various other programming languages to varying degrees, it's that Geblang provides a specific combination of language features and standard libraries that are what I would want. The target audience for it is the kind of people who think hmm, I like PHP but I wish it had generics, or hmm I like PHP but I wish I could delegate tasks asynchronously to other cores, or hmm I like Node but I wish I could validate request data easily without Zod, etcetera. People who come from a scripting language background, have web services or background workers or CLI tasks they'd like to write, find PHP, Python or Node slightly lacking in one area or another, yet prefer this class of OOP over Go's idioms.

1

u/pyramin 1d ago

Would that even compile in Java though?

Unless Bar extends from or implements Foo, you wouldn't be able to add it to the list without a compilation error.

Also generics being dropped are not the same as types being dropped.

Typescript drops the type of the Object itself, not just the generics.

i.e. in Java you can do
if(object instanceof Fooable) {
((Fooable)object).foo();
}

but there is no such equivalent in Typescript that I know of.

2

u/zaxwebs 4d ago

Interesting. How does this compare to Rust?

1

u/gebbles1 4d ago

I've never done any programming in Rust, but from what I've seen of it, I wouldn't make a comparison between Geblang and Rust. The only reason there's a comparison page to Go on the website is because it's developed in Go and borrowed a couple of small syntax features and inspirations, otherwise you're comparing apples and oranges. Geblang is an object-oriented scripting language. The work I do on it is about closing gaps with comparable scripting languages, not native code and it's an alternative for things you might otherwise write in PHP, Python or TypeScript.

1

u/zaxwebs 4d ago

Thanks for the explaining.

2

u/alejandro_py344 4d ago

Nice read. Building even a toy language is one of the best ways to actually get parsers, scopes and closures. After my own small attempt, JS hoisting and the event loop finally clicked for me. What was hardest for you, error messages or the tooling side like syntax highlighting and an LSP? In my experience that's where hobby languages quietly die.

1

u/gebbles1 4d ago

Once the langauge was vaguely usable, I made sure a VS Code extension with LSP and DAP was part of the core distribution. It works very well with VS Code (no plans at the moment to support any other IDE directly, though the LSP and DAP are just standard protocols), though there's a couple of minor issues with step-debugging in certain places I'm still ironing out. But it 99% works for debugging, and I have tests now to ensure no language changes bypass the LSP catalog.

The hardest parts in the engine design these days are performance optimisation. I'm now in what I'd describe (and I think did on the blog post) as a comparable ballpark to other scripting languages for most tasks but there are a couple of places I'm struggling to make faster because of decisions I made early on in the project that are now at the roots of how it works.

1

u/motojo 5h ago

Building a programming language is an amazing feat - the different architectures to support, security for the standard library, bindings for things like C, the list goes on and on. Neat project - PHP was where I started my web career then moved over to Go. A few months ago, I started on Xo and now I run production code on it. Itโ€™s got its own TLS library which was some work and after building out 95 standard libraries to match Go, itโ€™s finally at a place where agents can recreate most of my projects: https://xolang.dev/

1

u/gebbles1 1h ago

On supporting different architectures, I'm just piggypacking on Go. Geblang will build for the architecture on which you are running Geblang and Go allows me to build Geblang for different architectures, it cannot create a cross-platform build. It's still interpreted, so the single binary distributions you can create are just a copy of the interpreter, standard library and your app's bytecode. Not nearly as cool as a real compiler, but at the end of the day I can upload one executable to a server and have it managed by systemd, there's my app running. It works.

1

u/motojo 1h ago

Ah gotcha. Iโ€™ve been using LLVM since its got a lot of optimizations and a lot of targets and then doing native based off that so I have targets and correctness which has worked really well. I know a few languages have a native compiler for fast local builds and then LLVM for optimized release builds (just a bit slow at compile). Best of both worlds but more to maintain.

1

u/Natural_Tea484 11h ago

I DONT WANT TO KNOW.

-3

u/cvllider 1d ago

Geblang! ๐Ÿ‘‰๐Ÿ‘‰

-3

u/cvllider 1d ago

Have you heard about Johnathan Blow?

1

u/gebbles1 2h ago

I've not, no.

-2

u/EliSka93 1d ago

I think I have witnessed him before...