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…
Writing a C compiler in 500 lines of Python
101–110 of 183 posts
Re: Writing a C compiler in 500 lines of Python
#102Earlier 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.
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
#103Earlier 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.
Some dialects and optimising compilers, had multiple passes.
Re: Writing a C compiler in 500 lines of Python
#104Earlier 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?
Re: Writing a C compiler in 500 lines of Python
#105> 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
Re: Writing a C compiler in 500 lines of Python
#106For 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…
RatC did not need 500 lines for its preprocessor support, by the way.
Re: Writing a C compiler in 500 lines of Python
#107This 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.
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
#108Re: Writing a C compiler in 500 lines of Python
#109So, given the python is an interpreter and very well understood, can we say that we are sure this compiler does not include Thompson virus?
Re: Writing a C compiler in 500 lines of Python
#110Earlier 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…