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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
use std::str;
use std::rc::Rc;
use std::ops::Range;

use program::{Program, Command, Integer, BigInteger, SizedInteger, SourceLoc};
use ::WsError;
use ::WsErrorKind::ParseError;

impl Program {
    /// Disassemble a program into a human-readable whitespace assembly.
    pub fn disassemble(&self) -> String {
        use program::Command::*;

        let mut buffer = String::new();
        for (index, command) in self.commands.iter().enumerate() {
            buffer.push_str(match *command {
                Push {..} =>           "    push  ",
                PushBig {..} =>        "    push  ",
                Duplicate =>           "    dup",
                Copy {..} =>           "    copy  ",
                Swap =>                "    swap",
                Discard =>             "    pop",
                Slide {..} =>          "    slide ",
                Add =>                 "    add",
                Subtract =>            "    sub",
                Multiply =>            "    mul",
                Divide =>              "    div",
                Modulo =>              "    mod",
                Set =>                 "    set",
                Get =>                 "    get",
                Label =>               "",
                Call {..} =>           "    call  ",
                Jump {..} =>           "    jmp   ",
                JumpIfZero {..} =>     "    jz    ",
                JumpIfNegative {..} => "    jn    ",
                EndSubroutine =>       "    ret",
                EndProgram =>          "    exit",
                PrintChar =>           "    pchr",
                PrintNum =>            "    pnum",
                InputChar =>           "    ichr",
                InputNum =>            "    inum",
            });
            if let Label = *command {
                buffer.push_str(&if let Some(ref locs) = self.locs {
                    let label = locs[index].label.as_ref().unwrap();
                    format!("{}:", label)
                } else {
                    format!("_{:>04}:", index)
                });
            }
            match *command {
                Push {value: x} => buffer.push_str(&x.to_string()),
                PushBig {value: ref x} => buffer.push_str(&x.to_string()),
                Copy {index: x} => buffer.push_str(&x.to_string()),
                Slide {amount: x} => buffer.push_str(&x.to_string()),
                Call {index: x} |
                Jump {index: x} |
                JumpIfZero {index: x} |
                JumpIfNegative {index: x} => buffer.push_str(
                    &if let Some(ref locs) = self.locs {
                        let label = locs[index].label.as_ref().unwrap();
                        format!("{}", label)
                    } else {
                        format!("_{:>04}", x)
                    }),
                _ => ()
            };
            buffer.push_str("\n");
        }
        buffer
    }

    /// Parse a program written in whitespace assembly into a program.
    pub fn assemble(source: String) -> Result<Program, WsError> {
        // this is a bit more complex parser, we can't parse it in one go, need to tokenize
        let (commands, locs) = {
            let tokens = TokenizerState::tokenize(&source)?;
            let node = parse(&source, &tokens)?;
            compile(node)?
        };
        let mut program = Program {
            source: Some(source.into_bytes()),
            commands: commands,
            locs: Some(locs),
            source_is_whitespace: false
        };
        program.compile()?;
        Ok(program)
    }
}

/*
 * Code below tokenizes the input string
 */

#[derive(Debug, Clone)]
struct Token<'a> {
    data: TokenType<'a>,
    loc:  TextLoc
}

#[derive(Debug, Clone)]
enum TokenType<'a> {
    Name    {value: &'a str},
    Integer {value: SizedInteger},
    Newline,
    Comment,
    Colon,
    Comma,
    End
}

#[derive(Debug, Clone)]
struct TextLoc {
    line:   usize,
    column: usize,
    span:   Range<usize>,
}

#[derive(Clone)]
struct TokenizerState<'a> {
    source: str::CharIndices<'a>,
    line:   usize,
    column: usize,
    index:  usize,
    item:   Option<char>
}

impl<'a> Iterator for TokenizerState<'a> {
    type Item = char;
    fn next(&mut self) -> Option<char> {
        if let Some((index, c)) = self.source.next() {
            match self.item {
                Some('\n') => {self.line   += 1; self.column = 1},
                Some(_)    => {self.column += 1},
                None => ()
            }

            self.index = index;
            self.item = Some(c);
            Some(c)
        } else {
            self.index += 1;
            self.item = None;
            None
        }
    }
}

impl<'a> TokenizerState<'a> {
    fn tokenize<'b>(source: &'b str) -> Result<Vec<Token<'b>>, WsError> {
        let mut tokens = Vec::new();
        let mut state = TokenizerState {
            source: source.char_indices(),
            line: 1,
            column: 1,
            index: 0,
            item: None
        };
        state.next();
        loop {
            let item;
            // consume whitespace
            match state.item {
                Some(' ') | Some('\t') => {state.next(); continue},
                Some(c)                => item = c,
                None                   => break
            }
            // match tokens
            // we can distinguish what token to match just on the starting symbol.
            let start = (state.index, state.line, state.column);
            let data = match item {
                ':'             => {
                    state.next();
                    TokenType::Colon
                },
                ','             => {
                    state.next();
                    TokenType::Comma
                },
                '\n'            => {
                    // skip ahead to the next non-whitespace so we don't emit a million newline tokens
                    loop {
                        match state.next() {
                            Some('\n') |
                            Some(' ')  |
                            Some('\t') => continue,
                            _          => break
                        }
                    }
                    TokenType::Newline
                },
                ';'             => {
                    loop {
                        match state.next() {
                            Some('\n') | None => break,
                            _                 => continue
                        }
                    }
                    TokenType::Comment
                },
                'a'..='z' | 'A'..='Z' | '_' => {
                    loop {
                        match state.next() {
                            Some('a'..='z') |
                            Some('A'..='Z') |
                            Some('_') |
                            Some('0'..='9') => continue,
                            _               => break
                        }
                    }
                    TokenType::Name {value: &source[start.0 .. state.index]}
                },
                '0'..='9' | '-' => {
                    loop {
                        match state.next() {
                            Some('0'..='9') => continue,
                            _               => break
                        }
                    }
                    TokenType::Integer {
                        value: if let Ok(value) = source[start.0 .. state.index].parse::<Integer>() {
                            SizedInteger::Small(value)
                        } else if let Ok(value) = source[start.0 .. state.index].parse::<BigInteger>() {
                            SizedInteger::Big(value)
                        } else {
                            unreachable!()
                        }
                    }
                },
                x => return Err(WsError::new(
                    ParseError(state.line, state.column, state.index),
                    format!("Unrecognized symbol {}", x)
                ))
            };
            tokens.push(Token {
                loc: TextLoc {
                    line: start.1,
                    column: start.2,
                    span: start.0 .. state.index
                },
                data: data
            });
        }
        tokens.push(Token {
            loc: TextLoc {
                line: state.line,
                column: state.column,
                span: state.index .. state.index
            },
            data: TokenType::End
        });
        Ok(tokens)
    }
}

/*
 * Code below constructs an Abstract Syntax Tree from the Token Stream
 */

#[derive(Debug, Clone)]
struct Node<'a> {
    data: NodeType<'a>,
    loc: TextLoc
}

#[derive(Debug, Clone)]
enum NodeType<'a> {
    Root       {nodes: Vec<Node<'a>>},
    Label      {name: &'a str},
    Op         {name: &'a str, args: Vec<Node<'a>>},
    Name       {name: &'a str},
    Integer    {value: SizedInteger},
}

impl<'a> NodeType<'a> {
    fn as_type(&self) -> &'static str {
        match *self {
            NodeType::Name    {..} => "Name",
            NodeType::Label   {..} => "Label",
            NodeType::Integer {..} => "Integer",
            _ => unreachable!()
        }
    }
}

#[derive(Debug)]
enum ParseResult<'a: 'b, 'b> {
    Match(Node<'a>, &'b [Token<'a>]),
    Err(WsError),
    None
}

macro_rules! parse {
    ($f:ident, $t:ident) => (
        match $f($t) {
            ParseResult::Err(s) => return ParseResult::Err(s),
            ParseResult::Match(n, t) => {$t = t; Some(n)},
            ParseResult::None => None
        }
    );
    ($f:ident, $s:ident, $t:ident) => (
        match $f($s, $t) {
            ParseResult::Err(s) => return ParseResult::Err(s),
            ParseResult::Match(n, t) => {$t = t; Some(n)},
            ParseResult::None => None
        }
    );
}

macro_rules! token {
    ($m:pat) => (Some(&Token {data: $m, ..}));
    ($m:pat, $l:ident) => (Some(&Token {data: $m, loc: ref $l}));
}

fn parse<'a, 'b>(source: &str, tokens: &'b [Token<'a>]) -> Result<Node<'a>, WsError> {
    match parse_root(source, tokens) {
        ParseResult::Err(s)         => Err(s),
        ParseResult::Match(node, _) => Ok(node),
        ParseResult::None           => unreachable!()
    }
}

fn parse_root<'a, 'b>(source: &str, mut tail: &'b [Token<'a>]) -> ParseResult<'a, 'b> {
    // matches line (NEWLINE + line) END
    // where line = Label * op ? COMMENT ?
    let mut nodes = Vec::new();
    loop {
        while let Some(n) = parse!(parse_label, tail) {
            nodes.push(n);
        }
        if let Some(n) = parse!(parse_op, source, tail) {
            nodes.push(n);
        }
        if let token!(TokenType::Comment) = tail.get(0) {
            tail = &tail[1 ..];
        }

        let items = tail.len();
        while let token!(TokenType::Newline) = tail.get(0) {
            tail = &tail[1 ..];
        }

        if let token!(TokenType::End, l) = tail.get(0) {
            return ParseResult::Match(
                Node {
                    data: NodeType::Root {nodes: nodes},
                    loc: l.clone()
                },
                &tail[1..]
            );
        }
        // we should have hit end or parsed at least one newline. if we didn't do either our state should be the same.
        if items == tail.len() {
            let loc = &tail[0].loc;
            return ParseResult::Err(WsError::new(
                ParseError(loc.line, loc.column, loc.span.start),
                format!("Expected newline at {}", &source[loc.span.clone()])
            ));
        }
    }
}

fn parse_label<'a, 'b>(tail: &'b [Token<'a>]) -> ParseResult<'a, 'b> {
    // matches NAME COLON

    let (name, loc) = if let token!(TokenType::Name {value}, l) = tail.get(0) {
        (value, l)
    } else {
        return ParseResult::None;
    };

    if let token!(TokenType::Colon) = tail.get(1) {
        ParseResult::Match(
            Node {
                data: NodeType::Label {name: name},
                loc: loc.clone()
            }, 
            &tail[2..]
        )
    } else {
        ParseResult::None
    }
}

fn parse_op<'a, 'b>(source: &str, mut tail: &'b [Token<'a>]) -> ParseResult<'a, 'b> {
    // matches NAME (arg (COMMA arg) *) ?
    // where arg = integer | name
    let (op, loc) = if let token!(TokenType::Name {value}, l) = tail.get(0) {
        tail = &tail[1..];
        (value, l)
    } else {
        return ParseResult::None;
    };

    let mut args = Vec::new();

    let arg = if let Some(n) = parse!(parse_name, tail) {
        n
    } else if let Some(n) = parse!(parse_integer, tail) {
        n
    } else {
        return ParseResult::Match(
            Node {
                data: NodeType::Op {name: op, args: args},
                loc: loc.clone()
            },
            tail
        );
    };
    args.push(arg);

    while let token!(TokenType::Comma) = tail.get(0) {
        tail = &tail[1..];
        args.push(if let Some(n) = parse!(parse_name, tail) {
                n
            } else if let Some(n) = parse!(parse_integer, tail) {
                n
            } else {
                let loc = &tail[0].loc;
                return ParseResult::Err(WsError::new(
                    ParseError(loc.line, loc.column, loc.span.start),
                    format!("Expected argument at {}", &source[loc.span.clone()])
                ));
            }
        );
    }
    ParseResult::Match(
        Node {
            data: NodeType::Op{name: op, args: args},
            loc: loc.clone()
        },
        tail
    )
}

fn parse_name<'a, 'b>(tail: &'b [Token<'a>]) -> ParseResult<'a, 'b> {
    // matches NAME
    if let token!(TokenType::Name {value}, l) = tail.get(0) {
        ParseResult::Match(
            Node {
                data: NodeType::Name {name: value},
                loc: l.clone()
            },
            &tail[1..]
        )
    } else {
        ParseResult::None
    }
}

fn parse_integer<'a, 'b>(tail: &'b [Token<'a>]) -> ParseResult<'a, 'b> {
    // matches INTEGER
    if let token!(TokenType::Integer {ref value}, l) = tail.get(0) {
        ParseResult::Match(
            Node {
                data: NodeType::Integer {value: value.clone()},
                loc: l.clone()
            },
            &tail[1..]
        )
    } else {
        ParseResult::None
    }
}

/*
 * Code below deals with compiling the AST in a program
 */

macro_rules! validate_type {
    ($s:expr, $p:pat => $rv:expr, $a:ident[$i:expr], $loc:expr) => (
        if let $p = $a[$i].data {
            $rv
        } else {
            return Err(WsError::new(
                ParseError($loc.line, $loc.column, $loc.span.start),
                format!("Argument {} type mismatch, expected {}, got {}", $i + 1, $s, $a[$i].data.as_type())
            ));
        }
    );
}

fn validate_args(name: &str, args: &[Node], nargs: usize, loc: &TextLoc) -> Result<(), WsError> {
    if args.len() != nargs {
        Err(WsError::new(
            ParseError(loc.line, loc.column, loc.span.start),
            format!("opcode {} called with {} arguments while expecting {}", name, args.len(), nargs)
        ))
    } else {
        Ok(())
    }
}

fn compile<'a>(root: Node<'a>) -> Result<(Vec<Command>, Vec<SourceLoc>), WsError> {
    let nodes = match root {
        Node {data: NodeType::Root {nodes}, ..} => nodes,
        _ => panic!("Called compile on non-root node")
    };

    let mut commands = Vec::new();
    let mut locs = Vec::new();
    for node in &nodes {
        let mut label = None;
        let command = match node.data {
            NodeType::Label {name} => {
                let name = name.as_bytes();
                label = Some(name.into());
                Command::Label
            },
            NodeType::Op {name, ref args} => match name {
                "push" => {
                    validate_args(name, args, 1, &node.loc)?;
                    match *validate_type!("Integer", NodeType::Integer {ref value} => value, args[0], &node.loc) {
                        SizedInteger::Big(ref value) => Command::PushBig {value: value.clone()},
                        SizedInteger::Small(value) => Command::Push {value: value}
                    }
                },
                "dup"  => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::Duplicate
                },
                "copy" => {
                    validate_args(name, args, 1, &node.loc)?;
                    let value = match *validate_type!("Integer", NodeType::Integer {ref value} => value, args[0], &node.loc) {
                        SizedInteger::Small(value) => value,
                        SizedInteger::Big(ref value) => return Err(WsError::new(
                            ParseError(node.loc.line, node.loc.column, node.loc.span.start),
                            format!("Copy argument too large: {}", value)
                        ))
                    };
                    if value < 0 {
                        return Err(WsError::new(
                            ParseError(node.loc.line, node.loc.column, node.loc.span.start),
                            format!("Negative copy argument: {}", value)
                        ))
                    }
                    Command::Copy {index: value as usize}
                },
                "swap" => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::Swap
                },
                "pop"  => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::Discard
                },
                "slide" => {
                    validate_args(name, args, 1, &node.loc)?;
                    let value = match *validate_type!("Integer", NodeType::Integer {ref value} => value, args[0], &node.loc) {
                        SizedInteger::Small(value) => value,
                        SizedInteger::Big(ref value) => return Err(WsError::new(
                            ParseError(node.loc.line, node.loc.column, node.loc.span.start),
                            format!("Slide argument too large: {}", value)
                        ))
                    };
                    if value < 0 {
                        return Err(WsError::new(
                            ParseError(node.loc.line, node.loc.column, node.loc.span.start),
                            format!("Negative slide argument: {}", value)
                        ))
                    }
                    Command::Slide {amount: value as usize}
                },
                "add"  => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::Add
                },
                "sub"  => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::Subtract
                },
                "mul"  => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::Multiply
                },
                "div"  => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::Divide
                },
                "mod"  => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::Modulo
                },
                "set"  => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::Set
                },
                "get"  => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::Get
                },
                "lbl"  => {
                    validate_args(name, args, 1, &node.loc)?;
                    let name = validate_type!("Name", NodeType::Name {name} => name, args[0], &node.loc).as_bytes();
                    label = Some(name.into());
                    Command::Label
                },
                "call" => {
                    validate_args(name, args, 1, &node.loc)?;
                    let name = validate_type!("Name", NodeType::Name {name} => name, args[0], &node.loc).as_bytes();
                    label = Some(name.into());
                    Command::Call {index: 0}
                },
                "jmp"  => {
                    validate_args(name, args, 1, &node.loc)?;
                    let name = validate_type!("Name", NodeType::Name {name} => name, args[0], &node.loc).as_bytes();
                    label = Some(name.into());
                    Command::Jump {index: 0}
                },
                "jz"   => {
                    validate_args(name, args, 1, &node.loc)?;
                    let name = validate_type!("Name", NodeType::Name {name} => name, args[0], &node.loc).as_bytes();
                    label = Some(name.into());
                    Command::JumpIfZero {index: 0}
                },
                "jn"   => {
                    validate_args(name, args, 1, &node.loc)?;
                    let name = validate_type!("Name", NodeType::Name {name} => name, args[0], &node.loc).as_bytes();
                    label = Some(name.into());
                    Command::JumpIfNegative {index: 0}
                },
                "ret"  => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::EndSubroutine
                },
                "exit" => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::EndProgram
                },
                "pchr" => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::PrintChar
                },
                "pnum" => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::PrintNum
                },
                "ichr" => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::InputChar
                },
                "inum" => {
                    validate_args(name, args, 0, &node.loc)?;
                    Command::InputNum
                },
                op     => return Err(WsError::new(
                    ParseError(node.loc.line, node.loc.column, node.loc.span.start),
                    format!("Unrecognized opcode {}", op)
                ))
            },
            _ => unreachable!()
        };
        commands.push(command);
        locs.push(SourceLoc {
            line: node.loc.line,
            column: node.loc.column,
            span: node.loc.span.clone(),
            label: label.map(Rc::new)
        });
    }
    Ok((commands, locs))
}