Live data from Hacker News

Writing a C compiler in 500 lines of Python

vgel.me

51–60 of 183 posts

Re: Writing a C compiler in 500 lines of Python

#51

This looks a lot like the Tiny Pascal compiler that BYTE published a listing of back in 1978. http://www.trs-80.org/tiny-pascal/ I figured out the basics of how a compiler works by going through it line by line.

I think Borland’s Turbo Pascal was also a single pass compiler that emitted machine code as COM files.

Surely it is a feature of all Pascal compilers that they are single pass. I thought that it was part of the specification of the language that it be possible to compile in a single pass.

Re: Writing a C compiler in 500 lines of Python

#52

Earlier quoted context omitted.

I think Borland’s Turbo Pascal was also a single pass compiler that emitted machine code as COM files.

Surely it is a feature of all Pascal compilers that they are single pass. I thought that it was part of the specification of the language that it be possible to compile in a single pass.

[deleted]

Re: Writing a C compiler in 500 lines of Python

#53
post #24

Just for comparison the LOCs for some other small C or C like compilers. It's not that far away from Ritchie's C4x86 | 0.6K (very close) small C (x86) | 3.1K Ritchie's earliest struct compiler | 2.3K v7 Unix C compiler | 10.2K chibicc | 8.4K Biederman's romcc | 25.0K

This one is certainly stretching the definition of "C like", but it's just under 512 bytes : https://news.ycombinator.com/item?id=36064971

Re: Writing a C compiler in 500 lines of Python

#54
post #32

Earlier quoted context omitted.

at the very least it'll remove a lot of 'magic' from programming. Today a lot of people seem to be not so fond of university education but I'm personally very glad it made me go through implementing a shell, a compiler, a little toy kernel and so on. The feeling that you write code somewhere in the skies and have no idea how something works underneath has always really bugged me when I've used something.

You don't need a university education to do those things, just some curiosity. The function of the university in the near future will probably just be to have like-minded curious people to discuss ideas with, and to get a better grasp of what problems need to be solved (specifically scientific ideas, rather than just applying engineering). The prestige element (specifically of certain universities over others, perhap…

A university degree is much more than this, and I think most people who view its value as “dwindling” have not had the experience…

Re: Writing a C compiler in 500 lines of Python

#55
post #8

I made similar project in TypeScript[1]. Basically multipass compiler that generates x86 assembly, compiles it to binary and runs it. The worst thing were register allocator, designing IR code and assembler. [1] https://github.com/Mati365/ts-c-compiler

Nice project!

Re: Writing a C compiler in 500 lines of Python

#57
post #54
post #32

Earlier quoted context omitted.

You don't need a university education to do those things, just some curiosity. The function of the university in the near future will probably just be to have like-minded curious people to discuss ideas with, and to get a better grasp of what problems need to be solved (specifically scientific ideas, rather than just applying engineering). The prestige element (specifically of certain universities over others, perhap…

A university degree is much more than this, and I think most people who view its value as “dwindling” have not had the experience…

It massively depends on what degree and where I suppose. Most people I know with degrees view it as a complete waste of money.

Re: Writing a C compiler in 500 lines of Python

#58
post #49

Earlier quoted context omitted.

I wonder why =+ is so obviously a mistake. It does look vaguely wrong for some reason, but I’m prejudiced by current languages.

I think because it's ambiguous with unary plus (a = +b), since C isn't supposed to have significant whitespace in most circumstances.

You also run into problems with a=*p and a=-b, which are perhaps more likely.

Re: Writing a C compiler in 500 lines of Python

#59
post #38

Earlier quoted context omitted.

It does remind me of a project [1] Andrej Karpathy did, writing a neural network and training code in ~600 lines (although networks have easier logic to code than a compiler). [1] https://github.com/karpathy/nanoGPT

This is an implementation of GPT using the pytorch library. It is not meant to be the shortest implementation of a trainable GPT, however it is very clean code. Pytorch does a lot of the heavy lifting, especially when it comes to training on multiple GPU. This implementation only works with data distributed parallel training, so one could not train models of the size of GPT-4 with it out of the box.

Perhaps they were thinking of https://github.com/karpathy/micrograd

Re: Writing a C compiler in 500 lines of Python

#60
post #58
post #49

Earlier quoted context omitted.

I think because it's ambiguous with unary plus (a = +b), since C isn't supposed to have significant whitespace in most circumstances.

You also run into problems with a=*p and a=-b, which are perhaps more likely.

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**

Post reply on HN