Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
86 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net 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