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

main3.rs

Blame
  • Forked from Per Lindgren / D7050E
    Source project has a limited visibility.
    main3.rs 8.78 KiB
    use nom::{branch, bytes::complete::tag, character::complete::digit1, error, Err};
    use nom_locate::LocatedSpan;
    
    const INPUT: &str = "-2+3**2*3/5-4";
    //const INPUT: &str = "2+-3";
    //const INPUT: &str = "2+30000000000000000000000";
    //const INPUT: &str = "2";
    //const INPUT: &str = "30000000000000000000000";
    //const INPUT: &str = "3+2a";
    
    const UNARYS: [Funcmap; 1] = [Funcmap {
        keyword: "-",
        prec: 4,
        ass: Ass::Right,
        func: Function::UnSub,
    }];
    
    const INFIXS: [Funcmap; 5] = [
        Funcmap {
            keyword: "**",
            prec: 3,
            ass: Ass::Right,
            func: Function::Pow,
        },
        Funcmap {
            keyword: "*",
            prec: 2,
            ass: Ass::Left,
            func: Function::Mult,
        },
        Funcmap {
            keyword: "/",
            prec: 2,
            ass: Ass::Left,
            func: Function::Div,
        },
        Funcmap {
            keyword: "+",
            prec: 1,
            ass: Ass::Left,
            func: Function::Add,
        },
        Funcmap {
            keyword: "-",
            prec: 1,
            ass: Ass::Left,
            func: Function::Sub,
        },
    ];
    
    struct Expr<'a> {
        span: Span<'a>,
        val: Value<'a>,
    }
    
    enum Value<'a> {
        Int(i32),
        UnFunc(Function, Box<Expr<'a>>),
        Func(Function, Box<Expr<'a>>, Box<Expr<'a>>),
    }
    
    #[derive(Clone, Copy)]
    enum Function {
        UnSub,
        Pow,
        Mult,
        Div,
        Add,
        Sub,
    }