aboutsummaryrefslogtreecommitdiff
path: root/language.md
blob: 2440cc74063935a90888e56e9dbaff37f37bee51 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# Overview

TODO

This is an example of a program:

```
def fib(n: u16): u16 {
    if n < 2 {
        return n;
    } else {
        return fib(n - 1) + fib(n - 2);
    }
}

fib(20); # 6765
```

## 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?

TODO: entry point; `main` to be C compatible?

## 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.

TODO: "import" and use modules.

## Variables

Variable declaration:
```
var a: u8 = 123;
```

Group declaration:
```
var (
    a: u8 = 123,
    b: u16 = 1234
);
```

Variables must be initialized, there are not default values, with the exception of structures that is optional.

Variables can refer to a memory address with `@` operator and the data on that address will be used as initialization:
```
var p: u8 = @0x8000;

p; # whatever byte is in address 0x8000 (peek)
p = 0; # byte at 0x8000 is now 0 (poke)
```

Variables are exported by default, unless they are defined as private:
```
private val local: u8 = 123;
```

### Constants

Constant are immutable values and may not have memory allocated to them:
```
# won't allocate memory
const K: u8 = 10;

# will allocate 10 bytes
const V: [10]u8 = 255;
```

Group declaration:
```
const (
    A: u8 = 128,
    B: u16 = 4096
);
```

Must be resolved at compilation time.
```
var a: u8 = 1;

const A: u8 = a + 1; # error: unresolved value
```

## 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;`              |

Built-in 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` |

`hi` and `lo` can also be used with references (functions, structures and arrays):
```
def fn() { return; }

hi(fn); # MSB of fn address
```

Type conversion is explicit:
```
var a: u8 = 10;
var b: u16 = 10;

a + b; # error: type mismatch
u16(a) + b; # 20: u16
```

### Booleans

Logic operators result on a boolean type.

| Type | Description | Samples                |
| ---  | ---         | ---                    |
| bool | boolean     | `true; false; 1 == 1;` |

### Functions

| Type                       | Description                | Samples                           |
| ---                        | ---                        | ---                               |
| ([parameters]) -> [return] | function                   | `(a: u8) -> u8 { return a + 1; }` |
| ([parameters])             | function (no return value) | `() { return; }`                  |

Functions can be declared 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
```

Anonymous 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;
}
```

Variables of type function use references:
```
def fn() { return; }

# fn and fn2 refer to the same function
var fn2: () = fn;
```

### Special value: nil

`nil` is a reference not pointing to a value, used for example on variables with type function or structure.

Using a *nil* reference will result in a runtime error.

```
# fn doesn't hold a reference
var fn: (u8) -> u8 = nil;

fn(10); # runtime error
```

### Structures

Structures can be used to group data and functions.

Structure can be declared with `def` and provide values for the grouped data. Those values will be used when allocating an instance.

Any variable or function declared in the structure can be accessed like a local variable inside the structure, and in the instances using the dot (`.`) operator.

```
def A {
    # constants can be part of an structure as well
    const INC: u8 = 1;

    var (
        n: u8 = 100,
        m: bool = false,
        dec: (u8) -> u8 = nil
    );

    # it is possible to define functions local
    # to a structure
    def inc(): u8 {
        n = n + INC;
        return n;
    }
}

# allocate memory for structure A
var a: A;

a.n; # 100
a.m; # false
a.dec; # nil

a.inc(); # 101
a.inc(); # 102

a.n; # 102
```

Variables of type structure handle a reference:

```
var b: A = a;

# b points to the same data as a
b.inc(); # 103
a.n; # 103

# c doesn't hold a reference to an instance of A
var c: A = nil;
```

Recursive structures are not supported and local structures can't be used as return value in a function.

### Arrays

Arrays are zero based and are supported for all types.

XXX: including arrays? e.g. `[10][10]u8`.

Array size is a numeric literal or a constant expression (must be known at compilation type), and all the elements on an array must be of the same type.

Arrays are initialised to literals with `[` and `]` providing a list of values.
```
# array of 5 u8
var arr: [5]u8 = [0, 0, 0, 0, 0];
```

It is possible so initialize the array providing one single value that will be used for all the elements:
```
# this is equivalent to the previous example using [ and ]
var arr: [5]u8 = 0;
```

The array size is optional when initializing using literals:
```
var xs: []bool = [true, true, false];

len(xs); # 3

var xs2: []bool = true; # error: missing array size
```

Array elements can be accessed using `[]`:
```
var arr: [5]u8 = 0;

arr[0]; # 0
arr[0] = 100;
arr[0]; # 100
```

Variables of type array handle a reference.
```
# arr and arr2 refer to the same array
var arr2: [5]u8 = arr;
```

Local arrays can't be used as return value in a function.
```
def fn(): [5]u8 {
    var local [5]u8 = 0;
    return local; # error: returning a local value of type array
}
```

Built-in functions:

| Function | Description                       | Samples                               |
| ---      | ---                               | ---                                   |
| len      | get the length of an array as u16 | `len(arr); # 5`                       |
| incbin   | load a binary file as []u8        | `const f: []u8 = incbin("file.bin");` |

### Strings

TODO: a zero ended array of u8 with special initializers

## Operators

TODO

## Flow control

TODO

* if-else
* loops (while-like, infinite, break, continue)
* for-in

## External functions

TODO

* is this "import"?
* calling conventions?
* namespace support?

## In-line ASM

TODO