On Tue, 2007-07-17 at 13:21 -0700, Raoul Duke wrote:
> > > avoid DRY. Can one do something like:
> > > header
> > > #include<Foo.h>
> > ah, doing some empirical studies, it looks like that can work, coolio.
> 
> but that (the simpler route) doesn't get the Felix wrappering, ja? so
> there's still glue to be written before i can do things like
> 
> var bar = foo(42);

Felix uses a different syntax and type system than C or C++.

So you have to 'lift' C things into Felix: you have to map
names and types across.

However, since Felix uses the C/C++ object model, that's
generally ALL you have to do. Unlike other languages,
you don't need to write any nasty type conversions.

There's little need for things like SWIG, because there
is no substantive (executable) glue logic. Adding two
integers in Felix is the same as in C:

        type int = "int";
        fun add: int * int -> int = "$1+$2";
        var x = 1 + 2; // infix + calls the function 'add'

Of course this generates the C code like:

        int x = 1 + 2;

Why? Because you TOLD it to. There's no magic here.
You told it 'add' meant '$1+$2'.

You cannot actually DO this experiment at the moment
because int is already defined, but try this one:

        #import <flx.flxh>
        fun rsub: int * int -> int = "$2-$1";
        println (rsub (1,2));
        

Now really, that's pretty simple: that's a one line
definition .. and it is SHORTER than doing the same
thing in C:

        extern int rsub(int,int); // header file
        ..
        int rsub(int x, int y) { return y - x; }


It's also a lot more efficient. It's more like:

        #define rsub(x,y) ((y)-(x))

where you note the () in the RHS are mandatory because the C
preprocessor does not respect precedence .. Felix does.

And the C preprocessor does not respect types, Felix does.

So the Felix definition is BOTH short and typesafe.

Ok, so these are 'Micky Mouse' bindings. To see it in
practice in a REAL file (excerpt from sandbox/gpak.flx):

header '#include <gtk/gtk.h>';
header '#include <glib.h>';
header '#include <glib.h>';

// GLib
type GObject = "GObject*";
type GCallback = "GCallback";
type gpointer = "gpointer";

fun G_OBJECT[t]: t -> GObject = "G_OBJECT($1)";
fun G_CALLBACK[t]: t -> GCallback = "G_CALLBACK($1)";
proc g_print: string = "g_print($1.data());";

// GDK
type GdkEvent = "GdkEvent*";
const TRUE : bool = "TRUE";
cstruct GdkColor {
  pixel: uint32;
  red: uint16;
  green: uint16;
  blue: uint16;
};

nopix := 0u32;
bright := 65535u16;
dark := 0u16;

black := GdkColor(nopix,dark,dark,dark);
white := GdkColor(nopix,bright,bright,bright);
red := GdkColor(nopix,bright,dark,dark);
green:= GdkColor(nopix,dark,bright,dark);
blue := GdkColor(nopix,dark,dark,bright);
yellow := green + blue;

fun mul (a: GdkColor, x: double) =>
  GdkColor(
    nopix,
    cast[uint16](double(a.red) * x), 
    cast[uint16](double(a.green) * x), 
    cast[uint16](double(a.blue) * x)
  )
;
  
fun add (a: GdkColor, b: GdkColor) : GdkColor =>
  GdkColor (nopix,a.red + b.red, a.green + b.green, a.blue + b.blue)
;
/////////////////////////////

You can actually run this if you have GTK installed:

bin/flx --test `pkg-config --cflags --libs gtk+-2.0 gthread-2.0` \
sandbox/gpak

[OUCH OUCH .. that parsing is slow! This used to build in 5 seconds]




-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to