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
This commit is contained in:
Correl Roush 2009-04-23 15:29:37 +00:00
parent b2726144df
commit 8d1a03d239
6 changed files with 58 additions and 50 deletions

View file

@ -19,16 +19,16 @@ class MIRCScriptManager : public QObject {
Q_OBJECT Q_OBJECT
private: private:
QObject *parent; QObject *parent;
QVector<MIRCScript> scripts; QVector<MIRCScript*> scripts;
QMap<QString, QString> _variables; QMap<QString, QString> _variables;
QMap<QString,void (*)(QStringList)> internal_aliases; QMap<QString,void (*)(QStringList)> internal_aliases;
QMap<QString, mirc_alias> aliases;
QString _return_value; QString _return_value;
QTextStream *output; QTextStream *output;
public: public:
MIRCScriptManager(QObject *parent = 0); MIRCScriptManager(QObject *parent = 0);
/*
bool load(QString filename); bool load(QString filename);
/*
bool unload(QString filename); bool unload(QString filename);
*/ */
void call_alias(QString alias, QStringList arguments); void call_alias(QString alias, QStringList arguments);

View file

@ -8,7 +8,7 @@
*/ */
//#define BOOST_SPIRIT_DEBUG //#define BOOST_SPIRIT_DEBUG
#include <string> #include <string.h>
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/function.hpp> #include <boost/function.hpp>
#include <boost/spirit/core.hpp> #include <boost/spirit/core.hpp>

View file

@ -1,51 +1,24 @@
#include <QTextStream> #include <QTextStream>
#include "script.h" #include "script.h"
QTextStream output(stdout);
void alias_echo(QStringList arguments) { void alias_echo(QStringList arguments) {
QTextStream output(stdout);
output << "[ECHO] " << arguments.join(" ") << "\n"; output << "[ECHO] " << arguments.join(" ") << "\n";
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
QTextStream output(stdout);
output << "* Creating manager\n"; output << "* Creating manager\n";
MIRCScriptManager *mirc = new MIRCScriptManager; MIRCScriptManager *mirc = new MIRCScriptManager;
mirc->register_alias("echo", &alias_echo); mirc->register_alias("echo", &alias_echo);
output << "* Creating script object\n";
MIRCScript *ms = new MIRCScript(mirc);
if (argc < 2) { if (argc < 2) {
output << "! No mIRC script file was specified!\n"; output << "! No mIRC script file was specified!\n";
return(1); return(1);
} }
output << "* Attempting to load " << argv[1] << "\n"; output << "* Attempting to load " << argv[1] << "\n";
if (ms->load(argv[1])) { if (mirc->load(argv[1])) {
output << "* MRC LOADED\n"; // ??
output << "Code:\n" << ms->code();
output << "Aliases:\n";
QMapIterator<QString, mirc_alias> 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<QString, QString> i(mirc->variables());
output << "Variables:\n";
while(i.hasNext()) {
i.next();
output << i.key() << " = " << i.value() << "\n";
}
output << "Aliases:\n";
QMapIterator<QString, mirc_alias> alias(ms->aliases());
while(alias.hasNext()) {
alias.next();
output << (alias.value().global ? "global" : "local");
output << " " << alias.key() << "()\n";
}
}
} else { } else {
output << "Failed to load " << argv[1] << "\n"; output << "Failed to load " << argv[1] << "\n";
return(1); return(1);

View file

@ -4,6 +4,16 @@ MIRCScriptManager::MIRCScriptManager(QObject *parent) {
this->parent = 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) { bool MIRCScriptManager::hasVariable(QString variable) {
return _variables.contains(variable); return _variables.contains(variable);
} }
@ -30,15 +40,35 @@ void MIRCScriptManager::call_alias(QString alias, QStringList arguments) {
// Handle some builtins // Handle some builtins
if (alias == "lower") { if (alias == "lower") {
return_value(arguments.join(" ").toLower()); return_value(arguments.join(" ").toLower());
} else if (alias == "return") {
return_value(arguments.join(" "));
//TODO: Somehow terminate the currently running script
} else if (alias == "version") { } else if (alias == "version") {
return_value(MIRC_VERSION); return_value(MIRC_VERSION);
} else { } else {
qDebug() << aliases.keys(); bool found = false;
if (aliases.find(alias) != aliases.end()) { int script_index = 0;
mirc_alias a = aliases[alias]; for (int i = 0; i < scripts.size(); i++) {
qDebug() << "SCRIPTED ALIAS" << alias; QMap<QString, mirc_alias> 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)) { bool MIRCScriptManager::register_alias(QString alias, void (*fn)(QStringList)) {
internal_aliases[alias] = fn; internal_aliases[alias] = fn;
} }

View file

@ -48,7 +48,11 @@ QString MIRCScript::code() {
} }
QString MIRCScript::code(QString alias) { QString MIRCScript::code(QString alias) {
return QString(); if (_aliases.find(alias) != _aliases.end()) {
return _aliases[alias].code;
} else {
return QString();
}
} }
QMap<QString, mirc_alias> MIRCScript::aliases() { QMap<QString, mirc_alias> MIRCScript::aliases() {

View file

@ -6,17 +6,18 @@ set name Correl
%middle Roush %middle Roush
echo Hello %name $+ ! $& echo Hello %name $+ ! $&
How $lower(ArE YoU) $+ ? How $lower(ArE YoU) $+ ?
echo You're testing Mercenary v $+ $version showversion
alias dostuff { alias dostuff {
; Not very useful, but good for testing the parser! ; Not very useful, but good for testing the parser!
var %b = 42 var %b = 42
%b = $calc(%b * 3) %b = $calc(%b * 3)
return %b; return %b;
} }
alias -l dosomethingelse { alias -l getversion {
; Useless local alias! var %version = version $version
echo -s Busy doing nothing return %version
} }
dostuff that nobody will see alias -l showversion {
dosomethingelse entirely echo You're testing Mercenary $getversion
donothing because this alias doesn't exist }
dostuff