Upcoming Events
Virtual Worlds Forum Europe 2008
10/6 - 10/8 @ London, United Kingdom

Tokyo Game Show
10/9 - 10/12 @ Tokyo, Japan

IndieCade
10/10 - 10/17 @ Bellevue, WA

Blizzcon
10/10 - 10/11 @ Anaheim, CA

More events...


Quick Stats
4322 people currently visiting GDNet.
2220 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!



Link to us

  search:   

Contents
 Cleaning Up
 Loading From Text
 Scanning
 Matching
 Basic File Handling
 Loading From File

 Source code
 Printable version
 Discuss this article

The Series
 An Introduction
 Data Manipulation
 Dynamic Loading
 The Stack and
 Program Flow


Matching

An array of strings containing the name of each opcode could easily be searched for the purpose of matching. Certainly this is not the most efficient way to do it. A more efficient algorithm might make use of a hash table to allow matching in constant time, but a simple array will work for this example.

typedef std::vector<std::string>    StringArray;
. . .

private:
    StringArray _codeNames;
    . . .

MatchCode() will assume that _codeNames contains the name of each opcode at the time it is called:

bool MatchCode(const std::string& str)
{
    char codeVal;
    StringArray::iterator itr = _codeNames.begin();
    for (codeVal = 0; itr != _codeNames.end(); ++itr, ++codeVal)
    {
        if (str == *itr)
        {
            _tokBuffer.push_back(codeVal);
            return true;
        }
    }
    return false;
}

To make our assumption legitimate, we will provide the scanner with the list of opcode names at the time of its creation:

public:
    Scanner(const StringArray& codeNames) : _codeNames(codeNames)   {}
    Scanner(const std::string* codeNames, size_t nameCount)
    {
        _codeNames.resize(nameCount);
        const std::string* end = codeNames+nameCount;
        std::copy(codeNames, end, _codeNames.begin());
    }

In our testing code we now include an aggregate list of strings containing opcode names in the order you find them enumerated, and use it to create the scanner:

int main()
{
    string codeList[num_codes] =
    {
        "talk",
        "print",
        "set",
        "inc",
        "dec",
        "add",
        "end"
    };

    VirtualMachine vm;
    Scanner scanner(codeList, num_codes);
    . . .

One small change was made to the opcode enumeration list to allow an implicit count of the number of opcodes included:

// enumerations describing the bytecode instructions the VM is able to process
enum opcode
{
    op_talk=0,
    op_print,   // our new printing code

    // variable manipulators
    op_set, // char, char : destination index, value to set
    op_inc, // char : index to increment
    op_dec, // char : index to decrement
    op_add, // char, char, char : dest index, srce index1, srce index2

    op_end,

    // not an opcode
    num_codes   // value is the number of opcodes if first opcode has value of 0
};

It should now be possible to compile and execute the test to verify that the scanning process does indeed function correctly. With this complete we may move on to file handling.



Next : Basic File Handling