535 points · by mntmn · 13y ago · 157 comments
- As I said in an earlier comment, "var" is just a typedef'd "void *". The downside is that libCello code is essentially untyped, but the upsides are that the C preprocessor is now enough to do the processing needed for the rest of the macros in the language, and that you can still mix regular typed C with libCello code for a "best of both worlds" mix.
- Looks pretty, right? What you're responding to is not just the nice non-capitalized macros and the $ keyword, but the syntax highlighting in his examples. Fire up a text editor and write you some libCello code without this highlighting and it probably won't feel as nice.
I'm extremely interested in the idea of taking the syntax highlighting, formatting, and code completion OUT of these specialized IDEs and plugins and into some kind of standard "bidirectional channel" between a language processor or compiler, its macro system, and the user's editor of choice.
We should be able to make entire DSLs and specialized syntaxes that not only compile, but are able to provide rich information to the surrounding development environment in a standardized way. I'm not alone on this. F#'s Type Providers do exactly that. But imagine being able to control not only the "intellisense", but also the syntax highlighting, argument/return tooltips, documentation, preferred formatting, snippets, etc.
And by "surrounding development environment" I mean everything from the command line to vim and emacs all the way to Sublime Text, Eclipse, and Visual Studio. Even github! Why do you have to register a new syntax highlighter on github for a language and hope they turn it on?
http://bsumm.net/2012/08/11/steve-yegge-and-grok.html
http://www.youtube.com/watch?v=KTJs-0EInW8
HN discussion: https://news.ycombinator.com/item?id=4371267
I've long wanted to see something like an Eclipse daemon offering tool support to external editors etc. Ensime [1] does something along these lines, getting Scala' compiler functionality into Emacs using the Swank protocol from Slime [2].
Well maybe if we stopped using ASCII files as our source code and used a richer type of document we could start to make some improvements!
What kind of rich document? Well, what about HTML? Did you know you can even embed interactive elements in such documents? What if we started embedding our source code and editors in our documents instead of the other way around? The whole model is inverted, but mainly for historical reasons, because you know, in 1979 all we had was VT100 to interface with our machines... it's like we never moved on... why is my "code" a splattering of ASCII files in a directory? A package definition, tests, dotfiles for this and for that... ship the editor, the tests, the docs, the examples, and the source ALL as one document!
I would say the downside of this 'rich' environment is that your tools tend to become language dependent. Early Smalltalk code versioning tools were fairly easy to build (I'm guessing), but would definitely be Smalltalk centred. The unfortunate advantage of an ASCII text file is that it very difficult to have more than one interpretation of it's non-existent meta-data.
If you look at JavaScript and the web browser... well, it's more like a Smalltalk system than a UNIX system! There are no files! It has a weird little dynamic multi-paradigm language! The web browser become the spiritual offspring of Smalltalk as soon as Brendan tacked on JS. :)
Look how WEIRD package management gets for JS when you try and make it supported by UNIX and the filesystem!
As for tools becoming language dependent... what about sourceMaps? Look at how CoffeeScript integrates with our workflow tools these days!
The biggest problem with Smalltalk was that it was made for Personal Computing, and ALWAYS had issues running other people's code or integrating well with collaborators.
The web browser and JavaScript solve a lot of these issues with the whole sandboxed code environment...
The web has NOTHING to do with files, so it seems like the perfect place to just forget about them and move forward...
Relays and switches only got us so far... Machine code only got us so far... UNIX only got us so far...
Why have we lost the courage to continue looking for better solutions and being brave enough to throw away certain concepts?
I mean, look at mobile and touch computing! iOS and Android are completely abandoning the concept of files!
Why? Because they don't really have to be there... Again, look at Smalltalk... and I know, it "failed", but do you really think it failed because it didn't have files? Is that the one reason? Is it even a reason at all??
I've been thinking about a programming system that stores all the code in a database instead. Versioning would be easier that way too.
That does not necessarily have to be the case. Just like you have abstract collections capable of handling values of any type, I don't see why it should be impossible to have a model of versionable, editable code into which you could inject your own implementations for the particular language you're coding in right now.
I realized the other day how close persistent collections and tools like Git actually are. And given how most modern languages have programs that are essentially trees or groves, and given that said persistent collections tend to be trees...? Well, what do you think?
We all agreed that was a bad idea: http://en.wikipedia.org/wiki/Opendoc
Right now the terminal is our window and it is nothing but colored ASCII. You can create something interactive and "graphical" like vim or emacs inside of it, but then you can't ship that complete dev environment very easily... it has to be rebuilt from scratch by the individual programmer.
The web makes it very easy to ship interactive, dynamic documents and environments around.
I'm also a big fan of DSLs and would love see some of the things you mention. My next (fun) big project is actually not so far from the same lines ;)
Many thanks to everyone for the kind comments! It really is encouraging. :)
I wouldn't make them exactly like C++ templates. I'd just focus on the useful things that you can't currently do in C, like write a qsort()-equivalent that doesn't suffer the performance penalty of callbacks. You can almost get there with macros, but not quite -- it would be ridiculously ugly at best.
The idea with that was that you could provide a structured description of a mini-language and it would be sufficient to generate not only a parser, but also a syntax-highlighting IDE with smart code completion, debugging and other neat features. It looks like they're still developing it too, which is a nice surprise: back when I was still interested in it, it never seemed to have taken off.
What I took GP to mean, and indeed what Yegge's Grok appears to aim for, is the need for a universal modelling framework 'standard' for languages, tools and runtimes. Some kind of equivalent to LLVM but in the 'other direction' for modelling language semantics instead of code. Once a tool (e.g. editor) implements that standard it is able to 'plug in' to any tooling that also implements the standard. Potentially eliminating huge amounts of duplicated effort for tool makers etc.
http://clang.llvm.org/doxygen/group__CINDEX.html
(It is what Xcode uses underneath, a C API that any IDE or dynamic language module could take advantage of)
I spent the better part of the last two years writing a (closed source, sorry) library that does some of this, and some other stuff besides (state machines, events, 'proper' strings with automatic garbage collection and allocation, message passing).
Maintaining static typing was a big pre-requisite for that library, without it too much of value would be lost to offset the gains. It was a very educational project for me, it definitely re-inforced the 'half of common lisp' meme.
To program a piece of software using that library no longer felt like programming in C, every now and then you'd see a bit of C shine through in the lower level code. The whole thing relied to a ridiculous degree on macro cleverness (something to be avoided, for sure) and other deep knowledge of how C works under the hood to get certain effects, and I found this part of it less than elegant (even if the results were quite a big step up from programming in C).
The main justification for doing all this was to straighten out a project that had become bogged down under increasing complexity and a failure to abstract out the common elements. Choosing C for this project was a poor decision but since there was not going to be any budging on that front I tried to make the job work out as good as possible.
It's quite interesting to see how far you can push C but at the same time you really have to ask yourself if you are on the right road if you find yourself doing things in such a manner.
Like Cello, the lib I wrote is a way to force the language to become another language, which always has drawbacks in terms of unintended side effects and long term support.
Probably better to switch to a platform that is closer to your problem domain (in this case, such as erlang, clojure or even go), as much as I liked tinkering with C it felt like we were making life harder than it needed to be.
I'm looking for standards / set of libraries / best practices for "modern" C development, but I've yet to find a comprehensive resource.
Stuff like typedefing a manual fixed sized int type to be cross-platform compatible, that books don't really tell you to do but are important and come up often.
I'd be okay with a small, well written example library too. Does anyone happen to know something like this?
edit: Ah, sorry if I misled you, that was just an example of the kind of tips and pointers I was looking for. Or weird bits like the linux kernel list_head. http://kernelnewbies.org/FAQ/LinkedLists Or common libraries like bstring that make life easy. Or even a single, comprehensive implementation of good data structures, since everyone seems to have their own vector.h and/or hash.h that fails to cover much other than their own use case.
http://www.amazon.com/Interfaces-Implementations-Techniques-...
It uses Donald Knuth's literate programming which takes getting used to, however, it's a great case study of how to make a good library.
There is a new book on "21st Century C" which I haven't read. It has mixed reviews, however.
http://www.amazon.com/21st-Century-Tips-New-School/dp/144932...
The fixed-size int finally got a permanent solution: #include <stdint.h>
If you are targeting autoconf/automake as your build system, that has a lot of built-in solutions to portability issues, like defining macros. It's not easy to learn, and I don't pretend to know it well, but when I'm compiling someone else's project, I'm always happy to see a configure script.
The gtk project's libglib is a pretty comprehensive library of C data structures. The Apache Portable Runtime serves a similar purpose: http://en.wikipedia.org/wiki/Apache_Portable_Runtime
What I find annoying about libraries like glib is that they tend to impose their own style on your project by using their own typedef'ed types and such.
If you don't mind it, you can cobble together your own data structures from various open-source projects. Judy arrays are pretty fast, and you can use them in a variety of ways. Searching google for "c hash table" came up with a lot of excellent results, so try googling whatever data structure or algorithm you need, and chances are, you'll find something.
Then you have https://wiki.gnome.org/Vala which is a whole new language built on top of C + Glib/Object whose main selling point is that it compiles to, and is totally compatible with, plain C code.
Do keep in mind that all of this, Glib, Vala, Cello and other "make C more like a high-level language" are basically hacks to workaround the fact that C is a very low-level language and lacks many powerful features. I believe one is much more productive using Glib + C than just plain C, but you are still less productive than if you had choosen a modern language in the first place.
If you're lucky enough to be able to rely on C99: https://en.wikipedia.org/wiki/Stdint.h#Fixed-width_integer_t...
A fair bit of discussion around your standard exploits as well.
$ gcc -lCello -std=c99 -fnested-functions example.c -o example
Hope this helps someone!
I personally have no problem with it (other than the syntax needed to unbox/rebox values). I find that having the freedom to use a type system or not a very compelling feature in a language.
That being said, I understand why it might sit very poorly with some.
Source: someone who hasn't done much C and only a little bit of Go. So take it with a grain of salt.
For example, ’alloc’ and all ’init’ returns it.
non
typedef struct objc_class *Class;
typedef struct objc_object {
Class isa;
} *id;
with struct objc_class {
Class isa;
/* objc v1 members omitted */
};Gonna read some more when I have time, I could learn something from this.
I love the idea!
Hopefully, I can manage to make it work with CoreFoundation in OS X.
Maybe I'll end up with CoreFoundation+ :)
Side note: this book would certainly be down-voted on r/programming but I expect more grown-ups here.
But I think there could be quite interesting uses for Cello, namely you do your prototyping with it and then you trow out the library and refactor the code to pure C.
In the same vein, but more C-like: http://p99.gforge.inria.fr/p99-html/.
#define foreach(x, xs) \
for(var x = iter_start(xs); x = iter_next(xs); x != iter_end(xs))
It's all C: http://libcello.org/documentation/hacking foreach(x, get_next(queue)) f(x);
To solve this problem, with GCC, you have the option of defining foreach as { typeof (xs) _xs = xs; \
for (var x = iter_start(_xs); x = iter_next(_xs); x != iter_end(_xs))
but then you need a corresponding endforeach to apply the matching }. If you just leave off the opening {, then you end up with a mysterious compile error on if (ready(queue)) foreach(x, get_next(queue)) f(x);
since the declaration of _xs is not a statement.And this kind of thing is why a "macro system" means something very different in C and in Lisp, and why Brad Cox wrote a compiler in 1983 instead of a macro library.
#define foreach(x, xs) \
for (var _xs = xs, *x = iter_start(_xs); x = iter_next(_xs); x != iter_end(_xs))
Because var is just void* and anything you can iterate over will also be a void*.1. sizeof(function) -> would give user ability to copy functions around.
2. maybe new reserved keyword _Func -> function tagged with _Func would indicate that function must be compiled as function object (defined sizeof) and compiler needs to address fact that function may be moved around and used (relative addressing and i guess bunch or others problems that would arise). Only code, nothing to do with ELF or other formats.
Another interesting thing to do would be to, somehow, eliminate function pointers with _Func.
In any case, user would be responsible for creating environment for that (lambda?) functions, like manually setting mprotect or setting up stack (prologue.h & epilogue.h ???).
_Func int example_prototype(int i, int j) {
return i + j;
}And then do something like:
example_prototype func0;
memcpy(&func0, example_prototype, sizeof(example_prototype));
struct prologue_t *p_ptr = &func0;
p_ptr->sp = 0xdeedbeef; // Or some address that is used as stack
z = func0(5, 6)
So, what do you thing how hard would it be to implement something like that?
I am not a fan for C++ syntax, can I still get something from cello?
var int_item = $(Int, 5);
over this: int int_item = 5;
What am I missing?var item = $(Int, 5); ... code ... item = $(String, "5");
Not that you want to do that all the time but it is a way to implement duck typing
var my_func(var int_or_string, var int_or_obj) { ... return int_or_string_result; }
Simplifies certain operations, complicates debugging. Great for small scripts though.
totally awesome!
So no. It isn't object oriented, but it does some nice-looking things with structured types.
For example, there's a "show" class that converts your object into a string. Any object that implements the necessary methods can thus be printed.
Another example is an iterator class. If you implement a couple of methods (move the cursor to the start, increment the cursor, see if the cursor is at the end, and get the currently pointed-at item), then you can be iterated over by a foreach loop.
Unlike Go, though, with Cello you seem to need to explicitly specify the functions used to implement each class.
Does it do RAII? Really, calling destructors implicitly on auto variables on scope exit is the main thing missing in C here.