Skip to content
Snippets Groups Projects
Select Git revision
  • be36987625884e170263b1f06cb029aff3f28414
  • master default protected
  • home_exam
  • wip
4 results

tmp.rs

Blame
  • Forked from Per Lindgren / D7050E
    Source project has a limited visibility.
    tmp.rs 1.11 KiB
    use crust::{
        ast::Span,
        parse::{parse_assign, parse_expr},
        interpreter::{eval_expr}
    };
    
    fn test(s: &str, v: i32) {
        match parse_expr(Span::new(s)) {
            Ok((Span { fragment: "", .. }, e)) => {
                println!("{:?}", &e);
                println!("eval {} {}", eval_expr(&e), v);
                assert_eq!(eval_expr(&e), v);
            }
            Ok((s, t)) => println!(
                "parse incomplete, \n parsed tokens \t{:?}, \n remaining \t{:?}",
                t, s
            ),
            Err(err) => println!("{:?}", err),
        }
    }
    
    fn main() {
        // test("- -1 + + 1", - -1 + 1);  // rust does not allow + as a unary op (I do ;)
        // test("(-1-1)+(-1+3)", (-1 - 1) + (-1) + 3);
        // // just to check that right associative works (you don't need to implement pow)
        // test("2+3**2**3*5+1", 2 + 3i32.pow(2u32.pow(3)) * 5 + 1);
        // test("(12*2)/3-4", (12 * 2) / 3 - 4);
        // test("1*2+3", 1 * 2 + 3);
        // // just to check that we get a parse error
        // test("1*2+3+3*21-a12+2", 1 * 2 + 3 + 3 * 21 - 12 + 2);
        test("1 + (1 - 2)", 1 + (1 - 2) );
        println!("{:?}", parse_assign(Span::new("3 = a(1, 2+3)")));
    }