[Scummvm-cvs-logs] SF.net SVN: scummvm: [31865] scummvm/trunk/engines/parallaction

peres001 at users.sourceforge.net peres001 at users.sourceforge.net
Sun May 4 17:09:24 CEST 2008


Revision: 31865
          http://scummvm.svn.sourceforge.net/scummvm/?rev=31865&view=rev
Author:   peres001
Date:     2008-05-04 08:09:23 -0700 (Sun, 04 May 2008)

Log Message:
-----------
Added new Parser class, which will gradually grow to include all parsing code from the engine class.

Modified Paths:
--------------
    scummvm/trunk/engines/parallaction/parallaction.cpp
    scummvm/trunk/engines/parallaction/parallaction.h
    scummvm/trunk/engines/parallaction/parser.cpp
    scummvm/trunk/engines/parallaction/parser.h
    scummvm/trunk/engines/parallaction/parser_br.cpp
    scummvm/trunk/engines/parallaction/parser_ns.cpp

Modified: scummvm/trunk/engines/parallaction/parallaction.cpp
===================================================================
--- scummvm/trunk/engines/parallaction/parallaction.cpp	2008-05-04 15:06:47 UTC (rev 31864)
+++ scummvm/trunk/engines/parallaction/parallaction.cpp	2008-05-04 15:09:23 UTC (rev 31865)
@@ -89,6 +89,7 @@
 Parallaction::~Parallaction() {
 	delete _debugger;
 
+	delete _locationParser;
 	delete _globalTable;
 
 	delete _callableNames;
@@ -588,35 +589,6 @@
 	return;
 }
 
-
-void Parallaction::pushParserTables(OpcodeSet *opcodes, Table *statements) {
-	_opcodes.push(_currentOpcodes);
-	_statements.push(_currentStatements);
-
-	_currentOpcodes = opcodes;
-	_currentStatements = statements;
-}
-
-void Parallaction::popParserTables() {
-	assert(_opcodes.size() > 0);
-
-	_currentOpcodes = _opcodes.pop();
-	_currentStatements = _statements.pop();
-}
-
-void Parallaction::parseStatement() {
-	assert(_currentOpcodes != 0);
-
-	_lookup = _currentStatements->lookup(_tokens[0]);
-
-	debugC(9, kDebugParser, "parseStatement: %s (lookup = %i)", _tokens[0], _lookup);
-
-	(*(*_currentOpcodes)[_lookup])();
-}
-
-
-
-
 AnimationPtr Parallaction::findAnimation(const char *name) {
 
 	for (AnimationList::iterator it = _animations.begin(); it != _animations.end(); it++)

Modified: scummvm/trunk/engines/parallaction/parallaction.h
===================================================================
--- scummvm/trunk/engines/parallaction/parallaction.h	2008-05-04 15:06:47 UTC (rev 31864)
+++ scummvm/trunk/engines/parallaction/parallaction.h	2008-05-04 15:09:23 UTC (rev 31865)
@@ -237,35 +237,6 @@
 
 
 
-
-class Opcode {
-
-public:
-	virtual void operator()() const = 0;
-	virtual ~Opcode() { }
-};
-
-template <class T>
-class OpcodeImpl : public Opcode {
-
-	typedef void (T::*Fn)();
-
-	T*	_instance;
-	Fn	_fn;
-
-public:
-	OpcodeImpl(T* instance, const Fn &fn) : _instance(instance), _fn(fn) { }
-
-	void operator()() const {
-		(_instance->*_fn)();
-	}
-
-};
-
-typedef Common::Array<const Opcode*>	OpcodeSet;
-
-
-
 #define DECLARE_UNQUALIFIED_ZONE_PARSER(sig) void locZoneParse_##sig()
 #define DECLARE_UNQUALIFIED_ANIM_PARSER(sig) void locAnimParse_##sig()
 #define DECLARE_UNQUALIFIED_COMMAND_PARSER(sig) void cmdParse_##sig()
@@ -306,15 +277,6 @@
 	void updateGameInput();
 	void updateCommentInput();
 
-	uint	_lookup;
-	Common::Stack<OpcodeSet*>	_opcodes;
-	Common::Stack<Table*>		_statements;
-	OpcodeSet	*_currentOpcodes;
-	Table		*_currentStatements;
-	void	pushParserTables(OpcodeSet *opcodes, Table* statements);
-	void	popParserTables();
-	void	parseStatement();
-
 	OpcodeSet	_commandOpcodes;
 
 	struct ParallactionStruct1 {
@@ -364,6 +326,8 @@
 	Table		*_callableNames;
 	Table		*_localFlagNames;
 
+	Parser		*_locationParser;
+
 public:
 	int getGameType() const;
 	uint32 getFeatures() const;

Modified: scummvm/trunk/engines/parallaction/parser.cpp
===================================================================
--- scummvm/trunk/engines/parallaction/parser.cpp	2008-05-04 15:06:47 UTC (rev 31864)
+++ scummvm/trunk/engines/parallaction/parser.cpp	2008-05-04 15:09:23 UTC (rev 31865)
@@ -209,4 +209,47 @@
 	return fillTokens(line);
 }
 
+
+void Parser::reset() {
+	_currentOpcodes = 0;
+	_currentStatements = 0;
+	_script = 0;
+}
+
+void Parser::pushTables(OpcodeSet *opcodes, Table *statements) {
+	_opcodes.push(_currentOpcodes);
+	_statements.push(_currentStatements);
+
+	_currentOpcodes = opcodes;
+	_currentStatements = statements;
+}
+
+void Parser::popTables() {
+	assert(_opcodes.size() > 0);
+
+	_currentOpcodes = _opcodes.pop();
+	_currentStatements = _statements.pop();
+}
+
+void Parser::parseStatement() {
+	assert(_currentOpcodes != 0);
+
+	_script->readLineToken(true);
+	_lookup = _currentStatements->lookup(_tokens[0]);
+
+	debugC(9, kDebugParser, "parseStatement: %s (lookup = %i)", _tokens[0], _lookup);
+
+	(*(*_currentOpcodes)[_lookup])();
+}
+
+void Parser::bind(Script *script) {
+	reset();
+	_script = script;
+}
+
+void Parser::unbind() {
+	reset();
+}
+
+
 } // namespace Parallaction

Modified: scummvm/trunk/engines/parallaction/parser.h
===================================================================
--- scummvm/trunk/engines/parallaction/parser.h	2008-05-04 15:06:47 UTC (rev 31864)
+++ scummvm/trunk/engines/parallaction/parser.h	2008-05-04 15:09:23 UTC (rev 31865)
@@ -27,6 +27,7 @@
 #define PARALLACTION_PARSER_H
 
 #include "common/stream.h"
+#include "parallaction/objects.h"
 
 namespace Parallaction {
 
@@ -58,6 +59,63 @@
 
 
 
+class Opcode {
+
+public:
+	virtual void operator()() const = 0;
+	virtual ~Opcode() { }
+};
+
+template <class T>
+class OpcodeImpl : public Opcode {
+
+	typedef void (T::*Fn)();
+
+	T*	_instance;
+	Fn	_fn;
+
+public:
+	OpcodeImpl(T* instance, const Fn &fn) : _instance(instance), _fn(fn) { }
+
+	void operator()() const {
+		(_instance->*_fn)();
+	}
+
+};
+
+typedef Common::Array<const Opcode*>	OpcodeSet;
+
+
+class Parser {
+
+public:
+	Parser() { reset(); }
+	~Parser() {}
+
+	uint	_lookup;
+
+	Common::Stack<OpcodeSet*>	_opcodes;
+	Common::Stack<Table*>		_statements;
+
+	OpcodeSet	*_currentOpcodes;
+	Table		*_currentStatements;
+
+	void	bind(Script *script);
+	void	unbind();
+	void	pushTables(OpcodeSet *opcodes, Table* statements);
+	void	popTables();
+	void	parseStatement();
+
+protected:
+	void	reset();
+
+	Script	*_script;
+};
+
+
+
+
+
 } // namespace Parallaction
 
 #endif

Modified: scummvm/trunk/engines/parallaction/parser_br.cpp
===================================================================
--- scummvm/trunk/engines/parallaction/parser_br.cpp	2008-05-04 15:06:47 UTC (rev 31864)
+++ scummvm/trunk/engines/parallaction/parser_br.cpp	2008-05-04 15:09:23 UTC (rev 31865)
@@ -316,7 +316,7 @@
 DECLARE_COMMAND_PARSER(location)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(location) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	_locParseCtxt.cmd->u._string = strdup(_tokens[1]);
 	_locParseCtxt.nextToken++;
@@ -345,7 +345,7 @@
 DECLARE_COMMAND_PARSER(string)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(string) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	_locParseCtxt.cmd->u._string = strdup(_tokens[1]);
 	_locParseCtxt.nextToken++;
@@ -357,7 +357,7 @@
 DECLARE_COMMAND_PARSER(math)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(math) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	_locParseCtxt.cmd->u._lvalue = _countersNames->lookup(_tokens[1]);
 	_locParseCtxt.nextToken++;
@@ -372,7 +372,7 @@
 DECLARE_COMMAND_PARSER(test)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(test) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	uint counter = _countersNames->lookup(_tokens[1]);
 	_locParseCtxt.nextToken++;
@@ -405,7 +405,7 @@
 DECLARE_COMMAND_PARSER(music)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(music) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	_locParseCtxt.cmd->u._musicCommand = _audioCommandsNames->lookup(_tokens[1]);
 	_locParseCtxt.nextToken++;
@@ -423,7 +423,7 @@
 DECLARE_COMMAND_PARSER(zeta)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(zeta) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	_locParseCtxt.cmd->u._zeta0 = atoi(_tokens[1]);
 	_locParseCtxt.nextToken++;
@@ -445,7 +445,7 @@
 DECLARE_COMMAND_PARSER(give)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(give) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	_locParseCtxt.cmd->u._object = 4 + atoi(_tokens[1]);
 	_locParseCtxt.nextToken++;
@@ -471,7 +471,7 @@
 DECLARE_COMMAND_PARSER(text)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(text) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	if (isdigit(_tokens[1][1])) {
 		_locParseCtxt.cmd->u._zeta0 = atoi(_tokens[1]);
@@ -497,7 +497,7 @@
 DECLARE_COMMAND_PARSER(unary)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(unary) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	_locParseCtxt.cmd->u._rvalue = atoi(_tokens[1]);
 	_locParseCtxt.nextToken++;
@@ -548,7 +548,7 @@
 //		}
 	}
 
-	popParserTables();
+	_locationParser->popTables();
 }
 
 
@@ -592,7 +592,7 @@
 
 	_locParseCtxt.a->_flags |= 0x1000000;
 
-	popParserTables();
+	_locationParser->popTables();
 }
 
 
@@ -760,6 +760,8 @@
 
 void Parallaction_br::initParsers() {
 
+	_locationParser = new Parser;
+
 	static const OpcodeV2 op0[] = {
 		INSTRUCTION_PARSER(defLocal),	// invalid opcode -> local definition
 		INSTRUCTION_PARSER(zone),		// on
@@ -908,10 +910,6 @@
 
 	for (i = 0; i < ARRAYSIZE(op6); i++)
 		_locationAnimParsers.push_back(&op6[i]);
-
-	_currentOpcodes = 0;
-	_currentStatements = 0;
-
 }
 
 void Parallaction_br::parseLocation(const char* filename) {

Modified: scummvm/trunk/engines/parallaction/parser_ns.cpp
===================================================================
--- scummvm/trunk/engines/parallaction/parser_ns.cpp	2008-05-04 15:06:47 UTC (rev 31864)
+++ scummvm/trunk/engines/parallaction/parser_ns.cpp	2008-05-04 15:09:23 UTC (rev 31865)
@@ -112,7 +112,7 @@
 
 	_locParseCtxt.a->_flags |= 0x1000000;
 
-	popParserTables();
+	_locationParser->popTables();
 }
 
 
@@ -176,7 +176,7 @@
 
 	_locParseCtxt.a->_flags |= 0x1000000;
 
-	popParserTables();
+	_locationParser->popTables();
 }
 
 void Parallaction_ns::parseAnimation(Script& script, AnimationList &list, char *name) {
@@ -191,7 +191,7 @@
 	_locParseCtxt.a = a;
 	_locParseCtxt.script = &script;
 
-	pushParserTables(&_locationAnimParsers, _locationAnimStmt);
+	_locationParser->pushTables(&_locationAnimParsers, _locationAnimStmt);
 }
 
 void Parallaction_ns::parseInstruction(ProgramPtr program) {
@@ -469,7 +469,7 @@
 DECLARE_COMMAND_PARSER(flags)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(flags) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	if (_globalTable->lookup(_tokens[1]) == Table::notFound) {
 		do {
@@ -496,7 +496,7 @@
 DECLARE_COMMAND_PARSER(zone)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(zone) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	_locParseCtxt.cmd->u._zone = findZone(_tokens[_locParseCtxt.nextToken]);
 	if (!_locParseCtxt.cmd->u._zone) {
@@ -512,7 +512,7 @@
 DECLARE_COMMAND_PARSER(location)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(location) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	_locParseCtxt.cmd->u._string = strdup(_tokens[_locParseCtxt.nextToken]);
 	_locParseCtxt.nextToken++;
@@ -525,7 +525,7 @@
 DECLARE_COMMAND_PARSER(drop)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(drop) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	_locParseCtxt.cmd->u._object = 4 + _objectsNames->lookup(_tokens[_locParseCtxt.nextToken]);
 	_locParseCtxt.nextToken++;
@@ -538,7 +538,7 @@
 DECLARE_COMMAND_PARSER(call)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(call) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	_locParseCtxt.cmd->u._callable = _callableNames->lookup(_tokens[_locParseCtxt.nextToken]) - 1;
 	_locParseCtxt.nextToken++;
@@ -551,7 +551,7 @@
 DECLARE_COMMAND_PARSER(simple)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(simple) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 	addCommand();
 }
 
@@ -559,7 +559,7 @@
 DECLARE_COMMAND_PARSER(move)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(move) ");
 
-	createCommand(_lookup);
+	createCommand(_locationParser->_lookup);
 
 	_locParseCtxt.cmd->u._move.x = atoi(_tokens[_locParseCtxt.nextToken]);
 	_locParseCtxt.nextToken++;
@@ -573,7 +573,7 @@
 DECLARE_COMMAND_PARSER(endcommands)  {
 	debugC(7, kDebugParser, "COMMAND_PARSER(endcommands) ");
 
-	popParserTables();
+	_locationParser->popTables();
 
 	// temporary trick to handle dialogue commands
 	_locParseCtxt.endcommands = true;
@@ -679,7 +679,7 @@
 	_locParseCtxt.endcommands = false;
 	_locParseCtxt.script = &script;
 
-	pushParserTables(&_commandParsers, _commandsNames);
+	_locationParser->pushTables(&_commandParsers, _commandsNames);
 }
 
 Dialogue *Parallaction_ns::parseDialogue(Script &script) {
@@ -779,8 +779,7 @@
 		parseCommands(script, answer->_commands);
 		_locParseCtxt.endcommands = false;
 		do {
-			script.readLineToken(true);
-			parseStatement();
+			_locationParser->parseStatement();
 		} while (!_locParseCtxt.endcommands);
 
 		script.readLineToken(true);
@@ -980,6 +979,7 @@
 	_numForwardedCommands = 0;
 
 	Script *script = _disk->loadLocation(filename);
+	_locationParser->bind(script);
 
 	// TODO: the following two lines are specific to Nippon Safes
 	// and should be moved into something like 'initializeParsing()'
@@ -989,12 +989,12 @@
 	_locParseCtxt.script = script;
 	_locParseCtxt.filename = filename;
 
-	pushParserTables(&_locationParsers, _locationStmt);
+	_locationParser->pushTables(&_locationParsers, _locationStmt);
 	do {
-		script->readLineToken(true);
-		parseStatement();
+		_locationParser->parseStatement();
 	} while (!_locParseCtxt.end);
-	popParserTables();
+	_locationParser->popTables();
+	_locationParser->unbind();
 
 	delete script;
 
@@ -1048,6 +1048,8 @@
 
 void Parallaction_ns::initParsers() {
 
+	_locationParser = new Parser;
+
 	static const OpcodeV1 op0[] = {
 		INSTRUCTION_PARSER(defLocal),	// invalid opcode -> local definition
 		INSTRUCTION_PARSER(animation),	// on
@@ -1153,9 +1155,6 @@
 	for (i = 0; i < ARRAYSIZE(op6); i++)
 		_locationAnimParsers.push_back(&op6[i]);
 
-	_currentOpcodes = 0;
-	_currentStatements = 0;
-
 }
 
 //
@@ -1193,7 +1192,7 @@
 DECLARE_ZONE_PARSER(endzone)  {
 	debugC(7, kDebugParser, "ZONE_PARSER(endzone) ");
 
-	popParserTables();
+	_locationParser->popTables();
 }
 
 DECLARE_ZONE_PARSER(limits)  {
@@ -1226,7 +1225,7 @@
 		parseZoneTypeBlock(*_locParseCtxt.script, _locParseCtxt.z);
 	}
 
-	popParserTables();
+	_locationParser->popTables();
 }
 
 
@@ -1274,7 +1273,7 @@
 
 	list.push_front(z);
 
-	pushParserTables(&_locationZoneParsers, _locationZoneStmt);
+	_locationParser->pushTables(&_locationZoneParsers, _locationZoneStmt);
 
 	return;
 }


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list