Syntax Introduction

Functions (and methods and lambdas) are defined using ()->. The following also shows how the 'main' function - the entry point of the program - can be written. (The return type can be omitted if desired.)

main()->Int {
    return 0;
}


Fields / variables are defined using : =, and both implicit and explicit type declaration is supported. Note that literal values have the smallest type needed to hold the value, unless the literal type signifier is supplied (more details on this later).

ubyteValue := 32;
intValue := 32I;
intValue : Int = 32;


Type definitions require the type keyword. The following examples also show how simple array and reference types are defined.

type MyIntAlias : Int;
type MyIntArray : [4]Int;
type MyIntReference : ∬
Note: Tuplex naming convention is to start type names with a capital letter, and functions, fields / variables with a lower case letter (except global constants which should be all-caps).
The syntax of scalar and boolean expressions, as well as function calls, is straight-forward and similar to other languages like C++ and Java. If and while statements can be single-statement using colon, or multi-statement using brackets.

result := (2.0 + square( someLen )) / 3.14;
if result > 20.0:
    puts( c"big!"[0] );  ## (temporary c-string syntax)
~tmp := Int( result );   ## casts from floating-point to Int
while tmp > 0 {
    puts( c"iteration!"[0] );
    tmp = tmp - 1;
}
Note: Tuplex variables / fields are immutable by default. The 'mod' keyword or the '~' tilde token are used to indicate the value may be modified after initialization.
Other basic syntax elements:
  • Line comments begin with ##.
  • Multi-line comments are enclosed between /* and */. Comments can be nested.
  • Statements are terminated with ';'.
  • Bodies of modules, types, functions, and suites of statements are enclosed in '{}' braces.

In single-file programs it isn't necessary to declare a module name. In order to use types and functions of other modules however they must be imported. Import statements must appear at the beginning of a file or module.

import tx.c.*;  ## imports all names in the tx.c module
Note: The tx module is imported by default (equivalent to the statement 'import tx.*;'). This contains all the built-in types.

Now let's put it all together into a working program:

import tx.c.puts;  ## imports the putc name from the tx.c module

/** converts arg to Float and returns its square */
square( intVal : Int ) -> Float {
    return Float(intVal) * Float(intVal);
}

/** a global constant */
INT_VALUE := 9I;

main()->Int {
    someLen := INT_VALUE;
    result := (2.0 + square( someLen )) / 3.14;
    if result > 20.0:
        puts( c"big!"[0] );  ## (temporary c-string syntax)
    ~tmp := Int( result );   ## casts from floating-point to Int
    while tmp > 0 {
        puts( c"iteration!"[0] );
        tmp = tmp - 1;
    }
    return 0;
}

No comments:

Post a Comment