Live data from Hacker News

Writing a C compiler in 500 lines of Python

vgel.me

151–160 of 183 posts

Re: Writing a C compiler in 500 lines of Python

#151
post #93

Earlier quoted context omitted.

Infix parsing chews up a remarkable amount code and memory. It's scary just how much easier it is to parse languages without infix parsing.

where it takes up the memory in the human brain, where we have more limited working set, than in computers. That is probably why postfix notation is mostly a thing of the past by now in languages intended to be used by humans.

(in (is "prefix notation" (and "alive" "kicking")) "all lisps")

Re: Writing a C compiler in 500 lines of Python

#152
I am pretty certain the following is a valid "for"-loop translation:

    block
        ;; code for "i = 0"
        loop
            ;; code for "i 
It doesn't require cloning the lexer so probably would still fit in 500 lines? But yeah, in normal assembly it's way easier, even in one-pass:

        ;; code for "i = 0"
    .loop_test:
        ;; code for "i 
Of course, normally you'd want to re-arrange things like so:

        ;; code for "i = 0"
        jmp .loop_test
    .loop_body:
        ;; code for "j = j * 2 + 1"
    .loop_incr:
        ;; code for "i = i + 1"
    .loop_test:
        ;; code for "i 
I propose the better loop syntax for languages with one-pass implementations, then: "for (i = 0) { j = j * 2 + 1; } (i = i + 1; i < 5);" :)

Re: Writing a C compiler in 500 lines of Python

#154
post #149
post #94

Earlier quoted context omitted.

I wasn't really aware of the embedded DSL distinction. But even then. I was never really able to get into Observable because although it looks like JavaScript, there are kind of hidden objects and methods available to you that I found weirdly hard to discover.

What's 'Observable'?

https://observablehq.com

Kind of like Jupyter notebooks for JavaScript? Kind of?

Re: Writing a C compiler in 500 lines of Python

#155
post #151

Earlier quoted context omitted.

where it takes up the memory in the human brain, where we have more limited working set, than in computers. That is probably why postfix notation is mostly a thing of the past by now in languages intended to be used by humans.

(in (is "prefix notation" (and "alive" "kicking")) "all lisps")

    (- "alive"
         (and "kicking")
         (is "prefix notation" )
         (in "all lisps"))

Re: Writing a C compiler in 500 lines of Python

#158
post #60

Earlier quoted context omitted.

But they could've fixed that by going a=(*p) and a=(-b); Kind of how we use (*p)->next instead of *p->next where p is node_t**

That seems a little backwards and barbaric, though right? Imagine if we had to watch out for this as a common pitfall: // BUG! Actually subtracts x from current val of neg_x. neg_x = -x; Even moreso, how would these two lines behave? Would they differ in semantics? n = -5 n =- 5 Overall, -= is just so much less ambiguous. EDIT: To your point about ->, I personally think C would be better if: *p->next parsed as: (*p)-…

I've found struct fields that are pointers far more common than pointers to pointers to structs, so if nothing else it feels like *(p->target) is a more widely useful interpretation of *p->target than (*p)->target would be.

Regarding "n = -5", it would presumably be interpreted as "n=(-5)", same as today. Operators don't have spaces in them. So "n- -5" is "n-(-5)", rather than "n--5" (not valid).

Re: Writing a C compiler in 500 lines of Python

#159
post #144
post #50

I have to wonder if there's a Scheme to WASM compiler out there someplace right now I haven't found yet.

Have you seen Guile Hoot? https://gitlab.com/spritely/guile-hoot

No, thanks! Had a look, doesn’t seem to be ready to support WASI, but it’s active.
Post reply on HN