【メモ】Vala

仮想のMint17.1上で行った。

コンパイラのインストール

$ sudo apt-get install valac
$ valac --version
$ Vala 0.22.1

コンパイル

hello.vala

void main() 
{
stdout.printf("Hello Valan");
}

コンパイル操作

$ valac ./hello.vala

Cソース出力操作

$ valac ./hello.vala -C -H hello.h

hello.vala Cソース出力結果

/* hello.c generated by valac 0.22.1, the Vala compiler
* generated from hello.vala, do not modify */


#include
#include
#include




void _vala_main (void);


void _vala_main (void) {
FILE* _tmp0_ = NULL;
_tmp0_ = stdout;
fprintf (_tmp0_, "Hello Valan");
}


int main (int argc, char ** argv) {
g_type_init ();
_vala_main ();
return 0;
}

クラスを伴う場合。

hello2.vala

public class MyLib : Object {

public void hello() {
stdout.printf("Hello World, MyLibn");
}

public int sum(int x, int y) {
return x + y;
}
}

void main() {
var test = new MyLib();
test.hello();
int x = 4, y = 5;
stdout.printf("The sum of %d and %d is %dn", x, y, test.sum(x, y));
}

hello2.vala Cソース出力結果

/* hello2.c generated by valac 0.22.1, the Vala compiler
* generated from hello2.vala, do not modify */


#include
#include
#include


#define TYPE_MY_LIB (my_lib_get_type ())
#define MY_LIB(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MY_LIB, MyLib))
#define MY_LIB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MY_LIB, MyLibClass))
#define IS_MY_LIB(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MY_LIB))
#define IS_MY_LIB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MY_LIB))
#define MY_LIB_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MY_LIB, MyLibClass))

typedef struct _MyLib MyLib;
typedef struct _MyLibClass MyLibClass;
typedef struct _MyLibPrivate MyLibPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _MyLib {
GObject parent_instance;
MyLibPrivate * priv;
};

struct _MyLibClass {
GObjectClass parent_class;
};


static gpointer my_lib_parent_class = NULL;

GType my_lib_get_type (void) G_GNUC_CONST;
enum {
MY_LIB_DUMMY_PROPERTY
};
void my_lib_hello (MyLib* self);
gint my_lib_sum (MyLib* self, gint x, gint y);
MyLib* my_lib_new (void);
MyLib* my_lib_construct (GType object_type);
void _vala_main (void);


void my_lib_hello (MyLib* self) {
FILE* _tmp0_ = NULL;
g_return_if_fail (self != NULL);
_tmp0_ = stdout;
fprintf (_tmp0_, "Hello World, MyLibn");
}


gint my_lib_sum (MyLib* self, gint x, gint y) {
gint result = 0;
gint _tmp0_ = 0;
gint _tmp1_ = 0;
g_return_val_if_fail (self != NULL, 0);
_tmp0_ = x;
_tmp1_ = y;
result = _tmp0_ + _tmp1_;
return result;
}


MyLib* my_lib_construct (GType object_type) {
MyLib * self = NULL;
self = (MyLib*) g_object_new (object_type, NULL);
return self;
}


MyLib* my_lib_new (void) {
return my_lib_construct (TYPE_MY_LIB);
}


static void my_lib_class_init (MyLibClass * klass) {
my_lib_parent_class = g_type_class_peek_parent (klass);
}


static void my_lib_instance_init (MyLib * self) {
}


GType my_lib_get_type (void) {
static volatile gsize my_lib_type_id__volatile = 0;
if (g_once_init_enter (&my_lib_type_id__volatile)) {
static const GTypeInfo g_define_type_info = { sizeof (MyLibClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) my_lib_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (MyLib), 0, (GInstanceInitFunc) my_lib_instance_init, NULL };
GType my_lib_type_id;
my_lib_type_id = g_type_register_static (G_TYPE_OBJECT, "MyLib", &g_define_type_info, 0);
g_once_init_leave (&my_lib_type_id__volatile, my_lib_type_id);
}
return my_lib_type_id__volatile;
}


void _vala_main (void) {
MyLib* test = NULL;
MyLib* _tmp0_ = NULL;
gint x = 0;
gint y = 0;
FILE* _tmp1_ = NULL;
gint _tmp2_ = 0;
_tmp0_ = my_lib_new ();
test = _tmp0_;
my_lib_hello (test);
x = 4;
y = 5;
_tmp1_ = stdout;
_tmp2_ = my_lib_sum (test, x, y);
fprintf (_tmp1_, "The sum of %d and %d is %dn", x, y, _tmp2_);
_g_object_unref0 (test);
}


int main (int argc, char ** argv) {
g_type_init ();
_vala_main ();
return 0;
}

-Cオプションがおもしろい。局所の関数ポインタに値を与えることで、クラスのインスタンス化を行っている。という解釈でよろしい?

IDE

Geany
Anjuta
Vala IDE

参考

Vala
Vala -Ubuntu Documentation
Vala Tutorial -GNOME