mirror of
https://github.com/correl/mercenary.git
synced 2024-12-27 11:09:33 +00:00
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:
parent
b2726144df
commit
8d1a03d239
6 changed files with 58 additions and 50 deletions
|
@ -19,16 +19,16 @@ class MIRCScriptManager : public QObject {
|
|||
Q_OBJECT
|
||||
private:
|
||||
QObject *parent;
|
||||
QVector<MIRCScript> scripts;
|
||||
QVector<MIRCScript*> scripts;
|
||||
QMap<QString, QString> _variables;
|
||||
QMap<QString,void (*)(QStringList)> internal_aliases;
|
||||
QMap<QString, mirc_alias> 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);
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
//#define BOOST_SPIRIT_DEBUG
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/function.hpp>
|
||||
#include <boost/spirit/core.hpp>
|
||||
|
|
|
@ -1,51 +1,24 @@
|
|||
#include <QTextStream>
|
||||
#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<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";
|
||||
}
|
||||
}
|
||||
if (mirc->load(argv[1])) {
|
||||
// ??
|
||||
} else {
|
||||
output << "Failed to load " << argv[1] << "\n";
|
||||
return(1);
|
||||
|
|
|
@ -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<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)) {
|
||||
internal_aliases[alias] = fn;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<QString, mirc_alias> MIRCScript::aliases() {
|
||||
|
|
|
@ -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
|
||||
alias -l showversion {
|
||||
echo You're testing Mercenary $getversion
|
||||
}
|
||||
dostuff
|
||||
|
|
Loading…
Reference in a new issue