я кодирования парсер и у меня есть следующий интерфейс:интерфейс C++ проблема разработки
class IStatement
{
// Represents instructions like "HelloWorld();"
// Or flow control blocks like : "if(foo) { bar(); }", "return 0;" etc...
public:
virtual void execute(CScope &) const = 0;
};
И следующие классы:
class CGoto : public IStatement // Basically a sequence of IStatement sub classes.
{
protected:
vector<IStatement *>
public:
virtual void execute(CScope &) const; // Executes all statements.
};
class CConditional : public CGoto
{
protected:
CExpression // Condition that must be true
public:
virtual void execute(CScope &) const; // If expression is true, executes all statements (i.e call CGoto::execute()).
};
Моя проблема заключается в том, что я хотел бы сделать класс Cif:
class CIf : public CConditional // Repesents a whole if/else if/ else block.
{
// "this" is the "if" part of the if/else if/else block
vector<CConditional *> _apoElseIfs; // "else if" parts, if any.
CConditional * _poElse; // NULL if no "else" in if/else if/else block.
public:
virtual void execute(CScope & roScope) const
{
// HERE is my problem !
// If the condition in the "if" part is true, i'm not going to execute
// the else if's or the else.
// The problem is that i have no idea from here if i should return because the
// if was executed, or if i should continue to the else if's and the else.
CConditional::execute(roScope);
// Was the condition of the "if" true ? (i.e return at this point)
// For each else if
{
current else if -> execute(roScope);
// Was the condition of the current "else if" true ? (i.e return at this point)
}
else -> execute(roScope);
}
};
Я понятия не имею, после того, как я уже казнены «если» или «иначе если» если я должен продолжать или вернуться.
Я думал, что могу использовать логическое значение в качестве возвращаемого значения для метода execute(), который указывает, был ли выполнен оператор или нет, но это не имело бы смысла для реализаций IStatement, которые не являются условными.
Я также мог бы сделать это так, чтобы класс CConditional не тестировал само условие, и поэтому CConditional :: execute() выполняет инструкции независимо от условия и что все, что манипулирует классом, делает это сам, но Я хотел бы инкапсулировать этот тест внутри метода CConditional :: execute().
Надеюсь, я объяснил свою проблему как можно более четко. Вы хоть представляете, как я могу это сделать, пожалуйста?
Спасибо :)
Спасибо за ваш ответ. Я изучу ваше предложение :) – Virus721