Whole purpose of learning C++ over C# and whatnot is for performance and working at a lower level for more control. Since I've been learning a little SIMD I figured a good project to test myself and make stuff better would be to program a basic scripting language that handles integer and float point calculations.

I've got it parsing C-style syntax reasonably well, but I'm a bit confused on how to operate the stack.

Some PsuedoCode assuming x86 so no need for more than 32-byte registers
char* MemoryPool;

int* ir[3] //Integer Register

float* fr[3]; //Float Register fr = ir positions

const char* path;

stack<void (*)(void)> InstructionStack;

int main(void)
{
    const char* script = ReadTextFile(path);
    vector<Instruction> Instructions = Parse(script);
    InitScript(Instructions,&MemoryPool,ir,fr,&InstructionStack);

    /*Execute Stack Will Execute Script At This Point <- starting out simple,I know more dynamic implementation would be needed later*/
}

I know I need an Instruction for things like IntAdd,IntSubtract,Mov,FloatAdd,FloatSubtract. I can probably remove Mov but still working out a decent way to do this.

My problem is I don't know how I can setup a stack with function pointers that will accept all these type of instructions. Only thing I can think of is doing Functor type classes and using inheritance to have them accept a bunch of different forms, but I imagine there has to be another way. Any recommendations.

I think you are a bit confused about how instructions are executed on a virtual machine.

The "stack" is not a stack of instructions, that's why you are confused on how to set it up. The instructions themselves are more appropriately stored in a queue or stored in a random-access array (e.g. vector or deque), i.e., a virtual equivalent of the magnetic tape of the original Turing machine. Instructions are not stacked up, they are queued for execution and occasionally jumps are made (for conditionals, loops, or function calls). If you don't allow those flow-control constructs, then you can stick to just a queue of instructions, in which case, all you do is pop the instructions off the queue and execute them until there's nothing left on the queue.

As for the representation of the instructions, I think you definitely need a base-class with a virtual function to execute the operation. This is because each operation will require different operands which have to be embedded in them, so you might as well bundle that in an object.

What is commonly referred to as just the "stack" is the memory stack used by the code to store the variables used during the execution. You're gonna need that. But, unless you want to make your virtual machine an emulator of a real instruction set, what you might not need are the general-purpose registers. In real CPUs, the registers are used because they are stored directly on the CPU such that they can be used directly by the modules that do the instructions, but when you create your own virtual machine, you don't have that physical restriction and there is really no need to have a register for the operands of the operators (although, you probably will need register-like values for the instruction pointer, the return instruction pointer, etc., that is, if you decide to allow for jumps (conditionals, loops, functions)).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.