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
mod core;

use core::parser::Parser;
use core::program::Program;
use core::instruction::Instruction;
use core::error::Error;
use core::memory::Memory;

use std::io;
use std::io::prelude::*;
use std::path::Path;

// Struct for the bf Interpreter (brainpreter)
pub struct Inter {

    tape:Memory,
    program:Program,
    parser:Parser,

}

impl Inter {

    // Function to create and return a new Interpreter
    pub fn new() -> Inter {

        Inter {

            tape: Memory::new(),
            program: Program::new(),
            parser: Parser::new(),

        }

    }

    // Function to load code into the parser (text)
    pub fn load<S: Into<String>>(&mut self, p:S) -> Result<(), Error> {

        self.parser.load(p)

    }

    // Function to load code into the parser (file)
    pub fn load_from_file<P: AsRef<Path>>(&mut self, src:P) -> Result<(), Error> {

        self.parser.load_from_file(src)

    }

    // Function to compile the code
    pub fn parse(&mut self) -> Result<(), Error> {

        // Call the parser
        match self.parser.parse() {

            Ok(p) => {

                self.program = p;

                Ok(())

            }

            Err(e) => Result::Err(e),

        }

    }

    // Function to execute bf program
    pub fn run(&mut self) -> Result<(), Error> {

        // Check if a program is properly loaded
        if self.program.get_size() == 0 {
            return Result::Err(Error::EmptyProgram);
        }

        // Execute instructions
        while let Some(instr) = self.program.get() {

            match instr {

                Instruction::IncPtr => { match self.inc_ptr() { Ok(_) => {  } Err(e) => return Result::Err(e), } }
                Instruction::DecPtr => { match self.dec_ptr() { Ok(_) => {  } Err(e) => return Result::Err(e), } }
                Instruction::IncVal => { match self.inc_val() { Ok(_) => {  } Err(e) => return Result::Err(e), } }
                Instruction::DecVal => { match self.dec_val() { Ok(_) => {  } Err(e) => return Result::Err(e), } }
                Instruction::Input  => { self.input(); }
                Instruction::Output => { self.output(); }
                Instruction::OpenBracket(n)  => { self.open_bracket(n); }
                Instruction::CloseBracket(n) => { self.close_bracket(n); }

            };

            // If this was the last instruction
            if !self.program.inc_ptr() {
                break;
            }

        };

        return Result::Ok(());

    }

    // Function to increase the value on the memory (tape)
    fn inc_val(&mut self) -> Result<(), Error> {

        self.tape.inc_val()

    }

    // Function to decrease the value on the memory (tape)
    fn dec_val(&mut self) -> Result<(), Error> {

        self.tape.dec_val()

    }

    // Function to increase the pointer on the memory (tape)
    fn inc_ptr(&mut self) -> Result<(), Error> {

        self.tape.inc_ptr()

    }

    // Function to decrease the pointer on the memory (tape)
    fn dec_ptr(&mut self) -> Result<(), Error> {

        self.tape.dec_ptr()

    }

    // Function to read into the memory (tape)
    fn input(&mut self) {

        match io::stdin().bytes().next() {

            Some(v) => self.tape.set_val(v.unwrap()),

            None => {  }

        }

    }

    // Function to print a char from memory (tape)
    fn output(&mut self) {

        print!("{}", self.tape.get_val() as char);
        
    }

    // Function to handle the open bracket
    fn open_bracket(&mut self, pos:usize) {
        
        if self.tape.get_val() == 0 {
            self.program.set_ptr(pos);
        }

    }

    // Function to handle the close bracket
    fn close_bracket(&mut self, pos:usize) {

        if self.tape.get_val() != 0 {
            self.program.set_ptr(pos);
        }

    }

}