
Show HN: Simple virtual machine, assembler, and C compiler - ronsor
https://github.com/ronsoros/rvm
======
Galanwe
Nice 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
No 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
Wow, 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
They'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.

------
jsjohnst
The 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
The 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?

~~~
ronsor
It's the obtuse crappy macros -- I need to use fewer macros.

------
foota
I'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
It emulates a cpu, and custom assembly code is compiled and run. A simple c
compiler is in development though

------
andars
Cool 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
Yes. Its a bug with 'ret' \- try test2.c

~~~
giis
I tried with test2.c too, it hangs there too. Could you please update the
usage section and also if possible add screenshot of the output.

