about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/interpreter.rs
blob: 7c5a18dd9ac91f16b1bcd21d6cc952c65d486a30 (plain) (blame)
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
use crate::errors::{report, Error};
use crate::parser;
use crate::scanner::{self, Token};

// Run some Lox code and print it to stdout
pub fn run(code: &str) {
    let chars: Vec<char> = code.chars().collect();

    match scanner::scan(&chars) {
        Ok(tokens) => {
            print_tokens(&tokens);
            match parser::parse(tokens) {
                Ok(expr) => println!("Expression:\n{:?}", expr),
                Err(errors) => report_errors(errors),
            }
        }
        Err(errors) => report_errors(errors),
    }
}

fn print_tokens<'a>(tokens: &Vec<Token<'a>>) {
    println!("Tokens:");
    for token in tokens {
        println!("{:?}", token);
    }
}

fn report_errors(errors: Vec<Error>) {
    for error in errors {
        report(&error);
    }
}