From 8d1a03d239e6f7fbe969e988d0b95a54f86d605c Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Thu, 23 Apr 2009 15:29:37 +0000 Subject: [PATCH] Now supports scripted aliases. Note: return does not currently halt the script. It really needs to. git-svn-id: file:///srv/svn/ircclient/trunk@9 a9804ffe-773b-11dd-bd7c-89c3ef1d2733 --- mirc/includes/mirc.h | 6 +++--- mirc/includes/parser.h | 2 +- mirc/src/main.cpp | 37 +++++-------------------------------- mirc/src/mirc.cpp | 42 ++++++++++++++++++++++++++++++++++++------ mirc/src/script.cpp | 6 +++++- mirc/test.mrc | 15 ++++++++------- 6 files changed, 58 insertions(+), 50 deletions(-) diff --git a/mirc/includes/mirc.h b/mirc/includes/mirc.h index f67d911..5a523d6 100644 --- a/mirc/includes/mirc.h +++ b/mirc/includes/mirc.h @@ -19,16 +19,16 @@ class MIRCScriptManager : public QObject { Q_OBJECT private: QObject *parent; - QVector scripts; + QVector scripts; QMap _variables; QMap internal_aliases; - QMap aliases; QString _return_value; QTextStream *output; public: MIRCScriptManager(QObject *parent = 0); - /* + bool load(QString filename); + /* bool unload(QString filename); */ void call_alias(QString alias, QStringList arguments); diff --git a/mirc/includes/parser.h b/mirc/includes/parser.h index 629ba98..1d09b9f 100644 --- a/mirc/includes/parser.h +++ b/mirc/includes/parser.h @@ -8,7 +8,7 @@ */ //#define BOOST_SPIRIT_DEBUG -#include +#include #include #include #include diff --git a/mirc/src/main.cpp b/mirc/src/main.cpp index 4eb738b..52d0e98 100644 --- a/mirc/src/main.cpp +++ b/mirc/src/main.cpp @@ -1,51 +1,24 @@ #include #include "script.h" +QTextStream output(stdout); + void alias_echo(QStringList arguments) { - QTextStream output(stdout); output << "[ECHO] " << arguments.join(" ") << "\n"; } int main(int argc, char* argv[]) { - QTextStream output(stdout); - output << "* Creating manager\n"; MIRCScriptManager *mirc = new MIRCScriptManager; mirc->register_alias("echo", &alias_echo); - output << "* Creating script object\n"; - MIRCScript *ms = new MIRCScript(mirc); + if (argc < 2) { output << "! No mIRC script file was specified!\n"; return(1); } output << "* Attempting to load " << argv[1] << "\n"; - if (ms->load(argv[1])) { - output << "* MRC LOADED\n"; - output << "Code:\n" << ms->code(); - output << "Aliases:\n"; - QMapIterator alias(ms->aliases()); - while(alias.hasNext()) { - alias.next(); - output << (alias.value().global ? "global" : "local"); - output << " " << alias.key() << "()\n"; - } - output << "---\n"; - if (ms->run()) { - output << "\n---\nMRC RAN SUCCESSFULLY\n"; - QMapIterator i(mirc->variables()); - output << "Variables:\n"; - while(i.hasNext()) { - i.next(); - output << i.key() << " = " << i.value() << "\n"; - } - output << "Aliases:\n"; - QMapIterator alias(ms->aliases()); - while(alias.hasNext()) { - alias.next(); - output << (alias.value().global ? "global" : "local"); - output << " " << alias.key() << "()\n"; - } - } + if (mirc->load(argv[1])) { + // ?? } else { output << "Failed to load " << argv[1] << "\n"; return(1); diff --git a/mirc/src/mirc.cpp b/mirc/src/mirc.cpp index acbb362..1a9e011 100644 --- a/mirc/src/mirc.cpp +++ b/mirc/src/mirc.cpp @@ -4,6 +4,16 @@ MIRCScriptManager::MIRCScriptManager(QObject *parent) { this->parent = parent; } +bool MIRCScriptManager::load(QString filename) { + MIRCScript *script = new MIRCScript(this); + if (script->load(filename)) { + scripts << script; + script->run(); + return true; + } + return false; +} + bool MIRCScriptManager::hasVariable(QString variable) { return _variables.contains(variable); } @@ -30,15 +40,35 @@ void MIRCScriptManager::call_alias(QString alias, QStringList arguments) { // Handle some builtins if (alias == "lower") { return_value(arguments.join(" ").toLower()); + } else if (alias == "return") { + return_value(arguments.join(" ")); + //TODO: Somehow terminate the currently running script } else if (alias == "version") { return_value(MIRC_VERSION); } else { - qDebug() << aliases.keys(); - if (aliases.find(alias) != aliases.end()) { - mirc_alias a = aliases[alias]; - qDebug() << "SCRIPTED ALIAS" << alias; + bool found = false; + int script_index = 0; + for (int i = 0; i < scripts.size(); i++) { + QMap aliases = scripts.at(i)->aliases(); + if (aliases.find(alias) != aliases.end()) { + found = true; + script_index = i; + break; + } + } + if (found) { + MIRCScript *s = new MIRCScript(this); + QString code = scripts.at(script_index)->code(alias); + if (s->parse(code)) { + s->run(); + } else { + qDebug() << "SYNTAX ERROR IN " << alias; + } + delete s; + } else { + qDebug() << "UNKNOWN ALIAS" << alias; + emit unknown_alias(alias, arguments); } - emit unknown_alias(alias, arguments); } } } @@ -50,4 +80,4 @@ void MIRCScriptManager::return_value(QString value) { } bool MIRCScriptManager::register_alias(QString alias, void (*fn)(QStringList)) { internal_aliases[alias] = fn; -} \ No newline at end of file +} diff --git a/mirc/src/script.cpp b/mirc/src/script.cpp index 421b4e5..ff0e572 100644 --- a/mirc/src/script.cpp +++ b/mirc/src/script.cpp @@ -48,7 +48,11 @@ QString MIRCScript::code() { } QString MIRCScript::code(QString alias) { - return QString(); + if (_aliases.find(alias) != _aliases.end()) { + return _aliases[alias].code; + } else { + return QString(); + } } QMap MIRCScript::aliases() { diff --git a/mirc/test.mrc b/mirc/test.mrc index 74102e2..b54fe7b 100644 --- a/mirc/test.mrc +++ b/mirc/test.mrc @@ -6,17 +6,18 @@ set name Correl %middle Roush echo Hello %name $+ ! $& How $lower(ArE YoU) $+ ? -echo You're testing Mercenary v $+ $version +showversion alias dostuff { ; Not very useful, but good for testing the parser! var %b = 42 %b = $calc(%b * 3) return %b; } -alias -l dosomethingelse { - ; Useless local alias! - echo -s Busy doing nothing +alias -l getversion { + var %version = version $version + return %version } -dostuff that nobody will see -dosomethingelse entirely -donothing because this alias doesn't exist \ No newline at end of file +alias -l showversion { + echo You're testing Mercenary $getversion +} +dostuff