Live data from Hacker News

PicoC: A very small C interpreter

github.com

31–40 of 51 posts

Re: PicoC: A very small C interpreter

#32
post #30
post #14

Earlier quoted context omitted.

I am all in favor of hacking for studying and fun. But for practical reasons - are you familiar with Bellard's tcc?

The last time I checked, tcc lacked even a simple AST. This led to some pretty weird emitted code (such as swapping parameters on the stack.) Implementing an AST is not hard, just push and pop nodes on a stack. It also makes a nice front-end/back-end interface.

Eliminating memory allocations made tcc extremely fast, They use a value stack rather than an AST. I just think an AST is a bit easier to follow because it means the parser has less code generation logic embedded in it.

Re: PicoC: A very small C interpreter

#34
post #30

Earlier quoted context omitted.

The last time I checked, tcc lacked even a simple AST. This led to some pretty weird emitted code (such as swapping parameters on the stack.) Implementing an AST is not hard, just push and pop nodes on a stack. It also makes a nice front-end/back-end interface.

Eliminating memory allocations made tcc extremely fast, They use a value stack rather than an AST. I just think an AST is a bit easier to follow because it means the parser has less code generation logic embedded in it.

No malloc/free necessary. Just pile up nodes on a stack, then "deallocate" to any saved position.

Re: PicoC: A very small C interpreter

#35
post #16

Hi, author here. When I started writing PicoC it was mostly because I was thinking about how small AppleSoft BASIC was back in the day and I was curious how small you could make a C implementation. I also had in mind to use it for robotics/drone scripting on STM32 processors which have about 64KB of RAM. PicoC runs ok in 64KB although it is a bit cramped. I like that you can write scripts in C on the actual device wi…

Hey zik, has anyone tried getting it running through emscripten? It would be very cool to have interactive C purely client side in browser. Could make some cool c based jsbin like hosting websites from that.

I tried to port fastcomp (Emscripten's llvm backbend) for that purpose. Based largely off of work Alon Zakai (kripken) already did towards porting llvm and clang to js.

Basically, I was going to compile fastcomp to js then run the commands emscripten would normally run via "arguments" function in the modules corresponding to the tool I needed (clang, llc, llvm-link and opt IIRC). But I stopped short of getting clang to work correctly with the flags I needed it to use.

I know it's possible I just needed to work around a few system dependencies like posix_spawn. I was going to trace it's use using a macro to replace calls to it with a function that prints the file and line number instead. But I got fed up with the system I was using (Amazon Linux / ssh / tablet) to do all of this on and by extension everything else that has to do with programming or trouble shooting.

Re: PicoC: A very small C interpreter

#36
post #13

c4 remains the master class in minimal interpreted C implementations: https://github.com/rswier/c4 It's (of course) less complete than PicoC, but it might be the single most useful introduction to compiler construction I've read.

The fun thing about that one is that despite its amazing simplicity it can compile and interpret itself - which is considered a significant milestone toward a "real" language implementation.

Re: PicoC: A very small C interpreter

#37

Some time back i used picoc to write c programming visualizer. Such a tool is possible only with picoc. http://dev.pointers.io/#filename=test4.c

What an amazing thing!

I wish I had this back in the day when I first learnt C and was doing all this by hand with pencil and paper. The VCR-style rewind is a nice touch.

Re: PicoC: A very small C interpreter

#38
post #26
post #22

Earlier quoted context omitted.

Rather enjoyed flicking through that. I wish there was more of an overview for those of us who haven't thought about this stuff for a few years.

I recommend: just read and reread, commenting it as you go, until it all makes sense. It's written in a very limited dialect of C --- most notably, it doesn't use structs --- because it compiles itself. The expression parser in particular would be clearer with structs rather than array offsets. But once you realize that's why it's so gnarly in places, it's straightforward to mentally translate. It's a very simple des…

Alight then, I will - though I may have to come back to you if I get stuck :) (I haven't written anything in C for about 15 years)

Re: PicoC: A very small C interpreter

#39

Earlier quoted context omitted.

Hey zik, has anyone tried getting it running through emscripten? It would be very cool to have interactive C purely client side in browser. Could make some cool c based jsbin like hosting websites from that.

Given it's written in C, it seems like a direct port to Javascript might be a much better option.

Any particular reason for that?

Re: PicoC: A very small C interpreter

#40
post #11

I have used this to bring a form of scripting to the graphic calculator I used and hacked on during my high school years. I think this really shines on embedded systems: it's quite easy to map existing syscalls and functions to PicoC functions with minimal overhead, more complex things like struct passing are supported, and memory addresses can be accessed directly just like with "real" compiled C (something that has…

> more complex things like struct passing are supported, and memory addresses can be accessed directly just like with "real" compiled C (something that has upsides and downsides, but I like this kind of freedom a lot, and as far I know, other scripting languages like Lua don't support it).

Not Lua itself, but at least Luajit's FFI does: http://luajit.org/ext_ffi.html

Post reply on HN