T Programming Language Tutorial


This document provides an introduction to the T language, largely by example. However, it does contain links to the T language specification, where the technical details are given.

T is a simple object-oriented programming language, modeled on Java. T supports only one primitive type, the integer type. T also supports reference types via the class Object, which is the root of the inheritance hierarchy. T supports only single-inheritance.

T syntax is derived from Java syntax. However, T programs are single-file programs. The file can contain a collection of user-defined classes, but must always contain a main block where execution will begin.

Since T does not support character or string primitive types, you cannot implement the traditional "hello, world" program in T. But, you can write a program to reveal the meaning of life:

// "the meaning of life, the universe, and everything"
//
int main()
{
  out 42;
}
This example also illustrates that T has Java-like comments.

T has no input capability and can only output integers via the out statement, which prints the value of an expression to stdout.

Note that T only has these basic control-flow structures:

T provides basic support for integer operations, including addition, subtraction, multiplication, division, unary minus, greater than, less than, equals and logical not. Note that there are no operators for greater than or equal, less than or equal, and not equals.

Here is a T program to compute i! for i = 0 to 12:

int main()
{
  int i;
  int value;

  i = 0;
  value = 1;

  while (i < 13)
  {
    out i;
    out value;

    i = i + 1;
    value = i * value;
  }
}

T supports arrays. For example, here is a T program to initialize and then sum an array of integers.

int main()
{
  int x[];
  int i;
  int sum;

  // create the array
  x = new int[10];

  // initialize x[i] = i
  i = 0;
  while (i < 10)
  {
    x[i] = i;
    i = i + 1;
  }

  // sum x[i] for i = 0 to 9
  i = 0;
  sum = 0;
  while (i < 10)
  {
    sum = sum + x[i];
    i = i + 1;
  }

  // output the sum
  out sum;
}

All method invocations in T are virtual, meaning that the run-time type of an object is used to choose the method to invoke. Here is a T version of the classic "animals speaking" example: Unfortunately T does not have strings so we need to encode the animal sounds with numbers: pigs say "1999", roosters say "1066", dogs say "1492" and cows say "1980".

class Animal
{
  // all animals must speak
  int speak()
  {
    // abstract method: should not be called
    out -1;
    return 0;
  }
}

class Pig extends Animal
{
  int speak()
  {
    out 1999;
    return 1;
  }
}

class Rooster extends Animal
{
  int speak()
  {
    out 1066;
    return 1;
  }
}

class Dog extends Animal
{
  int speak()
  {
    out 1492;
    return 1;
  }
}

class Cow extends Animal
{
  int speak()
  {
    out 1980;
    return 1;
  }
}

int main()
{
  Animal zoo[];
  Animal animal;
  int i;

  // initialize an array and place some animals in it
  zoo = new Animal[10];
  zoo[0] = new Cow();
  zoo[1] = new Dog();
  zoo[2] = new Rooster();
  zoo[3] = new Pig();

  // now iterate over the array and ask each animal to speak 
  i = 0;
  while (!(zoo[i] == null))
  {
    animal = zoo[i];
    animal.speak();

    i = i + 1;
  }
}

T supports the overloading of methods. See if you can determine what this program outputs:

class A {
  int f(int i) { return 1; }
  int f(A a) { return 2; }
}

class B extends A {
  int f(int i, int j) { return i + j; }
}

int main()
{
  A a;
  B b;

  a = new A();
  b = new B();

  out b.f(19) + 14;
  out b.f(a) + 13;
  out b.f(b) + 13;
  out b.f(7, 8);
}


Last modified on June 26, 2018.

Comments and questions should be directed to pjh@cs.unh.edu