Hacker News new | ask | show | jobs
by backpropaganda 2958 days ago
Slightly OT, but do people know of any good small language which compiles to C? I'm not looking for big languages like C++, Nim, or Rust, but something which is essentially just C, but with some minimal modernization, such as perhaps being able to import modules, and doing away with header files.
5 comments

Cixl? :)

I realize the Forth & Lisp heritage might be a bit hard on the brain for those who haven't been exposed before, but what you're asking for is otherwise more or less what Cixl is aiming for.

https://github.com/basic-gongfu/cixl#compiling

Wow, cixl is a great thing! I love the Dijkstra quote against complexity that serves as inspiration for the language. However, you find this :

> To build Cixl yourself, you'll need a reasonably modern GCC and CMake installed.

I was very taken aback by this. Really? Why do you really need to depend on the cmake monstrosity? It's just a few lines of C without dependencies that they are distributing!

If there is something about cixl that will scare people is the ridiculous dependence on cmake, not its clean syntax.

I hear you; I'm not overly excited with CMake either, or the C build system story in general.

I spent several years trying to bend regular make into something I was happy with; then I spent several years on top of that using Rake, since at least it allowed me to say what I mean in a sane language. Compared to Rake, CMake is at least semi-standard, provides some kind of macro for most things I want to do, and mostly stays out of my way.

But you definitely have a point when it comes to simplicity and project fit. If someone would be willing to step up and help translate the makefile into something that doesn't look horrible, I'd be more than happy to let it go. Otherwise we'll have to wait until I get enough round tuits, which could take a while given how much remains to be done in Cixl.

Often it is disheartening to bend make to your bidding. But cixl has a neatly arranged tree and the makefile to build it is trivial. The following lines suffice (and by running 'make test -j' you compile everything in parallel and run all the tests) :

    CFLAGS   = -Isrc -O2
    LDLIBS   = -ldl -lm

    SRC      = $(shell ls src/cixl/*.c src/cixl/lib/*.c)
    OBJ      = $(SRC:%.c=%.o)

    src/main : src/main.o $(OBJ)

    clean    : ; $(RM) $(OBJ) src/main

    test: src/main ; for i in tests/*; do ./src/main<$$i; done

would you accept such a patch in your project?
Not bad; my make-fu was never that spectacular to begin with and it hasn't been aging well, this helps me a lot.

Sure thing, if you feel like giving the entire makefile the same treatment I'd be delighted to accept it.

Thanks for taking the time!

What's so bad about cmake? The first time I ever used it, I was very pleasantly impressed by the percentage system.

I take it that cmake tried to tackle autotools, partially succeeded, and partly succumbed to autotools' all-devouring complexity? :)

I just do not understand its point. Cmakefiles are much, much uglier than makefiles. And it takes several steps to compile a program, instead of a single one. And it seems larger than the operating system itself.

I do not know what is "the percentage system", but I doubt it can be so wonderful to make me forget about all the other unnecessary problems.

It may be justified to use cmake when it is really needed. But the cixl interpreter can be built by a five-line makefile.

Ah, I see, a bit more tentativity has been filed away for when I finally check it out.

I was regarding how it outputs progress indication, like

  [ 16%] Building C object ...
  [ 91%] Linking C executable ...
etc (many lines elided). Back on the slow machine I was using when I was very new to Linux, progress indication ("how soon can I stop freaking out about this maybe not working") was very nice to have :D

But (10 years on) I can understand it being icky to use. I guess, ugly as it is, it's a little more structured than automake is, and maybe that's its redeeming quality.

Maybe build systems are the dead-centre of a "sour spot" (opposite of sweet spot) in computer science, with no truly nice solutions out there? (Genuinely curious. The exponential profusion of JS build systems suggests it's rather hard to solve.)

The only explanation I have is that

- Maybe the author is used to cmake

- Maybe this project has Big Goals And Ventures™ syndrome - and, hence, all the enterprise build systems. (Hopefully not, it looks kinda nice)

> Maybe build systems are the dead-centre of a "sour spot" (opposite of sweet spot) in computer science, with no truly nice solutions out there?

I think it's the opposite. It is a very, very sweet spot for which many essentially perfect solutions exist. Then, it attracts all kinds of extreme bikeshedding and produces the monsters we see here and there.

Yeah, the syntax is pretty out-of-the-world for me, but I'll give it a go. Thanks!
If you're serious about writing code; I strongly recommend learning some C, Forth & Common Lisp to get an idea of what's possible. Once you have those under your belt, appearances matter less and Cixl will look less crazy banana.
Ion (https://github.com/pervognsen/bitwise/tree/master/ion) is a relatively new language with round-trippable conversion to C as a design goal.
Ooooh, bitwise looks really interesting...
Hmm. How would you do away with header files in a simple way if you wanted something similarly-scoped to C? Have the compiler vacuum in every source file at once to pick up all references, then discard via dead code elimination? That'd kill incremental compilation :/

(I'm genuinely curious about the answer to this question - I'm also looking for something similar to C)

Incremental compilation creates .o files for every compiled .c file, and so it could also create a .h file at the same time (perhaps fused in the .o file).
Ooooh, duh. Takes notes
Nim is small enough to be used for this purpose. You can easily use it as a nice layer on top of C.

Why do you consider it too big?

I haven't looked at Nim in detail, but since Nim does garbage collection, it didn't read to me like a thin layer over C. For instance, is it possible to instantiate a struct on the stack? I'd also have liked the compiled C code to be readable, which if I'm not mistaken, Nim doesn't do.
> For instance, is it possible to instantiate a struct on the stack?

Yeah, it is. You can easily avoid the GC as long as you don't mind managing memory yourself (unfortunately this means that you cannot use the stdlib either, or at least parts of it, if you disable the GC with the `--gc:none` flag you'll get warning messages from the compiler about procs that need the GC which is nice).

> I'd also have liked the compiled C code to be readable, which if I'm not mistaken, Nim doesn't do.

Yeah, that's one thing that Nim definitely doesn't do. I don't think there are many languages that do. It would be an interesting experiment to see how pretty you could make the generated C by either fixing things in the compiler or just running a C formatter on it.

> unfortunately this means that you cannot use the stdlib either

This is the kind of friction I expect from a language which uses GC, for my usecase. I think it might make sense for Nim to support a GC-less stdlib for people who do not want GC in their code.

This is giving me D flashbacks.
I think you want Zig!
Zig looks amazing! Thank you! While it doesn't compile to C, you read my mind that I actually wanted a C replacement rather than compile to C.
Great! Yeah, that's what I was guessing, glad that I guessed correctly.
Isn't Zig based on LLVM rather than compiled to C?
Yes, I didn’t assume that was the highest order bit here.
Zig explicitly does not depend on the C stdlib
Yeah, this is true, I was responding mostly to the semantics aspect rather than that part.