Wiki source for ValaGenieintro
[[HomePage]] > [[ComponentHowTo Components and HowTos]] > [[HowToProgramming Programming]]
====Vala Genie Intro====
==What is this?==
An introduction to using the Genie Programming language under Puppy Linux
==Main features:==
- A true compiler
- A "Hello world" console executable is 2.9KB, a GTK GUI "Hello world", with a OK button thrown in, is 5.8KB
- Easy Python-like language
- Easy GTK programming
- Link directly with the system shared libraries
- No huge binding libraries, nothing required at runtime. No bloat!
- Library support
- Compile-time bindings available for most shared libraries.
- Plain-C intermediate code
- Only needs Gnu C compiler -- so can be compiled for any operating system and CPU.
- Object oriented
- Objects are not "bolted on" as for some languages that started life as procedural-only. But Genie code can be non-object-oriented if you wish.
- Programs written in Genie should have have similar performance and resource usage to those written directly in Vala and C
- Genie has none of the bloat and overhead that comes with many other high level languages which utilize a VM (Eg Python, Mono, et al)
- Classes in Genie are actually gobjects so Genie can be used for creating platform code like widgets and libraries where gobjects are required for binding to other languages.
- The generic nature of Glib means that you can use Vala/Genie for any kind of programing, but Glib/Gobject does make it particularly easy for GTK coding.
====={{color text="What is the difference between Vala and Genie?" c="black"}}=====
Vala is more like C/Java, Genie like Basic.
====={{color text="Who Created Genie?" c="black"}}=====
Jamie McCracken created Genie,
Jürg Billeter and Raffaele Sandrini are the main developers of Vala.
====={{color text="When were Vala and Genie created?" c="black"}}=====
Vala came first in mid-2006, with a C#-like syntax.
Genie followed in mid-2008 influenced in part by Python and Delphi.
These languages use the same compiler and support all the same features
====={{color text="How do I use Genie in Puppy?" c="black"}}=====
[[http://puppylinux.org/wikka/GenieProgramming Check here]]
====={{color text="Is Genie a good language to start programming with?" c="black"}}=====
Being like Python it forces an easy to use spaced format with a simple modern syntax. It is more consistent and flexible than BASIC and logo or other languages recommended for beginners. **However** be aware: Documentation in English is available, but not redundant and not always beginner-friendly. Documentation in other languages, is almost inexistant.
====={{color text="Is it suitable for professionals?" c="black"}}=====
Code is compiled to C, making it fast and efficient. Linux uses C as its main language and the Gnome project is developing Vala
====={{color text="Basic Concepts" c="black"}}=====
**Files**
Genie code must be written in files with the *.gs extensions. Vala codes must be written in files with the .vala extension.
When you want to compile Genie code, you give the compiler a list of the files required, and Genie/Vala compiler will work out how they fit together.
**Syntax**
Genie's syntax mixes features of several high level languages including Python, Boo, Delphi and D
Genie is case sensitive as a result of it being compiled into c code - so be careful when mixing case in your code.
An identifier is defined by its name and its type, e.g. i:int meaning an integer called i. In the case of value types this also creates an object of the given type. For reference types these just defines a new reference that doesn't initially point to anything.
Genie has a mechanism called Type Inference, whereby a local variable may be defined using var instead of giving a type, so long as it is unambiguous what type is meant.
Genie support is now in Gtksourceview:
====={{color text="Where can I find out about String handling in Genie?" c="black"}}=====
http://puppylinux.com/blog/?viewDetailed=00609
Genie strings page:
http://puppylinux.com/blog/?viewDetailed=00608
Just to remind to install the gtksourcefile pet as well as the Valide one
Mark has kindly prepared
http://dotpups.de/puppy4/dotpups/Programming/Vala/
Especially Glib is important.
For example the string functions in Genie are the GLib functions:
http://valadoc.org/?pkg=glib-2.0&element=string
A bit difficult to find when you are new, but very essential!
====={{color text="Where is valide code saved?" c="black"}}=====
cd /root/valide/Genie
./Genie
Or:
/root/valide/Genie/Genie
====={{color text="How do you compile code?" c="black"}}=====
valac -C v5.gs
valac -C v4.vala
This just creates v5.c and v5.h.
====={{color text="Can the code be used in other Linux distributions?" c="black"}}=====
Yes
====={{color text="Is Vala available for Windows?" c="black"}}=====
Yes
http://dotpups.de/puppy4/dotpups/Programming/Vala/Vala-for-Windows/
====={{color text="Where can I find out about Valide?" c="black"}}=====
http://valaide.blogspot.com/2009/03/genie-support.html
====={{color text="Code example Vala" c="black"}}=====
%%(language-ref)
class Demo.HelloWorld : GLib.Object {
public static int main(string[] args) {
stdout.printf("Hello, World\n");
return 0;
}
}
%%
**random method**
from GLib.
http://valadoc.org/?pkg=glib-2.0&element=GLib.Random
Example:
Code:
%%(language-ref)
// return a random value from 1 to 9
a:int
a = GLib.Random.int_range(1,10)
print("%d" ,
%%init
Infinite Monkey theorem
%%(language-ref)
// Infinite Monkey Theorem
// Lobster and Shadow, March 2009 LGPL
// use from command line
// imt ["search text"]
// generates random characters and searches for a given pattern eg 'in the beginning was the world'
// warning strings longer than "in the" may take minutes or hours to find
// program demonstrates:
// basic functions
// passing parameters in command line
// random character generation
// constant
[indent=3]
def getRandomNumber(RangeFrom:int, RangeTo:int) : int /* function to create random number between range */
return GLib.Random.int_range(RangeFrom,RangeTo)
def getRandomChar() : char /* function to generate ascii codes from a-z and space (32) */
num:int = getRandomNumber(0,27)
if num == 0
num = 32
else
num = num + 96
return (char) num
def addRandomChar(myText:string) : string /* function add text from command line */
var retText = new StringBuilder
retText.append(myText)
retText.append_c(getRandomChar())
return retText.str
init
USAGE:string = "Usage:\n\t ./imt \"some text for monkeys to generate\" "
theText:string = args[1]
if theText == null
theText = ""
theText = theText.down() /* change any text input to lower case */
myText:string = ""
if theText != ""
do
do
myText = addRandomChar(myText)
while theText.len() != myText.len()
print("%s" , myText)
if theText != myText
stdout.printf("\n")
myText = ""
while theText != myText
stdout.printf("\n")
else
print("%s" , USAGE) /* this appears if program run without text */ a)
%%
Links
http://live.gnome.org/Genie official genie site
http://puppylinux.com/genie/ Barry's site
http://valadoc.org/?
http://www.vala-project.org/doc/vala/
http://live.gnome.org/Vala
http://live.gnome.org/Vala/Tutorial
http://library.gnome.org/devel/glib/
====Vala Genie Intro====
==What is this?==
An introduction to using the Genie Programming language under Puppy Linux
==Main features:==
- A true compiler
- A "Hello world" console executable is 2.9KB, a GTK GUI "Hello world", with a OK button thrown in, is 5.8KB
- Easy Python-like language
- Easy GTK programming
- Link directly with the system shared libraries
- No huge binding libraries, nothing required at runtime. No bloat!
- Library support
- Compile-time bindings available for most shared libraries.
- Plain-C intermediate code
- Only needs Gnu C compiler -- so can be compiled for any operating system and CPU.
- Object oriented
- Objects are not "bolted on" as for some languages that started life as procedural-only. But Genie code can be non-object-oriented if you wish.
- Programs written in Genie should have have similar performance and resource usage to those written directly in Vala and C
- Genie has none of the bloat and overhead that comes with many other high level languages which utilize a VM (Eg Python, Mono, et al)
- Classes in Genie are actually gobjects so Genie can be used for creating platform code like widgets and libraries where gobjects are required for binding to other languages.
- The generic nature of Glib means that you can use Vala/Genie for any kind of programing, but Glib/Gobject does make it particularly easy for GTK coding.
====={{color text="What is the difference between Vala and Genie?" c="black"}}=====
Vala is more like C/Java, Genie like Basic.
====={{color text="Who Created Genie?" c="black"}}=====
Jamie McCracken created Genie,
Jürg Billeter and Raffaele Sandrini are the main developers of Vala.
====={{color text="When were Vala and Genie created?" c="black"}}=====
Vala came first in mid-2006, with a C#-like syntax.
Genie followed in mid-2008 influenced in part by Python and Delphi.
These languages use the same compiler and support all the same features
====={{color text="How do I use Genie in Puppy?" c="black"}}=====
[[http://puppylinux.org/wikka/GenieProgramming Check here]]
====={{color text="Is Genie a good language to start programming with?" c="black"}}=====
Being like Python it forces an easy to use spaced format with a simple modern syntax. It is more consistent and flexible than BASIC and logo or other languages recommended for beginners. **However** be aware: Documentation in English is available, but not redundant and not always beginner-friendly. Documentation in other languages, is almost inexistant.
====={{color text="Is it suitable for professionals?" c="black"}}=====
Code is compiled to C, making it fast and efficient. Linux uses C as its main language and the Gnome project is developing Vala
====={{color text="Basic Concepts" c="black"}}=====
**Files**
Genie code must be written in files with the *.gs extensions. Vala codes must be written in files with the .vala extension.
When you want to compile Genie code, you give the compiler a list of the files required, and Genie/Vala compiler will work out how they fit together.
**Syntax**
Genie's syntax mixes features of several high level languages including Python, Boo, Delphi and D
Genie is case sensitive as a result of it being compiled into c code - so be careful when mixing case in your code.
An identifier is defined by its name and its type, e.g. i:int meaning an integer called i. In the case of value types this also creates an object of the given type. For reference types these just defines a new reference that doesn't initially point to anything.
Genie has a mechanism called Type Inference, whereby a local variable may be defined using var instead of giving a type, so long as it is unambiguous what type is meant.
Genie support is now in Gtksourceview:
====={{color text="Where can I find out about String handling in Genie?" c="black"}}=====
http://puppylinux.com/blog/?viewDetailed=00609
Genie strings page:
http://puppylinux.com/blog/?viewDetailed=00608
Just to remind to install the gtksourcefile pet as well as the Valide one
Mark has kindly prepared
http://dotpups.de/puppy4/dotpups/Programming/Vala/
Especially Glib is important.
For example the string functions in Genie are the GLib functions:
http://valadoc.org/?pkg=glib-2.0&element=string
A bit difficult to find when you are new, but very essential!
====={{color text="Where is valide code saved?" c="black"}}=====
cd /root/valide/Genie
./Genie
Or:
/root/valide/Genie/Genie
====={{color text="How do you compile code?" c="black"}}=====
valac -C v5.gs
valac -C v4.vala
This just creates v5.c and v5.h.
====={{color text="Can the code be used in other Linux distributions?" c="black"}}=====
Yes
====={{color text="Is Vala available for Windows?" c="black"}}=====
Yes
http://dotpups.de/puppy4/dotpups/Programming/Vala/Vala-for-Windows/
====={{color text="Where can I find out about Valide?" c="black"}}=====
http://valaide.blogspot.com/2009/03/genie-support.html
====={{color text="Code example Vala" c="black"}}=====
%%(language-ref)
class Demo.HelloWorld : GLib.Object {
public static int main(string[] args) {
stdout.printf("Hello, World\n");
return 0;
}
}
%%
**random method**
from GLib.
http://valadoc.org/?pkg=glib-2.0&element=GLib.Random
Example:
Code:
%%(language-ref)
// return a random value from 1 to 9
a:int
a = GLib.Random.int_range(1,10)
print("%d" ,
%%init
Infinite Monkey theorem
%%(language-ref)
// Infinite Monkey Theorem
// Lobster and Shadow, March 2009 LGPL
// use from command line
// imt ["search text"]
// generates random characters and searches for a given pattern eg 'in the beginning was the world'
// warning strings longer than "in the" may take minutes or hours to find
// program demonstrates:
// basic functions
// passing parameters in command line
// random character generation
// constant
[indent=3]
def getRandomNumber(RangeFrom:int, RangeTo:int) : int /* function to create random number between range */
return GLib.Random.int_range(RangeFrom,RangeTo)
def getRandomChar() : char /* function to generate ascii codes from a-z and space (32) */
num:int = getRandomNumber(0,27)
if num == 0
num = 32
else
num = num + 96
return (char) num
def addRandomChar(myText:string) : string /* function add text from command line */
var retText = new StringBuilder
retText.append(myText)
retText.append_c(getRandomChar())
return retText.str
init
USAGE:string = "Usage:\n\t ./imt \"some text for monkeys to generate\" "
theText:string = args[1]
if theText == null
theText = ""
theText = theText.down() /* change any text input to lower case */
myText:string = ""
if theText != ""
do
do
myText = addRandomChar(myText)
while theText.len() != myText.len()
print("%s" , myText)
if theText != myText
stdout.printf("\n")
myText = ""
while theText != myText
stdout.printf("\n")
else
print("%s" , USAGE) /* this appears if program run without text */ a)
%%
Links
http://live.gnome.org/Genie official genie site
http://puppylinux.com/genie/ Barry's site
http://valadoc.org/?
http://www.vala-project.org/doc/vala/
http://live.gnome.org/Vala
http://live.gnome.org/Vala/Tutorial
http://library.gnome.org/devel/glib/