[HN Gopher] Cake - A C23 compiler frond end written from scratch...
       ___________________________________________________________________
        
       Cake - A C23 compiler frond end written from scratch in C
        
       Author : thradams
       Score  : 166 points
       Date   : 2022-09-13 11:54 UTC (11 hours ago)
        
 (HTM) web link (github.com)
 (TXT) w3m dump (github.com)
        
       | numeromancer wrote:
       | You and this guy ought to get together:
       | 
       | https://github.com/libav/c99-to-c89
        
       | ape4 wrote:
       | Cool - like Cake by the Ocean.
       | https://www.youtube.com/watch?v=vWaRiD5ym74
       | 
       | Maybe you want to add some neat new features - like Circle C++
       | https://www.circle-lang.org/
        
         | rmatt2000 wrote:
         | Or Cake by the Ocean Blue.
         | https://www.youtube.com/watch?v=K9VfJqHwYKs
        
         | saagarjha wrote:
         | I suspect this project is not quite as cool as sex on the
         | beach.
        
           | aidenn0 wrote:
           | I don't know, it has lots of uses and I don't get sand in any
           | sensitive bits...
        
       | thradams wrote:
       | This is my hobby project of a C23 Front End.
       | 
       | One of its unique features is the transpiler that can generate
       | readable code, preserving macros and formatting.
       | 
       | See it online: http://thradams.com/web3/playground.html
        
         | asveikau wrote:
         | Tried this:                   #include <stdio.h>
         | int main()         {            puts("hello world");
         | return 0;         }
         | 
         | Got:                   source:5:4: error: not found 'puts'
         | 5 |  puts("hello world");        |
        
           | thradams wrote:
           | The declaration of puts is missing at the stdio.h used by the
           | web version. (If you output preprocessed you will see the
           | declarations are there)
           | 
           | I will added it. Thanks.
           | 
           | Meanwhile you can add:
           | 
           | int puts( const char *str );
        
         | rurban wrote:
         | Caught a typo already :)
        
           | thradams wrote:
           | thanks! :D
        
             | cygx wrote:
             | While we're at it, it's "analysis", not "analisys"...
        
               | nilsb wrote:
               | Also, there's "different" vs. "diferent" in the examples:
               | 
               | source:2:2: error: _Static_assert failed "types are
               | diferent"
               | 
               | 2 |static_assert(typeid(a) == typeid(double [10]), "types
               | are diferent"); |^
        
               | golem14 wrote:
               | Wow, the jackals are out in full force tonight ;)
               | 
               | Seth Meyers must have been offline for a while ?
        
             | rch wrote:
             | Typo in the title as well - although I like the idea that a
             | programmer in the future will search for a contemporary
             | feature called a 'compiler frond' and happen upon this
             | discussion.
        
               | thradams wrote:
               | Wow this is really bad!
               | 
               | This was my first topic here in hacker news.
               | 
               | I wasn't expecting all this traffic. Good to see and find
               | more people interested in this kind of project.
               | 
               | I will be more careful in the future.
        
               | bee_rider wrote:
               | Eh, users here just like to find things to complain
               | about. If you didn't have some little typos, the thread
               | would just be bikesheding. And, too much care and you'll
               | never share.
               | 
               | It is a hobby project anyway, it doesn't need to be
               | perfect, and it was good to share either way. People are
               | clearly interested!
        
               | rch wrote:
               | Agreed!
        
       | tambourine_man wrote:
       | That's a good name.
        
         | gpvos wrote:
         | No lie.
        
       | dj_mc_merlin wrote:
       | Love it. Seeing C being used in the browser to transpile C is..
       | weird. In the good way.
        
       | SV_BubbleTime wrote:
       | Help me out... Why do I want a transpiler to take C23 code and
       | convert it down to C99 code?
       | 
       | If I was interested in using the C23 features, wouldn't I use a
       | C23-ready compiler?
       | 
       | That said... I do wonder how long it will be until ARM and GCC
       | have a C23 toolchain - or did I just answer my own question?
        
         | [deleted]
        
         | saagarjha wrote:
         | Targeting a compiler with poor language support?
        
         | thradams wrote:
         | My goal with the transpiler is not only the transpiler,
         | although my front end had to be created differently of an
         | normal compiler and preserve more tokens that could be
         | discarded during the compilation. This also can be useful for a
         | tool that does refactoring.. like renaming variables etc.. so
         | in any case it it useful.
         | 
         | The new C23 language has a lot of features that makes your code
         | not compile in previous C versions like attributes digit
         | separators etc.. Someone may wants to create a new project in
         | C23 and soon regret because the users of the code may need C99.
         | This would be one use case, you can create a C23 code and have
         | C99 versions of the same base code.
         | 
         | Unfortunately my transpiler is not "production ready" yet and I
         | don't have IDE plugins etc.. that is required to make the tool
         | more productive.
         | 
         | The other advantage, if we had a production ready transpiler
         | with a IDE support etc.. it that we could use C23 and compile
         | to C99 without having to wait for compilers like msvc to
         | implements the standards.
         | 
         | Also some experimental features (like defer) can be used and
         | you can distribute your code in standard C99. We have more
         | freedom to use wherever we want and distribute a "readable" C99
         | code.
         | 
         | By the way most of the C transpilers or compilers generates C
         | code only for immediate compilation. CFront was like that.
         | 
         | My transpiler have two modes one is for direct compilation and
         | other is to distribute generated code.
         | 
         | Each mode has advantages and disadvantages.
        
           | SV_BubbleTime wrote:
           | > The new C23 language has a lot of features that makes your
           | code not compile in previous C versions like attributes digit
           | separators etc.. Someone may wants to create a new project in
           | C23 and soon regret because the users of the code may need
           | C99.
           | 
           | Ah. I was not aware of this.
        
       | an1sotropy wrote:
       | This looks impressive!
       | 
       | I should spend some more time reading through what you have, but
       | can you answer: what parts of this should I be looking at if I
       | just want something to generate an AST for C99 (no transpiling
       | needed)?
       | 
       | There's some source analysis I'd like to do (on student C code)
       | and right now I'm considering the (python-based) C parser in
       | CFFI, but your's might be more complete?
        
         | thradams wrote:
         | The part that is the "transpiler" is visit.c and visit.h.
         | 
         | You can use it as reference to create different "visits". For
         | instance, I am implementing a code format at "visitformat.c".
         | 
         | I have implemented a "naming convention checker" inside the
         | parser, but it also can be a "visit".
         | 
         | Static analysis etc..can be a visit.
         | 
         | Inside the visit context you will find
         | 
         | struct ast ast;
         | 
         | That is the AST.
         | 
         | I would say the syntactic analysis parser is 100% complete C23.
         | 
         | Semantic analysis that is not 100%.
        
       | cryptonector wrote:
       | Ohhhh, a transpiler is fantastic. It means I could use C23 +
       | extensions and still use VC as a backend when I have to. (One C
       | codebase I work on has to build on older VC, too, which means
       | accepting obnoxious limitations.)
       | 
       | EDIT: I see it has an option to target C99, but I wonder if it's
       | sufficiently constrained C99 to support VC's not-quite-C99-hah-
       | hah dialect of C. I'll have to try it at some point.
        
         | cygx wrote:
         | Note that while support for anything post C89 used to be spotty
         | at best, the Visual Studio situation has improved in recent
         | years, see e.g. [1].
         | 
         | [1]
         | https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-...
        
           | cryptonector wrote:
           | Yes, I'm aware. But a friend has to support very old VC and
           | Windows. Don't ask. Point is that Microsoft's failure to
           | support C99 a decade ago is still annoying.
        
       | einpoklum wrote:
       | > The compiler can be used to translate new versions of C (like
       | C23) to C99.
       | 
       | So is it a compiler, a front-end (which emits IR for another
       | compiler), or a transpiler?
        
         | kreco wrote:
         | It's all of them.
         | 
         | A transpiler is still a compiler. A front-end compiles to an
         | intermediate state (C99 in this case).
        
         | thradams wrote:
         | It's a front end. The backend implemented generates C code. I
         | hope to have other backends in the future.
        
       | imachine1980_ wrote:
       | this is really cool, i want to do something related, make a
       | language like vala. what is the biggest difficulty in this type
       | of transpiler in your short journey?
        
         | thradams wrote:
         | Even having experience with C/C++, C preprocessor and C
         | declarations took a long time. Some time to get used to the
         | grammar as well.
         | 
         | A language without preprocessor and a simple grammar saves a
         | lot time and you can go direct for the funny parts.
        
           | junon wrote:
           | German by chance? Hopefully you're okay with language
           | corrections :)
           | 
           | I live in Germany now and I often hear "funny" (lustig) used
           | where "fun" (Spass, used in English as an adjective too -
           | something like spassig but that's still considered "funny")
           | should instead be used. Just a tip!
           | 
           | This project is awesome, by the way. Thanks for posting it.
        
             | spyremeown wrote:
             | Probably brazilian by the Portuguese comments.
        
               | thradams wrote:
               | Yes! I am Brazilian.
        
           | synergy20 wrote:
           | with skills like this, mind to push cello forward?
           | https://github.com/orangeduck/Cello really like it but not
           | skillful enough to do it myself.
        
       | sfpotter wrote:
       | I thought defer and lambdas weren't making it to C23...
       | 
       | https://thephd.dev/ever-closer-c23-improvements
        
         | thradams wrote:
         | Everything that is not part of C23 I annotated with
         | "extension". For instance "Extension Defer" or "Extension
         | Lambda" are not part of C23.
        
           | whizzter wrote:
           | Iirc NarcissiusJS was a test-bed for future JS features at a
           | time when development was a bit slow that allowed quick
           | prototypes.
           | 
           | https://github.com/mozilla/narcissus
           | https://wiki.mozilla.org/Narcissus
        
       ___________________________________________________________________
       (page generated 2022-09-13 23:01 UTC)