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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//use crate::small;
use crate::lexer;
use crate::lexer::TT;

#[derive(Clone, Debug, PartialEq)]
pub enum Misc {
    Stop,
    IfStop,
    Unknown,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Var {
    Sstring(String),
    Var(String),
}

#[derive(Clone, Debug, PartialEq)]
pub enum Math {
    Eq(i64),
    Pass,
    Plus(i64),
    Minus(i64),
    Div(i64),
    Times(i64),


}


#[derive(Clone, Debug, PartialEq)]
pub enum Command {
    Prints(Var),
    If(Var, String, Var),

    Fn(String, [i64; 2], Vec<String>),
    Loop(String, [i64; 2], Vec<String>, Vec<String>, i64),
    Run(String, [i64; 2], Vec<String>, Vec<String>),

    Misc(Misc),
    Get(String),

    Var([String; 2]),
    Change(String, Var, Math),
    Delete(String),
    Cus(String,Vec<Var>),
}
#[derive(Clone)]
pub struct Parse {
    pub parsed_data: Vec<Command>,
}
impl Parse {
    pub fn new() -> Parse {
        Parse {
            parsed_data: Vec::new(),
        }
    }
    pub fn push(&mut self, ty: Command) {
        self.parsed_data.push(ty);
    }
    pub fn find_fn(&mut self, name: &str) -> Command {
        for x in 0..self.parsed_data.len() {
            match self.parsed_data[x].clone() {
                Command::Fn(a, _b, _c) => {
                    if a == name {
                        return self.parsed_data[x].clone();
                    }
                }
                _ => {}
            }
        }
        panic!("no function")
    }
}
// parses the garbage that comes out of the lexer
pub fn parser(code: lexer::Coder) -> Parse {
    let mut parsed = Parse::new();
    #[allow(unused_variables)]
    let curlin = 0;
    let mut modif = 0;
    // unified panic in the main parser
    fn unif_pan(a: TT){
        panic!("MP command not found {:#?}", a)
    }

    for line in 0..code.lex.clone().len() {
        let line = line + modif;
        if line >= code.lex.clone().len() {
            break;
        }
        match code.lex[line].clone() {
            TT::Char(a) =>{
                // skip line if the first char is /
                if a.as_str() == "/"{
                    let next_rb = code.next(TT::LBracket, line);
                    modif += next_rb -1  - line;
                }else{
                    unif_pan(TT::Char(a));
                }
            }
            TT::LBracket => {
                //let codes = code.lex.clone();
                let code = code.clone();
                // lesson to self never write (self) in a struct its just stupid
                let next_rb = code.next(TT::RBracket, line);
                // calls the sub parser
                let x = sub_parser([line + 1, next_rb], code.clone());
                // push whatever comes out of sub parser to the Parsed struct
                parsed.push(x);
                // find the next line
                modif += next_rb - line;
            }
            TT::WhiteSpace=>{

            }
            a => unif_pan(a),
        }
        //curlin += 1;
    }
    return parsed;
}
// this thing actually does the most
pub fn sub_parser(pos: [usize; 2], code: lexer::Coder) -> Command {
    let curlin = pos[0];
    //for line in pos[0]..pos[1] {
    #[allow(unused_assignments)]
    let mut command_return: Command = Command::Misc(Misc::Unknown);
    match code.lex[pos[0]].clone() {
        TT::LParen => {
            // LParen is for when the new function comes ()
            let code = code.clone();
            // com and args vectors
            // args is what gets the things after com gets parsed into so like [(com)args]
            let mut com: Vec<String> = Vec::new();
            let mut args: Vec<String> = Vec::new();
            //assembles together the com it only accepts letters and spaces
         
            for codes in curlin + 1..code.next(TT::RParen, pos[0]) {
                //
                match code.lex[codes].clone() {
                    TT::Letter(a) => {
                        com.push(a);
                    }
                   
                    TT::WhiteSpace => {
                        com.push(" ".to_string());
                    }
                    TT::Num (a) =>{
                        com.push(a);

                    }
                    _ => panic!("sub parsing error "),
                }
            }

            let com = com.join("");
           
            // then finds the right fit from com. kinda like thoes shitty find love programs
            match com.as_str() {
                "print" => {
                    // this makes the print it first gets and sees if its a variable in the args compartment or a string
                    // it then pushes that to the parsed code
                    let mut is_str = false;
                    let mut ne_q = code.next(TT::RParen, pos[0]) + 1;
                    let mut ne_q2 = code.next(TT::RBracket, pos[0]);
                    for x in code.next(TT::RParen, pos[0]) + 1..code.next(TT::RBracket, pos[0]) {
                        if code.lex[x] == TT::Quotation {
                            is_str = true;
                        }
                    }
                    if is_str {
                        ne_q = code.next(TT::Quotation, pos[0]) + 1;
                        ne_q2 = code.next(TT::Quotation, ne_q);
                    }

                    for codes in ne_q..ne_q2 {
                        //
                        //println!("{},{}",code.next(TT::RParen)+1,pos[1]-1);

                        match code.lex[codes].clone() {
                            TT::Letter(a) => {
                                args.push(a);
                            }
                            TT::Num(a) => {
                                args.push(a);
                            }
                            TT::WhiteSpace => {
                                args.push(" ".to_string());
                            }
                            TT::Char(a) => {
                                args.push(a);
                            }
                            a => panic!("sub parsing error 2: {:#?}", a),
                        }
                    }
                    let args = args.join("");
                    if is_str {
                        command_return = Command::Prints(Var::Sstring(args));
                    } else {
                        command_return = Command::Prints(Var::Var(args));
                    }
                }
                "var" => {
                    // assembles together the variable
                    // ! rewrite later
                    let ne_q = code.next(TT::RParen, pos[0]) + 1;
                    let ne_q2 = code.next(TT::RBracket, pos[0]);
                    let mut val: Vec<String> = Vec::new();
                    let mut push_to_val: bool = false;

                    for codes in ne_q..ne_q2 {
                        //
                        //println!("{},{}",code.next(TT::RParen)+1,pos[1]-1);

                        match code.lex[codes].clone() {
                            TT::Letter(a) => {
                                if push_to_val {
                                    val.push(a);
                                } else {
                                    args.push(a);
                                }
                            }
                            TT::Num(a) => {
                                if push_to_val {
                                    val.push(a);
                                } else {
                                    args.push(a);
                                }
                            }
                            TT::WhiteSpace => {
                                if push_to_val {
                                    val.push(" ".to_string());
                                } else {
                                }
                                //args.push(" ".to_string());
                            }
                            TT::Char(a) => {
                                args.push(a);
                            }
                            TT::Quotation => {
                                push_to_val = true;
                            }
                            a => panic!("sub parsing error 2: {:#?}", a),
                        }
                    }

                    let args = args.join("");
                    let val = val.join("");

                    //args.split("");

                    command_return = Command::Var([args, val]);
                }
                "if" => {
                    // parses together the if statement
                    let ne_q = code.next(TT::RParen, pos[0]) + 1;
                    let ne_q2 = code.next(TT::WhiteSpace, ne_q);
                    let x1 = collect_str([ne_q, ne_q2], code.clone());
                    let x1 = parse_str_var(x1);
                    // above: gets the first argument below: gets the if statement like == or !=
                    let ne_q = ne_q2 + 1;
                    let ne_q2 = code.next(TT::WhiteSpace, ne_q);
                    let _xe = collect_str([ne_q, ne_q2], code.clone());
                    //let _xe = parse_str_var(_xe);
                    // below: gets the last argument
                    let ne_q = ne_q2 + 1;
                    let ne_q2 = code.next(TT::RBracket, ne_q);
                    let x2 = collect_str([ne_q, ne_q2], code.clone());
                    let x2 = parse_str_var(x2);
                    //println!("if {:#?} {:#?} {:#?}",x1,_xe,x2);

                    command_return = Command::If(x1, _xe, x2);
                }
                "if stop" => {
                    // just the stop
                    command_return = Command::Misc(Misc::IfStop);
                }
                "edit" | "cha" => match var_par([pos[0], pos[1]], code) {
                    // finds the edit and if its a variable that wants to be the new value
                    // or just text
                    Command::Change(a, b, c) => {
                        command_return = Command::Change(a, b, c);
                    }
                    _ => {
                        panic!("this should never happen");
                    }
                },
                "drop" | "dump" => {
                    let var = collect_str([code.next(TT::RParen, pos[0]) + 1, pos[1]], code);
                    match parse_str_var(var.clone()) {
                        Var::Sstring(_) => {
                            panic!("To dump a variable it needs to be a variable not a string");
                        }
                        _ => {}
                    }
                    command_return = Command::Delete(var);
                }
                "com" =>{

                }
                a => {
                    //println!();

                    //panic!(format!("command not found {}", a))
                    let cus = collect_str([code.next(TT::RParen, pos[0]) + 1, pos[1]], code);
                    let cus: Vec<String> = cus.split(",<").collect::<Vec<&str>>().into_iter().map(|x|x.to_string()).collect();
                    

                    //println!("{:#?}",custom_arg);
                    /*let mut run = true;
                    let mut di = 1;
                    let code = code.clone();

                    while run{
                        let mut code  = &code.clone().to_owned();
                        let x = code.next(TT::Quotation, pos[0]);
                        cus.push(collect_str([x + di, code.next(TT::Quotation, x+di)], code.clone()));
                        di += 1;
                        if x >= pos[1]{
                            run = false;
                        }
                    }*/

                    let mut custom_arg:  Vec<Var> = Vec::new();
                    for x in cus{
                        custom_arg.push(parse_str_var(x));
                    }
                    
                    command_return = Command::Cus(a.to_string(),custom_arg)

                }
            }
        }
        a => panic!("SP command not found {:#?} line {}", a, curlin),
    }
    //curlin += 1;

    //}

    return command_return;
}

fn parse_str_var(str: String) -> Var {
    //parse string and retruns var that contains Sstring and Var
    let mut str = str;
    let mut am_of = 0;
    let mut _am_of_le = 0;

    for x in str.chars() {
        match x {
            '"' => am_of += 1,
            _ => _am_of_le += 1,
        }
    }
    if am_of == 2 {
        str.retain(|x| x != '"');

        Var::Sstring(str)
    } else {
        Var::Var(str)
    }
}
fn collect_str(pos: [usize; 2], code: lexer::Coder) -> String {
    let mut args: Vec<String> = Vec::new();
    // collect a str from random lexer garbage between pos[0] and pos[1]
    for codes in pos[0]..pos[1] {
        //
        //println!("{},{}",code.next(TT::RParen)+1,pos[1]-1);

        match code.lex[codes].clone() {
            TT::Letter(a) => {
                args.push(a);
            }
            TT::Num(a) => {
                args.push(a);
            }
            TT::WhiteSpace => {
                args.push(" ".to_string());
            }
            TT::Char(a) => {
                args.push(a);
            }
            TT::Quotation => {
                args.push(r#"""#.to_string());
            }
            a => panic!("sub_ parsing error 1: {:#?}", a),
        }
    }
    args.join("")
}

fn var_par(pos: [usize; 2], code: lexer::Coder) -> Command {
    // this does what var up in the sub parser does
    let ne_q = code.next(TT::RParen, pos[0]) + 1;
    let ne_q2 = code.next(TT::RBracket, pos[0]);
    let mut val: Vec<String> = Vec::new();
    let mut args: Vec<String> = Vec::new();

    let mut push_to_val: bool = false;
    let mut math: bool = true;
    let mut math_tp:&str = "_";

    for codes in ne_q..ne_q2 {
        //
        //println!("{},{}",code.next(TT::RParen)+1,pos[1]-1);

        match code.lex[codes].clone() {
            TT::Letter(a) => {
                if push_to_val {
                    val.push(a);
                } else {
                    args.push(a);
                }
            }
            TT::Num(a) => {
                if push_to_val {
                    val.push(a);
                } else {
                    args.push(a);
                }
            }
            TT::WhiteSpace => {
                if push_to_val {
                    val.push(" ".to_string());
                } else {
                    push_to_val = true;
                }
                //args.push(" ".to_string());
            }
            TT::Char(a) => {
                if push_to_val | math{
                    match a.as_str() {
                        "-" =>{
                            math_tp = "-";
                            math = false;
                        }
                        "+" =>{
                            math_tp = "+";
                            math = false;
                        }
                        "/" =>{
                            math_tp = "/";
                            math = false;
                        }
                        "*"|";" =>{
                            math_tp = "*";
                            math = false;
                        }
                        _=> args.push(a)
                    }
                }else{
                    args.push(a);
                }
            }
            TT::Quotation => {
                if push_to_val{
                    math = false;
                    val.push(r#"""#.to_string());
                } else {
                }
            }
            a => panic!("sub parsing error 2: {:#?}", a),
        }
    }
    // it just returns Command::Change instead
    let args = args.join("");
    let vals = val.join("");
    let val = parse_str_var(vals.clone());

    let mut mth = Math::Pass;
    match math_tp{
        "-" =>{
            mth = Math::Minus(vals.parse::<i64>().unwrap())
        }
        "/" =>{
            mth = Math::Div(vals.parse::<i64>().unwrap())
        }
        "+" =>{

            mth = Math::Plus(vals.parse::<i64>().unwrap())
        }
        "*" =>{
            mth = Math::Times(vals.parse::<i64>().unwrap())
        }
        _=>{

        }
    }

    //println!("{:#?},{}", val, vals);

    //args.split("");

    return Command::Change(args, val, mth);
}