Why not load a file specified on the commandline...

Also, some placeholder functions for the great and powerful script
manager!


git-svn-id: file:///srv/svn/ircclient/trunk@2 a9804ffe-773b-11dd-bd7c-89c3ef1d2733
This commit is contained in:
Correl Roush 2008-08-31 09:50:57 +00:00
parent 0381859219
commit 7315012830
6 changed files with 55 additions and 75 deletions

View file

@ -4,15 +4,26 @@
#include <QObject>
#include <QString>
#include <QMap>
#include <QVector>
#include "script.h"
class MIRCScript;
class MIRCScriptManager : public QObject {
Q_OBJECT
private:
QObject *parent;
QVector<MIRCScript> scripts;
QMap<QString, QString> _variables;
public:
//QMap<QString, QString> variables;
MIRCScriptManager(QObject *parent = 0);
/*
bool load(QString filename);
bool unload(QString filename);
MIRCScriptManager(QObject *parent);
QString call_alias(QString alias, QStringList arguments);
QString variable(QString variable);
*/
};
#endif

View file

@ -17,6 +17,8 @@
#include <QStringList>
#include <QMap>
#include <QStack>
//#include "mirc.h"
class MIRCScriptManager;
using namespace std;
using namespace boost;
@ -42,6 +44,7 @@ enum mirc_engine_stage {
class mirc_script_engine : public QObject {
Q_OBJECT
private:
MIRCScriptManager *manager;
mirc_aliases::iterator current_alias;
mirc_variables::iterator current_variable;
QStack<QStringList> stack;
@ -51,7 +54,8 @@ public:
mirc_aliases aliases;
mirc_variables vars;
mirc_script_engine() : script() {
mirc_script_engine(MIRCScriptManager *m) : script() {
manager = m;
stage = PARSE;
current_alias = aliases.end();
current_variable = vars.end();

View file

@ -10,12 +10,13 @@
class MIRCScript : public QObject {
Q_OBJECT
private:
MIRCScriptManager *manager;
QString script;
QMap<QString, mirc_alias> _aliases;
QMap<QString, QString> _variables;
bool loaded;
public:
MIRCScript();
MIRCScript(MIRCScriptManager *m);
bool load(QString filename);
bool parse(QString script);
bool run();

View file

@ -1,4 +1,5 @@
#include "mirc.h"
MIRCScriptManager::MIRCScriptManager(QObject *parent = 0) {
MIRCScriptManager::MIRCScriptManager(QObject *parent) {
this->parent = parent;
}

View file

@ -1,76 +1,38 @@
#include <QTextStream>
#include "script.h"
int main() {
int main(int argc, char* argv[]) {
QTextStream output(stdout);
QString foo;
foo.append("; Hey, here's some test code\n");
foo.append("set name Correl\n");
foo.append("%name = Correl $&\n\tRoush\n");
foo.append("echo Hello, %name!\n");
foo.append("alias dostuff {\n");
foo.append(" ; Not very useful, but good for testing the parser!\n");
foo.append(" var %b = 42\n");
foo.append(" %b = $calc(%b * 3)\n");
foo.append(" return %b;\n");
foo.append("}\n");
foo.append("alias -l dosomethingelse {\n");
foo.append(" ; Useless local alias!\n");
foo.append(" echo -s Busy doing nothing\n");
foo.append("}\n");
foo.append("alias -l dosomethingelse {\n");
foo.append(" ; Useless local alias!\n");
foo.append(" echo -s Busy doing nothing\n");
foo.append("}\n");
output << "Code:\n"
<< "================================================================================\n"
<< foo
<< "================================================================================\n"
<< "\n";
mirc_script_engine *interpreter = new mirc_script_engine();
mirc_script parser(interpreter);
parse_info<> info = parse((const char*)foo.toLatin1(), parser);
if (info.full)
{
output << "-------------------------\n";
output << "Parsing succeeded\n";
output << "-------------------------\n";
MIRCScript *ms = new MIRCScript();
output << "Creating new MIRCScript object\n";
//if (ms->parse(foo)) {
if (ms->load("test.mrc")) {
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";
}
if (ms->run()) {
output << "MRC RAN SUCCESSFULLY\n";
QMapIterator<QString, QString> i(ms->variables());
output << "Variables:\n";
while(i.hasNext()) {
i.next();
output << i.key() << " = " << i.value() << "\n";
}
MIRCScriptManager *mirc = new MIRCScriptManager;
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";
}
if (ms->run()) {
output << "MRC RAN SUCCESSFULLY\n";
QMapIterator<QString, QString> i(ms->variables());
output << "Variables:\n";
while(i.hasNext()) {
i.next();
output << i.key() << " = " << i.value() << "\n";
}
}
}
else
{
output << "-------------------------\n";
output << "Parsing failed\n";
output << "stopped at: \": " << info.stop << "\"\n";
output << "-------------------------\n";
} else {
output << "Failed to load " << argv[1] << "\n";
return(1);
}
return 0;

View file

@ -1,7 +1,8 @@
#include "script.h"
#include <QFile>
MIRCScript::MIRCScript() {
MIRCScript::MIRCScript(MIRCScriptManager *m) {
manager = m;
}
bool MIRCScript::load(QString filename) {
@ -16,7 +17,7 @@ bool MIRCScript::load(QString filename) {
}
bool MIRCScript::parse(QString code) {
mirc_script_engine *interpreter = new mirc_script_engine();
mirc_script_engine *interpreter = new mirc_script_engine(manager);
mirc_script parser(interpreter);
parse_info<> info = boost::spirit::parse((const char*)code.toLatin1(), parser);
loaded = info.full;
@ -29,7 +30,7 @@ bool MIRCScript::parse(QString code) {
bool MIRCScript::run() {
if (!loaded) return false;
mirc_script_engine *interpreter = new mirc_script_engine();
mirc_script_engine *interpreter = new mirc_script_engine(manager);
interpreter->stage = EXECUTE;
mirc_script parser(interpreter);
parse_info<> info = boost::spirit::parse((const char*)script.toLatin1(), parser);