Live data from Hacker News

Writing a C compiler in 500 lines of Python

vgel.me

101–110 of 183 posts

Re: Writing a C compiler in 500 lines of Python

#101
post #89
post #87

Earlier quoted context omitted.

> And somehow I have ended up with a very strong bias against DSLs. Most DSLs are bad, partially because most people are bad at designing languages. Embedded DSLs can be quite neat. Eg Haskell makes it easy to embed something like DSLs inside your Haskell code. (If you squint a bit, the ability to define your own functions in any language goes in that direction of allowing you to define your own mini-sub-language tha…

I don't necessarily think the DSLs are badly designed. But personally I find the overhead in learning and remembering a new language to be enormous. I instantly drop from extremely high productivity in my language of choice to fumbling around like a newbie. And often DSLs are used for smallish tasks that you work on, write the code, then leave for a long time. Then you come back to it and have no idea how that langua…

The issue is that a good DSL shouldn't be a "language", it should be a tool, a textual UI that is designed by and for power users for a particular task. When you are doing something 500 times a day you want a hyper efficient tool to accomplish that. Learning curve is irrelevant, only efficiency and expressibility. A DSL is a great fit. If you are doing some task once a month you just want something simple you can wrap your head around quickly. The problem is that the former often forget that the latter exists and so recommend their workflow to everyone, forgetting they have different needs. But both sets of needs are valid.

Re: Writing a C compiler in 500 lines of Python

#102
post #82

Earlier quoted context omitted.

I dunno. I did a compiler writing course once, writing a compiler for a subset of Pascal in Ada, generating a kind of quasi assembly. It was a team project. I did most of the codegen and static optimisation. It was super fun and interesting. But I wouldn't say it was a terribly useful exercise that has greatly enriched me as a programmer. And somehow I have ended up with a very strong bias against DSLs.

I thought the same when i did compiler course, but 8 years after that course i wrote tooling to mass refactor a java codebase by creating AST manipulate AST and convert the AST to code again.

Oh, yeah, true. I have done a couple of pretty complex things that involved parsing code or manipulating ASTs and it was probably useful for that.

I attempted to write a parser for Wikitext once in ANTLR which was an interesting nightmare.

Re: Writing a C compiler in 500 lines of Python

#103

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.

Not all, although the language was designed that way.

Some dialects and optimising compilers, had multiple passes.

Re: Writing a C compiler in 500 lines of Python

#104
post #85

Earlier quoted context omitted.

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.

There's a bunch of LLVM-based Pascal compilers these days. I doubt they are single pass, given how LLVM works. (And in general, any optimizing compiler is most likely doing multiple passes.) You are right about Pascal's original design. Though I'm not sure if that's still true about modern versions of the language?

Not even old ones, if we taken optimising compilers like VMS Pascal into account.

Re: Writing a C compiler in 500 lines of Python

#105
post #13
post #6

> Instead, we'll be single-pass: code generation happens during parsing IIRC, C was specifically designed to allow single-pass compilation, right? I.e. in many languages you don't know what needs to be output without parsing the full AST, but in C, syntax directly implies semantics. I think I remember hearing this was because early computers couldn't necessarily fit the AST for an entire code file in memory at once

using recursive descent, you don't need to build an ast

Only if the compiler doesn't do anything beyond basic peephole optimizations.

Re: Writing a C compiler in 500 lines of Python

#106
post #12

For some value of “C”: > Notably, it doesn't support: > structs :-( would be possible with more code, the fundamentals were there, I just couldn't squeeze it in > enums / unions > preprocessor directives (this would probably be 500 lines by itself...) > floating point. would also be possible, the wasm_type stuff is in, again just couldn't squeeze it in > 8 byte types (long/long long or double) > some other small thin…

Basically like many C compilers outside UNIX during the 1980's.

RatC did not need 500 lines for its preprocessor support, by the way.

Re: Writing a C compiler in 500 lines of Python

#107

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.

It makes development so much more fun when you see the results right away.

Pressing "build" in Turbo Pascal on my 386sx it was already done before you could even perceive any delay. Instant.

Re: Writing a C compiler in 500 lines of Python

#110
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…

That was already and always the function and value of a university... Notice the academics aren't sitting in lectures 6 hours a day or trying to pass exams to obtain degrees. They're the longterm users of universities and educating themselves their entire lives.
Post reply on HN