Comment by krackers

4 months ago

When Q folks try to write C: https://github.com/kparc/ksimple

Representative example:

  //!malloc
  f(a,y(x+2,WS+=x;c*s=malloc(y);*s++=0;*s++=x;s))     //!< (a)llocate x bytes of memory for a vector of length x plus two extra bytes for preamble, set refcount to 0
                                                      //!< and vector length to x in the preamble, and return pointer to the 0'th element of a new vector \see a.h type system
  f(_a,WS-=nx;free(sx-2);0)                           //!< release memory allocated for vector x.
  G(m,(u)memcpy((c*)x,(c*)y,f))                       //!< (m)ove: x and y are pointers to source and destination, f is number of bytes to be copied from x to y.
                                                      //!< \note memcpy(3) assumes that x/y don't overlap in ram, which in k/simple they can't, but \see memmove(3)
  //!memory management
  f(r_,ax?x:(++rx,x))                                 //!< increment refcount: if x is an atom, return x. if x is a vector, increment its refcount and return x.
  f(_r,ax?x                                           //!< decrement refcount: if x is an atom, return x.
         :rx?(--rx,x)                                 //!<   if x is a vector and its refcount is greater than 0, decrement it and return x.
            :_a(x))                                   //!<   if refcount is 0, release memory occupied by x and return 0.

Reminds me a bit of both the IOCCC and 70s Unix C from before anyone knew how to write C in a comprehensible way. But the above is ostensibly production code and the file was last updated six months ago.

Is there some kind of brain surgery you have to undergo when you accept the q license that damages the part of the brain that perceives beauty?

  • Lol no. It's just Arthur Whitney style C (with the repo in question being originally written by Whitney).

    Whitney kind of set the stage for this but it got adopted informally as a style by the people at IPSA (I. P. Sharp Associates) and spread throughout the industry.

    Whitney style C isn't great for everything but it's not bad for interpreter writing and other text/stream touching tasks.

  • author of the annotated version here.

    > representative example

    this is grossly taken out of context (which is explicitly stated at the very top of the corresponding readme, https://github.com/kparc/ksimple).

    ioccc is often brought up in such HN rants, but the comparison is off mark completely. if you can't read this, you're better off writing some python or some such. this works exactly because it is not obfuscated. this code can't be more regular. tripwires (lack of malloc errno check, possible overflow of refcount) are given as exercises.

    > Is there some kind of brain surgery you have to undergo when you accept the q license that damages the part of the brain that perceives beauty?

    this code has absolutely zero to do with commercial system k4/kdb+, where did you get that idea? this is published under MIT license. would you like to be reminded of the terms, or fancy an explanation of what they mean? all of them implicitly state that if you're unhappy with the terms you're very welcome to walk away.

    hopefully, no brain surgery required for you, god forbid. just an open mind, in methaphorical sense, healthy sense of humour and common sense that the less you type the more you think.

    you are also welcome to share examples of code which tickle your sense of beauty, authored by you or someone else.

    now, stand straight and defend your argument. we're all ears.

When EAX and RAX take too long to type.

  • oh no, not really.

    this is not writing in assembly, and definitely not in bytecode.

    but if you are indeed an elite intel(R) assembly coder, and old enough for eax and rax to have taken too much toll on you, or your time away from your family, and of your career as a computer programmer in general - you should probably consider a more modern, more simple and elegant isa, which is now omnipresent, royalty-free, has a huge encoding margin for custom extenstions, and spares you from the childhood trauma of 16 GPRs. it is called riscv.

    k family languages isn't asm. it doesn't expect you to hardly ever need anything other than x,y,z in terms of function arguments. this is why they are so conveniently implicitly auto-declared for you in your function - just reference them, done.

    it is useful, man. it works.