kasm 2.0.2

The Kerbal Compiler Collection assembler for kOS
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
use std::num::NonZeroU8;

use crate::errors::Span;
use crate::lexer::Token;

/// PAST stands for Preprocessor Abstract Syntax Tree
///
/// Basically, in KASM the preprocessor is treated as a tiny programming language, and is first
/// parsed, then "generated" which means that it generates the rest of the code that will be used
/// in KASM's subsequent operation.
///

#[derive(Debug, Clone)]
pub enum PASTNode {
    BenignTokens(BenignTokens),
    SLMacroDef(SLMacroDef),
    MacroInvok(MacroInvok),
    MLMacroDef(MLMacroDef),
    SLMacroUndef(SLMacroUndef),
    MLMacroUndef(MLMacroUndef),
    Repeat(Repeat),
    IfStatement(IfStatement),
    Include(Include),
}

impl PASTNode {
    pub fn span_end(&self) -> usize {
        match self {
            PASTNode::BenignTokens(benign_tokens) => benign_tokens.span.end,
            PASTNode::SLMacroDef(sl_macro_def) => sl_macro_def.span.end,
            PASTNode::MacroInvok(macro_invok) => macro_invok.span.end,
            PASTNode::MLMacroDef(ml_macro_def) => ml_macro_def.span.end,
            PASTNode::SLMacroUndef(sl_macro_undef) => sl_macro_undef.span.end,
            PASTNode::MLMacroUndef(ml_macro_undef) => ml_macro_undef.span.end,
            PASTNode::Repeat(repeat) => repeat.span.end,
            PASTNode::IfStatement(if_statement) => if_statement.span.end,
            PASTNode::Include(include) => include.span.end,
        }
    }
}

#[derive(Debug, Copy, Clone)]
pub struct Ident {
    pub span: Span,
    pub hash: u64,
}

impl Ident {
    pub fn new(span: Span, hash: u64) -> Self {
        Self { span, hash }
    }
}

impl PartialEq for Ident {
    fn eq(&self, other: &Self) -> bool {
        self.hash == other.hash
    }
}

#[derive(Debug, Clone)]
pub struct BenignTokens {
    pub span: Span,
    pub tokens: Vec<Token>,
}

impl BenignTokens {
    /// Creates a new BenignTokens struct using the tokens provided
    ///
    /// The vector MUST NOT BE EMPTY. If it is, this function will panic
    ///
    pub fn from_vec(tokens: Vec<Token>) -> Self {
        let mut span = Span::new(0, 0, 0);

        let first_span = tokens.first().unwrap().as_span();
        let last_span = tokens.last().unwrap().as_span();

        span.file = first_span.file;
        span.start = first_span.start;
        span.end = last_span.end;

        Self { span, tokens }
    }
}

/// A PAST Node representing a single line macro definition
///
/// Grammar:
///
/// ```sh,ignore,no_run
/// <SLMacroDef> ::= .define <identifier>
///              |   .define <identifier> <SLMacroDefContents>
///              |   .define <identifier> <SLMacroDefArgs>
///              |   .define <identifier> <SLMacroDefArgs> <SLMacroDefContents>
/// ```
///
#[derive(Debug, Clone)]
pub struct SLMacroDef {
    pub span: Span,
    pub identifier: Ident,
    pub args: Option<SLMacroDefArgs>,
    pub contents: Option<SLMacroDefContents>,
}

impl SLMacroDef {
    pub fn new(
        span: Span,
        identifier: Ident,
        args: Option<SLMacroDefArgs>,
        contents: Option<SLMacroDefContents>,
    ) -> Self {
        SLMacroDef {
            span,
            identifier,
            args,
            contents,
        }
    }
}

/// A PAST Node representing a single line macro definition's arguments
///
/// Grammar:
///
/// ```sh,ignore,no_run
/// <SLMacroDefArgs> ::= ()
///                  |   (<arguments>)
///
/// <arguments> ::= <identifier> | <identifier>, <arguments>
/// ```
///
#[derive(Debug, Clone)]
pub struct SLMacroDefArgs {
    pub span: Span,
    pub args: Vec<Ident>,
}

impl SLMacroDefArgs {
    pub fn new(span: Span, args: Vec<Ident>) -> Self {
        Self { span, args }
    }
}

/// A PAST Node representing a single line macro definition's contents
///
/// This grammar may be incomplete, however it is meant to convey that this can contain anything
/// except any preprocessor directives.
///
/// Grammar:
///
/// ```sh,ignore,no_run
/// <SLMacroDefContents> ::=
///                      |   <identifier> <SLMacroDefContents>
///                      |   <literal> <SLMacroDefContents>
///                      |   <non-definition directive> <SLMacroDefContents>
///                      |   <operator> <SLMacroDefContents>
///                      |   <keyword> <SLMacroDefContents>
/// ```
///
#[derive(Debug, Clone)]
pub struct SLMacroDefContents {
    pub span: Span,
    pub contents: Vec<PASTNode>,
}

impl SLMacroDefContents {
    pub fn new(span: Span, contents: Vec<PASTNode>) -> Self {
        Self { span, contents }
    }
}

#[derive(Debug, Clone)]
pub struct MacroInvok {
    pub span: Span,
    pub identifier: Ident,
    pub args: Option<MacroInvokArgs>,
}

impl MacroInvok {
    pub fn new(span: Span, identifier: Ident, args: Option<MacroInvokArgs>) -> Self {
        Self {
            span,
            identifier,
            args,
        }
    }
}

#[derive(Debug, Clone)]
pub struct MacroInvokArgs {
    pub span: Span,
    pub args: Vec<MacroInvokArg>,
}

impl MacroInvokArgs {
    pub fn new(span: Span, args: Vec<MacroInvokArg>) -> Self {
        Self { span, args }
    }

    pub fn from_vec(args: Vec<MacroInvokArg>) -> Self {
        let mut span = Span::new(0, 0, 0);

        let first_span = args.first().unwrap().span;
        let last_span = args.last().unwrap().span;

        span.start = first_span.start;
        span.file = first_span.file;
        span.end = last_span.end;

        MacroInvokArgs { span, args }
    }
}

#[derive(Debug, Clone)]
pub struct MacroInvokArg {
    pub span: Span,
    pub contents: Vec<PASTNode>,
}

impl MacroInvokArg {
    pub fn new(span: Span, contents: Vec<PASTNode>) -> Self {
        Self { span, contents }
    }
}

#[derive(Debug, Clone)]
pub struct MLMacroDef {
    pub span: Span,
    pub identifier: Ident,
    pub args: Option<MLMacroArgs>,
    pub defaults: Option<MLMacroDefDefaults>,
    pub contents: Vec<PASTNode>,
}

impl MLMacroDef {
    pub fn new(
        span: Span,
        identifier: Ident,
        args: Option<MLMacroArgs>,
        defaults: Option<MLMacroDefDefaults>,
        contents: Vec<PASTNode>,
    ) -> Self {
        Self {
            span,
            identifier,
            args,
            defaults,
            contents,
        }
    }
}

#[derive(Debug, Clone)]
pub struct MLMacroArgs {
    pub span: Span,
    pub required: u8,
    pub maximum: Option<NonZeroU8>,
}

impl MLMacroArgs {
    pub fn new(span: Span, required: u8, maximum: Option<NonZeroU8>) -> Self {
        Self {
            span,
            required,
            maximum,
        }
    }
}

#[derive(Debug, Clone)]
pub struct MLMacroDefDefaults {
    pub span: Span,
    pub values: Vec<BenignTokens>,
}

impl MLMacroDefDefaults {
    pub fn new(span: Span, values: Vec<BenignTokens>) -> Self {
        Self { span, values }
    }

    pub fn from_vec(values: Vec<BenignTokens>) -> Self {
        let mut span = Span::new(0, 0, 0);

        let first_span = values.first().unwrap().span;
        let last_span = values.last().unwrap().span;

        span.start = first_span.start;
        span.file = first_span.file;
        span.end = last_span.end;

        MLMacroDefDefaults { span, values }
    }
}

/// A PAST Node that represents a single line macro undefinition
///
/// Grammar:
///
/// ```sh,ignore,no_run
/// <SLMacroUndef> ::= .undef <ident>
///                |   .undef <ident> <SLMacroUndefArgs>
/// ```
///
#[derive(Debug, Clone)]
pub struct SLMacroUndef {
    pub span: Span,
    pub identifier: Ident,
    pub args: SLMacroUndefArgs,
}

impl SLMacroUndef {
    pub fn new(span: Span, identifier: Ident, args: SLMacroUndefArgs) -> Self {
        Self {
            span,
            identifier,
            args,
        }
    }
}

/// Represents a single line macro's number of arguments
///
/// ```sh,ignore,no_run
/// <SLMacroUndefArgs> ::= <number>
/// ```
///
#[derive(Debug, Clone)]
pub struct SLMacroUndefArgs {
    pub span: Span,
    pub num: u8,
}

impl SLMacroUndefArgs {
    pub fn new(span: Span, num: u8) -> Self {
        Self { span, num }
    }
}

/// A PAST Node that represents a multi line macro undefinition
///
/// Grammar:
///
/// ```sh,ignore,no_run
/// <MLMacroUndef> ::= .unmacro <ident>
///                |   .unmacro <ident> <MLMacroArgs>
/// ```
///
#[derive(Debug, Clone)]
pub struct MLMacroUndef {
    pub span: Span,
    pub identifier: Ident,
    pub args: MLMacroArgs,
}

impl MLMacroUndef {
    pub fn new(span: Span, identifier: Ident, args: MLMacroArgs) -> Self {
        Self {
            span,
            identifier,
            args,
        }
    }
}

/// A PAST node that represents a repeat directive
///
/// Grammar:
///
/// ```sh,ignore,no_run
/// <Repeat> ::= .rep <RepeatNumber>
/// ```
///
#[derive(Debug, Clone)]
pub struct Repeat {
    pub span: Span,
    pub number: RepeatNumber,
    pub contents: Vec<PASTNode>,
}

impl Repeat {
    pub fn new(span: Span, number: RepeatNumber, contents: Vec<PASTNode>) -> Self {
        Self {
            span,
            number,
            contents,
        }
    }
}

/// A PAST node that represents a repeat directive's number of repetitions
///
/// Grammar:
///
/// ```sh,ignore,no_run
/// <RepeatNumber> ::= <BenignTokens> | <MacroInvok>
/// ```
///
#[derive(Debug, Clone)]
pub struct RepeatNumber {
    pub span: Span,
    pub expression: Vec<PASTNode>,
}

impl RepeatNumber {
    pub fn new(span: Span, expression: Vec<PASTNode>) -> Self {
        Self { span, expression }
    }
}

#[derive(Debug, Clone)]
pub struct IfStatement {
    pub span: Span,
    pub clauses: Vec<IfClause>,
}

impl IfStatement {
    pub fn new(span: Span, clauses: Vec<IfClause>) -> Self {
        Self { span, clauses }
    }

    pub fn from_vec(clauses: Vec<IfClause>) -> Self {
        let mut span = Span::new(0, 0, 0);

        let first_span = clauses.first().unwrap().span;
        let last_span = clauses.last().unwrap().span;

        span.start = first_span.start;
        span.file = first_span.file;
        span.end = last_span.end;

        Self { span, clauses }
    }
}

#[derive(Debug, Clone)]
pub struct IfClause {
    pub span: Span,
    pub begin: IfClauseBegin,
    pub condition: IfCondition,
    pub contents: Vec<PASTNode>,
}

impl IfClause {
    pub fn new(
        span: Span,
        begin: IfClauseBegin,
        condition: IfCondition,
        contents: Vec<PASTNode>,
    ) -> Self {
        Self {
            span,
            begin,
            condition,
            contents,
        }
    }
}

/// This represents a single part like .if or .ifn
#[derive(Debug, Clone)]
pub struct IfClauseBegin {
    pub span: Span,
    pub inverse: bool,
}

impl IfClauseBegin {
    pub fn new(span: Span, inverse: bool) -> Self {
        Self { span, inverse }
    }
}

#[derive(Debug, Clone)]
pub enum IfCondition {
    Exp(IfExpCondition),
    Def(IfDefCondition),
    Else,
}

#[derive(Debug, Clone)]
pub struct IfDefCondition {
    pub span: Span,
    pub identifier: Ident,
    pub args: Option<MLMacroArgs>,
}

impl IfDefCondition {
    pub fn new(span: Span, identifier: Ident, args: Option<MLMacroArgs>) -> Self {
        Self {
            span,
            identifier,
            args,
        }
    }
}

#[derive(Debug, Clone)]
pub struct IfExpCondition {
    pub span: Span,
    pub expression: Vec<PASTNode>,
}

impl IfExpCondition {
    pub fn new(span: Span, expression: Vec<PASTNode>) -> Self {
        Self { span, expression }
    }
}

#[derive(Debug, Clone)]
pub struct Include {
    pub span: Span,
    pub path: IncludePath,
}

impl Include {
    pub fn new(span: Span, path: IncludePath) -> Self {
        Self { span, path }
    }
}

#[derive(Debug, Clone)]
pub struct IncludePath {
    pub span: Span,
    pub expression: Vec<PASTNode>,
}

impl IncludePath {
    pub fn new(span: Span, expression: Vec<PASTNode>) -> Self {
        Self { span, expression }
    }
}