Skip to content

Commit

Permalink
Rewrite bfi in C++ for future optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
xy-kasumi committed May 11, 2014
1 parent 243635d commit fd8ef0f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 106 deletions.
2 changes: 1 addition & 1 deletion bfi/conv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/env sh

gcc -O3 main.c -o bfi
g++ -std=c++11 -O3 main.cpp -o bfi
105 changes: 0 additions & 105 deletions bfi/main.c

This file was deleted.

79 changes: 79 additions & 0 deletions bfi/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

// 20MB
const int MEM_SIZE = 20*1000*1000;


void run(const std::string& code_memory) {
std::vector<unsigned char> data_memory(MEM_SIZE, 0);

unsigned char* ptr = data_memory.data();
const char* ptr_program = code_memory.data();
const char* const ptr_program_end = code_memory.data() + code_memory.size();

uint64_t steps = 0;
while(ptr_program < ptr_program_end) {
steps++;
switch(*ptr_program){
case '>': ++ptr; break;
case '<': --ptr; break;
case '+': ++*ptr; break;
case '-': --*ptr; break;
case '.': std::cout << std::unitbuf << *ptr; break;
// TODO: do disable stdin buffering.
case ',': *ptr = std::cin.get(); break;
case '[':
if(*ptr==0){
int c=1;
while(c>0){
ptr_program++;
if(*ptr_program=='[') c++;
if(*ptr_program==']') c--;
}
}
break;
case ']':
if(*ptr!=0){
int c=1;
while(c>0){
ptr_program--;
if(*ptr_program==']') c++;
if(*ptr_program=='[') c--;
}
}
default:
steps--;
}

ptr_program++;
}
std::cerr << steps << " steps" << std::endl;
}

int main(int argc, char** argv) {
// Parse argument
if(argc != 2) {
std::cout << "bfi (compiled with MEM_SIZE=" << MEM_SIZE << ")" << std::endl;
std::cout << "usage: bfi <file>" << std::endl;
return -1;
}
const std::string bf_code_path(argv[1]);

// Read bf code.
std::ifstream bf_code_file(bf_code_path);
if(!bf_code_file.is_open()) {
std::cout << "could not open " << bf_code_path << std::endl;
return -2;
}

const std::string bf_code(
(std::istreambuf_iterator<char>(bf_code_file)),
std::istreambuf_iterator<char>());

run(bf_code);
return 0;
}

0 comments on commit fd8ef0f

Please sign in to comment.