![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 27
Rep Power: 0
![]() |
Very annoying errors
I was going about trying to make a simplified version of the Red Code programming language. Well, everything should fit into the piece of code below.
The instructions are simplified versions of assembler directives (NOP, ADD, SUB, MUL, DIV, ...). The diference between this and assembler is that memory adressing is made relative to the current instruction pointer. That is, MOV -1 2 means "move memory from two blocks ahead one block behind". Other notable diferences are than the MUL and DIV operators are binary (equivalent to the C versions of "*=" and "/=" ). The idea is that you have a piece of memory (in my case 100 blocks) and two programs written in this simplified red-code. The programs "battle" for supremacy of the memory space. The loser, in this case is the one that attempts to execute a NOP instruction. So we have an interface that executes one instruction from each program (or more precisely, it executes instructions that are present under the avalible instruction pointers, one at a time then advances the pointers one memory unit if no jump instructions have been encountered). So I have a class mblock that codifies a unit of memory. For ease of use I made it an int. The assumption is that ints are 4 bytes. The first byte is empty and ignored, the second byte codifies the instruction to be executed (again, ADD, SUB, MUL, JMP, ...), the third byte codifies the first operand and the fourth byte the second operand. All instructions are made by this template: [empty space][instruction_code][operator1][operator2] Not all instructions take both operators. The JMP instruction takes only one operator, while the NOP takes none. This is again, for ease of use. Red Code does not make the diference between instructions to be executed and stored memory, everything is stored in one continous place. For instance, something like 173945(base 10) would mean something like (DIV -2 4). The player class encodes a "player" (or more distinctly, an instruction pointer and wether or not the player is "alive" and able to continue to execute commands ). Finally, the memory class encodes the memory and the execution interface. It contains std::vectors of players and mblocks. Theoretically, the thing should take all players, check wether or not they are alive then execute the instructions under their instruction pointers. Currently, I only use one instruction pointer, so don't be confused by this line: ip.at(0).set_pointer (p); . And now, the reason I posted this. I'm getting quite a lot of errors when I try to compile it. I used gcc from under Linux (KDevelop). All errors seem to concentrate around the STL vectors used. Now, if you can take a look at the source below and tell me the fundamental (or trivial) thing I seem to not understand about STL vectors. For instance, why can't I even initialize them (see the constructor of the memory class) ? I would very much appreciate any kind of input. I know the program is terrible. If only it would compile I would improve on it. #ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class mblock
{
int mem;
public:
mblock () { mem=0; }
mblock (mblock &mb) { mem = mb.get_mem(); }
mblock (int n) { mem = n; }
mblock (unsigned int cmd, unsigned int op1, unsigned int op2)
{
op1 += 127;
op2 += 127;
cmd <<= 16;
op1 <<= 8;
mem = cmd + op1 + op2;
}
mblock (string s, unsigned int a, unsigned int b)
{
set_command (s, a, b);
}
int get_cmd ()
{
unsigned int m = mem;
m <<= 8;
m >>= 24;
return m;
}
int get_op1 ()
{
unsigned int m = mem;
m <<= 16;
m >>= 24;
return ((signed)m-127);
}
int get_op2 ()
{
unsigned int m = mem;
m <<= 24;
m >>= 24;
return ((signed)m-127);
}
int get_mem () { return mem; }
void set_mem (int m) { mem = m; }
void set_command (string s, unsigned int a, unsigned int b)
{
a += 127;
b += 127;
if ( s == "NOP" ) mem = 0;
else if ( s == "MOV") mem = 1;
else if ( s == "ADD") mem = 2;
else if ( s == "SUB") mem = 3;
else if ( s == "MUL") mem = 4;
else if ( s == "DIV") mem = 5;
else if ( s == "MOD") mem = 6;
else if ( s == "CMP") mem = 7;
else if ( s == "JE") mem = 8;
else if ( s == "JNE") mem = 9;
else if ( s == "JG") mem = 10;
else if ( s == "JNG") mem = 11;
else if ( s == "JS") mem = 12;
else if ( s == "JNS") mem = 13;
else if ( s == "JMP") mem = 14;
else return;
mem <<=8;
mem += a;
mem <<= 8;
mem += b;
}
friend ostream& operator << (ostream &o, mblock &mb)
{
int cmd = mb.get_cmd();
int op1 = mb.get_op1();
int op2 = mb.get_op2();
o << mb.get_mem() << " ";
switch (cmd)
{
case 0: o << " NOP "; break;
case 1: o << " MOV "; break;
case 2: o << " ADD "; break;
case 3: o << " SUB "; break;
case 4: o << " MUL "; break;
case 5: o << " DIV "; break;
case 6: o << " MOD "; break;
case 7: o << " CMP "; break;
case 8: o << " JE "; break;
case 9: o << " JNE "; break;
case 10: o << " JG "; break;
case 11: o << " JNG "; break;
case 12: o << " JS "; break;
case 13: o << " JNS "; break;
case 14: o << " JMP "; break;
}
o << op1 << " " << op2 << endl;
}
mblock& operator = ( int _m)
{
mem = _m;
return *this;
}
mblock& operator = (mblock & _m)
{
mem = _m.get_mem();
return *this;
}
mblock& operator += (mblock & _m)
{
mem += _m.get_mem();
return *this;
}
mblock& operator -= (mblock & _m)
{
mem -= _m.get_mem();
return *this;
}
mblock& operator *= ( mblock & _m)
{
mem *= _m.get_mem();
return *this;
}
mblock& operator /= ( mblock & _m)
{
mem /= _m.get_mem();
return *this;
}
mblock& operator %= (mblock & _m)
{
mem %= _m.get_mem();
return *this;
}
bool operator == ( int _m)
{
if (mem == _m) return 1; return 0;
}
bool operator == (mblock & _m)
{
if (mem == _m.get_mem()) return 1; return 0;
}
bool operator != (int _m)
{
if (mem != _m) return 1; return 0;
}
bool operator > (mblock & _m)
{
if (mem > _m.get_mem()) return 1; return 0;
}
bool operator > (int _m)
{
if (mem > _m) return 1; return 0;
}
bool operator < (mblock & _m)
{
if (mem < _m.get_mem()) return 1; return 0;
}
bool operator < (int _m)
{
if (mem < _m) return 1; return 0;
}
};
class player
{
int ipointer;
int alive;
public:
player () { ipointer = alive = 1; }
player (player &p) { ipointer = p.get_pointer(); alive = p.get_pointer(); }
void kill () { alive=0; }
int get_life () { return alive; }
void set_life (int l) { alive = l; }
int get_pointer () { return ipointer; }
void set_pointer (int p) { ipointer = p; }
player& operator = (player &a)
{
ipointer = a.get_pointer();
alive = a.get_life();
return *this;
}
};
class memory
{
vector<mblock> blocks;
vector<player> ip; //the instruction pointers.
public:
memory () { blocks.clear(); ip.clear(); blocks.resize(100); ip.resize (2); }
void _mov (const int a, const int b, int const _ptr)
{
blocks.at(_ptr+a) = blocks.at(_ptr+b);
}
void _add (const int a, const int b, const int _ptr)
{
blocks.at(_ptr+a) += blocks.at(_ptr+b);
}
void _sub (const int a, const int b, const int _ptr)
{
blocks.at(_ptr+a) -= blocks.at(_ptr+b);
}
void _mul (const int a, const int b, const int _ptr)
{
blocks.at(_ptr+a) *= blocks.at(_ptr+b);
}
void _div (const int a, const int b, const int _ptr)
{
blocks.at(_ptr+a) /= blocks.at(_ptr+b);
}
void _mod (const int a, const int b, const int _ptr)
{
blocks.at(_ptr+a) %= blocks.at(_ptr+b);
}
void _cmp (const int a, const int b, const int _ptr)
{
int result;
if ( blocks.at(_ptr+a) == blocks.at(_ptr+b) ) { result = 0; }
if ( blocks.at(_ptr+a) > blocks.at(_ptr+b) ) { result = 1; }
if ( blocks.at(_ptr+a) < blocks.at(_ptr+b) ) { result = -1; }
blocks.at(_ptr+1) = result;
}
void _je (const int a, int &_ptr)
{
if (_ptr==0) _ptr += blocks.size();
if (_ptr>= blocks.size()) _ptr -= blocks.size();
if (blocks.at (_ptr-1)==0) _jmp (a, _ptr);
}
void _jne (const int a, int &_ptr)
{
if (_ptr==0) _ptr += blocks.size();
if (_ptr>= blocks.size()) _ptr -= blocks.size();
if (blocks.at (_ptr-1)!=0) _jmp (a, _ptr);
}
void _jg (const int a, int &_ptr)
{
if (_ptr==0) _ptr += blocks.size();
if (_ptr>= blocks.size()) _ptr -= blocks.size();
if (blocks.at (_ptr-1)==1) _jmp (a, _ptr);
}
void _jng (const int a, int &_ptr)
{
if (_ptr==0) _ptr += blocks.size();
if (_ptr>= blocks.size()) _ptr -= blocks.size();
if (blocks.at (_ptr-1)!=1) _jmp (a, _ptr);
}
void _js (const int a, int &_ptr)
{
if (_ptr==0) _ptr += blocks.size();
if (_ptr>= blocks.size()) _ptr -= blocks.size();
if (blocks.at (_ptr-1)==-1) _jmp (a, _ptr);
}
void _jns (const int a, int &_ptr)
{
if (_ptr==0) _ptr += blocks.size();
if (_ptr>= blocks.size()) _ptr -= blocks.size();
if (blocks.at (_ptr-1)!=-1) _jmp (a, _ptr);
}
void _jmp (const int a, int &_ptr)
{
_ptr+= a;
}
void run ()
{
int cmd, op1, op2;
int p = ip.at(0).get_pointer();
for (int i=0; i<blocks.size(); ++i)
{
cmd = blocks.at(p).get_cmd();
op1 = blocks.at(p).get_op1();
op2 = blocks.at(p).get_op2();
switch (cmd)
{
case 0: ip.at(0).kill(); break;
case 1: _mov (op1, op2, p); ++p; break;
case 2: _add (op1, op2, p); ++p; break;
case 3: _sub (op1, op2, p); ++p; break;
case 4: _mul (op1, op2, p); ++p; break;
case 5: _div (op1, op2, p); ++p; break;
case 6: _cmp (op1, op2, p); p+=2; break;
case 7: _je (op1, p); break;
case 8: _jne (op1, p); break;
case 9: _jg (op1, p); break;
case 10: _jg (op1, p); break;
case 11: _jng (op1, p); break;
case 12: _js (op1, p); break;
case 13: _jns (op1, p); break;
case 14: _jmp (op1, p); break;
}
ip.at(0).set_pointer (p);
}
}
};
int main(int argc, char *argv[])
{
return 0;
} |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
all of your errors come from not using the const keword in the right places (or not at all
), which you should really read up on.these are all the updated functions: mblock (mblock const &mb) { mem = mb.get_mem(); }
int get_mem () const { return mem; }
mblock& operator = (mblock const & _m)
{
mem = _m.get_mem();
return *this;
}
player (player const &p) { ipointer = p.get_pointer(); alive = p.get_life(); } //<-- changed get_pointer to get_life (i think thats want you wanted)
int get_life () const { return alive; }
int get_pointer () const { return ipointer; }
player& operator = (player const &a)
{
ipointer = a.get_pointer();
alive = a.get_life();
return *this;
}on vs2003 I only get warnings now (which you should fix as well ) |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 27
Rep Power: 0
![]() |
Thank you
![]() I'll check to see how that works. And I'll read up more on consts. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|