-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatements.t2t
88 lines (71 loc) · 2.02 KB
/
Statements.t2t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
Statements
%!includeconf: config.t2t
== Overview ==
A statement does something. Statements are terminated by a semi-colon and and
empty statement is valid. Here are some examples of statements.
```
var i = 10; // This is a statement.
i = 4; // This too.
; // Also a statement.
; i++; ; sout.writeln("Hello"); // Four statements.
i = // Part of a statement.
4 + 5 // More of it.
; // The end of it.
```
You should have noticed that lines have noting to do with statements.
== Blocks. ==
As well as semi-colon terminated statements we also have compound statements
called blocks. Blocks consist of any number of statements inside of braces.
```
{ // This is one statement.
var i; // It does the statements inside of it.
i = 5;
i += 10; // And returns the value of the last statement inside of it.
}
```
They are very useful for if statements.
```
// var i:int
var bt3;
if ( i > 3 )
{
stdout.writeln("i is bigger than three!");
bt3 = true;
}
else
{
stdout.writeln("i is smaller than three :(");
bt3 = false;
}
```
The value of a block is the value of the last statement executed. All exit
paths from a block must provide a value that can be stored inside the location
it is being sent to.
```
mutable s = {
var h = "1234567890"
h = sha256(s);
h = rot13(s);
} // Now r is the rot13 of a hash.
mutable i = {
var g = RandomGenerator(time.unix());
while ( g.next % 1_000_000 != 0 )
;
g.cur;
} // r is now a number that is a multiple of a million.
// var value:int
mutable i:real = {
if ( value < 0 )
{
sout.writeln("Oops, less than zero"); // Error: returns OutStream
}
else if ( value < 100 ) 100 * value; // Returns int, can be cast to real.
else 100 / value; // Returns int, can be cast to real.
}
var v = {
if (interger) 5;
else if (string) "five";
else if (float) 5.0;
else null;
} // All will be cast to Object. Possible compiler warning.
```