mice 0.11.1

messing with dice
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
//! This parser is ripped directly out of another language I was working on, called Shiv.
//! I'm considering using that name, "Shiv", for the eventual user-facing scripting language
//! we're building up to here.

use crate::tree::Tree;
use ::core::iter;
use ::core::ops::Range;
use ::id_arena::{Arena, Id};
use ::lasso::{Rodeo, Spur};
use ::logos::{Lexer, Logos};

fn string_literal(lex: &mut Lexer<Token>) -> bool {
    let remainder = lex.remainder().as_bytes();
    let mut cursor = remainder;
    while let [b, rest @ ..] = cursor {
        if *b == b'"' {
            lex.bump((rest.as_ptr() as usize) - (remainder.as_ptr() as usize));
            return true;
        }
        cursor = rest;
    }
    false
}
fn line_comment(lex: &mut Lexer<Token>) -> bool {
    let remainder = lex.remainder().as_bytes();
    let mut cursor = remainder;
    // 10 is a newline. b'\n'
    while let [0..=9 | 11..=255, rest @ ..] = cursor {
        cursor = rest;
    }
    lex.bump(cursor.as_ptr() as usize - remainder.as_ptr() as usize);
    true
}

// reminder that the `Variant` derive is something I've added in my local fork
#[derive(Logos, Debug, ::derive_more::IsVariant, Clone)]
pub enum Token {
    #[token("(")]
    OpenParenthesis,
    #[token(")")]
    CloseParenthesis,
    #[token("\n")]
    NewLine,
    #[token("::")]
    PathSeparator,
    #[token(";", line_comment)]
    LineComment,
    #[token("\"", string_literal)]
    StringLiteral,
    #[regex("[0-9]+")]
    Integer,
    #[token("&")]
    RefOp,
    #[regex(r##"[-_+=a-zA-Z*`.,>][-_+=a-zA-Z*`.,>0-9]*"##)]
    Ident,
    #[regex("[\t ]+")]
    Whitespace,
    #[error]
    Error,
}
pub fn lex_expression(input: &str) -> Result<Vec<(Token, Range<usize>)>, ()> {
    Ok(Token::lexer(input).spanned().collect())
}

#[derive(Debug, ::derive_more::Deref)]
pub struct AST {
    pub names: Rodeo,
    pub strings: Rodeo,
    #[deref]
    tree: Tree<Term>,
}
impl AST {
    fn fmt_rec(ast: &AST, id: Id<Term>, out: &mut String) {
        match &ast.arena[id] {
            Term::CreateRef(operand) => {
                out.push('&');
                Self::fmt_rec(ast, *operand, out);
            }
            Term::List(elements) => {
                out.push('(');
                match &**elements {
                    [first, rest @ ..] => {
                        Self::fmt_rec(ast, *first, out);
                        for elem in rest {
                            out.push(' ');
                            Self::fmt_rec(ast, *elem, out);
                        }
                    }
                    [] => (),
                }
                out.push(')');
            }
            Term::Variable(path) => path.fmt(ast, id, out),
            Term::IntegerLiteral(val) => {
                let mut itoa_buf = itoa::Buffer::new();
                out.push_str(itoa_buf.format(*val));
            }
            Term::StringLiteral(val) => {
                out.push('"');
                out.push_str(ast.strings.resolve(&val.0));
                out.push('"');
            }
            Term::Comment(_span) => todo!("formatting AST glued comments"),
        }
    }
    pub fn fmt_sexpr(&self) -> String {
        let mut output = String::new();
        Self::fmt_rec(self, self.top, &mut output);
        output
    }
    pub fn fmt_term(&self, id: Id<Term>) -> String {
        let mut output = String::new();
        Self::fmt_rec(self, id, &mut output);
        output
    }
}
#[derive(Debug, Clone)]
pub struct Span(Range<usize>);
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct Name(Spur);
impl Name {
    fn new(interner: &mut Rodeo, name: &str) -> Self {
        Self(interner.get_or_intern(name))
    }
    pub fn lookup(interner: &Rodeo, name: &str) -> Option<Self> {
        interner.get(name).map(|x| Self(x))
    }
    pub fn key(&self) -> &Spur {
        &self.0
    }
}

#[derive(Debug, Clone)]
pub struct StringLiteral(Spur);
impl StringLiteral {
    fn new(interner: &mut Rodeo, literal: &str) -> Self {
        Self(interner.get_or_intern(literal))
    }
}

/// Since modules are allowed to take arguments on import, paths must accommodate
/// parameter passing as well.
#[derive(Debug, Clone, PartialEq)]
pub enum PathSegment {
    Ident(Name),
    Value(Id<Term>),
    /// Path segments of this type are only usable by interpreter internals.
    /// Currently used for `#magic#` sub paths, to avoid needing to intern most internal
    /// magic strings.
    Hidden(&'static str),
}
/// A variable path of the form `bootstrap::get-args` or `elf::<archs::x86_64>::make-elf`.
// Note that module constructors must be pure functions, to avoid surprising performance
// overhead in path expressions.
// Invariant: a path must always begin with an ident.
#[derive(Debug, Clone, PartialEq)]
pub struct Path {
    parts: Vec<PathSegment>,
}
impl Path {
    pub fn ident(name: Name) -> Self {
        Self {
            parts: vec![PathSegment::Ident(name)],
        }
    }
    fn new(parts: Vec<Name>) -> Self {
        Self {
            parts: parts
                .into_iter()
                .map(|name| PathSegment::Ident(name))
                .collect(),
        }
    }
    /// Produce a hidden magic path.
    pub fn hidden_magic(names: &mut Rodeo, parts: Vec<&'static str>) -> Self {
        Self::from_parts(
            iter::once(PathSegment::Ident(Name::new(names, "#magic#")))
                .chain(parts.into_iter().map(|name| PathSegment::Hidden(name)))
                .collect(),
        )
    }
    fn from_parts(parts: Vec<PathSegment>) -> Self {
        Self { parts }
    }
    fn fmt(&self, ast: &AST, id: Id<Term>, out: &mut String) {
        if self
            .parts
            .iter()
            .all(|a| matches!(a, PathSegment::Ident(_) | PathSegment::Hidden(_)))
        {
            match &*self.parts {
                [PathSegment::Ident(name), rest @ ..] => {
                    out.push_str(ast.names.resolve(&name.0));
                    for name in rest {
                        match name {
                            PathSegment::Ident(name) => {
                                out.push_str("::");
                                out.push_str(ast.names.resolve(&name.0));
                            }
                            PathSegment::Hidden(name) => {
                                out.push_str("::");
                                out.push_str(name);
                            }
                            _ => todo!("full path expression formatting"),
                        }
                    }
                }
                _ => todo!("full path expression formatting"),
            }
        } else {
            todo!("full path expression formatting")
        }
    }
    pub fn into_ident(self) -> Option<Name> {
        // Can't move out of a slice pattern, eh?
        let mut iter = self.parts.into_iter();
        let res = iter.next().and_then(|segment| match segment {
            PathSegment::Ident(ident) => Some(ident),
            _ => None,
        });
        for _ in iter {
            return None;
        }
        res
    }
}

#[derive(Debug, ::derive_more::Unwrap, Clone)]
pub enum Term {
    CreateRef(Id<Term>),
    List(Vec<Id<Term>>),
    Variable(Path),
    IntegerLiteral(u64),
    StringLiteral(StringLiteral),
    Comment(Span),
}
use crate::tree::TreeIndex;
impl crate::tree::IndexNode for Term {
    fn index(&self, index: TreeIndex) -> Option<Id<Self>> {
        match self {
            Term::CreateRef(operand) if index.val() == 0 => Some(*operand),
            Term::CreateRef(_) => None,
            Term::List(elements) => elements.get(index.val() as usize).copied(),
            Term::Variable(_name) => None,
            Term::IntegerLiteral(_val) => None,
            Term::StringLiteral(_val) => None,
            Term::Comment(_span) => None,
        }
    }
}
#[derive(Debug)]
pub enum ParseError {
    IntegerTooLarge,
    // TODO: include spans on parse errors
    ExpectedIdent,
    UnexpectedEof,
    Eof,
}
fn parse_list(
    names: &mut Rodeo,
    strings: &mut Rodeo,
    terms: &mut Arena<Term>,
    lexer: &mut Lexer<Token>,
) -> Result<Id<Term>, ParseError> {
    let mut expressions = Vec::new();
    loop {
        let mut peeker = lexer.clone();
        if peeker
            .next()
            .ok_or(ParseError::UnexpectedEof)?
            .is_close_parenthesis()
        {
            // Consume closing parenthesis.
            lexer.next();
            break;
        } else {
            expressions.push(parse_expression(names, strings, terms, lexer).map_err(
                |e| match e {
                    // Unclosed parenthesis.
                    // If we left off this conversion, you could leave off trailing parentheses
                    // at the end of a file. (Who knows, maybe that could be considered a feature?)
                    ParseError::Eof => ParseError::UnexpectedEof,
                    e => e,
                },
            )?);
        }
    }
    Ok(terms.alloc(Term::List(expressions)))
}
fn parse_path(
    names: &mut Rodeo,
    _strings: &mut Rodeo,
    terms: &mut Arena<Term>,
    lexer: &mut Lexer<Token>,
    front: &str,
) -> Result<Id<Term>, ParseError> {
    let mut parts = vec![PathSegment::Ident(Name::new(names, front))];
    #[derive(Copy, Clone, Debug)]
    enum State {
        ExpectingSeparator,
        ExpectingSegment,
    }
    let mut state = State::ExpectingSeparator;
    loop {
        let mut peeker = lexer.clone();
        match (state, peeker.next()) {
            (State::ExpectingSegment, Some(Token::Ident)) => {
                parts.push(PathSegment::Ident(Name::new(names, {
                    lexer.next();
                    lexer.slice()
                })));
                state = State::ExpectingSeparator;
            }
            (State::ExpectingSegment, None) => return Err(ParseError::UnexpectedEof),
            (State::ExpectingSeparator, Some(Token::PathSeparator)) => {
                lexer.next();
                state = State::ExpectingSegment;
            }
            (State::ExpectingSeparator, _) => break,
            x => todo!("unexpected tokens in path parser: {:?}", x),
        }
    }
    Ok(terms.alloc(Term::Variable(Path { parts })))
}
fn parse_expression(
    names: &mut Rodeo,
    strings: &mut Rodeo,
    terms: &mut Arena<Term>,
    lexer: &mut Lexer<Token>,
) -> Result<Id<Term>, ParseError> {
    // Note that we perform stack growth here because most (or all?) recursion during parsing
    // will pass through this function, as this is an expression based language.
    // We talk in terms of 2048 because it is the nearest power of 2 to 1888,
    // which I have approximated to be the current width (in bytes) of the stack frames
    // between here and the farthest recursion.
    ::stacker::maybe_grow(16 * 2048, 2048 * 1024, || {
        while let Some((token, span)) = lexer.next().map(|token| (token, lexer.span())) {
            match token {
                Token::OpenParenthesis => return parse_list(names, strings, terms, lexer),
                Token::CloseParenthesis => todo!("no matching open parenthesis"),
                // New lines are ignored in most cases.
                Token::NewLine => (),
                // I haven't decided how I want to use path separators for the moment.
                Token::PathSeparator => todo!("unexpected `::`?"),
                // Comments are always ignored, at least for now.
                Token::LineComment => (),
                Token::StringLiteral => {
                    // Strip off preceding and trailing quotes.
                    let lit = &lexer.slice()[1..];
                    let lit = &lit[..lit.len() - 1];
                    return Ok(terms.alloc(Term::StringLiteral(StringLiteral::new(strings, lit))));
                }
                Token::Integer => {
                    let int = lexer
                        .slice()
                        .as_bytes()
                        .iter()
                        .try_fold(0u64, |a, b| {
                            (a * 10).checked_add((b - b'0') as u64).ok_or(())
                        })
                        .map_err(|()| ParseError::IntegerTooLarge)?;
                    return Ok(terms.alloc(Term::IntegerLiteral(int)));
                }
                Token::RefOp => {
                    let operand = parse_expression(names, strings, terms, lexer)?;
                    return Ok(terms.alloc(Term::CreateRef(operand)));
                }
                // TODO: keep ident spans (really, we should just keep **all the spans**)
                // TODO: consider peeking for a path separator
                Token::Ident => return parse_path(names, strings, terms, lexer, lexer.slice()),
                // Whitespace is ignored in most cases.
                Token::Whitespace => (),
                Token::Error => todo!("hm... {:?}", lexer.slice()),
            }
        }

        Err(ParseError::Eof)
    })
}
pub fn parse_program(input: &str) -> Result<AST, ParseError> {
    let mut names = Rodeo::<Spur>::new();
    let mut strings = Rodeo::<Spur>::new();
    let mut terms = Arena::<Term>::new();
    let mut lexer = Token::lexer(input);
    // todo: parse multiple expressions until we hit EOF,
    // then slap them all inside an implicit progn.

    // Since '#' is not permitted in identifiers by the lexer,
    // we can use it to indicate *super special* identifiers used
    // by the runtime itself.
    // In this case, that's a `#magic#` module that's always in scope, without a preceding
    // `(import-runtime "interpreter" (imports #magic#))` or what have you
    // (which would be impossible to write, as `#magic#` is not a syntactically valid identifier).
    // Uses of the `#magic#` module should vanish upon lowering to any IR a Shiv program
    // is allowed to observe.
    // Since `import-runtime` is indeed a runtime construct,
    // we need a way to talk about runtime internals in
    // magically inserted code without relying on modules of that sort.
    // In this case, that is the implicit toplevel progn.
    // At least for now.
    // The fact that this is necessary at all is only due to our packing *slightly*
    // more information into this tree than the structure and given semantics can strictly accommodate.
    // We do not *really* need to store the top level progn inside the tree itself,
    // but this hack allows us to do so, and doing so should simplify things.
    let mut implicit_progn = vec![terms.alloc(Term::Variable(Path::from_parts(vec![
        PathSegment::Ident(Name::new(&mut names, "#magic#")),
        PathSegment::Hidden("progn"),
    ])))];
    loop {
        match parse_expression(&mut names, &mut strings, &mut terms, &mut lexer) {
            Ok(top) => implicit_progn.push(top),
            Err(ParseError::Eof) => break,
            Err(e) => return Err(e),
        }
    }
    let top = terms.alloc(Term::List(implicit_progn));
    Ok(AST {
        names,
        strings,
        tree: Tree { arena: terms, top },
    })
}