mirror of
https://github.com/correl/mercenary.git
synced 2024-11-23 19:19:51 +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
|
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);
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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,17 +40,37 @@ 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;
|
||||||
|
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()) {
|
if (aliases.find(alias) != aliases.end()) {
|
||||||
mirc_alias a = aliases[alias];
|
found = true;
|
||||||
qDebug() << "SCRIPTED ALIAS" << alias;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
QString MIRCScriptManager::return_value() {
|
QString MIRCScriptManager::return_value() {
|
||||||
return _return_value;
|
return _return_value;
|
||||||
|
|
|
@ -48,7 +48,11 @@ QString MIRCScript::code() {
|
||||||
}
|
}
|
||||||
|
|
||||||
QString MIRCScript::code(QString alias) {
|
QString MIRCScript::code(QString alias) {
|
||||||
|
if (_aliases.find(alias) != _aliases.end()) {
|
||||||
|
return _aliases[alias].code;
|
||||||
|
} else {
|
||||||
return QString();
|
return QString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QMap<QString, mirc_alias> MIRCScript::aliases() {
|
QMap<QString, mirc_alias> MIRCScript::aliases() {
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue