Tiny Imperative Language (TIL) Example Programs

Only a couple so far, hopefully many more to come. We've assumed a C++ style commenting convention for the TIL language to make these more readable.

factors.til

Note: This program uses an unspecified assumption (that the "write" statement outputs a full line) -- JRC

// Find all factors of a given input number - J.R. Cordy August 2005
var n;
write "Input n please";
read n;
write "The factors of n are";
var f;
f := 2;
while n != 1 do
    while (n / f) * f = n do
        write f;
        n := n / f;
    end
    f := f + 1;
end

factorial.til

Note: this program, from the Stratego manual, has an error (declaring x) and uses a couple of unspecified assumptions (that the "write" statement does not output a full line, and that "\n" is allowed and outputs a newline) -- JRC

// TIL program computing the factorial - Eelco Visser 
var n;
read n;
var x;
var fact;
fact := 1;
for x := 1 to n do
    fact := x * fact;
end
write "factorial of ";
write n;
write " is ";
write fact;
write "\n";

multiples.til

A for loop testing program.

// Test of declaring for loop in TIL
// Output first 10 multiples of numbers 1 through 9
for i := 1 to 9 do
for j := 1 to 10 do
    write i*j;
end end

-- JamesCordy - 22 Aug 2005 (revised JamesCordy - 10 Oct 2005)

Revision: r1.3 - 10 Oct 2005 - 18:57 - JamesCordy
Sts > StsBench > TILChairmarks > TILTrainingSet
Copyright © 1999-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback