diff --git a/mirc/includes/mirc.h b/mirc/includes/mirc.h index 2449f12..74b5d30 100644 --- a/mirc/includes/mirc.h +++ b/mirc/includes/mirc.h @@ -22,8 +22,11 @@ public: bool unload(QString filename); QString call_alias(QString alias, QStringList arguments); - QString variable(QString variable); */ + bool hasVariable(QString variable); + QString variable(QString variable); + void variable(QString variable, QString value); + QMap variables(); }; #endif diff --git a/mirc/includes/parser.h b/mirc/includes/parser.h index 09b0c2d..e5e0468 100644 --- a/mirc/includes/parser.h +++ b/mirc/includes/parser.h @@ -1,3 +1,6 @@ +#ifndef MIRC_PARSER_H +#define MIRC_PARSER_H + /* Mercenary @@ -17,7 +20,7 @@ #include #include #include -//#include "mirc.h" + class MIRCScriptManager; using namespace std; @@ -54,83 +57,18 @@ public: mirc_aliases aliases; mirc_variables vars; - mirc_script_engine(MIRCScriptManager *m) : script() { - manager = m; - stage = PARSE; - current_alias = aliases.end(); - current_variable = vars.end(); - } + mirc_script_engine(MIRCScriptManager *m); - void handle_alias_definition(char const* str, char const* end) { - if (stage != PARSE) return; - - string s(str, end); - aliases.insert(s.c_str(), mirc_alias(true)); - current_alias = aliases.find(s.c_str()); - } - void handle_alias_definition_local(char const* str, char const* end) { - if (stage != PARSE) return; - - string s(str, end); - aliases.insert(s.c_str(), mirc_alias(false)); - current_alias = aliases.find(s.c_str()); - } - void close_alias(char const*, char const*) { - if (stage != PARSE) return; - - if (!aliases.empty() && current_alias != aliases.end()) { - current_alias = aliases.end(); - } - } - void store_code(char const* str, char const* end) { - if (stage != PARSE) return; - - string s(str, end); - if (!aliases.empty() && current_alias != aliases.end()) { - current_alias->code.append(s.c_str()).append("\n"); - } else { - script.code.append(s.c_str()).append("\n"); - } - } - void call_alias(char const*, char const*) { - if (stage != EXECUTE) return; - - } - void return_alias(char const*, char const*) { - } - void declare_variable(char const* str, char const* end) { - if (stage != EXECUTE) return; - string s(str, end); - vars.insert(s.c_str(), ""); - current_variable = vars.find(s.c_str()); - stack.push(QStringList()); - } - void assign_variable(char const* str, char const* end) { - if (stage != EXECUTE) return; - - if (current_variable != vars.end()) { - string s(str, end); - *current_variable = (!stack.isEmpty() ? stack.pop().join(" ") : ""); - current_variable = vars.end(); - } - } - void fetch_variable(char const*, char const*) { - if (stage != EXECUTE) return; - - } - void append_expression(char const* str, char const *end) { - if (stage != EXECUTE) return; - - string s(str, end); - QStringList list; - if (stack.isEmpty()) { - list << s.c_str(); - } else { - list = stack.pop(); - list << s.c_str(); - } - stack.push(list); - } + void handle_alias_definition(char const* str, char const* end); + 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 call_alias(char const*, char const*); + void return_alias(char const*, char const*); + void declare_variable(char const* str, char const* end); + void assign_variable(char const* str, char const* end); + void fetch_variable(char const*, char const*); + void append_expression(char const* str, char const *end); }; struct mirc_script : public grammar { @@ -164,8 +102,11 @@ 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_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 ) ); s_action v_assign ( bind( &mirc_script_engine::assign_variable, self.actions, _1, _2 ) ); + s_action v_fetch ( bind( &mirc_script_engine::fetch_variable, self.actions, _1, _2 ) ); s_action e_append ( bind( &mirc_script_engine::append_expression, self.actions, _1, _2 ) ); s_action s_code ( bind( &mirc_script_engine::store_code, self.actions, _1, _2 ) ); @@ -177,7 +118,6 @@ struct mirc_script : public grammar { ; space = ( blank_p - //| (ch_p('\\') >> eol_p) | str_p("$&") >> *blank_p >> eol_p ) ; @@ -185,10 +125,10 @@ struct mirc_script : public grammar { = alpha_p >> *alnum_p ; string - = ( variable - | alias_function - | +(graph_p - ch_p(',') - ch_p('(') - ch_p(')')) - )[e_append] + = ( variable[e_append][v_fetch] + | alias_function[e_append] + | (+(graph_p - ch_p(',') - ch_p('(') - ch_p(')')))[e_append] + ) ; expression = string >> *(*space >> string) @@ -217,19 +157,11 @@ struct mirc_script : public grammar { >> !( ch_p('(') >> *space >> parameters >> *space - /* - >> (expression - ch_p(',')) - >> *( *space - >> ch_p(',') >> *space - >> (expression - ch_p(',')) - ) - */ >> ch_p(')') ) ; alias_definition = str_p("alias") >> *space - //>> !(str_p("-l") >> *space) >> if_p(str_p("-l") >> *space)[identifier[l_def]].else_p[identifier[a_def]] >> *space >> !eol_p >> code_block @@ -271,3 +203,5 @@ struct mirc_script : public grammar { rule const& start() const { return script; } }; }; + +#endif diff --git a/mirc/mirc.pro b/mirc/mirc.pro index 649cdc6..61c4c03 100644 --- a/mirc/mirc.pro +++ b/mirc/mirc.pro @@ -13,4 +13,5 @@ INCLUDEPATH += . includes HEADERS += includes/mirc.h includes/parser.h includes/script.h SOURCES += src/mirc.cpp \ src/parser.cpp \ - src/script.cpp + src/script.cpp \ + src/main.cpp diff --git a/mirc/src/mirc.cpp b/mirc/src/mirc.cpp index dfbc098..e99def6 100644 --- a/mirc/src/mirc.cpp +++ b/mirc/src/mirc.cpp @@ -3,3 +3,19 @@ MIRCScriptManager::MIRCScriptManager(QObject *parent) { this->parent = parent; } + +bool MIRCScriptManager::hasVariable(QString variable) { + return _variables.contains(variable); +} + +QString MIRCScriptManager::variable(QString variable) { + return _variables.value(variable); +} + +void MIRCScriptManager::variable(QString variable, QString value) { + _variables[variable] = value; +} + +QMap MIRCScriptManager::variables() { + return _variables; +} \ No newline at end of file diff --git a/mirc/src/parser.cpp b/mirc/src/parser.cpp index 6b6e33f..c07965c 100644 --- a/mirc/src/parser.cpp +++ b/mirc/src/parser.cpp @@ -1,39 +1,90 @@ -#include -#include "script.h" +#include "parser.h" +#include "mirc.h" -int main(int argc, char* argv[]) { - QTextStream output(stdout); - - 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 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 i(ms->variables()); - output << "Variables:\n"; - while(i.hasNext()) { - i.next(); - output << i.key() << " = " << i.value() << "\n"; - } - } - } else { - output << "Failed to load " << argv[1] << "\n"; - return(1); - } - - return 0; +mirc_script_engine::mirc_script_engine(MIRCScriptManager *m) : script() { + manager = m; + stage = PARSE; + current_alias = aliases.end(); + current_variable = vars.end(); +} + +void mirc_script_engine::handle_alias_definition(char const* str, char const* end) { + if (stage != PARSE) return; + + string s(str, end); + aliases.insert(s.c_str(), mirc_alias(true)); + current_alias = aliases.find(s.c_str()); +} +void mirc_script_engine::handle_alias_definition_local(char const* str, char const* end) { + if (stage != PARSE) return; + + string s(str, end); + aliases.insert(s.c_str(), mirc_alias(false)); + current_alias = aliases.find(s.c_str()); +} +void mirc_script_engine::close_alias(char const*, char const*) { + if (stage != PARSE) return; + + if (!aliases.empty() && current_alias != aliases.end()) { + current_alias = aliases.end(); + } +} +void mirc_script_engine::store_code(char const* str, char const* end) { + if (stage != PARSE) return; + + string s(str, end); + if (!aliases.empty() && current_alias != aliases.end()) { + current_alias->code.append(s.c_str()).append("\n"); + } else { + script.code.append(s.c_str()).append("\n"); + } +} +void mirc_script_engine::call_alias(char const*, char const*) { + if (stage != EXECUTE) return; + +} +void mirc_script_engine::return_alias(char const*, char const*) { +} +void mirc_script_engine::declare_variable(char const* str, char const* end) { + if (stage != EXECUTE) return; + string s(str, end); + vars.insert(s.c_str(), ""); + current_variable = vars.find(s.c_str()); + stack.push(QStringList()); +} +void mirc_script_engine::assign_variable(char const* str, char const* end) { + if (stage != EXECUTE) return; + + if (current_variable != vars.end()) { + string s(str, end); + *current_variable = (!stack.isEmpty() ? stack.pop().join(" ") : ""); + manager->variable(current_variable.key(), current_variable.value()); + current_variable = vars.end(); + } +} +void mirc_script_engine::fetch_variable(char const*, char const*) { + if (stage != EXECUTE) return; + + if (!stack.isEmpty()) { + QStringList values = stack.pop(); + if (!values.isEmpty()) { + QString var = values.last(); + values.removeLast(); + values << (manager->hasVariable(var) ? manager->variable(var) : vars[var]); + } + stack.push(values); + } +} +void mirc_script_engine::append_expression(char const* str, char const *end) { + if (stage != EXECUTE) return; + + string s(str, end); + QStringList list; + if (stack.isEmpty()) { + list << s.c_str(); + } else { + list = stack.pop(); + list << s.c_str(); + } + stack.push(list); } diff --git a/mirc/test.mrc b/mirc/test.mrc index 857b10d..43cf682 100644 --- a/mirc/test.mrc +++ b/mirc/test.mrc @@ -1,6 +1,7 @@ ; Hey, here's some test code set name Correl -%name = Correl $& +%first = Correl +%name = %first $& Roush echo Hello, %name! alias dostuff {