Pretty.c

(github.com)

103 points | by synergy20 5 hours ago ago

53 comments

  • unwind an hour ago

    Meta: the naming is ... strange.

    The actual name of the repo is "pretty.c", but the name used for the language/dialect/result/horrorshow[*] is "Pretty C".

    The actual code file you include is called "pretty.h", which makes sense since it's a header, of course. Confusing!

    Edit: escapes.

    [*] Yes, I'm a C programmer, currently hunting for black-marked insulin to combat the rapid-onset diabetic attack from all that sugar. Sorta.

    • nneonneo an hour ago

      I mean, don’t say the repo didn’t warn you!

      > The goals for Pretty C are: Provide so much syntactic sugar as to cause any C developer a diabetes-induced heart attack.

  • nneonneo 42 minutes ago

    It claims to be a scripting language but you still have to compile the programs. Boo! Add CINT (https://root.cern.ch/root/html534/guides/users-guide/CINT.ht...) and you can have instantaneous execution and even a REPL!

  • gpuhacker an hour ago

    Reminds me of a C++ codebase I once had to inspect that was entirely written as if it were written in Java. With camelcase naming for everything, getters and setters for every class variable, interfaces everywhere.

    • jart an hour ago

      You ain't seen nothin. Check out the bourne shell source code from unix seventh edition. https://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd... I can't believe it's not ALGOL.

      • froh 31 minutes ago
      • teruakohatu 43 minutes ago

        Wow, I was not expecting that! Was this style of C common back then?

        Before he wrote the Bourne shell the author wrote an ALGOL compiler, and ALGOL inspired Bourne syntax:

        https://en.wikipedia.org/wiki/ALGOL_68C

        • pasc1878 39 minutes ago

          There were article suggesting #define BEGIN { and #define end }; to make C look more like Pascal.

          I think in Europe C was not as common as other languages at the time so the terseness looked odd.

          • pavlov 9 minutes ago

            Also because the special characters were (and are) difficult to type on European keyboards.

            Characters like []{}\|~ are behind multi-finger access and often not printed at all on the physical keys (at least in the past). You can see how this adds a hurdle to writing C…

            Pascal was designed by a European, so he preferred keywords which could be typed on every international keyboard. C basically just used every symbol from 7-bit ASCII that happened to be on the keyboards in Bell Labs.

    • bpye an hour ago

      Good job they weren't using MSVC I guess...

      https://learn.microsoft.com/en-us/cpp/cpp/property-cpp?view=...

    • pasc1878 42 minutes ago

      I had that as well but also Java passes strings in as f(String *) so the C++ code was f(new String("Hello")

    • worstspotgain an hour ago

      > camelcase naming for everything, getters and setters for every class variable, interfaces everywhere

      This is not far off from the guidelines in many cases, e.g. Windows code (well, not every variable of course.) A lot of Java design was copied from C++.

  • yjftsjthsd-h 3 hours ago

    I have not decided how I feel in general, but:

    > Everyone defines these, so why not provide them?

    Honestly, that's fair.

  • DeathArrow an hour ago

    This should have been invented 50 years ago!

  • textread 3 hours ago

    Is it possible to tangle the Readme into pretty.h? In other words, are the codeblocks in the orgfile exhaustive.

    I love the literate way you have explained your thought process in the readme.

  • tromp an hour ago

    > ifnt for if(!...).

    "unless" seems more readable than "ifnt".

    • tux3 an hour ago

      Another bikeshed is the infinite for(;;) loop being called "always"

      I've seen "loop" in other languages. But Qt calls it "forever", and that is indeed very pretty. Very Qt, even

      • poincaredisk 22 minutes ago

            #define ever ;;
            for(ever) {}
      • teruakohatu 41 minutes ago

        > I've seen "loop" in other languages. But Qt calls it "forever", and that is indeed very pretty. Very Qt, even

        You can break a "forever" loop so I think "loop" is a better name.

        • wwalexander 10 minutes ago

          I don’t know why “repeat” isn’t very common in place of while/loop/etc; it works out nicely grammatically.

              repeat {}
              repeat while <condition> {}
              repeat {} while <condition>
              repeat <count> {}
  • mydriasis 3 hours ago

    Wow, neat! The wildest part to me is

    > And it’s backwards-compatible with C and all of its libraries!

    I can't wait to give it a shot! This looks like a riot.

    • geon 2 hours ago

      Have you heard of Zig?

      • oguz-ismail 2 hours ago

        It requires a different compiler. This is just a collection of C preprocessor macros

  • varjag an hour ago

    This made me immediately think whether MIT Loop of Common Lisp was an inspiration here. Checked the user's profile and sure enough, a lisper!

  • lpapez an hour ago

    All that is missing is a garbage collector. Should be possible to implement one by overriding malloc & friends?

    • vidarh 14 minutes ago

      You can use the Boehm-Demers-Weiser GC with C. It's conservative, because it has to be with C, so it may/will miss things (it will treat integers etc. as potential pointers, and so avoid freeing anything "pointed to" by them), and so it works best as an extra layer of protection/leak detector, but it can be used as a replacement for freeing memory too.

  • nicman23 13 minutes ago

    as someone that just started C, it looks pretty :)

  • dymk 3 hours ago

    > turn any codebase into a beginner friendly one

    Okay then.

    I was hoping to see a “this is just for fun” disclaimer but didn’t see one. Please never actually use this in a project that other people will have to read or contribute to.

    • ipsum2 3 hours ago

      > Provide so much syntactic sugar as to cause any C developer a diabetes-induced heart attack.

      seems like its obvious to me that its a joke

      • rebolek 2 hours ago

        It's a joke that I would happily use.

  • swiftcoder 28 minutes ago

    This is as horrific as it is wonderful.

  • ipsum2 3 hours ago

    `equal(0.3, 0.2 + 0.1); // true`

    how is this wizardry possible?

    • masklinn 2 hours ago

      It uses type dispatch to perform an epsilon comparison:

          static int pretty_float_equal (float a, float b) { return fabsf(a - b) < FLT_EPSILON; }
      
      So it’s https://docs.python.org/library/math.html#math.isclose
    • defrost 2 hours ago

      Is What Every Computer Scientist Should Know About Floating-Point Arithmetic wrong ??!!

      addendum: why are obviously rhetorical questions are taken so literally here?

      • ipsum2 38 minutes ago

        It's meant as both humorous and a nerd snipe :)

      • leansensei 2 hours ago

        Because text doesn't convey sarcastic voice tonality, so the intent is far from obvious.

        • defrost 2 hours ago

          Sarcastic? Okay, if you say so.

          Picking out an obvious define function that compares a float with a float sum of that nature should indicate an good understanding of why that might be called wizardry and deserving of a second look.

          Hats off to the peer comment that suggested scaling against epsilon rather than simpliy regurging the substitution "as was" from the header.

          The scaling is better in general, optional in some specific contexts.

    • slimsag 2 hours ago

      it uses absolute difference epsilon equality ('close enough to be considered equal'):

          static int pretty_float_equal (float a, float b) { return fabsf(a - b) < FLT_EPSILON; }
          static int pretty_double_equal (double a, double b) { return fabs(a - b) < DBL_EPSILON; }
          static int pretty_long_double_equal (long double a, long double b) { return fabsl(a - b) < LDBL_EPSILON; }
      • vbezhenar 2 hours ago

        This is wrong code. It only works somewhat correctly when a and b around 1.

        • stabbles 2 hours ago

          Yeah, should be scaled like |x - y| <= ε * max(|x|, |y|)

    • wjbr 2 hours ago

      static int pretty_float_equal (float a, float b) { return fabsf(a - b) < FLT_EPSILON; }

  • OsrsNeedsf2P 3 hours ago

    This is terrifying

  • Validark 2 hours ago

    Evil, yet beautiful. Hats off to you.

  • Uptrenda 2 hours ago

    Type names are nice; Perfect choice for the in-built func macros (like min); Len -- love it. Named boolean operators -- might be a bit much but go for it; Ternaries are illegible so you can only improve them; Not completely sold on all your loop definitions but some make sense to me; Resource tracking is impressive; The for... look a bit ugly -- could probably call it something else.

    All in all: quite a solid attempt. I'll give you 8/10 for the design of this. The way you sketched this out in C using macros is really elegant. This actually looks like good code. Would I use it? It's a new language and I like C already. It could help people learn C and think about language design. Since the way you've done this is very clear.

  • SuperHeavy256 43 minutes ago

    So sweet :))

  • bitwize 3 hours ago

    I'm reminded of the guy who did

        #define BEGIN {
        #define END }
    
    and a whole bunch of other macro-larkey just to make C look more like Pascal. Only then would he deign to code in it.
  • looopTools 3 hours ago

    I cannot wait to show this to a colleague of mine. He will kill me XD

  • leetsbehonest an hour ago

    That is pretty cool

  • azinman2 2 hours ago

    > Deprecate Lua, Python, JavaScript, Ruby and a dozen other languages, because Pretty C is the ultimate scripting language, but lightning-fast and strongly typed!

    Umm… that’s quite the goal.

    I’ll stick with deprecated Python.

  • MrLeap an hour ago

    love. It.

  • ngcc_hk 37 minutes ago

    Can we just pascal?