lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Rob Kendrick wrote:
It's certainly interesting.  I looked at Sparse for writing a simple
compiler for my toy CPU, but gave up after hitting the wall of no

Yes, it's very annoying, but it does actually seem to work once you butt heads with it for long enough. Once you realise that sparse is actually about four different but related undocumented projects in the same directory things become easier. It's certainly the only open source C compiler front-end I've found that's comprehensible.

Could your work be modified such that it can emit some form of
intermediate code, which could then be converted to Lua or some other
high-level "language"?  Especially if you could then write that
conversion in Lua, it'd be a great way of playing with compilers for
emitting to all sorts of targets, be them toy CPUs or other languages.

Actually, sparse already does that --- *twice*. The sparse front end generates basic blocks containing SSA-form expression trees. There's also an add-on lineariser that converts the expression trees into list of pseudo-opcodes, which is what clue uses. Either way, it's actually quite straightforward to generate simple code from either one once you've got your head around how sparse works.

But I am planning to abstract out the actual nitty-gritty of the code generator so that I can make it produce code for other languages --- Javascript would be trivial, Python slightly less so but still pretty easy. The biggest problem is figuring out how to do GOTO in all these languages...

David Given