Tomato is a general purpose programming language.
Every programm consist of statements. Valid statements are:
- Print statement (
print <expression>
); - Expression;
- Variable declaration;
- Assignment;
- Control flow statement.
Tomato language is statically and strongly typed, i.e. once variable was declared, its type can't be changed and only values of that type can be assigned to variable.
Variables and constans must be declared only once and can't be used until declared.
There is a general grammar of declaring constant or variable:
<let|var> <name> [type] [= <expression>]
e.g.:
let pi = 3.14 let e float = 2.71 var x = 2 var i int var c char = 'a'
Declaring a constant requires initial value, while declaring a variable allows to omit it setting variable to default 'zero' value.
There are simple rules for deducting numerical types:
- Binary operation on integer and float produces float;
- Division produces float;
- Division by 0 is an error.
- Float exponent with negative base is an error;
- Negative integer exponent with an integer base is an error;
Expression can consist of literals, variables, binary and unary operators and nested expressions (using parentheses)
Supported operators are:
- Algebraic (
+
,-
,*
,/
,^
) - Comparative (
==
,!=
,>
,>=
,<
,<=
) - Boolean (
not
,and
,or
)
All operators are left-associative, except exponentiation (^
) which is right-associative.
Operator not
is unary. +
and -
can be both unary and binary depending on their position.
Expressions are evaluated according to next operator precedence:
^
*
and/
- unary
+
,-
andnot
+
and-
==
,!=
,>
,>=
,<
,<=
and
or
Both if
and while
define local name scope, so variables can be defined inside their bodies but not available after the end
of body bock.
Condition must be boolean expression. Alternative else
branch is optional.
For now else-if
branches are not supported, but nested statements works fine.
Shorten form:
if <condition> then [statements] end
Full form:
if <condition> then [statements] else [statements] end
Condition must be boolean expression:
while <condition> do [statements] end
Build using CMake, C++17 support is required:
$ mkdir cmake-build $ cd cmake-build $ cmake .. $ cmake --build . $ ./src/tomato
GNU readline library is used for interactive command-line interface.