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.
Writing a C compiler in 500 lines of Python
51–60 of 183 posts
Re: Writing a C compiler in 500 lines of Python
#52Earlier 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.
Re: Writing a C compiler in 500 lines of Python
#53Just 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
Re: Writing a C compiler in 500 lines of Python
#54Earlier 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…
Re: Writing a C compiler in 500 lines of Python
#55I 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
Re: Writing a C compiler in 500 lines of Python
#56I have to wonder if there's a Scheme to WASM compiler out there someplace right now I haven't found yet.
Re: Writing a C compiler in 500 lines of Python
#57Earlier 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…
Re: Writing a C compiler in 500 lines of Python
#58Earlier 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.
Re: Writing a C compiler in 500 lines of Python
#59Earlier 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.
Re: Writing a C compiler in 500 lines of Python
#60Earlier 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.
Kind of how we use (*p)->next instead of *p->next where p is node_t**