pub fn execute(variables: &mut HashMap<String, Numeric>, tokens: &Vec<Token>) -> bool {
    // get first token
    let first = tokens.get(0).unwrap();
    // get second token
    let operator = if tokens.len() > 1 {
        tokens.get(1)
    } else {
        None
    };
    // if first token in function
    if first.id == TokenType::IsFunction {
        match first.param.as_ref() {
            "print" => {
                let mut calculate_it = false;
                for item in tokens.iter().skip(1) {
                    if item.id == TokenType::IsOperation {
                        calculate_it = true;
                        break;
                    }
                }
                if calculate_it {
                    // convert infix to reverse polish
                    let rpn_statement = in2rpn(variables, tokens);
                    // calucate reverse polish
                    let value = rpn2value(&rpn_statement);
                    println!("{} ", value);
                } else {
                    // iterate by all tokens (except first)
                    for item in tokens.iter().skip(1) {
                        match item.id {
                            // find variable in hashmap and print
                            TokenType::IsVariable => {
                                match variables.get(&item.param) {
                                    Some(var) => println!("{} ", var),
                                    None => {
                                        println!("> error: variable '{}' is not defined!",
                                                 item.param);
                                        continue;
                                    }
                                }
                            }
                            // print numeric value
                            TokenType::IsNumeric => println!("{}", item.param),
                            // say no way!
                            TokenType::IsFunction | TokenType::IsOperation => {
                                println!("type '{}' : {:?}", item.param, item.id)
                            }
                        }
                    }
                }
            }
            "read" => {
                let mut buffer = String::new();
                for variable in tokens.iter().skip(1) {
                    print!("{} = ", variable.param);
                    io::stdout().flush()
                        .ok()
                        .expect( "[error] Can't flush to stdout!" );
                    io::stdin().read_line(&mut buffer)
                        .ok()
                        .expect( "[error] Can't read line from stdin!" );
                    let value: Numeric = match buffer.trim().parse() {
                        Ok(value) => value,
                        Err(why) => {
                            println!("> error: {}", why);
                            return false;
                        }
                    };
                    variables.insert(variable.param.clone(), value);
                    buffer.clear();
                }
            }
            "stack" => {
                println!("> variables stack: {:?}", variables);
            }
            "exit" => return true,
            _ => println!("> error: function '{}' not defined", first.param),
        }
    } else {
        let operator = match operator {
            Some(value) => value,
            None => {
                println!("> error: can't execute code");
                return false;
            }
        };
        // if second token is operation -- caclucate statement
        if operator.id == TokenType::IsOperation {
            match operator.param.as_ref() {
                "=" => {
                    // convert infix to reverse polish
                    let rpn_statement = in2rpn(variables, tokens);
                    // calucate reverse polish
                    let value = rpn2value(&rpn_statement);
                    // add value to hashmap
                    variables.insert(first.param.to_owned(), value);
                }
                _ => println!("> error: operator '{}' is not defined!", operator.param),
            }
        }
    }
    false
}
// check tokens type and calculate it
check tokens type and calculate it
check tokens type and calculate it
pub fn execute(variables: &mut HashMap<String, Numeric>, tokens: &Vec<Token>) -> bool {
    // get first token
    let first = tokens.get(0).unwrap();
    // get second token
    let operator = if tokens.len() > 1 {
        tokens.get(1)
    } else {
        None
    };
    // if first token in function
    if first.id == TokenType::IsFunction {
        match first.param.as_ref() {
            "print" => {
                let mut calculate_it = false;
                for item in tokens.iter().skip(1) {
                    if item.id == TokenType::IsOperation {
                        calculate_it = true;
                        break;
                    }
                }
                if calculate_it {
                    // convert infix to reverse polish
                    let rpn_statement = in2rpn(variables, tokens);
                    // calucate reverse polish
                    let value = rpn2value(&rpn_statement);
                    println!("{} ", value);
                } else {
                    // iterate by all tokens (except first)
                    for item in tokens.iter().skip(1) {
                        match item.id {
                            // find variable in hashmap and print
                            TokenType::IsVariable => {
                                match variables.get(&item.param) {
                                    Some(var) => println!("{} ", var),
                                    None => {
                                        println!("> error: variable '{}' is not defined!",
                                                 item.param);
                                        continue;
                                    }
                                }
                            }
                            // print numeric value
                            TokenType::IsNumeric => println!("{}", item.param),
                            // say no way!
                            TokenType::IsFunction | TokenType::IsOperation => {
                                println!("type '{}' : {:?}", item.param, item.id)
                            }
                        }
                    }
                }
            }
            "read" => {
                let mut buffer = String::new();
                for variable in tokens.iter().skip(1) {
                    print!("{} = ", variable.param);
                    io::stdout().flush()
                        .ok()
                        .expect( "[error] Can't flush to stdout!" );
                    io::stdin().read_line(&mut buffer)
                        .ok()
                        .expect( "[error] Can't read line from stdin!" );
                    let value: Numeric = match buffer.trim().parse() {
                        Ok(value) => value,
                        Err(why) => {
                            println!("> error: {}", why);
                            return false;
                        }
                    };
                    variables.insert(variable.param.clone(), value);
                    buffer.clear();
                }
            }
            "stack" => {
                println!("> variables stack: {:?}", variables);
            }
            "exit" => return true,
            _ => println!("> error: function '{}' not defined", first.param),
        }
    } else {
        let operator = match operator {
            Some(value) => value,
            None => {
                println!("> error: can't execute code");
                return false;
            }
        };
        // if second token is operation -- caclucate statement
        if operator.id == TokenType::IsOperation {
            match operator.param.as_ref() {
                "=" => {
                    // convert infix to reverse polish
                    let rpn_statement = in2rpn(variables, tokens);
                    // calucate reverse polish
                    let value = rpn2value(&rpn_statement);
                    // add value to hashmap
                    variables.insert(first.param.to_owned(), value);
                }
                _ => println!("> error: operator '{}' is not defined!", operator.param),
            }
        }
    }
    false
}
pub fn execute ( variables : & mut HashMap < String , Numeric > , tokens : & Vec < Token > ) -> bool { // get first token let first = tokens . get ( 0 ) . unwrap ( ) ; // get second token let operator = if tokens . len ( ) > 1 { tokens . get ( 1 ) } else { None } ; // if first token in function if first . id == TokenType :: IsFunction { match first . param . as_ref ( ) { " " => { let mut calculate_it = false ; for item in tokens . iter ( ) . skip ( 1 ) { if item . id == TokenType :: IsOperation { calculate_it = true ; break ; } } if calculate_it { // convert infix to reverse polish let rpn_statement = in2rpn ( variables , tokens ) ; // calucate reverse polish let value = rpn2value ( & rpn_statement ) ; println ! ( " " value ) ; } else { // iterate by all tokens (except first) for item in tokens . iter ( ) . skip ( 1 ) { match item . id { // find variable in hashmap and print TokenType :: IsVariable => { match variables . get ( & item . param ) { Some ( var ) => println ! ( " " var ) , None => { println ! ( " " item param ) ; continue ; } } } // print numeric value TokenType :: IsNumeric => println ! ( " " item param ) , // say no way! TokenType :: IsFunction | TokenType :: IsOperation => { println ! ( " " item param item id ) } } } } } " " => { let mut buffer = String :: new ( ) ; for variable in tokens . iter ( ) . skip ( 1 ) { print ! ( " " variable param ) ; io :: stdout ( ) . flush ( ) . ok ( ) . expect ( " " ) ; io :: stdin ( ) . read_line ( & mut buffer ) . ok ( ) . expect ( " " ) ; let value : Numeric = match buffer . trim ( ) . parse ( ) { Ok ( value ) => value , Err ( why ) => { println ! ( " " why ) ; return false ; } } ; variables . insert ( variable . param . clone ( ) , value ) ; buffer . clear ( ) ; } } " " => { println ! ( " " variables ) ; } " " => return true , _ => println ! ( " " first param ) , } } else { let operator = match operator { Some ( value ) => value , None => { println ! ( " " ) ; return false ; } } ; // if second token is operation -- caclucate statement if operator . id == TokenType :: IsOperation { match operator . param . as_ref ( ) { " " => { // convert infix to reverse polish let rpn_statement = in2rpn ( variables , tokens ) ; // calucate reverse polish let value = rpn2value ( & rpn_statement ) ; // add value to hashmap variables . insert ( first . param . to_owned ( ) , value ) ; } _ => println ! ( " " operator param ) , } } } false }
check tokens type and calculate it
check tokens type and calculate it
variables
&mut HashMap<String, Numeric>
tokens
&Vec<Token>
variables
&mut HashMap<String, Numeric>
tokens
&Vec<Token>