diff --git a/command.cpp b/command.cpp new file mode 100644 index 0000000..c78f4c8 --- /dev/null +++ b/command.cpp @@ -0,0 +1,80 @@ +#include "command.h" +#include "phase2-parser.h" +#include + +command::~command() +{} + + +int simple_command::execute() { + return 0; +} + +int or_command::execute() { + /* + for (const auto &c : children) { + if (!c) throw std::runtime_error("corruption in || command."); + } + */ + + int rv = 0; + for (auto &c : children) { + rv = c->execute(); + if (rv == 0) return rv; + } + return rv; +} + +int and_command::execute() { + + int rv = 0; + for (auto &c : children) { + rv = c->execute(); + if (rv != 0) return rv; + } + return rv; +} + + +int vector_command::execute() { + + int rv = 0; + for (auto &c : children) { + + rv = c->execute(); + // todo -- env.exit + } + return rv; +} + +int begin_command::execute() { + // todo -- parse end for redirection. + + int rv = vector_command::execute(); + + return rv; +} + +int if_command::execute() { + + int rv = 0; + bool ok = false; + // todo -- parse end for redirection. + for(auto &c : clauses) { + if (ok) continue; + ok = c->evaluate(); + if (!ok) continue; + rv = c->execute(); + } + return rv; +} + +/* +int if_else_clause::execute() { + return 0; +} +*/ + +bool if_else_clause::evaluate() { + return false; +} diff --git a/command.h b/command.h index 95e9171..eea4ec1 100644 --- a/command.h +++ b/command.h @@ -4,7 +4,8 @@ #include #include #include -#include "mpw-shell-grammar.h" +#include +#include "phase2-parser.h" typedef std::unique_ptr command_ptr; typedef std::vector command_ptr_vector; @@ -18,7 +19,7 @@ struct command { int type = 0; virtual ~command(); - virtual int execute(); + virtual int execute() = 0; }; struct simple_command : public command { @@ -27,6 +28,8 @@ struct simple_command : public command { {} std::string text; + + virtual int execute() final override; }; struct binary_command : public command { @@ -36,6 +39,7 @@ struct binary_command : public command { {} command_ptr_pair children; + }; struct or_command : public binary_command { @@ -43,19 +47,35 @@ struct or_command : public binary_command { binary_command(PIPE_PIPE, std::move(a), std::move(b)) {} + virtual int execute() final override; }; struct and_command : public binary_command { and_command(command_ptr &&a, command_ptr &&b) : binary_command(AMP_AMP, std::move(a), std::move(b)) {} + + virtual int execute() final override; }; + +#if 0 +struct pipe_command : public binary_command { + and_command(command_ptr &&a, command_ptr &&b) : + binary_command(PIPE, std::move(a), std::move(b)) + {} + + virtual int execute() final override; +}; +#endif + + struct vector_command : public command { vector_command(int t, command_ptr_vector &&v) : command(t), children(std::move(v)) {} command_ptr_vector children; + virtual int execute() override; }; struct begin_command : public vector_command { @@ -65,11 +85,14 @@ struct begin_command : public vector_command { {} std::string end; + + virtual int execute() final override; }; +typedef std::unique_ptr if_else_clause_ptr; struct if_command : public command { - typedef std::vector clause_vector_type; + typedef std::vector clause_vector_type; template if_command(clause_vector_type &&v, S &&s) : @@ -79,6 +102,8 @@ struct if_command : public command { clause_vector_type clauses; std::string end; + + virtual int execute() final override; }; struct if_else_clause : public vector_command { @@ -91,6 +116,7 @@ struct if_else_clause : public vector_command { std::string clause; bool evaluate(); + //virtual int execute() final override; }; #endif \ No newline at end of file