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
/*!
A simple macro-based language inspired by Rust and Python.

This crate can be used either directly as an interpreter binary or embedded
in another program.

Parsing is still a little weird but just add parentheses! :^)

# Direct Usage

To run the REPL simply use `cargo run`. To see examples of possible code
constructs look at the scripts in `test/`. You can run scripts by providing
the name as an argument as follows: `cargo run -- scriptname`. You can also
run modules with `cargo run -- modname`. This is the same as running
`cargo run -- modname/main.um`. One such module is `test`.

# Embedded Usage

This crate can be added to your dependencies by adding the following to your
project's `Cargo.toml`:

```
[dependencies]
umbra_lang = "0.21.0"
```

To use the library, call the desired `run*()` functions (documented below). For
example:

```
let env = Env::prelude();
let (vars, val) = run_path("script.um", &env, true);
```
*/

#![allow(incomplete_features)]
#![feature(adt_const_params)]

#![feature(iter_intersperse)]

#![deny(rust_2018_idioms)]

#![warn(clippy::pedantic)]
#![allow(clippy::upper_case_acronyms)]
#![allow(clippy::similar_names)]
#![allow(clippy::needless_pass_by_value)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::type_complexity)]
#![allow(clippy::unnested_or_patterns)]
#![allow(clippy::if_then_panic)]

#[cfg(not(target_env = "msvc"))]
#[cfg(feature = "jemallocator")]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

use std::{fmt, fs, io};
use std::collections::{HashMap, hash_map::DefaultHasher};
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::ffi::OsStr;

#[cfg(target_env = "msvc")]
use std::io::{Write, BufRead};

use lazy_static::lazy_static;
use maplit::hashmap;

mod hashablemap;
use hashablemap::HashableMap;
mod threadhandle;
pub use threadhandle::{ThreadConfig, ThreadHandle};
mod thrio;
mod internaldata;
use internaldata::InternalData;

mod parsing;
use parsing::{ENUM_VALUE, Expr, Exprs, Line, Lines, Tokens, VecMap, unescape, match_pat, parse_number};
pub use parsing::{Pos, RefTVal, TVal, Token, AST, Callable, FnArgs, FnReturn, TokenType, Type, Value};
mod runnable;
pub use runnable::*;

mod ops;
use ops::{Op, OpTrait, Precedence};
mod umcore;
mod umstd;
mod ummod;

/// An error that occured during argument handling, parsing, script execution, or for control flow or other purposes.
#[derive(Debug, Clone)]
pub enum Error {
    /// A provided argument is invalid.
    Argument(String),
    /// The Token stream could not be reduced to a valid AST.
    Parse(String, Option<Pos>),
    /// The script encountered an error during execution.
    Script(String, Option<Pos>),
    /// A control flow macro such as `return` created this error.
    Control(String, Option<Box<TVal>>),

    /// An error from the script that couldn't be parsed as any other error.
    Custom(TVal),
}
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Argument(m) => write!(f, "Argument Error: {}", m),
            Error::Parse(m, pos) => match pos {
                Some(pos) => write!(f, "Parse Error at {}: {}", pos, m),
                None => write!(f, "Parse Error: {}", m),
            },
            Error::Script(m, pos) => match pos {
                Some(pos) => write!(f, "Script Error at {}: {}", pos, m),
                None => write!(f, "Script Error: {}", m),
            },
            Error::Control(m, v) => {
                match v {
                    Some(v) if m == "panic" => {
                        if let Value::Enum(_, e) = &v.val {
                            if let Value::List(l) = e.1[0].clone_out().val {
                                if let Some(pos) = Pos::from_value(&l[0].clone_out().val) {
                                    if let Value::String(s) = l[1].clone_out().val {
                                        return write!(f, "Panic at {}: {}", pos, s);
                                    }
                                }
                            }
                        }
                        write!(f, "Unknown panic: {}", v.val)
                    },
                    _ => write!(f, "Control Error: {}: {:?}", m, v),
                }
            },
            Error::Custom(v) => write!(f, "Other Error: {}", v.val),
        }
    }
}
impl std::error::Error for Error {}

/**
A collection of interpreter flags and script arguments.

Currently no actual handling is done here, just parsing.
*/
pub struct Args {
    /// The flags passed to the interpreter, everything before the first non-flag.
    pub longflags: Vec<String>,
    /// The script arguments, everything after and including the first non-flag.
    pub script: Vec<String>,
}
impl Args {
    /// Parse `std::env::args()` into flags beginning with `--` and script arguments.
    ///
    /// # Errors
    /// Will return `Err` if any arguments are invalid, i.e. only have a single dash.
    pub fn handle() -> Result<Args, Error> {
        let mut args = Args {
            longflags: vec![],
            script: vec![],
        };

        let eargs: Vec<String> = std::env::args().collect();
        let mut i = 1;
        let mut sa = false;
        while i < eargs.len() {
            if sa {
                // Add script args
                args.script.push(eargs[i].clone());
            } else if eargs[i].starts_with('-') {
                if eargs[i].starts_with("--") {
                    // Parse double dash flags
                    args.longflags.push(eargs[i][2..].into());
                } else {
                    // Parse single dash flags
                    return Err(Error::Argument(format!("Unsupported arg: {}", eargs[i])))
                }
            } else {
                // Enable script arg mode when a non-dash arg is met
                sa = true;
                continue;
            }
            i += 1;
        }

        Ok(args)
    }
}

/**
The environment that scripts use to store and access variables.

This contains a reference to a parent `Env` whose variables are accessible but
immutable.
*/
#[derive(Clone, PartialEq, Eq)]
pub struct Env<'a> {
    parent: Option<&'a Env<'a>>,
    vars: HashMap<String, RefTVal>,
}
lazy_static! {
    static ref CORE: Env<'static> = {
        log::debug!("Initializing core...");

        let mut env = Env::from(hashmap!{
            "_".into() => Value::Type(Type::any(), HashableMap::arc()).into(),
        });

        // Import Umbra Core only
        umcore::init(&mut env);

        env
    };
    static ref PRELUDE: Env<'static> = {
        let mut env = Env::child(&CORE);

        log::debug!("Initializing prelude...");

        // Import Umbra STD
        umstd::init(&mut env);

        env
    };
}
impl<'a> Env<'a> {
    /**
    Clones the Core `Env` which contains only the variables necessary to
    bootstrap the language.
    */
    #[must_use]
    pub fn core() -> Env<'a> {
        CORE.clone()
    }
    /**
    Clones the Prelude `Env` which contains the usual variables for writing
    programs.
    */
    #[must_use]
    pub fn prelude() -> Env<'a> {
        PRELUDE.clone()
    }
    /// Constructs an `Env` with no parent and no variables.
    #[must_use]
    pub fn empty() -> Env<'a> {
        Env {
            parent: None,
            vars: hashmap!{},
        }
    }
    /// Constructs an `Env` with the given parent and no variables.
    #[must_use]
    pub fn child(parent: &'a Env<'a>) -> Env<'a> {
        Env {
            parent: Some(parent),
            vars: hashmap!{},
        }
    }

    /**
    Creates a recursive `Iterator` over all variables in the given `Env` and
    its parents. Parent variables will come first.
    */
    #[must_use]
    pub fn iter_flatten<'b>(it: &'b Env<'a>) -> Box<dyn Iterator<Item = (&'b String, &'b RefTVal)> + 'b> {
        if let Some (p) = it.parent {
            return Box::new(Self::iter_flatten(p).chain(it.vars.iter()));
        }
        return Box::new(it.vars.iter());
    }
    /**
    Constructs an `Env` with no parent and all the variables of the given `Env`
    and its parents. Duplicate variables will be overwritten by the one in the
    child.
    */
    #[must_use]
    pub fn flatten(old: &Env<'a>) -> Env<'a> {
        Env::from(
            Self::iter_flatten(old)
                .map(|(k, v)| {
                    (k.clone(), v.clone())
                }).collect::<HashMap<String, RefTVal>>()
        )
    }

    /**
    Returns the value of the variable with the given name if it exists. This
    `Env` will be checked first, followed by the parents recursively.
    */
    #[must_use]
    pub fn get(&self, name: &str) -> Option<&RefTVal> {
        match self.vars.get(name) {
            Some(v) => Some(v),
            None => match &self.parent {
                Some(p) => p.get(name), // Recursively fetch variable values
                None => None,
            },
        }
    }
    /// Returns whether the given name corresponds to a variable.
    #[must_use]
    pub fn has(&self, name: &str) -> bool {
        if self.vars.contains_key(name) {
            true
        } else {
            match &self.parent {
                Some(p) => p.has(name),
                None => false,
            }
        }
    }
    /// Inserts the given value with the given name as a variable.
    pub fn set(&mut self, name: &str, rv: &RefTVal) {
        self.vars.insert(name.into(), RefTVal::clone(rv));
    }
    /// Removes the given name from this `Env`.
    pub fn unset(&mut self, name: &str) {
        self.vars.remove(name);
    }
    /// Adds all of the variables in the given map to this `Env`.
    pub fn update(&mut self, vars: HashMap<String, RefTVal>) {
        self.vars.extend(vars);
    }

    /**
    Computes the changed vars in the new env `nenv` that exist in the outside
    env `self`.
    */
    #[must_use]
    pub fn diff(&self, mut nenv: Env<'a>) -> HashMap<String, RefTVal> {
        // let outer = match self.parent {
        //     Some(p) => p,
        //     None => self,
        // };
        let outer = self;
        let mut vars = hashmap!{};
        for (k, v) in nenv.vars.drain() {
            if let Some(ov) = outer.get(&k) {
                if ov != &v {
                    vars.insert(k, v);
                }
            }
        }
        vars
    }
}
impl<'a> From<HashableMap<String, RefTVal>> for Env<'a> {
    fn from(vars: HashableMap<String, RefTVal>) -> Env<'a> {
        Env {
            parent: None,
            vars: vars.map,
        }
    }
}
impl<'a> From<HashMap<String, RefTVal>> for Env<'a> {
    fn from(vars: HashMap<String, RefTVal>) -> Env<'a> {
        Env {
            parent: None,
            vars,
        }
    }
}
impl<'a> fmt::Debug for Env<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let parent = match self.parent {
            Some(_) => "Some(Env {...})",
            None => "None",
        };
        write!(f, "Env {{ parent: {}, vars: {{{:?}}} }}", parent, self.vars)
    }
}

/**
Splits, tokenizes, parses, and constructs a Runnable from `script`. Generally
not necessary to use directly.

The given environment is used to determine how to parse possible macros.

Debug output can be toggled with `printout` which will print split `Line`s
when greater than 0. When greater than 1 it will also print `Token`s.

# Errors
Will return `Err` if `AST::parse()` resulted in an error for any expressions.
*/
pub fn compile<'a>(scriptname: &str, script: &str, env: &'a Env<'a>, pos: Option<Pos>) -> Result<Runnable<'a>, Error> {
    // Split script string into lines while preserving containers
    let lines: Vec<Line> = Lines::split(scriptname, script, pos)
        // Expression debug print
        .inspect(|l| {
            if !l.data.starts_with("//") {
                log::trace!("{}", l);
            }
        }).collect();

    // Convert the line into a series of expressions
    let exprs: Vec<Expr> = lines.iter().flat_map(Exprs::split).collect();

    // Convert the expr into a series of token streams
    let mut tokens: Vec<Tokens> = exprs.iter()
        .map(Tokens::tokenize)
        // Token debug print moved to AST::parse()
        .collect();

    // Parse the token streams into ASTs
    let asts: Result<Vec<AST>, Error> = tokens.drain(0..).map(|ts| {
            AST::parse(ts, env)
        }).collect();

    let mut shash = DefaultHasher::new();
    env!("CARGO_PKG_VERSION").hash(&mut shash);
    script.hash(&mut shash);

    Ok(Runnable {
        hash: shash.finish(),
        env: Env::child(env),
        ast: AST::Container {
            token: Token {
                ttype: TokenType::Container,
                pos: Pos {
                    filename: scriptname.into(),
                    line: 0,
                    col: 0,
                },
                data: "{".into(),
            },
            children: asts?,
        },
    })
}
/**
Compiles the script then executes it in the given environment.

Debug output can be toggled with printout whose first element will be sent to
`compile` and whose second element will determine whether or not to print the
value of each top-level expression in the script.
*/
pub fn run(scriptname: &str, script: &str, env: &Env<'_>, pos: Option<Pos>, printout: bool) -> FnReturn {
    // Compile the script
    let run = match compile(scriptname, script, env, pos) {
        Ok(r) => r,
        Err(m) => {
            if printout {
                log::info!("{}\n", m);
            }
            return (None, Err(m));
        },
    };

    // Execute the AST
    match run.ast {
        AST::Container { children, .. } => {
            let mut nenv = Env::child(env);
            let vals: Result<Vec<RefTVal>, Error> = children.iter()
                .filter_map(|ast| {
                    if let AST::Container { token, .. } = &ast {
                        // Skip comments
                        if token.data.starts_with("//") {
                            return None;
                        }
                    }

                    let (vars, val) = ast.run(&nenv);
                    if let Some(vars) = vars {
                        nenv.update(vars);
                    }

                    // Expression value debug print
                    if printout {
                        match val {
                            Ok(ref v) => log::info!("==> {}\n", v),
                            Err(ref m) => log::error!("{}\n", m),
                        }
                    }

                    Some(val)
                }).collect();
            match vals {
                Ok(mut vals) => match vals.pop() {
                    Some(v) => (Some(nenv.vars), Ok(v)),
                    None => (None, Ok(Value::none().into())),
                },
                Err(m) => (None, Err(m)),
            }
        },
        _ => (None, Err(Error::Script("expected script container AST".into(), None))),
    }
}
/**
Loads a script from `path` and runs it in the given environment.

If `path` is a directory it will be adjusted by appending `/main.um`. Or if the
file doesn't exist `.um` will be appended.

If `run_main` is true and the script defines a function named `main` then that
function will be run after the script body is done.
*/
pub fn run_path<P: AsRef<Path>>(path: &P, env: &Env<'_>, run_main: bool) -> FnReturn {
    // Correct path for modules
    let path = path.as_ref();
    let path: PathBuf = if path.is_dir() {
        path.join("main.um")
    } else if !path.is_file() && path.extension() != Some(OsStr::new("um")) {
        path.with_extension("um")
    } else {
        PathBuf::from(path)
    };

    // Read script into string
    let script = match fs::read_to_string(&path) {
        Ok(s) => s,
        Err(m) => return (None, Err(Error::Argument(format!("Failed to load {}: {}", path.display(), m)))),
    };
    // Run script
    let (vars, val) = match run(&path.to_string_lossy(), &script, env, None, false) {
        (vars, Ok(v)) => (vars, v),
        (vars, Err(m)) => return (vars, Err(m)),
    };

    // Run main if exists and needed
    if run_main {
        if let Some(vars) = &vars {
            if let Some(main) = vars.get("main") {
                if let Value::Function { body, .. } = main.clone_out().val {
                    let mut nenv = Env::child(env);
                    nenv.update(vars.clone());
                    let (_, val) = body.call(&nenv, FnArgs::Normal {
                        this: Box::new(main.clone()),
                        pos: None,
                        args: TVal {
                            ttype: Type::none(),
                            attr: hashmap!{}.into(),
                            val: Value::none(),
                        }.into(),
                    });
                    return (Some(nenv.vars), val);
                }
            }
        }
    }
    (vars, Ok(val))
}

#[cfg(not(target_env = "msvc"))]
struct EmptyCompleter;
#[cfg(not(target_env = "msvc"))]
impl liner::Completer for EmptyCompleter {
    fn completions(&mut self, _start: &str) -> Vec<String> {
        vec![]
    }
}
#[cfg(not(target_env = "msvc"))]
struct EnvCompleter<'a> (Env<'a>);
#[cfg(not(target_env = "msvc"))]
impl<'a> liner::Completer for EnvCompleter<'a> {
    fn completions(&mut self, start: &str) -> Vec<String> {
        if start.is_empty() {
            Env::flatten(&self.0).vars.keys()
                .cloned()
                .collect()
        } else {
            Env::flatten(&self.0).vars.keys()
                .filter(|k| k.starts_with(start))
                .cloned()
                .collect()
        }
    }
}

/// Init vars specific to the interactive REPL
fn interactive_init(env: &mut Env<'_>) {
    env.set("help", &Value::String(
"\n
Welcome to the Umbra interpreter!

Try pressing TAB to see variables that are available in the current
environment. TAB can also be used to complete initial variable names.

For examples of language usage take a look at the test module, and for
instructions on usage as a library check out https://docs.rs/umbra-lang/

To exit, either press Ctrl-C or run `exit()` without quotes.
\n".into()).into());
}

/**
Start an interactive interpreter session (REPL) in the given environment.

The prompt uses `name` to make it easier to differentiate debug points.

# Panics
Will panic if IO fails to flush or lock.
*/
pub fn run_interactive(name: &str, env: &Env<'_>) {
    #[cfg(target_env = "msvc")]
    return run_interactive_basic(name, env);

    #[cfg(not(target_env = "msvc"))]
    {
        let mut nenv = Env::child(env);
        interactive_init(&mut nenv);
        let mut pos = Pos::start(&format!("<stdin/{}>", name));
        let mut ctx = liner::Context::new();
        loop {
            // Read a line from input
            match ctx.read_line(&format!("{}:{}> ", name, pos.line), None, &mut EnvCompleter(nenv.clone())) {
                Ok(line) => {
                    if line.trim().is_empty() {
                        eprintln!();
                        continue;
                    }
                    if let Err(m) = ctx.history.push(line.clone().into()) {
                        log::error!("Failed to write line to history: {}", m);
                        break;
                    };

                    // Run the line
                    let (vars, _) = run(&format!("<stdin/{}>", name), &line, &nenv, Some(pos.clone()), true);
                    if let Some(vars) = vars {
                        nenv.update(vars);
                    }
                    pos.line += 1;
                },
                Err(e @ io::Error { .. }) if e.kind() == io::ErrorKind::Interrupted => {
                    eprintln!("^C");
                    return;
                },
                Err(e @ io::Error { .. }) if e.kind() == io::ErrorKind::UnexpectedEof => {
                    continue;
                },
                Err(m) => {
                    log::error!("Error: {:?}", m);
                    continue;
                },
            }
        }
    }
}
#[cfg(target_env = "msvc")]
fn run_interactive_basic(name: &str, env: &Env<'_>) {
    let mut nenv = Env::child(env);
    interactive_init(&mut nenv);
    let mut pos = Pos::start(&format!("<stdin/{}", name));
    let mut line = String::new();
    loop {
        // Read a line from input
        line.clear();
        eprint!("{}:{}> ", name, pos.line);
        io::stdout().flush().unwrap();
        io::stdin().lock().read_line(&mut line).unwrap();
        if line.trim().is_empty() {
            eprintln!();
            continue;
        }

        // Run the line
        let (vars, _) = run(&format!("<stdin/{}", name), &line, &nenv, Some(pos.clone()), true);
        if let Some(vars) = vars {
            nenv.update(vars);
        }
        pos.line += 1;
    }
}