asa 0.9.0

Advanced Subleq Assembler. Assembles 'sublang' to subleq
Documentation
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# Introduction to Sublang

Sublang is a bare bones assembly languages consisting of four main elements:
* The SUBLEQ instruction
* Labels
* Macros
* Syntax sugar


## Subleq


```clojure
1 2 3 ; This is interpreted as standard subleq: mem[2] -= mem[1] jump to 3 if LEQ otherwise it goes to the next instruction.
; This syntax is valid, but pointless.

; memory addresses and instructions may be labeled
a -> 1
b -> 2
c ->
    a -= b ; SUBLEQ: Subtract b from a
    a -= b c ; Subtract b from a and jump to c if the result is less than or equal to zero

; If no `c` argument is given, the next instruction will always be executed, even if the result is LEQ
; So these two are equivalent
a -= b
a -= b $1 ; `$1` gives a relative address with offset one

a -= b ; is equivalent to
b a $1 ; This syntax works but is not recommended, since it makes it harder for the assembler to give hints

; Other examples, literals and labels may freely be combined
a -= b 0x0000
'\0' -= 0 c
```

## Labels and Literals

```clojure
a -> 123 ; Decimal
b -> 0x4C6 ; Hex
c -> "Hello, World!" ; Strings are null-terminated by the assembler
d -> 'P' ; Character literals

.label ->
    a -= b .label   ; Repeats as long as (a -= b) <= 0
```

## Scopes

Scoping works like in most other languages. Note: Only labels are affected by scopes, macro definitions in scopes will still be globally accessible
```clojure
Z -> 123
X -> 456
Y -> 0
{
    Z -> 789
    {
        X -= Z  ; 456 - 789
    }
}
Y -= Z ; 0 - 123
```


## IO

```clojure

char -> 'a'
input -> 0
W -> 0

-1 -= char ; Prints 'a' to the screen

input -= -1
-1 -= input ; Echoes back users input

W -= W -1 ; Halts execution

```

## Types

The assembler has a simple type-checker, which can be disabled.

* `value` normal label
* `l_value` literal value
* `s_value` scoped value
* `a_value` anything, no type checking
* `b_value` a braced value
* `m_value` a macro call passed as argument, must be braced. In practice it's the same as `b_value`
Currently types are only checked for macro parameters.

## Naming conventions

* `@MyMacro` macros in CamelCase
* `my_label` labels in snake_case, with the exception of single character 'registers', like `Z` or `W`
* `MyModule.sbl` modules (files) are in CamelCase
### Labels
* `p_value` pointer (not type-checked)
* `p_p_value` pointer to pointer (not type-checked)
* `n_value` negated value (not type-checked)
* `value?`  macro argument in definition
* `.value` label to jump to
* `CONST_VALUE` constant, can be applied to all of the above and should be applied to macro arguments, but NOT to literals (l_name), since they are always constant by definition
* `Module::Value` or `!Module::Macro` namespacing
* `Module::SubModule::value`


## Macros

### Definition

```clojure
@Name {
    ...
}
@Name   ; This is allowed as well, but discouraged
{
    ...
}

; with parameters
@Name a? b? c? {
    ...
}

; It is also possible to define a macro that isn't scoped:
@Name a? [
    ...
]
; This, however, is dangerous when label definitions take place in the macro, so it is generally discouraged.

; Linebreaks are allowed between parameters

@Name a?
      b?
      c?
      d?
      e? {
    ...

}

```
### Expanding

```clojure
!Name
; With arguments
!Name2 a b c
; Linebreaks are  allowed between arguments
```

### Hygiene

Macros are hygienic. Variables won't be shadowed.
```clojure
; Macros
a -> 0
@MyMacro b {
    a -= b
    a -> 123
}

!MyMacro a
; Is completely fine, and will become the following:
{
    ?MyMacro?a -= a
    ?MyMacro?a -> 123
}
```

### Macro arguments

You may pass scopes as macro arguments

```clojure
!Mac s_my_scope? {
    s_my_scope?
}


!Mac { a -= b } 
; =>
{
    { a -= b }
}

; If a macro takes multiple scopes, they can be chained as follows:
!Mac {
    ...
} {
    ...
} {
    ...
}
```
If you don't want the argument to be surrounded by scopes, you can use braces

```clojure
@Mac b_my_braced? {
    b_my_braced?
}

!Mac ( a -= b )
; =>
{
    a -= b
}
```
This means that you can 'curry' macros (using that term loosely)
```clojure
@Mac b_some_macro? {
    b_some_macro? 10
    b_some_macro? 3
}

@CurriedMacro l_a? l_b? {
    l_a? -= l_b?
}
!Mac ( !CurriedMacro 5 ) 
; =>
{
    {
        5 -= 10
        5 -= 3
    }
}

```

## Pointers

### Referencing

To create a pointer to a value, the relative address syntax `$1` must be used to get the address of the next token
```clojure
ptr -> $1 0x1234 ; This takes up two words of memory, one for the pointer and one for the value

ptr -> $1 "String"
; or
ptr -> &'A'
ptr -> &"String"
ptr -> &123
; & is equivalent to $1
```

### Dereferencing

```clojure
; Generic sequence for dereferencing. The value that 'b' points to will be subtracted from 'a'
!Copy ptr b
a -= (b -> 0)
; If 'ptr' is constant the following is also legal. Note: ptr doesn't have to be constant but this syntax will give unexpected results if it isn't
a -= (b -> PTR)

; The '*' operator may also be used:
a -= *ptr
; This is effectively syntax sugar for
!Copy ptr b
a -= (b -> 0)


; (But in reality it is exactly equivalent to)
_ASM    _ASM    &1
*ID*ptr *ID*ptr &1
ptr     _ASM    &1
_ASM    *ID*ptr &1
a -= (*ID*ptr -> 0)
; *ID*ptr is a safe and automatically generated name

```
Remember that because of how Subleq works, what are called 'Labels' here, are also just pointers! But since Subleq dereferences them, we can think of them as values. But keep in mind that literals require indirection `a -= 10` doesn't subtract 10 from a


## Inclusions

The `#` symbol may be used to include another .sbl file anywhere
```Clojure
#MySblFile.sbl

; You may leave out the .sbl extension:
#MySblFile


; You can also do this:
...
Z -= Z
P -= Z
#IncludeMe

!Macro P
...
; But of course beware of the contents of the included file
```
If you want to create a module (a set of .sbl files in a folder) you must create a folder with the name of the module (for example 'sublib'). And in that folder create a Lib.sbl file. Whenever the 'sublib' folder is imported, this is automatically resolved to 'sublib/Lib.sbl'. In this .sbl file you may include any other files you might need. Includes are initially resolved relative to the file
that is including, and if the target isn't found in the *LIBS* folder, defined using the `-l` command line argument.


```clojure
; ./subleq/MyFile.sbl
#math/FastSqrt

```
The order in which files are checked is as follows. The first one that exists will be included.
* `./subleq/math/FastSqrt/Lib.sbl`
* `./subleq/math/FastSqrt.sbl`
* `LIBS/math/FastSqrt/Lib.sbl`
* `LIBS/math/FastSqrt.sbl`


See subleq/libs/sublib for an example.

## Syntax sugar

### Mult operator

When the '*' is placed before a literal ?? The previous token is repeated n times
```clojure
label * 3 ; =>
label label label

0x123 * 0x4 ; =>
0x123 0x123 0x123 0x123

; mind that 3 * label will dereference `label`!
```
### Dereference operator




## Style guide

Just make sure it looks good :), or follow the style of the Sublib

## Namespacing

The format `Namespace::Macro` or `Namespace::label` should be used. This is solely a naming convention and not enforced in any way. This means that module authors must decide what namespace their macros or labels should have. This is obviously bad design, but it is simple.

## Conclusion

For many more examples see the standard library, called Sublib



## Sublib

Sublib is the standard library. It has a range of very basic features (Prelude.sbl, IO.sbl and Symbols.sbl) to quite advanced ones like functions and control flow.


## Examples

### Basic

```clojure
!Print p_string


Z -= Z -1 ; Jumping to -1 halts
; equivalent to !Halt



p_string -> &"Hello, World!\n"
Z -> 0 ; Temp register
N_ONE -> -1

**
    Pure no dependency implementation
**
@Print P_STRING? {


    ; Copy the pointer into the local ptr
    Z -= Z
    Z -= P_STRING?
    ptr -= Z

    Z -= Z
    .loop -> 
        char -= char ; Clear char
        Z -= (ptr -> 0) ; Z -= *ptr
    
        char -= Z .fin ; Flip the character, since it is negative, and jump if
                       ; result is LEQ (i.e. finish if it is a NULL)
        -1 -= char ; Writes the character to the screen

        ptr -= N_ONE ; Increment the pointer
        Z -= Z .loop ; Infinite loop

    char -> 0 ; This point is never reached, so it is safe to define the
              ; label 'char' here. It is very important to keep in mind
              ; that, in this case the zero, will be put in memory in
              ; this exact place and, if execution crosses it, it will
              ; be interpreted as an instruction. To define values in between
              ; instructions, use the '=' operator

    .fin ->
}
```

### Sublib


```clojure
; This is how Sublang could should be written, making extensive use of macros
!J .main ; Jump to main
#sublib

#sublib/Control


p_string -> &"Hello, Sublang!\n"

**
    Or using the standard lib
**
@PrintStdLib P_STRING? {
    != p_local P_STRING? ; p_local = P_STRING?
    !=0 char ; char = 0
    
    !Loop {
        !DerefAndCopy p_local char ; char = *p_local
        !IfFalse char {
            !Break
        }
        !IO -= char
        !Inc p_local
    }
}

.main -> {
    !PrintStdLib p_string
    !Halt
}

```
```clojure
; Or you can just use one of the Print macros from sublib/IO
!J .main
#sublib


main -> {
    IO::PrintLnLit "Hello, Sublib!"
    !Halt
}
```