Skip to content

Basic Types

This section describes how to use fundamental variable types with examples.

A literal is a value that you include in the code that is known at compile time (i.e. without running the code).

These can be:

  • Integers: -3, 0, 5, …
  • Floats (decimal numbers): -7.845, 0.0, 5.776, …
  • Booleans: true, false
  • Strings: “a string is anything inside double quotes”
  • and Enumerations, which start with ’:’ and are alpha-numeric, for example :BEAST_TYPE_2. Enumerations are useful because they can only be one unique value, which is the name of the enumeration.

Basic variable types are the foundational elements for storing data in Tenta.

These types are:

  • Integers (whole numbers),
  • Floating point (decimal numbers),
  • Booleans (true or false),
  • Strings,
  • and Enumerations.

Integers can store either 16, 32, or 64 bit signed data (positive and negative values), Floats can store either 32 or 64 bit values (positive and negative), Booleans can only be true or false, Strings hold 8 bit characters inside quotes, and Enumerations hold a unique value that is created at compile time.

Variables are created by starting a statment with the type then the identifier (variable name). Integers and floats default to zero when created, booleans default to false, and strings default to "" (empty) when created.

Example:

i32 i;
f32 f;
bool b;
string s;
enum e;
//
println(i); // 0
println(f); // 0.0000000
println(b); // false
println(s); // doesn't print anything
println(e); // <Enum Invalid> <-- invalid because it's not set yet.

To do - add table of types and ranges

Numeric type details:

TypeFormatBitsMinimumMaximum
i16signed integer16-32768+32767

Literal values can be assigned to variables by using the ’=’ equals sign. This can be at the time the variable is created or later in the program.

// Creating and assigning values to 16, 32, and 64 bit integers:
i16 h = 3;
i32 i = 1;
i64 g = -5;
// for 32 and 64 bit floats:
f32 m = -5.5;
f64 f = 2.2;
// assignment after creation:
h = 2;
f = 8.922;
// for a boolean:
bool b = true;
// for a string:
string s = "test";
// and for an enumeration:
enum e = :TEST;

Multiple variables can be defined and assigned at the same time.

i16 a, b; // both zero by default
i32 c, d, e = 1, 2, 3; // c = 1, d = 2, e = 3
string s, t = "one", "two"; // s = "one", t = "two"

Casting a variable is used when you want to convert it from one type to another type. This can either be implicit (automatic) or explicit (manual).

Implicit casting is only used for integers and floats and is dependent upon the context. Any math operations with a mix of integers and floats will temporarily turn the integers into floats while doing the calculations (i.e. 5 becomes 5.0).

If a float is assigned to an integer it is always rounded downward. However, if an integer is assigned to a float variable it is unchanged.

An integer will automatically be converted to a float anywhere a float is expected.

// float to int automatic cast
i32 c = 2.9;
println(c); // prints 2
f64 d = 1;
println(d); // prints 1.000000

Warning: If a variable is assigned to the same type but with fewer bits the extra bits will be lost.

i32 x = 500000; // 32 bit integer
println(x); // prints 500000
i16 y = x; // 16 bit integer
println(y); // prints -24288

Explicit casting is used to manually convert between integer, float, bool, or string types.

// float to integer
println(5.7); // 5.700000
println(5.7 as i32); // 5
// integer to float
println(4); // 4
println(4 as f32); // 4.000000
// bool to number
println(true as i32); // -1
println(false as i32); // 0
// bool to string
string b = true as string;
// number to string
string c = 5.55 as string;
string d = 22 as string;
// combining strings after casts
string e = b + "--" + c + "--" + d;
println(e); // true--5.550000--22
// string to number
i32 g = "5" as i32;
f64 h = "33.2" as f64;
// enum to string
string s = :A as string;
println(s); // :A
// multiple casts in a chain
string k = true as i32 as f32 as string;
println(k); // -1.000000

Variable names can be reused by redefining them. This is particularly useful when converting types or swapping variables.

// reusing name for same type
i32 c = 5;
i32 c = 9;
println(c); // 9
// reusing name for different types
f32 age = 22.2;
string age = age as string; // converting age to string
i32 age = age as i32; // converting age to integer
println(age); // 22
// inline swap using redefinition
i32 a, b = 1, 2; // compound assignment
println(a as string + ", " + b as string); // 1, 2
i32 a, b = b, a; // inline swap
println(a as string + ", " + b as string); // 2, 1

Augmented assignment provides a shortcut when setting a variable to a modified version of itself.

f32 f = 5.2;
f = f + 0.2; // add 0.2 to f: f = 5.4
f += 0.2; // add 0.2 to f using augmented assignment: f = 5.6
// augmented assignment options
i32 i = 1;
i += 2; println(i); // 3 (addition)
i -= 1; println(i); // 2 (subtraction)
i *= 4; println(i); // 8 (multiplication)
i /= 2; println(i); // 4 (division)
i ^= 2; println(i); // 16 (exponential)
i %= 5; println(i); // 1 (modulus)

The variable augmentation can be used inside another statement or expression. The assignment happens before the variable is used.

i32 i;
println(i); // 0
println(i += 1); // 1
println(i); // 1
i32 h = 5 + (i += 2); // first add 2 to i, then add i to 5, then store the result in h.
println(i); // 3
println(h); // 8

Variable assignment and augmented assignment can be chained if desired to save space. Chains are evaluated right to left.

Warning: while this is convenient, it can make code harder to follow.

i32 i, j, k;
i = j = k = 3; // set k to 3, then set j to k, then set i to j
println(i as string + ", " + j as string + ", " + k as string); // 3, 3, 3
i32 a, b, c = 1, 2, 3;
a += b += c += 3; // add 3 to c, then add the result to b, then add the result to a
println(a as string + ", " + b as string + ", " + c as string); // 9, 8, 6

The type of a variable can be inferred during assignment by using the ‘auto’ type keyword.

auto i = 5; // i64
auto f = 4.7; // f64
auto g = i + f; // 9.7 as f64: (i + f becomes a f64 float type during addition)
auto b = true; // bool
auto s = "sss"; // string
auto e = :A; // enum