Jump to content

User:C.Ezra.M/sandbox/Thyme (Algodoo)

From Wikipedia, the free encyclopedia

Thyme is a dynamically, weakly typed scripting language used in Algodoo.

Syntax

[edit]

The syntax largely resembles C++, Java, JavaScript, and Go syntax. Semicolons are required after every but the last statement in a block. They are not required in the console too.

Notably, the ^ symbol indicates exponentiation.

// Explanation of the syntax
/* This is a multiline,
   non-nestable comment */

apples := 1;   // := creates variables
apples = 5;    // = assigns value to variable

print(2 + 3);              // prints 5 to the console
print("Hello " + "World"); // prints "Hello World" -- the + symbol can concatenate
print(2 - 3);              // -1
print(2 * 3);              // 6
print(2 / 3);              // 0 -- integer division
print(2.0 / 3);            // 0.66666669 -- single-precision float
print(3 % 2);              // 1
print(3 ^ 2);              // 9
print(-2);                 // -2 -- demonstrates the unary minus

list := [1, 2, 3, 4, 5];

/*
  Lists are ordered and untyped collections of data.
  Lists are also immutable, so it's not possible to change
  a single item without assigning a list of the same size.
*/

list = list + [0, 0, 0, 2, 0];
// This is the only way to change a list's item.
// Will throw error if the lists to add are of different lengths.

//list(3) = list(3) + 2;
// This will throw an error!

print(list(3)); // prints 6: lists are 0-indexed

list = 1..5; // The .. operator creates an inclusive range list

list = list ++ 6..10; // the ++ operator concatenates lists

print(list(9)); // prints 10

print(!true); // prints false
print(false && true); // prints false
print(false || false); // prints false

/* In Thyme, the && and || operators are somewhat slower because
   they do not short-circuit. */

print(2 ^ 3 ^ 2); // 512 -- the exponentiation operator is right-associative

print(list(2..4)) // [3, 4, 5] -- lists can be used for indexing too

Thyme only has eight keywords: true, false, null, undefined, +inf, -inf, NaN, and infix.

Data types

[edit]

Thyme has six immutable primitive data types, two function types, and two structural types.

  • Primitive
    • int: 32-bit integer, corresponds to C++'s long type
    • float: IEEE 754 single-precision float (corresponds to C++'s float type)
    • string: corresponds to C++'s char * type
    • boolean: true and false
    • null
    • undefined
  • Function types
    • function (requires arguments)
    • block (no arguments)
  • Structures
    • list: immutable, ordered collection of mixed-type items
    • ClassObject: mutable, indexed collection of mixed-type items