8 ms·
Pretty.c
- varjag 2y agoThis made me immediately think whether MIT Loop of Common Lisp was an inspiration here. Checked the user's profile and sure enough, a lisper!
- aartaka 2y agoYes, LOOP is a huge inspiration and my favorite programming language!
- jcmontx 2y agodoes this transpile to C or how does it actually work?
- jdthedisciple 2y agoit's just aliases preprocessor is using those #defines to replace tokens in the code so it ends up as usual C
- mydriasis 2y agoWow, 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 2y agoHave you heard of Zig?
- oguz-ismail 2y agoIt requires a different compiler. This is just a collection of C preprocessor macros
- mydriasis 2y agoI hadn't yet, but it does look nice. I especially like that you can just say "defer deinit", that's really nice.
- textread 2y agoIs 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.
- aartaka 2y agoYes, code blocks in Org are executable, but I was aiming for simple embedding and zero build-time, thus conservative choice of separating README and the actual header.
- OsrsNeedsf2P 2y agoThis is terrifying
- aartaka 2y agoThanks!
- yjftsjthsd-h 2y agoI have not decided how I feel in general, but: > Everyone defines these, so why not provide them? Honestly, that's fair.
- dymk 2y 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 2y 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 2y agoIt's a joke that I would happily use.
- AnthonBerg 2y agoC is funny, in many ways.
- aartaka 2y agoYou're welcome!
- thiht 2y agoNot everything needs to be stated explicitly, where's the fun in that?
- TheRealPomax 2y agoNo promises, people who want to have fun are going to have fun despite requests not to have fun.
- ipsum2 2y ago`equal(0.3, 0.2 + 0.1); // true` how is this wizardry possible?
- defrost 2y agoIs What Every Computer Scientist Should Know About Floating-Point Arithmetic wrong ??!! addendum: why are obviously rhetorical questions are taken so literally here?
- leansensei 2y agoBecause text doesn't convey sarcastic voice tonality, so the intent is far from obvious.
- defrost 2y agoSarcastic? 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.
- ipsum2 2y agoIt's meant as both humorous and a nerd snipe :)
- wjbr 2y agostatic int pretty_float_equal (float a, float b) { return fabsf(a - b) < FLT_EPSILON; }
- slimsag 2y agoit 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; }
- looopTools 2y agoI cannot wait to show this to a colleague of mine. He will kill me XD
- aartaka 2y agoCan't wait to learn of how it went!
- bitwize 2y agoI'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.
- epcoa 2y agohttps://en.wikipedia.org/wiki/Stephen_R._Bourne https://en.wikipedia.org/wiki/Stephen_R._Bourne https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh
- sph 2y agoNow that's just silly. And I see the backwards keyword terminators (LOOP/POOL). I have wondered why we have case/esac, if/fi but while/done. I imagine the author himself figured that while/elihw would just be entirely ridiculous.
- pavlov 2y agoJust call it wyl/lyw. Pronunciation maintained, problem solved.
- giucal 2y ago> I have wondered why we have case/esac, if/fi but while/done. With the reverse-keyword convention we'd get "od", not "elihw", though. while ... for ... do do ... ... od od The 'od' utility already existed, apparently, so Bourne opted for "done". [edit: typos]
- epcoa 2y agoNo, there’s OD. DONE is different but no less perverse.
- azinman2 2y 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.
- fancyfredbot 2y agoDeprecate Python?! You'll have to deprecate me first! ;-)
- psychoslave 2y agoWe will all reach this status at some point, though the silly code we produced might stick a bit longer in some legacy codebase. :D
- Uptrenda 2y agoType 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.
- aartaka 2y agoWell, you don't have to use it all. My projects mostly use booleans, len(), max/min, and some operator aliases, because there wasn't much need for other tasty stuff yet. So give it a shot, even if for a couple of operator macros!
- Uptrenda 2y agoYou know I expected your macro file to be unreadable moon math. But it actually doesn't look bad.
- Validark 2y agoEvil, yet beautiful. Hats off to you.
- aartaka 2y agoThanks!
- lpapez 2y agoAll that is missing is a garbage collector. Should be possible to implement one by overriding malloc & friends?
- vidarh 2y agoYou 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.
- gpuhacker 2y agoReminds 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.
- bpye 2y agoGood job they weren't using MSVC I guess... https://learn.microsoft.com/en-us/cpp/cpp/property-cpp?view=msvc-170 https://learn.microsoft.com/en-us/cpp/cpp/property-cpp?view=...
- jart 2y agoYou 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/sh/word.c https://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd... I can't believe it's not ALGOL.
- teruakohatu 2y agoWow, 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 https://en.wikipedia.org/wiki/ALGOL_68C
- pasc1878 2y agoThere 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 2y agoAlso 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.
- leetsbehonest 2y agoThat is pretty cool
- unwind 2y agoMeta: 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 2y agoI 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.
- deleted 2y ago[deleted]
- tromp 2y ago> ifnt for if(!...). "unless" seems more readable than "ifnt".
- tux3 2y agoAnother 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
- teruakohatu 2y 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 2y agoI 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> {}
- af78 2y agoOne possible reason: > The word "REPEAT" should not be used in place of "SAY AGAIN", especially in the vicinity of naval or other firing ranges, as "REPEAT" is an artillery proword defined in ACP 125 U.S. Supp-2(A) with the wholly different meaning of "request for the same volume of fire to be fired again with or without corrections or changes" (e.g., at the same coordinates as the previous round). https://en.wikipedia.org/wiki/Procedure_word#Say_again https://en.wikipedia.org/wiki/Procedure_word#Say_again More seriously, PASCAL has repeat-until loops, similar to do-while loops in C.
- aartaka 2y agoPretty C does aliases "repeat" for "do", so yeah, I've got you covered!
- MrLeap 2y agolove. It.
- aartaka 2y agoThanks!
- DeathArrow 2y agoThis should have been invented 50 years ago!
- aartaka 2y agoYes, and it's a shame that underlying features were only shipped in C11 (generics) and C23 (auto type inference!)
- SuperHeavy256 2y agoSo sweet :))
- aartaka 2y agoYeah, I love sugar ;)
- nneonneo 2y agoIt 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.html https://root.cern.ch/root/html534/guides/users-guide/CINT.ht...) and you can have instantaneous execution and even a REPL!
- shakna 2y agoI'd prefer just using tcc [0]. Far lighter weight than that monster. And C, not C++. [0] https://bellard.org/tcc/tcc-doc.html https://bellard.org/tcc/tcc-doc.html
- maccard 2y agoGiven the idea behind this repo is to cause pain, why not add a shebang to your file [0] to make it executable. I saw a blog post a long time ago that went into the details of how ./foo worked, and how it executed an elf file. You could register `.c` programs in the same way to be compiled and run? [0] https://gist.github.com/jdarpinian/1952a58b823222627cc1a8b83a7aa4e2 https://gist.github.com/jdarpinian/1952a58b823222627cc1a8b83...
- nneonneo 2y agoNow I have a very evil idea: what about registering a binfmt handler for the header bytes “#include”? Sure, it doesn’t handle all C/C++ programs (notably any program that dares to start with a comment), but it would not require modifying any source code! (For even more insanity I guess you could also trigger on // and /*, although there’s some risk of false positives then!)
- aartaka 2y agoWell, who said that scripting language cannot be compiled? And yeah, Clang-REPL is another way to make it REPL-friendly.
- knome 2y agogenerally they aren't, as scripting usually implies an interpreter, though no one is stopping you from using a wrapping script that quietly compiles on first run and caches a bunch of executables somewhere. not much different than python producing bytecode files as it goes along.
- ngcc_hk 2y agoCan we just pascal?
- aartaka 2y agoI won't stop you, so be my guest.
- swiftcoder 2y agoThis is as horrific as it is wonderful.
- aartaka 2y agoThanks!
- nicman23 2y agoas someone that just started C, it looks pretty :)
- aartaka 2y agoThanks!
- codeflo 2y agoCan someone clarify whether this is intended as a joke or whether the author is actually confused? I mean, nothing about this makes sense: it's not "scripting"; it claims to introduce "strong typing" while it does nothing about typing; it introduces all kinds of operator aliases "modeled after Lua and Lisp" that are present in neither of these languages. But it's not an obvious parody either, so I'm genuinely not sure.
- AnthonBerg 2y agoI do not at all think the author is confused. Being confused is OK though.
- sandos 2y agoI mean he has to be serious, right: "Deprecate Lua, Python, JavaScript, Ruby and a dozen other languages, because Pretty C is the ultimate scripting language, but lightning-fast and strongly typed!!"
- aartaka 2y agoAuthor here. I don't see any problem with this
- byroot 2y agoWell, as a starter C is rarely considered as "strongly typed". Statically typed yes, but strongly typed not so much.
- aartaka 2y agoWith C23 (nullptr, auto typing, typeof) and C11 (generics) it got more guarantees and type-related primitives. You can still do void*, but you are strongly discouraged from it.
- byroot 2y ago#include "pretty.h" void print_int(int value){ println(value); } int main (int argc, string argv[]) { long value = 23849234723748234; print_int(value); } How is this strongly typed? $ cc test.c -o test && ./test -1411401334 And to be clear, weak vs strong isn't a boolean property but a spectrum, but would be hard to argue with a straight face than C is a strongly typed language.
- throwaway19972 2y agoDoes "strong typing" now just mean "static typing"? Afaik both lua and python are already strongly typed. Javascript is not and I have no clue about ruby.
- lelanthran 2y ago> Does "strong typing" now just mean "static typing"? The distinction strong and weak typing is irrelevant in practice. Weak (but present) static typing beats strong dynamic typing every single time, because what is valuable is NOT "Do I see a type mismatch error only when a user accesses it?", it's "does this mismatch prevent a deployment?" IOW, the only distinction in production is dynamic typing vs static typing, not strong typing vs weak typing.
- throwaway19972 2y ago> because what is valuable is NOT "Do I see a type mismatch error only when a user accesses it?", it's "does this mismatch prevent a deployment?" I argue that understanding the semantics clearly and unambiguously is the most relevant thing, and strong typing tends to do that better (imho—with my only other examples being javascript and the "stringly-typed" perl and some random irrelevant BASIC dialects). > Weak (but present) static typing beats strong dynamic typing every single time, Can you give me an example? I don't think I've ever heard of such a thing. The closest I can think to this is maybe, arguably, the use of `void*` pointers in C which is difficult to see as anything other than pragmatism rather than some deeply beneficial way to write code—even explicit casts produce much more readable code. Another argument I could see is for operator overloading, which (IMO) produces much less readable code, or the implicit conversions feature of Scala (which also, IMO, produces less readable code, but they've addressed a lot of the major problems with it).
- syrrim 2y agorecommend setting up tests for your code s/t failures block deployment. can catch categories of bugs beyond typing errors
- w4rh4wk5 2y agoI've seen this implementation of defer a few times now. I really dislike calling this defer (like the keyword in Go) as the given code won't be executed on return.
- aartaka 2y agoScoping defer to a block is actually more useful and explicit than function-exclusive that Go does, so I consider that a feature.
- w4rh4wk5 2y agoNo no, I think you misunderstood my critic. Defer working on block-scope is fine; however, if I exit the block through a return (or break), the deferred function is not called. To my knowledge, you need a compiler extension to implement this in C (or use a C++ destructor).
- shakna 2y agoWell, there's a few things I should probably get around to adding to CNoEvil[0] and ogw[1]... There always seem to be more every few months when this project reappears. [0] https://git.sr.ht/~shakna/cnoevil3/ https://git.sr.ht/~shakna/cnoevil3/ [1] https://git.sr.ht/~shakna/ogw https://git.sr.ht/~shakna/ogw
- cptnapalm 2y ago"It takes a whole lot of bad ideas and mashes them into an abhorrent monstrosity." I love this to the very core of my being.
- pineaux 2y agoI feel compelled to try it out in a serious way and contribute to it. I have strong knowledge of python and am learning C. Are there good reasons -apart from attracting the ire of c-programmers- to not use it?
- aartaka 2y agoAuthor here. I'll be glad to accept any contribution that makes C more readable, so PR away!
- nanis 2y ago> if (argc above 1) I give up.
- aartaka 2y agoYou're welcome!
- fnord77 2y agoI feel like this would have been cool 25 years ago
- dfox 2y agoThe code asumes that C17 has C++-style auto (https://github.com/aartaka/pretty.c/blob/master/pretty.h#L118 https://github.com/aartaka/pretty.c/blob/master/pretty.h#L11...), it does not (in C auto is storage specifier that is equivalent to no storage specifier).
- Alifatisk 2y agoIf you find this interesting, you might like libcello.h aswell! https://www.libcello.org https://www.libcello.org
- aartaka 2y agoYes, it's one of the inspirations!
- IgorPartola 2y agoFor what it’s worth this makes the same mistake that Python 2 did: string and bytes are not the same type and shouldn’t be treated as such.
- School-Cotton 2y agoWhat is your definition of "string"? If it's "human-readable text", then fine, a string is not the same thing as an arbitrary byte array. But lots of languages don't enforce that definition.
- bobbylarrybobby 2y agoWell that's the very thing: not enforcing that distinction is the very mistake in question.
- michaelsbradley 2y agoGiven the nature of it (pretty.c) and the stated intention of being "backwards-compatible with C and all of its libraries", what would make more sense than sticking with C's multibyte strings? https://en.cppreference.com/w/c/string/multibyte https://en.cppreference.com/w/c/string/multibyte
- IgorPartola 2y agoRight but pretty.c doesn’t seem to explicitly support those.
- michaelsbradley 2y agoHow so? it’s just char*
- IgorPartola 2y agoWhat if len() of a char* vs a Unicode string?
- _benj 2y ago> The goals for Pretty C are: > Provide so much syntactic sugar as to cause any C developer a diabetes-induced heart attack. :-D
- spacedcowboy 2y agoAs someone who just got diagnosed with type-1 diabetes (the auto-immune variety, not the “you eat too much sugar” variety), this was far more depressing than funny. I’m probably being overly-sensitive, but man my life has gone to shit in the last couple of years…
- jdthedisciple 2y agoSorry to hear. Honest advice: I have heard fasting + healthy diet can permanently fix diabetes (though I forgot which type) Maybe try it? Good luck in any case!
- spacedcowboy 2y agoThere’s nothing wrong with my diet, that’s type-2. Type-1 is an auto-immune disease, and in my case was triggered by a couple of years of immense stress. Cortisol and the like wreak havoc over the long term. Mine isn’t curable, I don’t have many insulin-producing cells left in my pancreas. All because two years ago, a hospital tried to make a little bit more money by turning beds faster, and fed sodium into my wife about 2x the recommended rate. Although awake, she has never recovered from the coma, and of course she has brain damage. It’s not like in the movies where you just wake up. I’m not going into details, but when you scream yourself hoarse from the agony in your limbs, and there’s no painkiller that works, and eventually you lose your voice but you’re still screaming, silently… Yeah, that’s pretty terrible. Neither of us will recover. I, at least, can manage it with insulin injections/pumps/CGM-patches… She is currently in a “mental health facility”, something she’s had happen to her several times this year. It looks pretty on the outside and resembles a prison once you step beyond the secured entry. It’s where hope goes to die. All because a “hospital” wanted to make even more money than normal, and didn’t give a shit about their actual patients. Yes, I’m bitter. I appreciate the sentiment behind your words, thank you for that. It won’t help, but still.
- pipeline_peak 2y agoFor each looks convoluted, you shouldn’t have to list the type. It should be no harder than C#’s foreach(var i in list)
- aartaka 2y agoIndeed, I might need to revisit foreach with type inference. Should be totally possible.
- sneed_chucker 2y agoCreating DSLs within C has a long tradition. Stephen Bourne wanted to write his shell in ALGOL so badly that he relentlessly beat C with its own preprocessor until it began to resemble his preferred language. https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh/mac.h https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh...
- sheepscreek 2y agoHere is an example of what we wrote using it: https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh/expand.c https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh...
- Keyframe 2y agoThis is not even half as bad as I expected it to be.
- amy-petrik-214 2y agoexcellent lore. said to have been the inspiration for the obfuscated C code contest. https://www.ioccc.org/ https://www.ioccc.org/
- mjan22640 2y agoAre variadic macros turning complete?
- ay 2y agoYes. https://github.com/rofl0r/chaos-pp https://github.com/rofl0r/chaos-pp
- taylorius 2y agoSorry for what is probably a stupid question. Does pretty.c act as a preprocessor or sorts, converting your pretty.c script into actual c, that is then compiled? Or is it a virtual machine that interprets your pretty.c script?
- aartaka 2y agoIt's a set of C Preprocessor macros, so you don't even need to somehow process the code—you just #include the header and hack away as if it was regular C!
- designed 2y agoPreprocessor of sorts. From the readme: The goals for Pretty C are: - Provide so much syntactic sugar as to cause any C developer a diabetes-induced heart attack. - Deprecate Lua, Python, JavaScript, Ruby and a dozen other languages, because Pretty C is the ultimate scripting language, but lightning-fast and strongly typed! - Including only one header (yes, Pretty C is a header-only library #include-able from arbitrary C file!) to turn any codebase into a beginner friendly one.
- wffurr 2y agoIt’s not a preprocessor or compiler. There’s no binary. It’s just a bunch of C code in a header: macros, functions, etc. that you can use to write different looking programs but that are still C.
- revskill 2y agoOr just use Ruby.
- chris_wot 2y agoI’m waiting for someone to write a lambda calculus based C++ library that allows everything to be defined in terms of function. Peano axioms and all.
- jay-barronville 2y agoThis project looks really cool! Unfortunately, there’s just way too much magic involved. In my humble opinion, C is simply not the language for this level of magic—extreme use of macros (and hidden behavior in general) is how you end up with hard-to-detect (and hard-to-debug) bugs and security vulnerabilities. As cool as this project looks, I’d never feel comfortable using it in anything serious. A+ for the effort though!
- kvirani 2y ago> Provide so much syntactic sugar as to cause any C developer a diabetes-induced heart attack. Haha love this!
- stankot 2y agoThis reminded me of ArnoldC https://lhartikk.github.io/ArnoldC/ https://lhartikk.github.io/ArnoldC/
- shiomiru 2y ago> max and min of two numbers. Influenced by windows.h I see :)
- cmcconomy 2y agosomeone's salty about tiobe!
- layer8 2y agoGiven the title, shouldn’t that be #include "pretty.c" instead of #include "pretty.h"?
- drbaggy 2y agoBeing able to control indents for a particular case { right-aligning variable names - while keeping "=" in line can be useful for instance }... Is the perfect example of why indenting should be left for the eye of the reader not the computer. It's exactly why I find python code difficult - nay impossible - to read at times. So you are really doing absolutely the opposite of what you are claiming... Obviously beauty is in the eye of the beholder - but the one thing that C got right was using "{" & "}" to specify code flow.