I am trying to write a brainfuck interpreter but I am missing some context or something. The function that is supposed to be called to handle the conversion of "+><>
" etc is supposed to be:
std::vector<int> Interpreter::interpret(const std::string &src_,
const std::vector<int> & input_)
A test of the program is as follows:
int main()
{
std::vector<int> res;
// output: 1
res = interpret("+.");
for (auto i : res)
std::cout << i << " ";
2
// output: 2
res = interpret(",.", {2});
for (auto i : res)
std::cout << i << " ";
return 0;
}
http://www.muppetlabs.com/~breadbox/bf/
I really don't get what this is doing. I have seen other videos but it just doesn't make sense. Can someone explain the objective?
What is the point of the 30000 byte array if the function is given an array to translate?
Edit: I am supposed to write C++ code that will translate the characters the characters for the commands of brainfuck and they are supposed to do the corresponding commands on some array of 30000 bytes and some how that means something.
Edit: Provided instructions
Abstract Write a simple interpreter for Brainfk. 1 Introduction
A Brainfk program has an implicit byte pointer, called the pointer, which is free to move around within an array of 30000 bytes, initially all set to zero. The pointer itself is initialized to point to the beginning of this array. The Brainfuck programming language consists of eight commands, each of which is represented as a single character.
•
>
Increment the pointer.
•<
Decrement the pointer.
•+
Increment the byte at the pointer.
•-
Decrement the byte at the pointer.
•.
A dot, output the byte at the pointer.
•,
A comma, input a byte and store it in the byte at the pointer.
•[
Jump forward past the matching ] IF the byte at the pointer is zero.
•]
Jump backward to the matching [ UNLESS the byte at the pointer is zero.For example, one version of the "Hello, World!" program in Brainfk is
++++++++++[>+++++++>++++++++++>+++>+<<<<-] >++.>+.+++++++..+++.>++.<<+++++++++++++++.>. +++.------.--------.>+.>.
2 Requirement
2.1 Test Program
I will use program to test and grade your code in batch. So please double check your function signature. Failure to run properly may impact your project grade. The entry function will all have the nameinterpret
. And you may implement as many other helper functions as you want. The following sections elaborate on the specifications.2.1.1 C++ I would use C++11 (
g++ -std=c++11 ...
) to test your program. So feel free to employ some of the recent goodies added to C++, e.g., lambda function, array initialization, etc. For convenience, please separate your declaration and implementation code inbf.h
andbf.cpp
. The function signature isstd::vector<int> interpret(const std::string &src, const std::vector<int> &input = {});
My test program would look like
int main() { std::vector<int> res; // output: 1 res = interpret("+."); for (auto i : res) std::cout << i << " "; // output: 2 res = interpret(",.", {2}); for (auto i : res) std::cout << i << " "; return 0; }
Edit: What I have so far:
BFK.h
#pragma once
#include <vector>
#include <iostream>
using namespace std;
char arr[30000];
char* p = arr;
void incPtr();
void decPtr();
void incByte();
void decByte();
void printByte();
void setByte();
void jumpF();
void jumpB();
std::vector<int> interpret(const std::string &src,
const std::vector<int> & input = {});
BFK.cpp
#include "BFK.h"
void incPtr() {
p++;
}
void decPtr() {
p--;
}
void incByte() {
(*p)++;
}
void decByte() {
(*p)--;
}
void printByte() {
std::cout << *p;
}
void setByte() {
std::cin >> *p;
}
void jumpF() {
if (*p == 0) {
}
}
void jumpB() {
}
std::vector<int> interpret(const std::string &src_,
const std::vector<int> & input_){
int i = 0;
int max = src_.size();
while (i < max) {
switch (src_[i]) {
case '>':
incPtr();
break;
case '<':
decPtr();
break;
case '+':
incByte();
break;
case '-':
decByte();
break;
case '.':
printByte();
break;
case ',':
setByte();
break;
case '[':
jumpF();
break;
case ']':
jumpB();
break;
}
}
return input_;
}
You are supposed to be able to call interpret without instantiating anything so I didn't know of another way to put this together. I have yet to implement the jump functions.