top | item 14091630

Cello – A library that brings higher level programming to C

348 points| dlsym | 9 years ago |libcello.org

151 comments

order

int_19h|9 years ago

Looking at the code, a great deal of this is non-portable and/or U.B, so I would hesitate to call it C. And I'm not talking about some hypothetical problems, but stuff that should surface quite soon. For example, this is how stack allocations are made:

    #define alloc_stack(T) ((struct T*)header_init( \
      (char[sizeof(struct Header) + sizeof(struct T)]){0}, T, AllocStack))
So far as I can see, there are basically no alignment guarantees here - the returned pointer to the char array is not guaranteed to be aligned properly for Header (which is a struct of a single void* field), nor is there any attempt to align T inside the array. If things get misaligned, on x86 and x64, it'll work (but possibly much slower than normal), but on e.g. ARM you'll get all kinds of weird things happening.

ant6n|9 years ago

Apparently ARM allows unaligned loads now (permitted in ARMv6, default in ARMv7). There seem to be some instructions that don't support it, like STM. Also, afaik code needs to be aligned properly, and function pointers need to be aligned properly lest you will switch between arm and thumb mode. Apparently you can tell the ARM compiler to assume all memory accesses are unaligned.

Under ARMv8, I think alignment is less of an issue.

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc....

AnimalMuppet|9 years ago

> Can it be used in Production?

> It might be better to try Cello out on a hobby project first. Cello does aim to be production ready, but because it is a hack it has its fair share of oddities and pitfalls, and if you are working in a team, or to a deadline, there is much better tooling, support and community for languages such as C++.

Wow. Straight talk instead of salesmanship. High marks for that.

Safety1stClyde|9 years ago

As a superstitious C programmer, typedefing (void star) feels like walking on the cracks in the pavement, crossing the path of a black cat, walking under a ladder, or squeezing a lemon under the full moon to me. These kinds of tricks seem very clever at first but there always comes a point when they start to break down. I'd be leery about using union in 2017, but typedefing (void star) is like putting on your underpants outside your trousers, thinking you're superman and jumping out of a window thinking that you can fly.

simias|9 years ago

I'm not super familiar with Cello (and I'm not sure I get the point of it overall, are there that many platforms left that you can target from C but not C++?) but in its defense it does seem to implement fat pointers and runtime checks to have a degree of type safety. Not sure how thorough it is but it's not just decaying everything to void pointers behind the scenes.

It's a pretty clever hack though, like using setjmp for exception handling. I'm pretty sure I'd never want to use that in production anywhere but it was probably fun to implement.

wyc|9 years ago

The style reminds me of some J implementations!

   #define DO(n,x) {I i=0,_n=(n);for(;i<_n;++i){x;}}
http://code.jsoftware.com/wiki/Essays/Incunabulum

jnordwick|9 years ago

I think Arthur Whitney's (in)famous code makes its way to HN about once a year.

dangerbird2|9 years ago

The worst part of that macro is that it is inherently unhygienic: it leaks the variable name 'i' up the scope

antirez|9 years ago

Cool. This is a bit extreme but on purpose I think, it is an exploratory project AFAIK. If I had my hands free I would spend my time writing a new C library since I firmly believe that the problems of C are, for the larger part, in its standard library and not in the language itself. C memory model is unsafe but if mediated by a sane library for strings, and if by default you have most of the things where bugs are put (data structures, facilities for parsing, ...) things get a lot simpler and safer.

wybiral|9 years ago

What features make it "higher level"? It looks like syntactic sugar on C code without any real improvements (aside from GC).

shakna|9 years ago

I've used Cello for a few side projects, and it doesn't even really feel like C in the end.

Just a few off the top of my head:

* No need to specify type. Use var.

* Simpler for loops

* Inbuilt types for Hash Tables

* File types, making file reading much easier

* Function types, making it easier to pass functions around

* Doctype access

* Threads & Mutexes

* Format strings

* GC (with ability to turn C-types into Cello-types for GCing.)

It is fundamentally syntax-sugar, but enough that what you end up with doesn't necessarily look like C at the end.

    with(f in new(File, $S("test.txt"), $S("r"))) {
      var k = new(String); resize(k, 100);
      var v = new(Int, $I(0));
      foreach (i in range($I(2))) {
        scan_from(f, 0, "%$ is %$ ", k, v);
        show(k); show(v);
      }
    }
(From: http://libcello.org/learn/file)

goldbabelfish|9 years ago

I believe those things are exactly what make it higher level. Unless I'm misunderstanding your question, all that makes a language higher level is the amount of abstraction between the human and the computer.

khedoros1|9 years ago

What makes [your favorite language] "higher level"? It looks like syntactic sugar on top of assembly language.

It's higher level because it provides abstractions that improve the language's expressiveness.

easytiger|9 years ago

> without any real improvements (aside from GC).

You've really gone down the rabbit hole if you think GC is an improvment

scythe|9 years ago

IIRC Cello doesn't enforce type safety, which means you can foldl but it's not much different from writing foldl in C and using void pointers everywhere.

I had thought about trying to make a type-safe version of Cello but I eventually realized that I can't do it in cpp so at that point it became its own language and too much work (I did not write Cello).

stephen_g|9 years ago

I'm going to say you're right, given one of the first declarations is

    typedef void* var;
Cello is extremely impressive and could be fun for hobby projects, but for something you'd actually want to use on a real product you'd be better off just creating a new language (it could compile down to C even, like Vala did).

amaks|9 years ago

There are several reasons why people use C, one of them is that the language makes it very explicit what generated code is going to look like. That's one of the reasons why Windows kernel is written in C (https://msdn.microsoft.com/en-us/library/windows/hardware/ff..., https://view.officeapps.live.com/op/view.aspx?src=http://dow...). Libraries like this obfuscate source code.

95014_refugee|9 years ago

A problem with C in modern usage is that many people believe this to be more the case than is warranted.

zaiste|9 years ago

Here's the author of Cello talking about the project at PolyConf in 2015: https://www.youtube.com/watch?v=bVxfwsgO00o

« In this talk I dig into the depths of my programming library Cello - a fun experiment to see what C looks like when pushed to it's limits. I'll cover how Cello works internally, some of the cuter tricks used to make it look and feel so different, what is in store for future versions of Cello, and why it is important to push languages to their boundaries. »

kbart|9 years ago

How about performance? As I understand, it uses fat pointers fat pointers and GC, so performance drop is expected. There are not many reasons to use C nowadays beside performance.

camus2|9 years ago

> There are not many reasons to use C nowadays beside performance.

portability? stable ABI? ... writing something in C make it easy for any other higher level language to link to it. That's why we're not done with C, at all ... it's basically the only serious language out there used to share code among every possible platform or language. Even C++ which is a bit safer in practice is harder to link.

I just wished C was a bit safer by default (arrays with bound checking,...)

hydrocat|9 years ago

Isn't this something similar to vala ?

chme|9 years ago

No not really.

Vala has an own seperate transpiler that generates C code from Vala code.

Cello uses the normal C preprocessor to generate C code.

ndesaulniers|9 years ago

Why not just use C++?

feelix|9 years ago

That's like saying "Why not just use lisp?" to a python programmer. They're entirely separate languages (albeit it they do share some commonalities, but not as many as you might think).

C is my main language and I dabble in C++. I really dislike C++. I love C. I welcome any efforts to add a bit of higher level functionality to C. I have no desire (ever) to switch to C++.

astrodust|9 years ago

As a long-time C++ programmer I can say there's a lot of reasons to not use C++, but still, yeah.

What about D? Rust?

catnaroek|9 years ago

Cello and C++ are have very different design goals. Cello is essentially a dynamic language implementation on top of C, and uses runtime reflection in places where a C++ programmer would probably prefer compile-time template hackery.

jokoon|9 years ago

A good and valid reason would be compiler availability. C++11, 14 and 17 might not stable for every compiler out there.

C++11 and further can be fancy, and not be supported in every possible platform like exotic ones.

Bad reasons would be to talk about the gimmicks of C++, but generally it boils down to programmers being taught well about the language, because it's not an easy one to understand fully, as there many weird pitfalls.

C++ still has to fix some its flaws, like compile times (this should be improved with modules).

So to put it simply, you can still do good work with C because C compilers are just simple and straightforward. When you don't need C anymore (because you only use for it's size and speed, not for the high level), you use something more high level like python. C++ should allow programmers to do everything, but C++ is not perfect and not fully accessible to everyone.

lacampbell|9 years ago

slow compile times, complex language, sanity

frostirosti|9 years ago

Why not just write another language, that compiles to machine code, but doesn't totally enforce type safety. Call it Iron.

ensiferum|9 years ago

[deleted]

wruza|9 years ago

>C++ does everything that C does except only better.

This expression is too dangerous to use without try {} catch {}.

kgabis|9 years ago

I guess you should stop using software written in this obsolete language then.

r00t-|9 years ago

Either the examples are terrible or the library is. I'm leaning towards the latter.