Hacker News new | ask | show | jobs
by stevekemp 2224 days ago
I wrote something very similar recently, also a single-pass, but with a trivial internal representation:

https://github.com/skx/math-compiler

The real difference between the compiler in the example, and mine, is that I handle floating-point operations and also explicitly use printf to show the output. (Because otherwise your return value is limited to an 8bit number.)

2 comments

This is nice. The -debug flag is a cool feature. But if you first build a representation of the whole program in memory, then traverse that representation to generate code, it's not really single-pass.
Sorry my comment was wrong, I should have said "not a single-pass", that's why I mentiond the trivial internal representation.

Too late to edit now!

Because otherwise your return value is limited to an 8bit number

Only on Linux; Windows lets you use the full 32 bits.

That's true. Though I guess even there there are limitations, because you can't return non-integers :)