#include <iostream>
#include <fstream>
usingnamespace std;
ifstream::pos_type size;
char * memblock;
int main () {
ofstream asmsource ("asmsource.s");
if (asmsource.is_open())
{
asmsource << " .section .bss\n .lcomm mainarray, 30000 \n .section .rodata\n .section .data\ninoutput:\n .byte 0\n .section .text\n .globl _start\n_start:\n xorl %eax, %eax\n xorl %edi, %edi\n"; //Writes the first part of the source code in. Conventional assembler stuff.
}
elsereturn 1;
ifstream source ("source.bf", ios::in|ios::binary|ios::ate);
if (source.is_open())
{
int size = source.tellg();
char memsource[size];
source.seekg (0, ios::beg);
source.read (memsource, size);
source.close();
//I don't need it to be dynamic, since I'm only going to read it.
}
elsereturn 1;
for (int stringpointer = 0; size > stringpointer; stringpointer++) {
switch (memsource[stringpointer]) {
case'>':
asmsource << " call increment\n"; //I put them in functions so they can be set back if they go past zero.
break;
case'<':
asmsource << " call decrement\n"; //Same, only in case they go over 30,000.
break;
case'.':
asmsource << " call print\n";
break;
case',':
asmsource << " call scan\n";
break;
case'+':
asmsource << " call add\n";
break;
case'-':
asmsource << " call subtract\n";
default:
break;
}
}
asmsource.close();
}
brainfuck_compiler_0.1.cpp: In function ‘int main()’:
brainfuck_compiler_0.1.cpp:27: error: ‘memsource’ was not declared in this scope
This is part of the brainfuck compiler I'm writing. I'm getting an error that says I didn't declare the variables, but they've clearly been declared.
It isn't declared. You declared the variable in the scope of an if statement. When the block bound to that if statement ends, the variable is destroyed. It also really doesn't help that you declare it from a variable size, but you can't declare an array from a variable unless it's dynamically allocated.