use crate::err::SJFLError;
use crate::Value;
use super::{Parse, Session, Variable, match_exact, skip_whitespaces_and_comments};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Assignment {
    pub lhs: Variable,
    pub rhs: Value
    // TODO: add_assign, mul_assign, sub_assign, div_assign
}

impl Parse for Assignment {

    fn parse(s: &[u8], mut ind: usize, session: &mut Session) -> Result<(Self, usize), SJFLError> {
        let (lhs, new_ind) = Variable::parse(s, ind, session)?;
        ind = skip_whitespaces_and_comments(s, new_ind)?;
        ind = match_exact(s, ind, b"=")?;
        ind = skip_whitespaces_and_comments(s, ind)?;
        let (rhs, end_ind) = Value::parse(s, ind, session)?;
        ind = skip_whitespaces_and_comments(s, end_ind)?;
        ind = match_exact(s, ind, b";")?;

        Ok((
            Assignment {
                lhs,
                rhs,
            },
            ind
        ))
    }

}