Live data from Hacker News

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

github.com

71–80 of 159 posts

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

#71

It's odd that the async/await syntax _exclusively_ uses threads under the hood. I guess it makes for a straightforward implementation, but in every language I've seen the point of async/await is to use an event loop/cooperative multitasking.

Noob question: if it just compiles to threads, is there any need for special syntax in the first place? My understanding was that no language support should be required for blocking on a thread.

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

#73

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…

Did you manage to compile this example?

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

#74

> 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…

Other languages also have non-local qays of influencing compiler behavior, for example attributes in rust (standard) or compiler pragmas in C (non-standard).

When reading working code, it doesn't matter whether the language mode allows variable reassignment. It only matters when you want to change it. And even then, the compiler will yell at you when you do the wrong thing. Testing it out is probably much faster than searching the codebase for a directive. It doesn't seem like a big deal to me.

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

#75
post #71

It's odd that the async/await syntax _exclusively_ uses threads under the hood. I guess it makes for a straightforward implementation, but in every language I've seen the point of async/await is to use an event loop/cooperative multitasking.

Noob question: if it just compiles to threads, is there any need for special syntax in the first place? My understanding was that no language support should be required for blocking on a thread.

Async/await should do a little more under the hood than what the typical OS threading APIs provide, for example forwarding function parameters and return values automatically instead of making the user write their own boilerplate structs for that.

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

#76
post #70

Earlier quoted context omitted.

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

I love CHICKEN Scheme! Nice to see it mentioned. Though I think it's worth pointing out it compiles to something pretty far from handwritten C, to my understanding. I think this is true of both performance and semantics; for example you can return a pointer to a stack allocated struct from a foreign lambda (this is because chicken's generated C code here doesn't really "return", I think. Not an expert).

Of course you can always drop to manually written C yourself and it's still a fantastic language to interop with C. And CHICKEN 6 (still pre-release) improves upon that! E.g structs and Unions can be returned/passed directly by/to foreign functions, and the new CRUNCH extension/subset is supposed to compile to something quite a bit closer to handwritten C; there are even people experimenting with it on embedded devices.

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

#77
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.

[deleted]

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

#78
post #76
post #70

Earlier quoted context omitted.

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

I love CHICKEN Scheme! Nice to see it mentioned. Though I think it's worth pointing out it compiles to something pretty far from handwritten C, to my understanding. I think this is true of both performance and semantics; for example you can return a pointer to a stack allocated struct from a foreign lambda (this is because chicken's generated C code here doesn't really "return", I think. Not an expert). Of course you…

> this is because chicken's generated C code here doesn't really "return", I think. Not an expert.

not an expert either, but you're right about that, it uses cps transformations so that functions never return. there's a nice write up here: https://wiki.call-cc.org/chicken-compilation-process#a-guide...

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

#79

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…

Did you manage to compile this example?

Yes, actually:

    $ cat kekw.zc
    include 
    
    fn main() {
        var f = fopen("file.txt", "r");
        defer fclose(f);
    
        var ch: byte;
        if fread(&ch, 1, 1, f) 
Post reply on HN