Live data from Hacker News

Writing a C compiler in 500 lines of Python

vgel.me

71–80 of 183 posts

Re: Writing a C compiler in 500 lines of Python

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

The "uni degree value" discussion on here is very biased towards the US, where expensive courses exist, some excellent but others perhaps not providing sufficient value for money.

Note that in several countries in Europe, studying is free of charge or only costs a symbolic fee (scholarships aside).

The university system is thousands of years old, and it would be disastrous if it were abolished. I'm a self-taught developer, who also obtained two Masters and a Ph.D. later, and I can attest to it that the speed of learning at uni cannot be compared with the speed of learning via self study, especially self-study in isolation.

Having said this, learning only in groups/herds is also not the best approach IMHO, there is something to sitting alone and figuring something out without external help, at least occasionally, as it trains your analytical skills, your ability to concentrate and it gives you grit/perseverance.

Re: Writing a C compiler in 500 lines of Python

#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 congratulations, on top of other forms of abstraction, you have mastered meta-linguistic abstraction (see the latter part of Structure and Interpretation of Computer Programs, preferably the 1st or 2nd ed.).

Re: Writing a C compiler in 500 lines of Python

#73
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 would be great if there were more emphasis on some of the aspects that made academia so great many years ago, but unfortunately the scene has changed dramatically.

My perspective on the value dwindling comes from several graduate degrees and teaching at some of these 'prestigious' universities in the northeast.

It is of course just my singular experience, but the handful of research institutions I have worked at actually provided more of a typical 'academic' atmosphere than the universities, unfortunately. I still really want to believe that universities provide the most innovative research environment, but the incentive and operational structures don't really make it as optimal.

The reason I mention the new generation viewing its value as dwindling is that the cost isn't really 'worth it' and many of the best educational resources are actually free online now.

I'm an academic, so the fact that the investment doesn't pay off doesn't really bother me (academics typically don't care about money), but many people do want some financial advantage from education, and that isn't as present anymore.

Re: Writing a C compiler in 500 lines of Python

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

Took me seven years to do my own dynamic one that ended up being very similar to python2 with curly braces. Every programmer should try it

Re: Writing a C compiler in 500 lines of Python

#75
post #71
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…

The "uni degree value" discussion on here is very biased towards the US, where expensive courses exist, some excellent but others perhaps not providing sufficient value for money. Note that in several countries in Europe, studying is free of charge or only costs a symbolic fee (scholarships aside). The university system is thousands of years old, and it would be disastrous if it were abolished. I'm a self-taught deve…

Lectures were made obsolete by Gutenberg. There is little of value (but not none, ok) in wasting time of reputable professors on repeating the same stuff each semester to a hall of 200 students.

Seminars/labs and office hours is where the biggest value of university can come from, but that’s the part that is often left to TA…

Re: Writing a C compiler in 500 lines of Python

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

What is implied by “much more than this” and why shouldn’t those things be available to people not enrolled into a university?

Re: Writing a C compiler in 500 lines of Python

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

Took me seven years to do my own dynamic one that ended up being very similar to python2 with curly braces. Every programmer should try it

“Crafting Interpreters” is a phenomenal place to start. It’s very accessible. If you think you’re “not good enough” to write a programming language, it will show you just how wrong you are. Really boosted my confidence. (Hi Bob!)

Re: Writing a C compiler in 500 lines of Python

#78

It is interesting to think that 500 lines of code is something one can write in one or two days. But, writing a C compiler in 500 of comprehensible code (even in python) is challenge in itself that may take months after a few years of solid learning. I wonder if is this a good path to becoming an extremely productive developer. If some one spends time developing projects like this, but for different areas... A kernel…

As an experienced developer who did not do a compilers course at university I was able to write a SQL/JSONPath evaluator in TypeScript in a week or so. I don’t expect a minimal C compiler would be that much more complex. Essentially all you need is a grammar, parser library and a couple of tree walkers to convert the AST first to expand macros and then convert to assembly. A production compiler with all its optimisat…

Using a library is cheating. Any one with enough knowledge can create their own programming language in two weekends using parser combinators.

Re: Writing a C compiler in 500 lines of Python

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

How do later editions of SICP differ? I have no idea which I used.

Re: Writing a C compiler in 500 lines of Python

#80
post #60
post #58

Earlier quoted context omitted.

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

That seems a little backwards and barbaric, though right?

Imagine if we had to watch out for this as a common pitfall:

    // BUG! Actually subtracts x from current val of neg_x.
    neg_x = -x;
Even moreso, how would these two lines behave? Would they differ in semantics?

    n = -5
    n =- 5
Overall, -= is just so much less ambiguous.

EDIT: To your point about ->, I personally think C would be better if:

    *p->next
parsed as:

    (*p)->next
instead of:

    *(p->next)
but maybe now I'm not thinking through all the parsing impliciations :)
Post reply on HN