asa 1.0.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! Reads, typechecks and expands macros
use crate::args;
use crate::asm_details;
use crate::asm_error;
use crate::asm_error_no_terminate;
use crate::asm_hint;
use crate::asm_info;
use crate::asm_warn;
use crate::symbols;
use crate::terminate;
use crate::tokens::*;
use crate::utils::IterVec;

use colored::Colorize;
use std::collections::HashMap;
use std::fmt;

#[derive(Clone, Default)]
pub struct Macro {
    name: String,
    info: Info,
    params: Vec<(String, Info)>,
    body: Vec<Token>,
    labels_defined_in_macro: Vec<String>,
}

impl fmt::Debug for Macro {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "{}:    ", self.name.yellow())?;
        for i in &self.params {
            write!(fmt, "{} ", i.0)?;
        }
        write!(fmt, "\n{: >4?}\n", self.body)?;
        Ok(())
    }
}

/// Grab all macro definitions, returns the tokens without macro definitions and a
/// map with macros
pub fn read_macros(tokens: &[Token]) -> (Vec<Token>, HashMap<String, Macro>) {
    let mut new_tokens: Vec<Token> = Vec::with_capacity(tokens.len());
    let mut macros: HashMap<String, Macro> = HashMap::new();

    enum Mode {
        Normal,
        Parameters,
        /// A macro body may be scoped {} or not scoped []
        Body {
            bounded_by_scopes: bool,
        },
    }
    let mut mode: Mode = Mode::Normal;
    // Tracks scopes inside of a macro body
    let mut internal_scope_tracker = 0;
    // Tracks scopes over all the tokens
    let mut global_scope_tracker = 0;
    let mut cur_macro: Option<Macro> = None;

    for i in 0..tokens.len() {
        let token: &Token = &tokens[i];
        match mode {
            Mode::Normal => match &token.variant {
                TokenVariant::MacroDeclaration { name } => {
                    cur_macro = Some(Macro {
                        name: name.clone(),
                        info: token.info.clone(),
                        params: Vec::new(),
                        body: Vec::new(),
                        labels_defined_in_macro: Vec::new(),
                    });
                    internal_scope_tracker = 0;
                    if global_scope_tracker != 0 {
                        asm_warn!(
                            &token.info,
                            "Macros defined inside of a scope will still be accessible globally"
                        );
                    }
                    if let Some(x) = macros.get(&cur_macro.as_mut().unwrap().name) {
                        asm_warn!(
                            &token.info,
                            "A macro with this name has already been defined {}",
                            x.name
                        );
                        asm_details!(&x.info, "Here");
                    }
                    mode = Mode::Parameters;
                }
                TokenVariant::MacroBodyStart | TokenVariant::MacroBodyEnd => {
                    asm_error!(&token.info, "Unexpected token");
                }
                TokenVariant::Scope => {
                    global_scope_tracker += 1;
                    new_tokens.push(token.clone())
                }
                TokenVariant::Unscope => {
                    global_scope_tracker -= 1;
                    new_tokens.push(token.clone())
                }
                _ => {
                    new_tokens.push(token.clone());
                }
            },
            Mode::Parameters => match &token.variant {
                TokenVariant::Linebreak => { /* Linebreaks are allowed between parameters */ }

                TokenVariant::Label { name } => {
                    cur_macro
                        .as_mut()
                        .unwrap()
                        .params
                        .push((name.clone(), token.info.clone()));
                    if !name.ends_with('?') {
                        asm_info!(
                            &token.info,
                            "Notate macro parameters with a trailing question mark ",
                        );
                        asm_hint!("'{name}' -> '{name}?'");
                    }
                }
                TokenVariant::MacroBodyStart => {
                    mode = Mode::Body {
                        bounded_by_scopes: false,
                    };
                }
                TokenVariant::Scope => {
                    mode = Mode::Body {
                        bounded_by_scopes: true,
                    };
                    cur_macro.as_mut().unwrap().body.push(token.clone());
                    internal_scope_tracker += 1;
                }

                _ => {
                    asm_error!(
                        &token.info,
                        "Only labels may be used as parameters for '{}'",
                        cur_macro.unwrap().name
                    );
                }
            },
            Mode::Body { bounded_by_scopes } => match &token.variant {
                TokenVariant::LabelArrow { .. } if !bounded_by_scopes => {
                    cur_macro.as_mut().unwrap().body.push(token.clone());
                    if internal_scope_tracker > 0 {
                        continue;
                    }
                    if let TokenVariant::Label { name } = &tokens[i - 1].variant {
                        if !name.ends_with('?') {
                            asm_warn!(
                                &token.info,
                                "Label definitions in non-scoped macros are very dangerous, though it is acceptable if the label being defined is a macro parameter",
                            );
                            asm_hint!("Use '{{' and '}}' instead of '[' and ']'");
                        }
                    }
                }
                // Special case for labels defined in macros, because of macro
                // hygiene
                TokenVariant::LabelArrow { .. } if bounded_by_scopes => {
                    cur_macro.as_mut().unwrap().body.push(token.clone());
                    match &tokens[i - 1].variant {
                        TokenVariant::Label { name } => {
                            cur_macro
                                .as_mut()
                                .unwrap()
                                .labels_defined_in_macro
                                .push(name.clone());
                        }
                        _ => {
                            asm_error!(&tokens[i - 1].info, "Only labels may precede a label arrow")
                        }
                    }
                }
                TokenVariant::Scope => {
                    cur_macro.as_mut().unwrap().body.push(token.clone());
                    internal_scope_tracker += 1;
                }

                TokenVariant::MacroDeclaration { .. } => {
                    asm_error!(
                        &token.info,
                        "Macros may not be defined inside of other macros"
                    );
                }
                TokenVariant::MacroCall { name } => {
                    if *name == cur_macro.as_mut().unwrap().name {
                        asm_error!(&token.info, "Macros may not contain a call to themselves");
                    }
                    cur_macro.as_mut().unwrap().body.push(token.clone());
                }

                TokenVariant::MacroBodyEnd if !bounded_by_scopes => {
                    let mac = cur_macro.as_mut().unwrap();

                    // For macros not bound by scope we remove the newlines around [ and ]
                    if !mac.body.is_empty() {
                        if let TokenVariant::Linebreak = mac.body[0].variant {
                            mac.body.remove(0);
                        }
                    }
                    if !mac.body.is_empty() {
                        if let TokenVariant::Linebreak = mac.body[mac.body.len() - 1].variant {
                            mac.body.remove(mac.body.len() - 1);
                        }
                    }

                    macros.insert(mac.name.clone(), cur_macro.unwrap());
                    cur_macro = None;
                    mode = Mode::Normal;
                }

                TokenVariant::Unscope => {
                    let mac = cur_macro.as_mut().unwrap();
                    mac.body.push(token.clone());
                    internal_scope_tracker -= 1;
                    if !bounded_by_scopes {
                        continue;
                    }
                    if internal_scope_tracker != 0 {
                        continue;
                    }

                    macros.insert(mac.name.clone(), cur_macro.unwrap());
                    cur_macro = None;
                    mode = Mode::Normal;
                }
                _ => {
                    cur_macro.as_mut().unwrap().body.push(token.clone());
                }
            },
        }
    }
    (new_tokens, macros)
}

fn generate_macro_body(
    current_macro: &Macro,
    macros: &HashMap<String, Macro>,
    param_to_arg_map: &HashMap<String, TokenOrTokenVec>,
    context: Vec<Info>,
) -> Vec<Token> {
    let mut body: Vec<Token> = Vec::new();

    for base_body_token in &current_macro.body {
        match &base_body_token.variant {
            TokenVariant::Label { name } => {
                let name = if current_macro.labels_defined_in_macro.contains(name) {
                    format!("?{}?{}", current_macro.name, name) // MACRO HYGIENE HACK
                } else {
                    name.clone()
                };

                let new_token = param_to_arg_map.get(&name);
                match new_token {
                    Some(t) => match t {
                        TokenOrTokenVec::Tok(x) => {
                            let mut copy = x.clone();
                            copy.origin_info = context.clone();
                            copy.origin_info.push(base_body_token.info.clone());

                            body.push(copy);
                        }
                        TokenOrTokenVec::TokVec(v) => {
                            for i in v {
                                let mut copy = i.clone();
                                copy.origin_info = context.clone();
                                copy.origin_info.push(base_body_token.info.clone());

                                body.push(copy);
                            }
                        }
                    },
                    None => {
                        let mut origin_info = context.clone();
                        origin_info.push(base_body_token.info.clone());

                        body.push(Token {
                            info: base_body_token.info.clone(),
                            variant: TokenVariant::Label { name },
                            origin_info,
                        });
                    }
                }
            }
            _ => {
                let mut c = base_body_token.clone();
                c.origin_info = context.clone();
                c.origin_info.push(base_body_token.info.clone());

                body.push(c);
            }
        }
    }

    insert_macros(body, macros, context)
}

#[derive(Debug)]
enum TokenOrTokenVec {
    Tok(Token),
    TokVec(Vec<Token>),
}

fn macro_argument_type_check(argument_info: &Info, token: &Token, argument_name: &str) {
    fn wrong_type(tok: &Token, arg_info: &Info, expected: &str) {
        asm_error_no_terminate!(&tok.info, "Expected a '{}' as argument ", expected);
        asm_hint!("See the documentation for information on the typing system");
        asm_details!(arg_info, "Macro definition");
        terminate!();
    }

    if args::exist() && args::get().disable_type_checking {
        return;
    }
    let lower = argument_name.to_ascii_lowercase();
    if lower.len() > 1 {
        match &lower[..2] {
            symbols::SCOPE_TYPE_PREFIX => {
                if !matches!(token.variant, TokenVariant::Scope) {
                    wrong_type(token, argument_info, "scope");
                }
                return;
            }
            symbols::BRACED_TYPE_PREFIX => {
                if !matches!(token.variant, TokenVariant::BraceOpen) {
                    wrong_type(token, argument_info, "braced");
                }
                return;
            }
            symbols::MACRO_TYPE_PREFIX => {
                if !matches!(token.variant, TokenVariant::BraceOpen) {
                    wrong_type(token, argument_info, "macro call");
                }
                return;
            }
            symbols::LITERAL_TYPE_PREFIX => {
                if !matches!(
                    token.variant,
                    TokenVariant::DecLiteral { .. } | TokenVariant::StrLiteral { .. }
                ) {
                    wrong_type(token, argument_info, "literal");
                }
                return;
            }
            symbols::ANY_TYPE_PREFIX => {
                return;
            }
            _ => {}
        }
    }
    if !matches!(
        token.variant,
        TokenVariant::Label { .. } | TokenVariant::MacroCall { .. } | TokenVariant::Relative { .. }
    ) {
        wrong_type(token, argument_info, "label");
    }
}

/// Recursively (combined with generate_macro_body) expand all macro calls
pub fn insert_macros(
    tokens: Vec<Token>,
    macros: &HashMap<String, Macro>,
    context: Vec<Info>,
) -> Vec<Token> {
    #[derive(Debug, PartialEq)]
    enum CompoundArgType {
        Braced,
        Scoped,
    }
    #[derive(Debug, PartialEq)]
    enum Mode {
        Normal,
        Args,
        CompoundArg(CompoundArgType),
    }

    let mut new_tokens: Vec<Token> = Vec::with_capacity(tokens.len());
    let mut tokens = IterVec::new(&tokens);

    let mut scope_tracker = 0;

    let mut mode = Mode::Normal;
    let mut current_macro: Option<&Macro> = None;
    let mut param_to_arg_map: HashMap<String, TokenOrTokenVec> = HashMap::new();
    let mut caller_info: Option<Info> = None;
    let mut cur_param_name: String = String::new();

    while !tokens.finished() {
        let token = tokens.current();
        match &mode {
            Mode::Normal => match &token.variant {
                TokenVariant::MacroCall { name } => {
                    let mac = macros.get(name);
                    match mac {
                        None => {
                            asm_error_no_terminate!(
                                &token.info,
                                "No declaration found for the macro '{name}'"
                            );
                            if name.starts_with("ASM::") {
                                asm_hint!(
                                    "This is an assembler macro. Please include the ASM module"
                                );
                                asm_hint!("Add '#ASM' or '#sublib' somewhere in your code");
                            }
                            terminate!();
                        }
                        Some(x) => {
                            current_macro = Some(x);
                            caller_info = Some(token.info.clone());
                            mode = Mode::Args;
                            if args::exist() {
                                if name == "ASM::Breakpoint" && args::get().pedantic {
                                    asm_info!(
                                        &token.info,
                                        "Breakpoints are non-canonical and specific to this assembler"
                                    );
                                }
                                if name == "ASM::Debug" && args::get().pedantic {
                                    asm_info!(
                                        &token.info,
                                        "Debug prints are non-canonical and specific to this assembler"
                                    );
                                }
                            }
                        }
                    }
                }
                _ => {
                    new_tokens.push(token.clone());
                }
            },
            Mode::Args => {
                let current_macro_safe = current_macro.unwrap();
                // It has read all arguments
                if param_to_arg_map.len() >= current_macro_safe.params.len() {
                    let mut c = context.clone();
                    c.push(caller_info.unwrap());
                    let mut body =
                        generate_macro_body(current_macro_safe, macros, &param_to_arg_map, c);
                    new_tokens.append(&mut body);

                    caller_info = None;
                    mode = Mode::Normal;
                    current_macro = None;
                    param_to_arg_map = HashMap::new();
                    scope_tracker = 0;

                    continue;
                }
                let (parameter_name, parameter_info) =
                    &current_macro_safe.params[param_to_arg_map.len()];

                if let TokenVariant::Linebreak = token.variant {
                    asm_error_no_terminate!(
                        &caller_info.unwrap(),
                        "Expected {} args, found {}",
                        current_macro_safe.params.len(),
                        param_to_arg_map.len(),
                    );
                    asm_hint!("A newline may not separate macro arguments.");
                    asm_hint!(
                        "Scopes containing newlines are allowed. Multiple scopes as arguments must be chained with }} and {{ on the same line"
                    );
                    asm_details!(&token.info, "Expected the argument(s) here");
                    terminate!();
                }
                macro_argument_type_check(parameter_info, token, parameter_name);

                if let TokenVariant::Scope = token.variant {
                    mode = Mode::CompoundArg(CompoundArgType::Scoped);
                    param_to_arg_map
                        .insert(parameter_name.clone(), TokenOrTokenVec::TokVec(Vec::new()));
                    cur_param_name = parameter_name.clone();
                    continue;
                }
                if TokenVariant::Unscope == token.variant {
                    asm_error_no_terminate!(&token.info, "Unexpected token",);
                    asm_hint!(
                        "If you want to pass a macro as an argument, you must surround it with '(' and ')' instead of '{{' and '}}'"
                    );
                    terminate!();
                }

                if let TokenVariant::BraceOpen = token.variant {
                    mode = Mode::CompoundArg(CompoundArgType::Braced);
                    let toks: Vec<Token> = vec![];
                    param_to_arg_map.insert(parameter_name.clone(), TokenOrTokenVec::TokVec(toks));
                    cur_param_name = parameter_name.clone();
                    scope_tracker = 1;
                    tokens.consume();
                    continue;
                }
                param_to_arg_map
                    .insert(parameter_name.clone(), TokenOrTokenVec::Tok(token.clone()));
            }

            Mode::CompoundArg(arg_type) => match token.variant {
                TokenVariant::Scope if *arg_type == CompoundArgType::Scoped => {
                    scope_tracker += 1;

                    if let TokenOrTokenVec::TokVec(compound_arg) =
                        param_to_arg_map.get_mut(&cur_param_name).unwrap()
                    {
                        compound_arg.push(token.clone());
                    }
                }
                TokenVariant::Unscope if *arg_type == CompoundArgType::Scoped => {
                    scope_tracker -= 1;

                    if let TokenOrTokenVec::TokVec(compound_arg) =
                        param_to_arg_map.get_mut(&cur_param_name).unwrap()
                    {
                        compound_arg.push(token.clone());
                    }
                    if scope_tracker > 0 {
                        tokens.consume();

                        continue;
                    }
                    cur_param_name.clear();
                    mode = Mode::Args;
                }
                TokenVariant::BraceOpen if *arg_type == CompoundArgType::Braced => {
                    scope_tracker += 1;
                    if let TokenOrTokenVec::TokVec(compound_arg) =
                        param_to_arg_map.get_mut(&cur_param_name).unwrap()
                    {
                        compound_arg.push(token.clone());
                    }
                }
                TokenVariant::BraceClose if *arg_type == CompoundArgType::Braced => {
                    scope_tracker -= 1;
                    if scope_tracker <= 0 {
                        cur_param_name.clear();
                        mode = Mode::Args;
                        tokens.consume();

                        continue;
                    }

                    if let TokenOrTokenVec::TokVec(compound_arg) =
                        param_to_arg_map.get_mut(&cur_param_name).unwrap()
                    {
                        compound_arg.push(token.clone());
                    }
                }
                _ => {
                    if let TokenOrTokenVec::TokVec(compound_arg) =
                        param_to_arg_map.get_mut(&cur_param_name).unwrap()
                    {
                        compound_arg.push(token.clone());
                    }
                }
            },
        }
        tokens.consume();
    }
    // HACK
    if mode == Mode::Args {
        let current_macro_safe = current_macro.unwrap();
        if current_macro_safe.params.len() != param_to_arg_map.len() {
            asm_error!(
                &caller_info.unwrap(),
                "Not enough arguments have been supplied"
            );
        }
        // It has read all arguments
        let mut c = context.clone();
        c.push(caller_info.unwrap());
        let mut body = generate_macro_body(current_macro_safe, macros, &param_to_arg_map, c);
        new_tokens.append(&mut body);
    }

    new_tokens
}