4 ms·
Show HN: Simple virtual machine, assembler, and C compiler
- foota 10y agoI'm on my phone so I didn't get a good look, but it looks like this compiles a subset of c? Is this accurate?
- ronsor 10y agoIt emulates a cpu, and custom assembly code is compiled and run. A simple c compiler is in development though
- jsjohnst 10y agoThe code style really hurts to read, but this is a pretty cool demo of how to do this. Congrats ronsor, good work! Don't try to run anything in the VM that's larger than 4K though right now... ;)
- userbinator 10y agoThe style looks fine to me, it's typical C, although the indentation is a bit inconsistently weird in some places. Perhaps you're used to a different style/language?
- andars 10y agoCool project. Virtual machines and assembly are always fun. Not sure if the C compiler is quite ready for the headline, though. I was trying to get the test.c program to run on the vm, but after compiling and assembling, the vm just hangs. Is that expected?
- ronsor 10y agoYes. Its a bug with 'ret' - try test2.c
- Galanwe 10y agoNice tiny little project, but the headline is a bit clickbait. I wouldn't really call it a C compiler, all it does is read a file, look for 3 or 4 hardcoded strings like "int" or "char*" or "return" and generate some really simple labels/movs in consequence.
- userbinator 10y agoNo kidding, it's barely got a parser. C4[1] on the other hand, is still tiny enough to comprehend, does have a pretty complete parser, and probably wouldn't be too difficult to modify to generate code for this VM. [1] https://news.ycombinator.com/item?id=8558822 https://news.ycombinator.com/item?id=8558822
- taneq 10y agoWow, calling yourself "C in 4 functions" when those functions are effectively minimized and with not a whole bunch of whitespace is a bit cheeky. Might as well strip out all but four newlines and call it "C in 4 lines". :P Still looks well worth a read, though.
- vidarh 10y agoThey're not minimized. They use slightly short variable names, but there's few of them and they make sense very quickly. It does use a few unnecessarily clever tricks, like the opcode selection as indexes into a string, but other than that, as is pointed out in that thread: if you've read a Wirth-style compiler before (or Wirths books on compile construction, which will have you read a Wirth-style compiler...) it becomes fairly simple to understand.