1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use crate::parser;
use crate::parser::{Command, Parse};
use crate::var::Var;
//what the public sees
pub fn inter(size: [usize; 2], code: Parse) -> Var {
    let mut vars = Var::new();
    inter_back(size, code, &mut vars)
}
// the disgusting backend
pub fn inter_back(size: [usize; 2], code: Parse, vars: &mut Var) -> Var {
    let mut _modif = 0;
    //let mut vars = vars;
    let size_1 = size[0];
    let size_2 = size[1];

    for pos_ in size_1..size_2 {
        //print!("pos_{}",pos_);
        let pos_ = pos_ + _modif;
        if pos_ >= size_2 {
            break;
        }
        let cur = code.parsed_data[pos_].clone();
        match cur {
            Command::Prints(a) => match a {
                parser::Var::Var(a) => {
                    println!("{}", vars.get_var(a));
                }
                parser::Var::Sstring(a) => {
                    println!("{}", a);
                }
            },
            Command::Var(a) => {
                //println!("new var{},{}", a[0], a[1]);
                vars.new_var(&a[0], &a[1]);
            }
            // executes the if
            Command::If(x1, e, x2) => {
                let mut skip = true;
                match e.as_str() {
                    "==" => {
                        //println!("{},{},",get_data(x1,vars),get_data(x2,vars));

                        if get_data(x1, vars) == get_data(x2, vars) {
                            skip = false;
                        } else {
                        }
                    }
                    "!=" => {
                        //println!("{},{},",get_data(x1,vars),get_data(x2,vars));

                        if get_data(x1, vars) != get_data(x2, vars) {
                            skip = false;
                        } else {
                        }
                    }

                    a => {
                        panic!("{} can not be used in a if statement ", a)
                    }
                }
               // println!("pos{},f {}",pos_,skip);
                if skip {
                    // skip until the stop command if the if is wrong 
                    let cur = code.parsed_data.clone();
                    // get the stop 
                    //let tt = Command::Misc(parser::Misc::IfStop);
                    // find the pos 
                    //let x = cur[pos_..size_2].iter().position(|x| *x == tt);
                    let mut inc = 1; // this must be zero for it to have found the correct stop

                    let mut correct_stop = 0;// the correct if stop
                    let mut post = pos_; // current position of the pointer
                    for x in cur[pos_+1..size_2].iter(){//lets now find that stop if 
                        match x {
                            Command::If (_,_,_)=>{// adds to the inc if there are more ifs in there
                                inc += 1;
                            }
                            Command::Misc(parser::Misc::IfStop) =>{// if there is a if stop it removes a inc
                                inc -= 1;

                                if inc <= 0{
                                    correct_stop = post;
                                    //println!("{},{}",correct_stop,inc);
                                    break;
                                }

                            }
                            _=>{}
                        }
                        post += 1;

                    }
                    let mut _pos = 0;
                    // now its just to jump
                    /*match  {
                        Some(x) => {
                            _pos = x + pos_;
                            //println!("test {}", pos);
                        }
                        None => {
                            panic!("cannot find the stop for the if")
                        }
                    }*/
                    _pos = correct_stop ;
                    _modif = _pos - pos_;
                   // println!("{},{}",_modif,(_modif + pos_));

                    
                    //println!("xxxx{},{},{:#?}", pos, pos_, cur[pos]);
                    // dubble check that its correct
                    
                    if _pos >= size_2{
                        break;
                    }
                   
                }
            }
            //changes variables
            Command::Change(a, b ,c) => {
                use parser::Math;
                if c != Math::Pass{
                    //println!("{:#?} {:#?}",data,c);
                    // this is for plus minus and stuff like that
                    match c{
                        Math::Minus(ch) =>{
                            let f = vars.get_var(a.clone()).parse::<i64>().unwrap() - ch;
                            vars._up_var(a.as_str(), f.to_string().as_str());
                            
                        }
                        Math::Plus(ch) =>{

                            let f = vars.get_var(a.clone()).parse::<i64>().unwrap() + ch;
                            
                            vars._up_var(a.as_str(), f.to_string().as_str());
                            
                        }
                        Math::Times(ch) =>{
                            let f = vars.get_var(a.clone()).parse::<i64>().unwrap() * ch;
                            vars._up_var(a.as_str(), f.to_string().as_str());
                            
                        }
                        Math::Div(ch) =>{
                            let f = vars.get_var(a.clone()).parse::<i64>().unwrap() / ch;
                            vars._up_var(a.as_str(), f.to_string().as_str());
                            
                        }
                        _=>{
                            panic!("why do i need this? oh right i dont")
                        }
                    }

                }else{
                let data = get_data(b.clone(), vars);

                //println!("chan {}", data);
                vars._up_var(a.as_str(), data.as_str());
                }
                
            }
            Command::Delete(a) => {
                vars.del_var(a.as_str());
            }
            Command::Misc(parser::Misc::IfStop) => {}

            _ => {
                panic!("function not found");
            }
        }
    }
    return vars.clone();
}
// get the data from parser::Var
fn get_data(v: parser::Var, vars: &mut Var) -> String {
    match v {
        parser::Var::Var(a) => return vars.get_var(a),
        parser::Var::Sstring(a) => return a,
    }
}
fn get_str(v: parser::Var) -> String {
    match v {
        parser::Var::Var(a) => return a,
        parser::Var::Sstring(a) => return a,
    }
}