86 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C++
		
	
	
	
			
		
		
	
	
			86 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			C++
		
	
	
	
/*
 | 
						|
 * Parsodus - A language agnostic parser generator
 | 
						|
 * Copyright © 2016-2017 Thomas Avé, Robin Jadoul, Kobe Wullaert
 | 
						|
 * 
 | 
						|
 * Permission is hereby granted, free of charge, to any person obtaining
 | 
						|
 * a copy of this software and associated documentation files (the "Software"),
 | 
						|
 * to deal in the Software without restriction, including without limitation
 | 
						|
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 | 
						|
 * and/or sell copies of the Software, and to permit persons to whom the
 | 
						|
 * Software is furnished to do so, subject to the following conditions:
 | 
						|
 * 
 | 
						|
 * The above copyright notice and this permission notice shall be included
 | 
						|
 * in all copies or substantial portions of the Software.
 | 
						|
 * 
 | 
						|
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 | 
						|
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 | 
						|
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 | 
						|
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 | 
						|
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 | 
						|
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 | 
						|
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 | 
						|
 */
 | 
						|
 | 
						|
#include "generator.h"
 | 
						|
#include "instruction.h"
 | 
						|
#include <iostream>
 | 
						|
#include <cstring>
 | 
						|
 | 
						|
namespace bf {
 | 
						|
 | 
						|
void Generator::run() {
 | 
						|
	
 | 
						|
	counter = 0;
 | 
						|
	std::memset(array, 0, sizeof(array));
 | 
						|
	while (!lbrackets.empty())
 | 
						|
		lbrackets.pop();
 | 
						|
	
 | 
						|
	for(int instruct = 0; instruct < program.size(); instruct++) {
 | 
						|
 | 
						|
		switch(program[instruct]) {
 | 
						|
		case Instruction::EMPTY: break;
 | 
						|
		case Instruction::PLUS: 
 | 
						|
			array[counter]++; 
 | 
						|
			break;
 | 
						|
		case Instruction::MINUS: 
 | 
						|
			array[counter]--; 
 | 
						|
			break;
 | 
						|
		case Instruction::GREATER: 
 | 
						|
			if (++counter == 30000) {
 | 
						|
				std::cout << "Tried to access unavailable memory" << std::endl;
 | 
						|
				return;
 | 
						|
			} break;
 | 
						|
		case Instruction::LESS:
 | 
						|
			if (--counter == -1) {
 | 
						|
				std::cout << "Tried to access unavailable memory" << std::endl;
 | 
						|
				return;
 | 
						|
			} break;
 | 
						|
		case Instruction::POINT:
 | 
						|
			std::cout << (char)array[counter];
 | 
						|
			break;
 | 
						|
		case Instruction::COMMA:
 | 
						|
			char c;
 | 
						|
			std::cin >> c; 
 | 
						|
			array[counter] = (int)c;
 | 
						|
			break;
 | 
						|
		case Instruction::LBRACKET: 
 | 
						|
			lbrackets.push(instruct);
 | 
						|
			if (array[counter] == 0) 
 | 
						|
				while (program[++instruct] != Instruction::RBRACKET) {}		
 | 
						|
			break;
 | 
						|
		case Instruction::RBRACKET: 
 | 
						|
			if (array[counter] != 0)  
 | 
						|
				instruct = lbrackets.top();
 | 
						|
			else 
 | 
						|
				lbrackets.pop();		
 | 
						|
			break;
 | 
						|
		default: // Won't happen
 | 
						|
			break;
 | 
						|
		}
 | 
						|
	} 
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
} // namespace bf
 |