Live data from Hacker News

Writing a C compiler in 500 lines of Python

vgel.me

111–120 of 183 posts

Re: Writing a C compiler in 500 lines of Python

#111
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.

Yeah, nowadays a DSL is almost never a good idea. A library for some pre-existing flexible language can do the job without reinventing a whole lot of wheels. The most specific languages I can think of that make sense are SQL and Solidity. But DSLs are tempting, especially among the more passionate programmers, so at work we've ended up with a lot of them. All of them are tripping hazards.

I'm wondering, is Bash/Shell a DSL?

You can do anything with it because it allows launching other programs, but I'd say it was designed specifically for the domain of launching other programs and building data pipelines with their in- and outputs.

Re: Writing a C compiler in 500 lines of Python

#112
post #107

Earlier quoted context omitted.

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.

I think Turbo Pascal had the capability of generating code directly to memory without generating disk files.

Re: Writing a C compiler in 500 lines of Python

#113
post #82
post #72

Writing your own compiler - demystifies compilers, interpreters, linkers/loaders and related systems software, which you now understand. This understanding will no doubt one day help in your debugging efforts; - elevates you to become a higher level developer: you are now a tool smith who can make their own language if needed (e.g. to create domain specific languages embedded in larger systems you architect). So cong…

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 wrote so many compilers and interpreters in my life, simply because I cannot stand the long compile times and waiting while developing. Only single pass to make it as fast as possible, foregoing many optimisations (I don’t need them during dev); I wrote them for parts (so basically dsls which are compatible with the original compiler of the respective language) of c++, Java, c#, typescript etc. It gave us a competitive advantage as others were waiting for builds. Then I went back to Lisp (sbcl) and found it to be perfect; instant feedback, high performance, trivial to make dsls.

Re: Writing a C compiler in 500 lines of Python

#115
post #107

Earlier quoted context omitted.

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.

I think Turbo Pascal had the capability of generating code directly to memory without generating disk files.

It did that on floppy based systems too. The '87 equipped pcs in the physics building were much faster at 4500 line project, which I worked on for a year. So... First thing to test on a new 33Mhz 386 w/ wolf3d (install only, no source) my project loaded and 33mhz itt 387. Less than 2 seconds. If you brought everything into the ide,and hit run... It screamed.

Re: Writing a C compiler in 500 lines of Python

#117
post #10

Earlier quoted context omitted.

Linked from another thread: http://cm.bell-labs.co/who/dmr/chist.html It explains the memory limits and what happened :) > After the TMG version of B was working, Thompson rewrote B in itself (a bootstrapping step). During development, he continually struggled against memory limitations: each language addition inflated the compiler so it could barely fit, but each rewrite taking advantage of the feature reduced its s…

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

>I wonder why =+ is so obviously a mistake

Consider

  x = -5
now how about:

  x =- 5
One makes x negative 5, the other subtracts 5 from it. And under this design of the operator the only difference is a space.

It's the same with +, just that we're not used to seeing unary plus in the wild.

Re: Writing a C compiler in 500 lines of Python

#120
post #72

Writing your own compiler - demystifies compilers, interpreters, linkers/loaders and related systems software, which you now understand. This understanding will no doubt one day help in your debugging efforts; - elevates you to become a higher level developer: you are now a tool smith who can make their own language if needed (e.g. to create domain specific languages embedded in larger systems you architect). So cong…

[deleted]
Post reply on HN