-
Notifications
You must be signed in to change notification settings - Fork 4
Examples
Rui Ventura edited this page Apr 3, 2017
·
1 revision
The examples below aren't exhaustive nor do they demonstrate every aspect of the language.
The following example illustrates a program in two modules: one that defines the
factorial function and the other defines the xpl function (main function).
Factorial function definition in a file (factorial.xpl)
public int factorial(int n) = 1 {
if (n > 1) factorial = n * factorial(n-1); else factorial = 1;
}Example of the usage of the factorial function in another file (main.xpl)
// external builtin functions
use int argc()
use string argv(int n)
use int atoi(string s)
// external user functions
use int factorial(int n)
// the main function
public int xpl() = 0 {
int f = 1;
"Factorial function test"!!
if (argc() == 2) f = atoi(argv(1));
f! "! = "! factorial(f)!!
}How to compile:
$ xpl --target asm factorial.xpl
$ xpl --target asm main.xpl
$ yasm -felf32 factorial.asm
$ yasm -felf32 main.asm
$ ld -melf_i386 -o main factorial.o main.o -lrts