Lurker News

Comments (131) 24 threads

Earlier comments
Shared4042 replies#
It is submissions like this that keep me coming back to HN.

Even though I understand little of the OP, I understand more than last time, and have the hope to be able understand more in the future.
Thanks for the inspiration OP!

jart#
Author here. I tend to learn best by doing, so I'd say the same thing. Each time I write one of these blog posts I feel like I understand LISP a bit more and it's my pleasure to share that knowledge. Thanks for your words of support!
psyc#
It’s submissions like this that keep me coming back to HN 3 times per day, in order to see such a submission 3 times per quarter.
timonoko1 reply#
My great invention was that you dont have to collect all the garbage, only most of it. So I was assuming all the numbers on the CPU-stack were pointers to live cells. Never run out of memory because of this heresy.

    > (ncompile '(cons 1 (cons 2 3)))
    $09CB:$7163:   MOV  AX,$04 ; S-object 1
    $09CB:$7166:   PUSH AX
    $09CB:$7167:   MOV  AX,$05 ; S-object 2
    $09CB:$716A:   PUSH AX
    $09CB:$716B:   MOV  AX,$06 ; S-object 3
    $09CB:$716E:   MOV  BX,AX
    $09CB:$7170:   POP  AX
    $09CB:$7171:   CALL $05AE   ; cons
    $09CB:$7174:   MOV  BX,AX
    $09CB:$7176:   POP  AX
    $09CB:$7177:   CALL $05AE   ; cons
    $09CB:$717A:   JMP  $1DA7
      (subru: eval=$7163, compile=$3B6F)
jart1 reply#
One thing you can do is `push $05` which only needs 2 bytes. The idea of using the x86 stack instructions to construct cons cells is potentially brilliant. I experimented with it a little bit. Couldn't make it work. For example, here's how it changes certain aspects of the implementation:

    Cons:   xchg    %sp,%cx            # Cons(m:di,a:ax):ax
            push    %di
            push    %ax
            xchg    %sp,%cx
            mov     %cx,%di
            xchg    %di,%ax
            ret

    Apply:  ...
            xchg    %cx,%sp
    Pairlis:test    %di,%di           # Pairlis(X:di,Y:si,a:dx):ax
            jz      1f                # for x,y in zip(X,Y)
            push    (%bx,%di)         #      (- . y)
            push    (%bx,%si)         #      (x . y)
            push    %sp               #     ((x . y))
            push    %dx               #     ((x . y) . a)
            mov     %sp,%dx           # a = ((x . y) . a)
            mov     (%si),%si
            mov     (%di),%di
            jmp     Pairlis
    1:      xchg    %cx,%sp
            ...
timonoko#
My "compiler" was totally context-free and made in assembler. Which means that it compiles every instruction in complete vacuum and assumes stuff comes in AX and BX registers (with rest-pointer in CX, I think). And result in AX.

But this proved not to be a bad start at all. Once you understand the limitations of the "compiler", you can modify the macros accordingly. One of the feature of the compiler was that it assigned absolute memory places for variables, so you could stop wasting stack and do early assignments to temporary variables.
Unfortunately the source is quite incomprehensible now because of insane use of nested macros: https://github.com/timonoko/nokolisp
But the example given above works, no doubt about it:

    > (setq test (ncompile '(cons 1 (cons 2 3))))
    > (test)

    (1 2 . 3)
chubot1 reply#
Wow, when did the C-JavaScript polyglot thing happen? I don't recall that, and looking back on the October article, it doesn't mention that. What's the motivation for it?

https://news.ycombinator.com/item?id=29047584
I would expect that to add a constraint that makes the code bigger, not smaller? Or I'm probably misunderstanding how the different variants of the interpreter work.

jart2 replies#
SectorLISP has always had a C implementation for explainability. It started off as ugly C because it was actually used to generate the assembly code for the first ~900 byte version. See https://github.com/jart/sectorlisp/blob/a561e031aec03270459f... and https://github.com/jart/sectorlisp/blob/a561e031aec03270459f... Once we reached 512 bytes I started deleting a lot of the C code since things like assembly macros weren't needed anymore, since the assembly was now being written by hand. https://github.com/jart/sectorlisp/blob/main/lisp.c

Once I cleaned up the C code, I noticed that the entire program didn't use pointers at all! (Except of course to interop with Bestline, but that could be replaced with fgetwc() instead). That's when the idea occurred to me that, since it didn't use pointers, it was also technically valid JavaScript too. So I asked around on Twitter to see if anyone's done a C / JS polyglot before. I got some helpful tips from a code golfer in Estonia who experimented with the idea and he told me about the paragraph separator trick. https://twitter.com/angealbertini/status/1463755612345540611 So I updated the C code to hybridize the two: https://justine.lol/sectorlisp2/lisp.js It's a shell script and a C program and it runs in the browser unaltered without needing to be compiled.

int_19h1 reply#
For the sake of just-for-lulz purity, you could #define var auto instead, and then it wouldn't have any uses of int in the code at all.
jart1 reply#
Wow. I didn't notice that. A real C program without types, pointers, or macros. Who'd've thought such a thing would be possible!

Continue this thread — 1 reply

machawinka#
This blew my mind.
Waterluvian#
This inspires me to craft a language just for fun.

I absolutely LOVE projects that immediately build on themselves. I wrote a GameBoy emulator from scratch and without any prerequisite knowledge and it felt amazing because of how incremental it is: you can break it into thousands of sensibly sized pieces.
I’m looking for the next project that’s like that. At the moment it’s an ECS video game.
I think work is what makes me seek projects with really good feedback loops.

Load more comments