Skip to content
Snippets Groups Projects
Select Git revision
  • 0b5afce771cb9e5cc42c4fd4c5e18f020bf1ecad
  • master default
  • dma
  • v0.2.0
  • v0.1.1
  • v0.1.0
6 results

local-token.rs

Blame
  • 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)")));
    }