Permalink
Browse files

MUTATIONOFJB: Add support for labels and goto.

  • Loading branch information...
LubomirR authored and sev- committed Mar 18, 2018
1 parent 523f973 commit 54c2c0aee6e371f8058c50e0a4113f5b5492169e
@@ -0,0 +1,80 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#include "mutationofjb/commands/gotocommand.h"
#include "mutationofjb/commands/labelcommand.h"
#include "mutationofjb/game.h"
#include "mutationofjb/script.h"

/*
GOTO <label>
Jumps to a label.
*/

namespace MutationOfJB {

bool GotoCommandParser::parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command) {
if (line.firstChar() != '_') {
return false;
}

Common::String label = line.c_str() + 1;
GotoCommand *gotoCmd = new GotoCommand();

if (parseCtx._labels.contains(label)) {
// We already have the label, set it.
gotoCmd->setLabelCommand(parseCtx._labels[label]);
} else {
// Label is after goto, add to pending list.
parseCtx._pendingGotos[label].push_back(gotoCmd);
}

command = gotoCmd;

return true;
}


void GotoCommand::setLabelCommand(LabelCommand *labelCmd) {
_labelCommand = labelCmd;
}

Command::ExecuteResult GotoCommand::execute(GameData &) {
// Intentionally empty.

return Finished;
}

Command *GotoCommand::next() const {
return _labelCommand;
}

Common::String GotoCommand::debugString() const {
if (!_labelCommand) {
return "GOTO (null)";
}

return Common::String::format("GOTO %s", _labelCommand->getName().c_str());
}

}
@@ -0,0 +1,55 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#ifndef MUTATIONOFJB_GOTOCOMMAND_H
#define MUTATIONOFJB_GOTOCOMMAND_H

#include "mutationofjb/commands/command.h"
#include "common/str.h"

namespace MutationOfJB {

class LabelCommand;

class GotoCommandParser : public CommandParser {
public:
GotoCommandParser() {}

virtual bool parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command);
};

class GotoCommand : public Command {
public:
GotoCommand() : _labelCommand(nullptr) {}

void setLabelCommand(LabelCommand *labelCmd);

virtual ExecuteResult execute(GameData &gameData) override;
virtual Command *next() const override;
virtual Common::String debugString() const override;
private:
LabelCommand *_labelCommand;
};

}

#endif
@@ -0,0 +1,78 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#include "mutationofjb/commands/labelcommand.h"
#include "mutationofjb/commands/gotocommand.h"
#include "mutationofjb/game.h"
#include "mutationofjb/script.h"

/*
<label> ":"
Creates a label.
*/

namespace MutationOfJB {

bool LabelCommandParser::parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command) {
if (line.lastChar() != ':') {
return false;
}

Common::String label = line;
label.deleteLastChar();

LabelCommand *labelCmd = new LabelCommand(label);
if (!parseCtx._labels.contains(line)) {
parseCtx._labels[line] = labelCmd;
} else {
warning("Label '%s' already exists", label.c_str());
}

if (parseCtx._pendingGotos.contains(label)) {
GotoCommands &gotos = parseCtx._pendingGotos[label];
for (GotoCommands::const_iterator it = gotos.begin(); it != gotos.end(); ++it) {
(*it)->setLabelCommand(labelCmd);
}
gotos.clear();
}

command = labelCmd;
return true;
}

const Common::String &LabelCommand::getName() const
{
return _name;
}

Command::ExecuteResult LabelCommand::execute(GameData &) {
// Intentionally empty.

return Finished;
}

Common::String LabelCommand::debugString() const {
return Common::String::format("LABEL %s", _name.c_str());
}

}
@@ -0,0 +1,51 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

#ifndef MUTATIONOFJB_LABELCOMMAND_H
#define MUTATIONOFJB_LABELCOMMAND_H

#include "mutationofjb/commands/seqcommand.h"
#include "common/str.h"

namespace MutationOfJB {

class LabelCommandParser : public SeqCommandParser {
public:
LabelCommandParser() {}

virtual bool parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command);
};

class LabelCommand : public SeqCommand {
public:
LabelCommand(const Common::String &name) : _name(name) {}
const Common::String &getName() const;

virtual ExecuteResult execute(GameData &gameData) override;
virtual Common::String debugString() const override;
private:
Common::String _name;
};

}

#endif
@@ -6,9 +6,11 @@ MODULE_OBJS := \
commands/command.o \
commands/conditionalcommand.o \
commands/endblockcommand.o \
commands/gotocommand.o \
commands/ifcommand.o \
commands/ifitemcommand.o \
commands/ifpiggycommand.o \
commands/labelcommand.o \
commands/removeallitemscommand.o \
commands/removeitemcommand.o \
commands/saycommand.o \
@@ -36,6 +36,8 @@
#include "mutationofjb/commands/additemcommand.h"
#include "mutationofjb/commands/removeitemcommand.h"
#include "mutationofjb/commands/removeallitemscommand.h"
#include "mutationofjb/commands/labelcommand.h"
#include "mutationofjb/commands/gotocommand.h"

namespace MutationOfJB {

@@ -53,6 +55,8 @@ static CommandParser **getParsers() {
new AddItemCommandParser,
new RemoveItemCommandParser,
new RemoveAllItemsCommandParser,
new GotoCommandParser,
new LabelCommandParser,
nullptr
};

@@ -24,15 +24,19 @@
#define MUTATIONOFJB_SCRIPT_H

#include "common/array.h"
#include "common/hashmap.h"
#include "common/hash-str.h"

namespace Common {
class SeekableReadStream;
class String;
class SeekableReadStream;
class String;
}

namespace MutationOfJB {

class Command;
class LabelCommand;
class GotoCommand;
class ConditionalCommand;
typedef Common::Array<Command *> Commands;

@@ -53,6 +57,7 @@ struct ActionInfo {
};

typedef Common::Array<ActionInfo> ActionInfos;
typedef Common::Array<GotoCommand *> GotoCommands;

class ScriptParseContext
{
@@ -71,9 +76,14 @@ class ScriptParseContext
char _tag;
};
typedef Common::Array<ConditionalCommandInfo> ConditionalCommandInfos;

ConditionalCommandInfos _pendingCondCommands;

typedef Common::HashMap<Common::String, LabelCommand *> LabelMap;
LabelMap _labels;

typedef Common::HashMap<Common::String, GotoCommands> PendingGotoMap;
PendingGotoMap _pendingGotos;

ActionInfos _actionInfos;

private:

0 comments on commit 54c2c0a

Please sign in to comment.