#include "GuiScript.h" #include "itextstream.h" #include "parser/DefTokeniser.h" #include "Gui.h" #include "GuiExpression.h" #include "Variable.h" #include "string/case_conv.h" #include "string/trim.h" #include "string/predicate.h" namespace gui { GuiScript::GuiScript(GuiWindowDef& owner) : _owner(owner), _ip(0), _curLevel(0) {} void GuiScript::parseIfStatement(parser::DefTokeniser& tokeniser) { // Prototype: if () [else ] // The initial "if" has already been parsed StatementPtr ifStatement(new Statement(Statement::ST_IF)); // Any opening and closing parentheses are handled by the expression parser ifStatement->_condition = getIfExpression(tokeniser); // condition // Add the statement at the current position pushStatement(ifStatement); // Parse the statement(s) to execute if the above condition is true parseStatement(tokeniser); // Check the next token to see where we need to jump to if the condition evaluates to false std::string nextToken = tokeniser.nextToken(); if (nextToken == "else") { // There is an "else" block, so we need to add a JMP statement before proceeding StatementPtr jmpStatement(new Statement(Statement::ST_JMP)); pushStatement(jmpStatement); // Set the original IF jump position to this else ifStatement->jmpDest = getCurPosition(); // As next step, parse the code in the else block parseStatement(tokeniser); // Finally, position the jump at the location right after the else block jmpStatement->jmpDest = getCurPosition(); } else { // No else, execution falls through, but we need to set the jump destination first ifStatement->jmpDest = getCurPosition(); switchOnToken(nextToken, tokeniser); } } void GuiScript::parseSetStatement(parser::DefTokeniser& tokeniser) { // Prototype: set [window::] StatementPtr st(new Statement(Statement::ST_SET)); st->args.push_back(_owner.parseString(tokeniser)); // variable // Add all tokens up to the semicolon as arguments while (true) { std::string token = tokeniser.peek(); // Sometimes the semicolon is missing if (token == ";" || token == "}") break; st->args.push_back(ConstantExpression::Create(tokeniser.nextToken())); // argument } pushStatement(st); } void GuiScript::parseTransitionStatement(parser::DefTokeniser& tokeniser) { // Prototype: transition [window::]