Live data from Hacker News

Writing a C compiler in 500 lines of Python

vgel.me

61–70 of 183 posts

Re: Writing a C compiler in 500 lines of Python

#62
post #23

Earlier quoted context omitted.

> 0.1 Bellard Off topic, but a log scale might be useful: 0.1 Bellard --> -10 deciBellards. That allows for: 0.001 Bellard --> -30 deciBellards. Problem: Programmers with negative productivity cannot be represented on the same log scale.

Sure they can. Abs(). Or if you prefer to not have quite so much micro-nano, you could also use Cumulative Distribution Functions [1] which are basically just sums of Probability Density [2]. Are they a 4σ programmer, 1σ programmer, -0.5σ programmer, -2σ programmer? Plus, most people are "average" not negative productivity, and CDFs let you use really fun stuff like Beta Distributions (variable, shapable distribution…

The number of people that are average is infinitesimal, even if you define average as “mode” instead of median or mean.

About half are better than median though.

Re: Writing a C compiler in 500 lines of Python

#63

Earlier quoted context omitted.

> 0.1 Bellard Off topic, but a log scale might be useful: 0.1 Bellard --> -10 deciBellards. That allows for: 0.001 Bellard --> -30 deciBellards. Problem: Programmers with negative productivity cannot be represented on the same log scale.

>>> Problem: Programmers with negative productivity cannot be represented on the same log scale. This is similar to the problem of price-to-earnings ratio. The ratio goes asymptotic as earnings goes through zero. It would be better to quote earnings-to-price ratio. Another screwy reciprocal unit is miles per gallon for cars.

Now that I have an EV with 130 miles of range, I’m finding miles per kwh to be much more useful than efficiency.

I’d bet range anxiety was also a thing for early gasoline powered cars, so the early adopters of those probably preferred mpg over gpm.

Re: Writing a C compiler in 500 lines of Python

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

I found that attending my home state university afforded me access to facilities and resources I would never have had on my own, and to not just a wealth of knowledge but also mentors and incredibly capable guides for how to navigate that knowledge while putting into productive practice.

Motivation and internet access can get some of those things, but even among those things one would struggle to find them in one place and so readily available to them.

Re: Writing a C compiler in 500 lines of Python

#65
post #54

Earlier quoted context omitted.

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 massively depends on what degree and where I suppose. Most people I know with degrees view it as a complete waste of money.

Most people with a university degree don’t have a traditional university education.

I’m very much pro-education, and think that US high school should include the first two years of a current university education (grades should be roughly split between D, C, B and A, where D means “meets curriculum requirements”).

After that, I don’t think many people should go to a university. Instead, the state should provide up to 10 years of vocational education (splittable and redeemable at any point in life) or 8 years of university education (subject to a B or better average in high school).

Most vocational programs would be under 3 years, so you’d get three-four cracks at finding the right profession for you.

University track would be targeted at educators, researchers and entrepreneurs.

I think this would be better for every socioeconomic demographic in the US, and also help alleviate the housing shortage, enable modern manufacturing, fix many current issues with healthcare, and so on.)

Re: Writing a C compiler in 500 lines of Python

#66

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…

[deleted]

Re: Writing a C compiler in 500 lines of Python

#68
post #54

Earlier quoted context omitted.

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 massively depends on what degree and where I suppose. Most people I know with degrees view it as a complete waste of money.

[deleted]

Re: Writing a C compiler in 500 lines of Python

#69
post #13

Earlier quoted context omitted.

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

the call stack during recursive descent is an ephemeral ast, for the recursive descent parsers I've written.

That implies the memory usage is proportional to the depth of the AST, not the total size (which is the point, I think).

Re: Writing a C compiler in 500 lines of Python

#70

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 optimisation steps is of course far more complex and more modern languages have many more features, but C is really pretty simple (the K&R book is concise and good!) as it was built to work on the computers of half a century ago.

Post reply on HN