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
use num_bigint::Sign;

use std::i64;
use std::rc::Rc;

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

#[derive(Debug, Clone)]
struct ParseState<'a> {
    line: usize,
    column: usize,
    index: usize,
    item: Option<u8>,
    buffer: &'a [u8]
}

impl<'a> Iterator for ParseState<'a> {
    type Item = u8;

    fn next(&mut self) -> Option<u8> {
        loop {
            match self.item {
                Some(b'\n') => {self.index += 1; self.line   += 1; self.column = 1;},
                Some(_)     => {self.index += 1; self.column += 1;}
                None        => ()
            }

            return match self.buffer.get(self.index) {
                Some(&new) => {
                    self.item = Some(new);
                    match new {
                        b'\n' | b'\t' | b' ' => Some(new),
                        _ => continue
                    }
                } 
                None => None
            }
        }
    }
}

impl<'a> ParseState<'a> {
    fn new(buffer: &'a [u8]) -> ParseState {
        ParseState {line: 1, column: 1, index: 0, item: None, buffer: buffer}
    }

    fn parse_arg(&mut self) -> Result<SizedInteger, WsError> {
        let mut accum = 0u64;
        let negative;

        // this could be rewritten to a while let loop if rust'd support while { } else { }
        negative = match self.next() {
            Some(b'\n') => return Ok(SizedInteger::Small(0)),
            Some(b'\t') => true,
            Some(b' ' ) => false,
            None        => return Err(WsError::new(ParseError(self.line, self.column, self.index), "Hit EOF while expecting argument")),
            _           => unreachable!()
        };

        loop {
            match self.next() {
                Some(byte) => {
                    accum = accum << 1 | match byte {
                        b'\n' => return Ok(SizedInteger::Small(if negative {
                            -(accum as Integer)
                        } else {
                            accum as Integer
                        })),
                        b'\t' => 1,
                        b' '  => 0,
                        _     => unreachable!()
                    };
                    if accum > i64::MAX as u64 {
                        break;
                    }
                }
                None => return Err(WsError::new(ParseError(self.line, self.column, self.index), "Hit EOF while parsing argument"))
            }
        }

        // The only way we get here is if the upper bit of accum is set. This
        // means that exactly two segments can be filled. Due to the way the ws
        // format works we store in big-endian order first. Then the order is
        // reversed, making the smallest digit the frontmost one. An extra 0 is
        // added to the end, and the whole vector is shifted to make up for space
        // left over in the smallest digit.
        let mut segments: Vec<u32> = Vec::new();
        segments.push((accum >> 32) as u32);
        segments.push(accum as u32);

        let mut bit = 31;
        let mut accum = 0u32;
        while let Some(byte) = self.next() {
            accum |= match byte {
                b'\n' => {
                    segments.push(accum);
                    segments.reverse();
                    segments.push(0);
                    bit += 1; // the amount we need to shift everything to align the lowest bit
                    for i in 0..segments.len() - 1 {
                        segments[i] = ((segments[i] as u64 | ((segments[i + 1] as u64)<< 32)) >> bit) as u32;
                    };
                    return Ok(SizedInteger::Big(BigInteger::new(if negative {Sign::Minus} else {Sign::Plus}, segments)));
                },
                b'\t' => 1,
                b' '  => 0,
                _     => unreachable!()
            } << bit;

            if bit == 0 {
                segments.push(accum);
                bit = 32;
                accum = 0;
            }
            bit -= 1;
        }
        Err(WsError::new(ParseError(self.line, self.column, self.index), "Hit EOF while parsing argument"))
    }

    fn parse_label(&mut self) -> Result<Label, WsError> {
        let mut label = Label::new();

        while let Some(byte) = self.next() {
            match byte {
                b'\n' => return Ok(label),
                b'\t' => label.push(true),
                b' '  => label.push(false),
                _ => unreachable!()
            };
        }
        Err(WsError::new(ParseError(self.line, self.column, self.index), "Hit EOF while parsing label"))
    }
}

impl Program {
    /// Parse a program written in whitespace to a format suitable
    /// for execution.
    pub fn parse(code: Vec<u8>) -> Result<Program, WsError> {

        let mut commands = Vec::<Command>::new();
        let mut sourcelocs = Vec::<SourceLoc>::new();
        {
            let mut state = ParseState::new(&code);

            let mut hash = 0;
            let mut hash_length = 0;

            let mut startline = 0usize;
            let mut startcolumn = 0usize;
            let mut startindex = 0usize;

            while let Some(byte) = state.next() {
                if hash_length == 0 {
                    startline   = state.line;
                    startcolumn = state.column;
                    startindex  = state.index;
                }

                hash = hash * 3 + match byte {
                    b' '  => 0,
                    b'\t' => 1,
                    b'\n' => 2,
                    _     => unreachable!()
                };

                hash_length += 1;

                let command = match (hash_length, hash) {
                    (2, 0)  => match state.parse_arg()? {
                        SizedInteger::Small(value) => Command::Push {value: value},
                        SizedInteger::Big(value) => Command::PushBig {value: value}
                    },
                    (3, 6)  => Command::Duplicate,
                    (3, 3)  => match state.parse_arg()? {
                        SizedInteger::Small(value) => Command::Copy {index: value as usize},
                        SizedInteger::Big(value) => return Err(WsError::new(ParseError(startline, startcolumn, startindex), format!("Copy argument too large: {}", value)))
                    },
                    (3, 7)  => Command::Swap,
                    (3, 8)  => Command::Discard,
                    (3, 5)  => match state.parse_arg()? {
                        SizedInteger::Small(value) => Command::Slide {amount: value as usize},
                        SizedInteger::Big(value) => return Err(WsError::new(ParseError(startline, startcolumn, startindex), format!("Slide argument too large: {}", value)))
                    },

                    (4, 27) => Command::Add,
                    (4, 28) => Command::Subtract,
                    (4, 29) => Command::Multiply,
                    (4, 30) => Command::Divide,
                    (4, 31) => Command::Modulo,

                    (3, 12) => Command::Set,
                    (3, 13) => Command::Get,

                    (3, 18) => Command::Label,
                    (3, 19) => Command::Call {index: 0},
                    (3, 20) => Command::Jump {index: 0},
                    (3, 21) => Command::JumpIfZero {index: 0},
                    (3, 22) => Command::JumpIfNegative {index: 0},
                    (3, 23) => Command::EndSubroutine,
                    (3, 26) => Command::EndProgram,

                    (4, 45) => Command::PrintChar,
                    (4, 46) => Command::PrintNum,
                    (4, 48) => Command::InputChar,
                    (4, 49) => Command::InputNum,

                    (3, 4) | 
                    (3, 11) | 
                    (3, 14) | 
                    (3, 17) | 
                    (3, 24) | 
                    (3, 25) | 
                    (4, _) => {
                        let mut buf = [0u8; 5];
                        for i in 0 .. hash_length {
                            buf[i] = hash % 3;
                            hash /= 3;
                        }

                        let s = buf[..hash_length].iter().rev().map(|c| if *c == 0 {
                            'S'
                        } else if *c == 1 {
                            'T'
                        } else {
                            'N'
                        }).collect::<String>();

                        return Err(WsError::new(ParseError(startline, startcolumn, startindex), format!("invalid command: {}", s)));
                    },
                    (_, _) => continue
                };

                hash = 0;
                hash_length = 0;

                let label = match command {
                    Command::Label | Command::Call {..} | Command::Jump {..} |
                    Command::JumpIfZero {..} | Command::JumpIfNegative {..} => Some(state.parse_label()?.into()),
                    _ => None
                };

                commands.push(command);
                sourcelocs.push(SourceLoc {
                    line: startline,
                    column: startcolumn,
                    span: startindex .. state.index + 1,
                    label: label
                });
            }

            if hash_length != 0 {
                return Err(WsError::new(ParseError(state.line, state.column, state.index), "Hit EOF while parsing command"));
            }
        }

        let mut program = Program {
            source: Some(code),
            commands: commands,
            locs: Some(sourcelocs),
            source_is_whitespace: true
        };

        program.compile()?;

        Ok(program)
    }

    /// Serialize the internal representation back into a whitespace program.
    pub fn dump(&self) -> Vec<u8> {
        let mut buffer = Vec::<u8>::new();

        use program::Command::*;
        for (index, command) in self.commands.iter().enumerate() {
            let label = self.locs.as_ref().and_then(|l| l[index].label.as_ref());
            let (code, arg): (&[u8], _) = match *command {
                Push {value}           => (b"  ", Some(number_to_ws(value))),
                PushBig {ref value}    => (b"  ", Some(large_number_to_ws(value))),
                Duplicate              => (b" \n ", None),
                Copy {index}           => (b" \t ", Some(number_to_ws(index as Integer))),
                Swap                   => (b" \n\t", None),
                Discard                => (b" \n\n", None),
                Slide {amount}         => (b" \t\n", Some(number_to_ws(amount as Integer))),
                Add                    => (b"\t   ", None),
                Subtract               => (b"\t  \t", None),
                Multiply               => (b"\t  \n", None),
                Divide                 => (b"\t \t ", None),
                Modulo                 => (b"\t \t\t", None),
                Set                    => (b"\t\t ", None),
                Get                    => (b"\t\t\t", None),
                Label                  => (b"\n  ", Some(label_to_ws(label, index))),
                Call {index}           => (b"\n \t", Some(label_to_ws(label, index - 1))),
                Jump {index}           => (b"\n \n", Some(label_to_ws(label, index - 1))),
                JumpIfZero {index}     => (b"\n\t ", Some(label_to_ws(label, index - 1))),
                JumpIfNegative {index} => (b"\n\t\t", Some(label_to_ws(label, index - 1))),
                EndSubroutine          => (b"\n\t\n", None),
                EndProgram             => (b"\n\n\n", None),
                PrintChar              => (b"\t\n  ", None),
                PrintNum               => (b"\t\n \t", None),
                InputChar              => (b"\t\n\t ", None),
                InputNum               => (b"\t\n\t\t", None),
            };
            buffer.extend(code);
            if let Some(arg) = arg {
                buffer.extend(arg);
            }
        }
        buffer
    }
}

use std::mem::size_of;

fn number_to_ws(mut n: Integer) -> Vec<u8> {
    let mut res = Vec::new();
    if n < 0 {
        n = -n;
        res.push(b'\t');
    } else if n > 0{
        res.push(b' ');
    }

    let n = n as usize;
    let mut i = size_of::<usize>() * 8;
    let mut force = false;
    while i != 0 {
        i -= 1;
        if (n & (1 << i)) != 0 {
            force = true;
            res.push(b'\t');
        } else if force {
            res.push(b' ');
        }
    }
    res.push(b'\n');
    res
}

fn large_number_to_ws(n: &BigInteger) -> Vec<u8> {
    let mut res = Vec::new();
    let (sign, bytes) = n.to_bytes_be();

    match sign {
        Sign::Minus  => res.push(b'\t'),
        Sign::Plus   => res.push(b' '),
        Sign::NoSign => {
            res.push(b'\n');
            return res;
        }
    }

    let mut force = false;
    for byte in bytes {
        for n in 0..8 {
            if byte & (1 << (7 - n)) != 0 {
                force = true;
                res.push(b'\t');
            } else if force {
                res.push(b' ');
            }
        }
    }

    res.push(b'\n');
    return res;
}

fn label_to_ws(label: Option<&Rc<Label>>, i: usize) -> Vec<u8> {
    let label_storage: Label;
    let label = if let Some(l) = label {
        l
    } else {
        label_storage =  i.to_string().as_bytes().into();
        &label_storage
    };
    let mut res: Vec<u8> = (&label).into_iter()
                                   .map(|i| if i {b'\t'} else {b' '})
                                   .collect();
    res.push(b'\n');
    res
}