• Stars
    star
    6,212
  • Rank 6,447 (Top 0.2 %)
  • Language
    C
  • License
    Other
  • Created over 12 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Higher level programming in C

Cello

Cello is a library that brings higher level programming to C.

By acting as a modern, powerful runtime system Cello makes many things easy that were previously impractical or awkward in C such as:

  • Generic Data Structures
  • Polymorphic Functions
  • Interfaces / Type Classes
  • Constructors / Destructors
  • Optional Garbage Collection
  • Exceptions
  • Reflection

And because Cello works seamlessly alongside standard C you get all the other benefits such as great performance, powerful tooling, and extensive libraries.

Examples

#include "Cello.h"

int main(int argc, char** argv) {

  /* Stack objects are created using "$" */
  var i0 = $(Int, 5);
  var i1 = $(Int, 3);
  var i2 = $(Int, 4);

  /* Heap objects are created using "new" */
  var items = new(Array, Int, i0, i1, i2);
  
  /* Collections can be looped over */
  foreach (item in items) {
    print("Object %$ is of type %$\n",
      item, type_of(item));
  }
  
  /* Heap objects destructed via Garbage Collection */
  return 0;
}
#include "Cello.h"

int main(int argc, char** argv) {
  
  /* Shorthand $ can be used for basic types */
  var prices = new(Table, String, Int);
  set(prices, $S("Apple"),  $I(12)); 
  set(prices, $S("Banana"), $I( 6)); 
  set(prices, $S("Pear"),   $I(55)); 

  /* Tables also support iteration */
  foreach (key in prices) {
    var val = get(prices, key);
    print("Price of %$ is %$\n", key, val);
  }
  
  return 0;
}

Articles

Learning Resources:

Articles about its creation and internal workings:

More Examples

#include "Cello.h"

int main(int argc, char** argv) {

  var items = new(Array, Int, 
    $I( 8), $I( 5), $I(20), 
    $I(15), $I(16), $I(98));

  /* Iterate over indices using "range" */
  foreach (i in range($I(len(items)))) {
    print("Item Range %i is %i\n", i, get(items, i));
  }

  /* Iterate over every other item with "slice" */ 
  foreach (item in slice(items, _, _, $I(2))) {
    print("Item Slice %i\n", item);
  }
  
  return 0;
}
#include "Cello.h"

/* Define a normal C structure */
struct Point {
  float x, y;
};

/* Make it compatible with Cello */
var Point = Cello(Point);

int main(int argc, char** argv) {
  
  /* Create on Stack or Heap */
  var p0 = $(Point, 0.0, 1.0);
  var p1 = new(Point, $(Point, 0.0, 2.0));
  
  /* It can be shown, compared, hashed, etc...
  **
  ** p0: <'Point' At 0x000000000022FC58>
  ** p1: <'Point' At 0x00000000004C7CC8>
  ** cmp: 1
  ** hash: 2849275892l
  */ 
  print("p0: %$\np1: %$\ncmp: %i\nhash: %ul\n",
    p0, p1, $I(cmp(p0, p1)), $I(hash(p0)));
  
  /* And collected by the GC when out of scope */
  return 0;
}

F.A.Q

  • Why does this exist?

I made Cello as a fun experiment to see what C looks like hacked to its limits. As well as being a powerful library and toolkit, it should be interesting to those who want to explore what is possible in C.

  • How does it work?

I recommend reading A Fat Pointer Library to get an overview of how Cello works. You can also peek at the source code, which I'm told is fairly readable, or ask me any questions you like via e-mail.

  • 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++.

  • Is anyone using Cello?

People have experimented with it, but there is no high profile project I know of that uses it. Cello is too big and scary a dependency for new C projects if they want to be portable and easy to maintain.

  • Can I get involved?

Yes! That would be great. If you do anything with Cello I'd love to know, you can e-mail me at [email protected], or help with the development at the Cello github repo. Contributions are very welcome.

  • Who are you?

Hello! I'm Daniel Holden. You many know me from a book I wrote or my personal website. I also have a rarely updated twitter account.

More Repositories

1

BuildYourOwnLisp

Learn C and build your own programming language in under 1000 lines of code!
HTML
2,792
star
2

mpc

A Parser Combinator library for C
C
2,648
star
3

Corange

Pure C Game Engine
C
1,720
star
4

tgc

A Tiny Garbage Collector for C
C
902
star
5

Motion-Matching

Learned Motion Matching example implementation and source code for the article "Code vs Data Driven Displacement"
C++
615
star
6

LuaAutoC

Automagically use C Functions and Structs with the Lua API
C
257
star
7

CPP_COMPLETE

Brainfuck interpreter written in the C preprocessor
C++
226
star
8

BVHView

A simple viewer for the .bvh animation file format written using raylib.
C
202
star
9

json2c

Convert JSON to C data literals
C
122
star
10

imgcurses

ncurses Image Viewer
C
114
star
11

Spring-It-On

Code for the article Spring-It-On
C
86
star
12

ptest

DRY Microtesting Framework for C
C
79
star
13

Joint-Limits

Source code for the article "Joint Limits"
C++
76
star
14

PyAutoC

Automatically wrap C functions & structs at run time for the Python/C API
Python
70
star
15

Python-xNormal

Python Wrapper for xNormal
Python
65
star
16

GenoView

An example raylib application for viewing animation on the Geno character
C
62
star
17

Animation-Looping

Source code for the article "Creating Looping Animations from Motion Capture"
C++
52
star
18

Animation-Velocities

Source code for the article Propagating Velocities through Animation Systems
C++
32
star
19

QuaternionAverage

Source code for the article "Quaternion Weighted Average"
C++
20
star
20

PyMark

Python flavoured object markup.
C++
17
star
21

Ranges

Source code for the article Tags, Ranges and Masks
C++
17
star
22

zeroeggs-retarget

Retargeting of the ZeroEGGs dataset onto a common character
Python
2
star
23

Cello-Website

libCello Website
JavaScript
1
star
24

lafan1-resolved

Resolving of the Ubisoft La Forge Animation dataset onto a common skeleton
Python
1
star
25

motorica-retarget

Retargeting of the Motorica Dance dataset onto a common skeleton
Python
1
star