Code examples

Here are a few ufLang examples to show you the synthax of the language.

PrintUf

printuf prints a string and adds a little "uf" at the beginning:

printuf("Hello World!");

// Output: "uf Hello World!"

var x : Int = 43;
var y : String = "hello";
var z : Double = 2.5;

print("Here is x: ");
println(x);
print("Here is y: ");
println(y);
print("Here is z: ");
println(z);

y = "hihi";
print("Here is y after set: ");
println(y);
output :
Here is x: 43
Here is y: hello
Here is z: 2.5
Here is y after set: hihi

var variable = 23; // int created

variable = "now this is a text"; // becomes a string

Classes

class Foo {
  inFoo() {
    println "in foo";
  }
}

class Bar : Foo {
  inBar() {
    println "in bar";
  }
}

class Baz : Bar {
  inBaz() {
    println "in baz";
  }
}

var baz = Baz();
baz.inFoo(); // expect: in foo
baz.inBar(); // expect: in bar
baz.inBaz(); // expect: in baz

Return types

fun test(n)
{
    if (n == 1) {
        return "uf"; // string returned
    }
    if (n == 2) {
        return 23; // int returned
    }
    else {
        return 92.0; // double returned
    }
}

println test(1);
println test(2);
println test(3);
output:
uf
23
92

Loops

// Single-expression body.
/*
for (var c = 0; c < 3;) {
    println c = c + 1;
}
*/
// expect: 1
// expect: 2
// expect: 3

println "before for";
for (var c = 0; c < 3; c++) {
    println c;
}
println "after for";



// Block body.
for (var a = 0; a < 3; a = a + 1) {
  println a;
}
// expect: 0
// expect: 1
// expect: 2

// No clauses.

// No variable.
var i = 0;
for (; i < 2; i = i + 1) println i;
// expect: 0
// expect: 1

// No condition.
fun bar() {
  for (var i = 0;; i = i + 1) {
    println i;
    if (i >= 2) return;
  }
}
bar();
// expect: 0
// expect: 1
// expect: 2

// No increment.
for (var i = 0; i < 2;) {
  println i;
  i = i + 1;
}
// expect: 0
// expect: 1

// Statement bodies.
for (; false;) if (true) 1; else 2;
for (; false;) while (true) 1;
for (; false;) for (;;) 1;
// Single-expression body.
var c = 0;
while (c < 3) println c = c + 1;
// expect: 1
// expect: 2
// expect: 3


// Block body.
var a = 0;
while (a < 3) {
  println a;
  a = a + 1;
}
// expect: 0
// expect: 1
// expect: 2


// Statement bodies.
while (false) if (true) 1; else 2;
while (false) while (true) 1;

Last updated