Cello: Higher level programming in C

2014-12-27 09:38:20 -0500

Cello is a library for C

It provides some features that commonly found in high level programming languages, for example dynamic type:

var int_item = $(Int, 5);
var float_item = $(Real, 2.4);
var string_item = $(String, "Hello");

It uses a "var" structure where it is able to hold different types of values

The way of defining a universal structure to describe different types of values is commonly used in the core engines of dynamic type programming languages like PHP.

Here is how Zend - the PHP Core engine - is doing this universal type structure:

typedef struct _zval_struct zval;

typedef union _zvalue_value {
    long lval;                  /* long value */
    double dval;                /* double value */
    struct {
        char *val;
        int len;
    } str;
    HashTable *ht;              /* hash table value */
    zend_object_value obj;
} zvalue_value;

struct _zval_struct {
    /* Variable information */
    zvalue_value value;     /* value */
    zend_uint refcount;
    zend_uchar type;    /* active type */
    zend_uchar is_ref;
};

Now the Cello did something similar and much more to get the C programming being more efficient on writing and maybe reading as well

The list of features quoted from http://libcello.org

Examples

/* Example libCello Program */

#include "Cello.h"

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

  /* Stack objects are created using "$" */
  var int_item = $(Int, 5);
  var float_item = $(Real, 2.4);
  var string_item = $(String, "Hello");

  /* Heap objects are created using "new" */
  var items = new(List, int_item, float_item, string_item);

  /* Collections can be looped over */
  foreach (item in items) {
    /* Types are also objects */
    var type = type_of(item);
    print("Object %$ has type %$\n", item, type);
  }

  /* Heap objects destroyed with "delete" */
  delete(items); 
}
/* Another Example Cello Program */

#include "Cello.h"

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

  /* Tables require "Eq" and "Hash" on key type */
  var prices = new(Table, String, Int);
  put(prices, $(String, "Apple"),  $(Int, 12)); 
  put(prices, $(String, "Banana"), $(Int,  6)); 
  put(prices, $(String, "Pear"),   $(Int, 55));

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

  /* "with" automatically closes file at end of scope. */
  with (file in open($(File, NULL), "prices.bin", "wb")) {

    /* First class function object */
    lambda(write_pair, args) {

      /* Run time type-checking with "cast" */
      var key = cast(at(args, 0), String);
      var value = cast(get(prices, key), Int);

      try {
        print_to(file, 0, "%$ :: %$\n", key, value);
      } catch (e in IOError) {
        println("Could not write to file - got %$", e);
      }

      return None;
    };

    /* Higher order functions */
    map(prices, write_pair);
  }

  delete(prices);
}
«Newer      Older»
Comment:
Name:

Back to home

Subscribe | Register | Login | N