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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
## Micro2 programs
A program is a sequence of:
* function declarations
* statements
* TODO: if-else, for, for-in, etc; with no semicolon
Statements are delimited with semicolons (`;`), and are one of:
* expression (e.g. `1+2`)
* function call
* lambda definition
* variable
* variable declaration
* return statement
XXX: do we really want expressions without effects?
## Modules
A `micro2` file must start with the module name.
```
# our main module (this is a comment)
module main
```
The module name is used by the linker.
## Built-in types
### Integers
| Type | Description | Samples |
| --- | --- | --- |
| u8 | unsigned 8-bit | `123; 0xce; 0b10000;` |
| s8 | signed 8-bit | `-123;` |
| u16 | unsigned 16-bit | `65535; 0xffff;` |
| s16 | signed 16-bit | `-4096;` |
Builting functions:
| Function | Description | Samples |
| --- | --- | --- |
| hi | Get the MSB on a 16-bit number | `hi(0xaabb); # 0xaa`|
| lo | Get the LSB on a 16-bit number | `lo(0xaabb); # 0xbb`|
Type conversion is explicit:
```
var a: u8 = 10;
var b: u16 = 10;
a + b; # error: type mismatch
u16(a) + b; # 20: u16
```
### Booleans
| Type | Description | Samples |
| --- | --- | --- |
| bool | boolean | `true; false;` |
### Functions
| Type | Description | Samples |
| --- | --- | --- |
| ([params]) [-> [return]] | functions | `() { return; }` |
Functions can be defined with `def` when they have a name, or as anonymous using the lambda syntax.
```
def add(a: u8, b: u8): u8 {
return a + b;
}
add(2, 8); # 10
```
Anonymous function:
```
# increment the value passed as argument
(a: u8): u8 {
return a + 1;
}(10); # 11
```
Function are higher order functions:
```
def apply(fn: (u8) -> u8, a: u8, b: u8): u8 {
return fn(a, b);
}
apply(add, 2, 10); # 8
# using a lambda
apply((a: u8, b: u8): u8 {
return a + b;
},
2, 8); # 10
# lambdas can be assigned to variables
var double: (u8) -> u8 = (a: u8): u8 {
return a + a;
};
double(10); # 20
```
Anomymous functions can only access local variables (closures aren't supported):
```
def closure(a: u8): () -> u8 {
return (): u8 {
return a; # invalid return, undefined a
};
}
```
Functions are exported by default, unless they are defined as private:
```
# not exported (exported is the default)
private def dec(a: u8): u8 {
return a - 1;
}
```
### Special value: nil
`nil` is a reference not pointing to a value, used for example with variables with type function.
```
# fn doesn't hold a reference
var fn: (u8) -> u8 = nil;
fn(10); # runtime error
```
## Variables
Variable declaration:
```
var a: u8 = 123;
```
Group declaration:
```
var (
a: u8 = 123,
b: u16 = 1234;
);
```
Vaiables must be initialized, there are not default values.
### Constants
Constant are not variables, they don't use memory.
```
const K: u8 = 10;
```
Group declaration:
```
const (
A: u8 = 128,
B: u16 = 4096
);
```
Must be a constant expression and that can be resolved at compilation time.
```
var a: u8 = 1;
const A: u8 = a + 1; # error: unresolved value
```
|