Ejemplo n.º 1
0
#include "Cello/Prelude.h"
#include "Cello/Type.h"

#include <string.h>

var Bool = methods {
  methods_begin(Bool),
  method(Bool, Eq),
  method(Bool, Ord),
  method(Bool, Hash),
  method(Bool, AsChar),
  method(Bool, AsLong),
  method(Bool, AsDouble),
  method(Bool, AsStr),
  method(Bool, Show),
  methods_end(Bool)
};

var Bool_Eq(var self, var obj) {
  return (var)(intptr_t)(self == obj);
}

var Bool_Gt(var self, var obj) {
  return (var)(intptr_t)(self > obj);
}

var Bool_Lt(var self, var obj) {
  return (var)(intptr_t)(self < obj);
}

long Bool_Hash(var self) {
Ejemplo n.º 2
0
#include "Cello/Exception.h"

#include <stdlib.h>
#include <string.h>

var Dictionary = methods {
  methods_begin(Dictionary),
  method(Dictionary, New),
  method(Dictionary, Assign),
  method(Dictionary, Copy),
  method(Dictionary, Eq),
  method(Dictionary, Collection),
  method(Dictionary, Dict),
  method(Dictionary, Iter),
  method(Dictionary, Show),
  methods_end(Dictionary)
};

var Dictionary_New(var self, va_list* args) {
  DictionaryData* dict = cast(self, Dictionary);
  dict->size = 1024;
  dict->keys = new(List, 0);
  
  dict->key_buckets = calloc(dict->size, sizeof(var));
  dict->val_buckets = calloc(dict->size, sizeof(var));
  
  for (int i = 0; i < dict->size; i++) {
    dict->key_buckets[i] = new(List, 0);
    dict->val_buckets[i] = new(List, 0);
  }
  
Ejemplo n.º 3
0
#include "Cello/File.h"

#include "Cello/Exception.h"
#include "Cello/Number.h"
#include "Cello/String.h"

var File = methods {
  methods_begin(File),
  method(File, New),
  method(File, With),
  method(File, Stream),
  method(File, Dict),
  method(File, Format),
  methods_end(File),
};

var File_New(var self, va_list* args) {
  FileData* fd = cast(self, File);
  const char* filename = va_arg(*args, const char*);
  const char* access = va_arg(*args, const char*);
  stream_open(self, filename, access);
  return self;
}

var File_Delete(var self) {
  FileData* fd = cast(self, File);
  if (fd->f != NULL) { stream_close(self); }
  return self;
}

size_t File_Size(void) {
Ejemplo n.º 4
0
#include "Exception+.h"

#include <stdlib.h>
#include <string.h>

var Table = methods {
  methods_begin(Table),
  method(Table, New),
  method(Table, Assign),
  method(Table, Copy),
  method(Table, Eq),
  method(Table, Collection),
  method(Table, Dict),
  method(Table, Iter),
  method(Table, Show),
  methods_end(Table)
};

var Table_New(var self, va_list* args) {
  TableData* tab = cast(self, Table);
  
  tab->key_type = cast(va_arg(*args, var), Type);
  tab->val_type = cast(va_arg(*args, var), Type);
  
  tab->size = 1024;
  tab->keys = new(Array, tab->key_type, 0);
  
  tab->key_buckets = malloc(tab->size * sizeof(var));
  tab->val_buckets = malloc(tab->size * sizeof(var));
  
  if (tab->key_buckets == NULL) { throw(OutOfMemoryError, "Cannot create Table. Out of memory!"); }
Ejemplo n.º 5
0
  return call_with(self, $(List, num, num, args, 0));
}

var call_with(var self, var args) {
  Call* icall = type_class(type_of(self), Call);
  assert(icall->call_with);
  return icall->call_with(self, args);
}

var Function = methods {
  methods_begin(Function),
  method(Function, New),
  method(Function, Copy),
  method(Function, Assign),
  method(Function, Call),
  methods_end(Function),
};

var Function_New(var self, va_list* args) {
  FunctionData* fd = cast(self, Function);
  fd->func = va_arg(*args, var);
  return self;
}

var Function_Delete(var self) {
  return self;
}

var Function_Copy(var self) {
  FunctionData* fd = cast(self, Function);
  return new(Function, fd->func);
Ejemplo n.º 6
0
  return type_class_method(type_of(self), Vector, dot, self, obj);
}

float length(var self) {
  return type_class_method(type_of(self), Vector, length, self);
}

var Vec2 = methods {
  methods_begin(Vec2),
  method(Vec2, New),
  method(Vec2, Assign),
  method(Vec2, Copy),
  method(Vec2, Eq),
  method(Vec2, Show),
  method(Vec2, Vector),
  methods_end(Vec2)
};

var Vec2_New(var self, var_list vl) {
  Vec2Data* v = cast(self, Vec2);
  v->x = as_double(var_list_get(vl));
  v->y = as_double(var_list_get(vl));
  return self;
}

var Vec2_Delete(var self) {
  return self;
}

size_t Vec2_Size(void) {
  return sizeof(Vec2Data);
Ejemplo n.º 7
0
#include "Cello/Channel.h"

var Channel = methods {
  methods_begin(Channel),
  method(Channel, New),
  method(Channel, Push),
  methods_end(Channel)
};

var Channel_New(var self, va_list* args) {
  ChannelData* co = cast(self, Channel);

  co->buffer_size = va_arg(*args, int);
  co->read_pos = 0;
  co->write_pos = 0;
  co->buffer = NULL;
  pthread_mutex_init(&co->mutex, NULL);
  pthread_cond_init(&co->read_ready_cond, NULL);
  pthread_cond_init(&co->write_ready_cond, NULL);

  if (co->buffer_size) {
    co->buffer = malloc(co->buffer_size * sizeof(*co->buffer));
  }

  return self;
}

var Channel_Delete(var self) {
  ChannelData* co = cast(self, Channel);
  pthread_mutex_destroy(&co->mutex);
  pthread_cond_destroy(&co->read_ready_cond);
Ejemplo n.º 8
0
var Array = methods {
  methods_begin(Array),
  method(Array, New),
  method(Array, Assign),
  method(Array, Copy),
  method(Array, Eq),
  method(Array, Collection),
  method(Array, Push),
  method(Array, At),
  method(Array, Iter),
  method(Array, Reverse),
  method(Array, Append),
  method(Array, Sort),
  method(Array, Show),
  methods_end(Array)
};

var Array_New(var self, va_list* args) {
  
  ArrayData* ad = cast(self, Array);
  ad->item_type = cast(va_arg(*args, var), Type);
  ad->num_items = 0;
  ad->num_slots = 0;
  ad->items = NULL;
  
  int count = va_arg(*args, int);
  for(int i = 0; i < count; i++) {
    push(self, va_arg(*args, var));
  }
  
Ejemplo n.º 9
0
}

var Int = methods {
  methods_begin(Int),
  method(Int, New), 
  method(Int, Assign),
  method(Int, Copy),
  method(Int, Eq), 
  method(Int, Ord),
  method(Int, Hash),
  method(Int, AsLong),
  method(Int, AsDouble),
  method(Int, Num),
  method(Int, Serialize),
  method(Int, Show),
  methods_end(Int)
};

var Int_New(var self, va_list* args) {
  IntData* intdata = cast(self, Int);
  intdata->value = va_arg(*args, int);
  return self;
}

var Int_Delete(var self) {
  return self;
}

void Int_Assign(var self, var obj) {
  IntData* intdata = cast(self, Int);
  intdata->value = as_long(obj);
Ejemplo n.º 10
0
#include "Cello/Type.h"
#include "Cello/Exception.h"

#include <string.h>

var Reference = methods {
  methods_begin(Reference),
  method(Reference, New), 
  method(Reference, Assign),
  method(Reference, Copy),
  method(Reference, Eq), 
  method(Reference, Hash),
  method(Reference, At),
  method(Reference, With),
  method(Reference, Show),
  methods_end(Reference)
};

var Reference_New(var self, va_list* args) {
  ReferenceData* rd = cast(self, Reference);
  rd->ref = va_arg(*args, var);
  return rd;
}

var Reference_Delete(var self) {
  return self;
}

void Reference_Assign(var self, var obj) {
  ReferenceData* rd0 = cast(self, Reference);
  ReferenceData* rd1 = cast(obj, Reference);
Ejemplo n.º 11
0
#include "Tree+.h"

#include "Array+.h"
#include "Bool+.h"
#include "None+.h"

var Tree = methods {
  methods_begin(Tree),
  method(Tree, New),
  method(Tree, Assign),
  method(Tree, Copy),
  method(Tree, Collection),
  method(Tree, Dict),
  method(Tree, Iter),
  methods_end(Tree),
};

var Tree_New(var self, va_list* args) {
  TreeData* td = cast(self, Tree);
  td->key_type = cast(va_arg(*args, var), Type);
  td->val_type = cast(va_arg(*args, var), Type);
  td->keys = new(Array, td->key_type, 0);
  td->root = NULL;
  return self;
}

var Tree_Delete(var self) {
  TreeData* td = cast(self, Tree);
  clear(self);
  delete(td->keys);
  return self;
Ejemplo n.º 12
0
#include "Type.h"

#include "Bool.h"
#include "Exception.h"
#include "Number.h"
#include "String.h"

#include <string.h>

var Type = methods {
  methods_begin(Type),
  method(Type, New),
  method(Type, AsStr),
  method(Type, Show),
  methods_end(Type),
};

var Type_Cast(var obj, var t, const char* func, const char* file, int line) {
  
  if (type_of(obj) is t) {
    return obj;
  } else {
    return throw(TypeError,
      "Argument to function '%s' at '%s:%i' :: "
      "Got Type '%s' :: "
      "Expected Type '%s'", 
      $(String, (char*)func), $(String, (char*)file), $(Int, line), type_of(obj), t);
  }
  
}
Ejemplo n.º 13
0
var String = methods {
  methods_begin(String),
  method(String, New),
  method(String, Assign),
  method(String, Copy),
  method(String, Eq),
  method(String, Ord),
  method(String, Collection),
  method(String, Hash),
  method(String, Reverse),
  method(String, AsStr), 
  method(String, Append), 
  method(String, Format),
  method(String, Show),
  methods_end(String)
};

var String_New(var self, va_list* args) {
  StringData* s = cast(self, String);
  const char* init = va_arg(*args, const char*);
  s->value = malloc(strlen(init) + 1);
  if (s->value == NULL) { throw(OutOfMemoryError, "Cannot allocate string, out of memory!"); }
  strcpy(s->value, init);
  return self;
}

var String_Delete(var self) {
  StringData* s = cast(self, String);
  free(s->value);
  return self;