pub fn opcode_open(&mut self, chunk: &Chunk, i: usize) -> i32 {
        if self.stack.len() < 2 {
            print_error(chunk, i, "open requires two arguments");
            return 0;
        }

        let rw_rr = self.stack.pop().unwrap();
        let rw_rrb = rw_rr.borrow();
        let path_rr = self.stack.pop().unwrap();
        let path_rrb = path_rr.borrow();
        let path_str_pre = path_rrb.to_string();
        let path_str_opt = to_string_2(&path_str_pre);

        match &*rw_rrb {
            Value::String(rw_str, _) => match rw_str.as_ref() {
                "r" => match path_str_opt {
                    Some(s) => {
                        let file_res = File::open(s);
                        match file_res {
                            Ok(file) => {
                                self.stack.push(Rc::new(RefCell::new(
                                    Value::FileReader(BufReader::new(file)),
                                )));
                            }
                            Err(e) => {
                                let err_str = format!(
                                    "unable to open file: {}",
                                    e.to_string()
                                );
                                print_error(chunk, i, &err_str);
                                return 0;
                            }
                        }
                    }
                    _ => {
                        print_error(chunk, i, "path for open must be a string");
                        return 0;
                    }
                },
                "w" => match path_str_opt {
                    Some(s) => {
                        let file_res = File::create(s);
                        match file_res {
                            Ok(file) => {
                                self.stack.push(Rc::new(RefCell::new(
                                    Value::FileWriter(LineWriter::new(file)),
                                )));
                            }
                            Err(e) => {
                                let err_str = format!(
                                    "unable to open file: {}",
                                    e.to_string()
                                );
                                print_error(chunk, i, &err_str);
                                return 0;
                            }
                        }
                    }
                    _ => {
                        print_error(chunk, i, "path for open must be a string");
                        return 0;
                    }
                },
                _ => {
                    print_error(chunk, i, "mode for open must be 'r' or 'w'");
                    return 0;
                }
            },
            _ => {
                print_error(chunk, i, "mode for open must be 'r' or 'w'");
                return 0;
            }
        }
        return 1;
    }
/// Takes a file path and a mode string (either 'r' or 'w') as its
/// arguments, and puts a FileReader or FileWriter object on the
/// stack as appropriate.
Takes a file path and a mode string (either 'r' or 'w') as its
arguments, and puts a FileReader or FileWriter object on the
stack as appropriate.
Takes a file path and a mode string ( either ' r ' or ' w ' ) as its arguments and puts a FileReader or FileWriter object on the stack as appropriate .
pub fn opcode_open(&mut self, chunk: &Chunk, i: usize) -> i32 {
        if self.stack.len() < 2 {
            print_error(chunk, i, "open requires two arguments");
            return 0;
        }
        let rw_rr = self.stack.pop().unwrap();
        let rw_rrb = rw_rr.borrow();
        let path_rr = self.stack.pop().unwrap();
        let path_rrb = path_rr.borrow();
        let path_str_pre = path_rrb.to_string();
        let path_str_opt = to_string_2(&path_str_pre);
        match &*rw_rrb {
            Value::String(rw_str, _) => match rw_str.as_ref() {
                "r" => match path_str_opt {
                    Some(s) => {
                        let file_res = File::open(s);
                        match file_res {
                            Ok(file) => {
                                self.stack.push(Rc::new(RefCell::new(
                                    Value::FileReader(BufReader::new(file)),
                                )));
                            }
                            Err(e) => {
                                let err_str = format!(
                                    "unable to open file: {}",
                                    e.to_string()
                                );
                                print_error(chunk, i, &err_str);
                                return 0;
                            }
                        }
                    }
                    _ => {
                        print_error(chunk, i, "path for open must be a string");
                        return 0;
                    }
                },
                "w" => match path_str_opt {
                    Some(s) => {
                        let file_res = File::create(s);
                        match file_res {
                            Ok(file) => {
                                self.stack.push(Rc::new(RefCell::new(
                                    Value::FileWriter(LineWriter::new(file)),
                                )));
                            }
                            Err(e) => {
                                let err_str = format!(
                                    "unable to open file: {}",
                                    e.to_string()
                                );
                                print_error(chunk, i, &err_str);
                                return 0;
                            }
                        }
                    }
                    _ => {
                        print_error(chunk, i, "path for open must be a string");
                        return 0;
                    }
                },
                _ => {
                    print_error(chunk, i, "mode for open must be 'r' or 'w'");
                    return 0;
                }
            },
            _ => {
                print_error(chunk, i, "mode for open must be 'r' or 'w'");
                return 0;
            }
        }
        return 1;
    }
pub fn opcode_open ( & mut self , chunk : & Chunk , i : usize ) -> i32 { if self . stack . len ( ) < 2 { print_error ( chunk , i , " " ) ; return 0 ; } let rw_rr = self . stack . pop ( ) . unwrap ( ) ; let rw_rrb = rw_rr . borrow ( ) ; let path_rr = self . stack . pop ( ) . unwrap ( ) ; let path_rrb = path_rr . borrow ( ) ; let path_str_pre = path_rrb . to_string ( ) ; let path_str_opt = to_string_2 ( & path_str_pre ) ; match & * rw_rrb { Value :: String ( rw_str , _ ) => match rw_str . as_ref ( ) { " " => match path_str_opt { Some ( s ) => { let file_res = File :: open ( s ) ; match file_res { Ok ( file ) => { self . stack . push ( Rc :: new ( RefCell :: new ( Value :: FileReader ( BufReader :: new ( file ) ) , ) ) ) ; } Err ( e ) => { let err_str = format ! ( " " e to_string ( ) ) ; print_error ( chunk , i , & err_str ) ; return 0 ; } } } _ => { print_error ( chunk , i , " " ) ; return 0 ; } } , " " => match path_str_opt { Some ( s ) => { let file_res = File :: create ( s ) ; match file_res { Ok ( file ) => { self . stack . push ( Rc :: new ( RefCell :: new ( Value :: FileWriter ( LineWriter :: new ( file ) ) , ) ) ) ; } Err ( e ) => { let err_str = format ! ( " " e to_string ( ) ) ; print_error ( chunk , i , & err_str ) ; return 0 ; } } } _ => { print_error ( chunk , i , " " ) ; return 0 ; } } , _ => { print_error ( chunk , i , " " ) ; return 0 ; } } , _ => { print_error ( chunk , i , " " ) ; return 0 ; } } return 1 ; }
Takes a file path and a mode string (either 'r' or 'w') as its
arguments, and puts a FileReader or FileWriter object on the
stack as appropriate.
Takes a file path and a mode string ( either ' r ' or ' w ' ) as its arguments and puts a FileReader or FileWriter object on the stack as appropriate .
&mut self
chunk
&Chunk
i
usize