Live data from Hacker News

PicoC: A very small C interpreter

github.com

21–30 of 51 posts

Re: PicoC: A very small C interpreter

#21
post #14

Shameless self promotion of my work in progress C compiler https://github.com/andrewchambers/cc which is attempting to create a modern cross platform C compiler. One of my goals is to make the entire toolchain rapid to port and hack on. I am pretty sick of gcc and llvm taking 20 mins just to build from source. I would love to find serious collaborators.

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

From the README:

The code is heavily inspired by https://github.com/rui314/8cc as well as http://bellard.org/tcc/. I recommend studying the source code of 8cc before contributing here, as 8cc is currently far more mature.

Re: PicoC: A very small C interpreter

#22
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.

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.

Re: PicoC: A very small C interpreter

#23
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.

Re: PicoC: A very small C interpreter

#24
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 don't think anyone's tried that. It sounds like a pretty cool idea though.

Re: PicoC: A very small C interpreter

#25

When I read "very small C" I'm reminded of the BD Software C compiler I used about 35 years ago for the 8080/Z80 and CP/M. It was relatively complete K&R C except for no floating point support. It comfortably ran in under 64K bytes of memory. That's 64 kilobytes, or as they now say kibibytes! It was quite fast to compile. It was also quite fast to run, since it generated true object code, no run-time interpreter need…

Very nice. This is the kind of philosophy I was thinking about when I wrote it.

Re: PicoC: A very small C interpreter

#26
post #22
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.

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 design: a simple lexer with a "pull" API (next()) feeds a precedence-climbing expression parser that spits out bytecode for a simple stack machine.

If you grok expr(), you grok the whole thing.

Re: PicoC: A very small C interpreter

#27

When I read "very small C" I'm reminded of the BD Software C compiler I used about 35 years ago for the 8080/Z80 and CP/M. It was relatively complete K&R C except for no floating point support. It comfortably ran in under 64K bytes of memory. That's 64 kilobytes, or as they now say kibibytes! It was quite fast to compile. It was also quite fast to run, since it generated true object code, no run-time interpreter need…

As I recall BDS stood for "Brain Damaged Software", a joke made by Leor Zolman, the author. Leor had not taken a compiler construction class, so it's not a recursive descent parser and can be confused by overly complex expressions. It also wrote the generated code into the memory that held the source, expecting that the generated code took fewer bytes than the source it was replacing.

Re: PicoC: A very small C interpreter

#28
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.

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

Re: PicoC: A very small C interpreter

#29
More than 20 years ago, I used a C interpreter called EiC.

Hmm, someone tried to resurrect it on SourceForge a couple of years ago, it seems, but it's dead again.

http://sourceforge.net/projects/eic/

Someone threw it on GitHub:

https://github.com/kungfooman/EiC-C-Interpreter

PDF of doc [2009]:

http://www.mirrorservice.org/sites/downloads.sourceforge.net...

Re: PicoC: A very small C interpreter

#30
post #14

Shameless self promotion of my work in progress C compiler https://github.com/andrewchambers/cc which is attempting to create a modern cross platform C compiler. One of my goals is to make the entire toolchain rapid to port and hack on. I am pretty sick of gcc and llvm taking 20 mins just to build from source. I would love to find serious collaborators.

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.
Post reply on HN