diff --git a/mirc/includes/mirc.h b/mirc/includes/mirc.h index 408915e..f67d911 100644 --- a/mirc/includes/mirc.h +++ b/mirc/includes/mirc.h @@ -2,6 +2,7 @@ #define MIRC_H #include +#include #include #include #include @@ -10,6 +11,8 @@ #include "script.h" #include "parser.h" +#define MIRC_VERSION "0.1" + class MIRCScript; class MIRCScriptManager : public QObject { @@ -20,6 +23,7 @@ private: QMap _variables; QMap internal_aliases; QMap aliases; + QString _return_value; QTextStream *output; public: MIRCScriptManager(QObject *parent = 0); @@ -34,6 +38,9 @@ public: QString variable(QString variable); void variable(QString variable, QString value); QMap variables(); + QString return_value(); +public slots: + void return_value(QString value); signals: void unknown_alias(QString alias, QStringList arguments); }; diff --git a/mirc/includes/parser.h b/mirc/includes/parser.h index 1175b61..629ba98 100644 --- a/mirc/includes/parser.h +++ b/mirc/includes/parser.h @@ -53,6 +53,7 @@ private: mirc_variables::iterator current_variable; QStringList current_value; QStack stack; + QString _alias; public: mirc_engine_stage stage; @@ -66,6 +67,7 @@ public: void handle_alias_definition_local(char const* str, char const* end); void close_alias(char const*, char const*); void store_code(char const* str, char const* end); + void set_alias(char const* str, char const* end); void call_alias(char const*, char const*); void return_alias(char const*, char const*); void declare_variable(char const* str, char const* end); @@ -109,6 +111,7 @@ struct mirc_script : public grammar { s_action a_def ( bind( &mirc_script_engine::handle_alias_definition, self.actions, _1, _2 ) ); s_action l_def ( bind( &mirc_script_engine::handle_alias_definition_local, self.actions, _1, _2 ) ); s_action a_close ( bind( &mirc_script_engine::close_alias, self.actions, _1, _2 ) ); + s_action a_set ( bind( &mirc_script_engine::set_alias, self.actions, _1, _2 ) ); s_action a_call ( bind( &mirc_script_engine::call_alias, self.actions, _1, _2 ) ); s_action a_return ( bind( &mirc_script_engine::return_alias, self.actions, _1, _2 ) ); s_action v_def ( bind( &mirc_script_engine::declare_variable, self.actions, _1, _2 ) ); @@ -141,7 +144,7 @@ struct mirc_script : public grammar { ; value = ( variable[v_append][v_fetch] - | alias_function[v_append] + | alias_function[v_append][a_return] | string[v_append] ) ; @@ -150,7 +153,8 @@ struct mirc_script : public grammar { >> *(+space >> (+(*nospace >> value))[e_append]) ; expression_group - = expression | expression_group + = (*nospace >> *(*(ch_p(',') | ch_p('(') | ch_p(')'))[v_append] >> value))[e_append] + >> *(+space >> (+(*nospace >> *(*(ch_p(',') | ch_p('(') | ch_p(')'))[v_append] >> value))[e_append])[e_append]) ; parameters = expression >> *(*space >> ch_p(',') >> *space >> expression) @@ -166,10 +170,10 @@ struct mirc_script : public grammar { ; alias_action = !ch_p('/') >> !ch_p('/') - >> (identifier >> *(*space >> parameters))[a_call] + >> (identifier[a_set] >> *(*space >> parameters))[a_call] ; alias_function - = ch_p('$') >> identifier + = ch_p('$') >> identifier[a_set] >> !( ch_p('(') >> *space >> parameters >> *space diff --git a/mirc/src/mirc.cpp b/mirc/src/mirc.cpp index 8e792dd..acbb362 100644 --- a/mirc/src/mirc.cpp +++ b/mirc/src/mirc.cpp @@ -26,10 +26,28 @@ void MIRCScriptManager::call_alias(QString alias, QStringList arguments) { internal_aliases[alias](arguments); } else { //TODO: check known scripted aliases!!! - emit unknown_alias(alias, arguments); + + // Handle some builtins + if (alias == "lower") { + return_value(arguments.join(" ").toLower()); + } 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; + } + emit unknown_alias(alias, arguments); + } } } - +QString MIRCScriptManager::return_value() { + return _return_value; +} +void MIRCScriptManager::return_value(QString value) { + _return_value = 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/parser.cpp b/mirc/src/parser.cpp index 4d957b8..e5960e1 100644 --- a/mirc/src/parser.cpp +++ b/mirc/src/parser.cpp @@ -41,14 +41,26 @@ void mirc_script_engine::store_code(char const* str, char const* end) { script.code.append(s.c_str()).append("\n"); } } +void mirc_script_engine::set_alias(char const* str, char const* end) { + string s(str, end); + _alias = s.c_str(); + stack.push(QStringList()); +} void mirc_script_engine::call_alias(char const* str, char const* end) { if (stage != EXECUTE) return; string s(str, end); QStringList params = QString(s.c_str()).split(" "); QString alias = params.takeFirst(); manager->call_alias(alias, stack.top()); + stack.pop(); } -void mirc_script_engine::return_alias(char const*, char const*) { +void mirc_script_engine::return_alias(char const* str, char const* end) { + if (stage != EXECUTE) return; + string s(str, end); + manager->call_alias(_alias, stack.top()); + stack.pop(); + current_value.clear(); + current_value << manager->return_value(); } void mirc_script_engine::declare_variable(char const* str, char const* end) { if (stage != EXECUTE) return; diff --git a/mirc/test.mrc b/mirc/test.mrc index b913545..74102e2 100644 --- a/mirc/test.mrc +++ b/mirc/test.mrc @@ -3,9 +3,10 @@ set name Correl %first = Correl %middle = Joseph %name = %first $& - %middle $+ Roush $& - $+ ... how are you? -echo Hello, %name! + %middle Roush +echo Hello %name $+ ! $& + How $lower(ArE YoU) $+ ? +echo You're testing Mercenary v $+ $version alias dostuff { ; Not very useful, but good for testing the parser! var %b = 42