Live data from Hacker News

Zen-C: Write like a high-level language, run like C

github.com

61–70 of 159 posts

Re: Zen-C: Write like a high-level language, run like C

#61

Earlier quoted context omitted.

i think so. The biggest hurdle with new languages is that you are cut off from a 3rdparty library ecosystem. Being compatible with C 3rd party libraries is a big win.

Makes it easy to "try before you buy", too. If you decide it's not for you, you can "step out" and keep the generated C code and go from there.

Very good point that I never considered! Thanks.

Re: Zen-C: Write like a high-level language, run like C

#62

Earlier quoted context omitted.

i think so. The biggest hurdle with new languages is that you are cut off from a 3rdparty library ecosystem. Being compatible with C 3rd party libraries is a big win.

Makes it easy to "try before you buy", too. If you decide it's not for you, you can "step out" and keep the generated C code and go from there.

This isn't a very sane plan. The ~300 LOC example mini_grep (https://github.com/z-libs/Zen-C/blob/main/examples/tools/min...) compiles to a ~3.3k LOC monstrosity (https://pastebin.com/raw/6FBSpt1z). It's easier to rewrite the whole thing than going from the generated code.

At least for now, generated code shouldn't be considered something you're ever supposed to interact with.

Re: Zen-C: Write like a high-level language, run like C

#63
post #19

Earlier quoted context omitted.

Assembly requires way more work than compiling to, say C. Clang and gcc do a lot of the heavy lifting regarding optimisation, spilling values to the stack, etc

Then you're stuck with the C stack, though, and no way to collect garbage.

I have a couple interpreters I've been poking at and one uses 'musttail' while the other uses a trampoline to get around blowing up the C stack when dispatching the operators. As for the GC, the trampoline VM has a full-blown one (with bells-and-whistles like an arena for short lived intermediate results which get pushed/popped by the compiled instructions) while the other (a peg parser VM) just uses an arena as the 'evaluation' is short lived and the output is the only thing really needing tracking so uses reference counting to be (easily) compatible with the Python C-API. No worries about the C stack at all.

I mean, I could have used the C stack as the VM's stack but then you have to worry about blowing up the stack, not having access (without a bunch of hackery, looking at you scheme people) to the values on the stack for GC and whatnot and, I imagine, all the other things you have issues with but it's not needed at all, just make your own (or, you know, tail call) and pretend the C one doesn't exist.

And I've started on another VM which does the traditional stack thing but it's constrained (by the spec) to have a maximum stack depth so isn't too much trouble.

Re: Zen-C: Write like a high-level language, run like C

#64

Syntax aside, how does this compare to Nim? Nim does similar, I think Crystal does as well? Not entirely sure about Crystal tbh. I guess Nim and Vala, since I believe both transpile to C, so you really get "like C" output from both.

Crystal compiles directly to object code, using LLVM. It does provide the ability to interoperate with C code; as an example, I use this feature to call ncursesw functions from Crystal.

Re: Zen-C: Write like a high-level language, run like C

#65
post #49

> Mutability > By default, variables are mutable. You can enable Immutable by Default mode using a directive. > //> immutable-by-default > var x = 10; > // x = 20; // Error: x is immutable > var mut y = 10; > y = 20; // OK Wait, but this means that if I’m reading somebody’s code, I won’t know if variables are mutable or not unless I read the whole file looking for such directive. Imagine if someone even defined custo…

Given an option that is configurable, why would the default setting be the one that increases probability of errors? For some niches the answer is "because the convenience is worth it" (e.g. game jams). But I personally think the error prone option should be opt in for such cases. Or to be blunt: correctness should not be opt-in. It should be opt-out. I have considered such a flag for my future language, which I name…

> Or to be blunt: correctness should not be opt-in. It should be opt-out.

One can perfectly fine write correct programs using mutable variables. It's not a security feature, it's a design decision.

That being said, I agree with you that the author should decide if Zen-C should be either mutable or immutable by default, with special syntax for the other case. As it is now, it's confusing when reading code.

Re: Zen-C: Write like a high-level language, run like C

#66
post #55

Syntax aside, how does this compare to Nim? Nim does similar, I think Crystal does as well? Not entirely sure about Crystal tbh. I guess Nim and Vala, since I believe both transpile to C, so you really get "like C" output from both.

man I haven't heard anything about Vala in ages . is it still actively developed/used? how is it?

Yes, it is actively being developed.

Quite easy to make apps with it and GNOME Builder makes it really easy to package it for distribution (creates a proper flatpak environment, no need to make all the boilerplate). It's quite nice to work with, and make stuff happen. Gtk docs and awful deprecation culture (deprecate functions without any real alternative) are still a PITA though.

Re: Zen-C: Write like a high-level language, run like C

#67
post #49

> Mutability > By default, variables are mutable. You can enable Immutable by Default mode using a directive. > //> immutable-by-default > var x = 10; > // x = 20; // Error: x is immutable > var mut y = 10; > y = 20; // OK Wait, but this means that if I’m reading somebody’s code, I won’t know if variables are mutable or not unless I read the whole file looking for such directive. Imagine if someone even defined custo…

Given an option that is configurable, why would the default setting be the one that increases probability of errors? For some niches the answer is "because the convenience is worth it" (e.g. game jams). But I personally think the error prone option should be opt in for such cases. Or to be blunt: correctness should not be opt-in. It should be opt-out. I have considered such a flag for my future language, which I name…

> I have considered such a flag for my future language, which I named #explode-randomly-at-runtime ;)

A classic strategy!

https://p-nand-q.com/programming/languages/java2k/

Re: Zen-C: Write like a high-level language, run like C

#68
post #53
post #36

The whole language examples seem pretty rational, and I'm especially pleased / shocked by the `loop / repeat 5` examples. I love the idea of having syntax support for "maximum number of iterations", eg: repeat 3 { try { curl(...) && break } except { continue } } ...obviously not trying to start any holy wars around exceptions (which don't seem supported) or exponential backoff (or whatever), but I guess I'm kindof sh…

Ruby has a similarly intuitive `3.times do ... end` syntax

go also has

    for range 5 { ... }

Re: Zen-C: Write like a high-level language, run like C

#69
From what I can see in the codegen, defer is not implemented "properly": the deferred statements are only executed when the block exits normally; leaving the block via "return", "break", "continue" (including their labelled variants! those interact subtly with outer defers), or "goto" skips them entirely. Which, arguably, should not happen:

    var f = fopen("file.txt", "r");
    defer fclose(f);

    if fread(&ch, 1, 1, f) 
would not close file if it was empty. In fact, I am not sure how it works even for normal "return 0": it looks like the deferred statements are emitted after the "return", textually, so they only properly work in void-returning function and internal blocks.

Re: Zen-C: Write like a high-level language, run like C

#70

Earlier quoted context omitted.

Nim is a high-level language as well and compiles to C.

Odin and Jai are others.

chicken scheme compiles to c as well. it's a pretty convenient compilation target, you get to use all the compilers and tool chains out there and you don't add a dependency on llvm
Post reply on HN