From 0e5fa6c85457e43b12b8197bcdadb95d99213d5a Mon Sep 17 00:00:00 2001 From: dm1sh Date: Fri, 16 Apr 2021 20:33:24 +0500 Subject: [PATCH] Added push and pop commands --- .gitignore | 3 ++- Makefile | 3 +++ command.c | 6 ++++++ command.h | 19 ++++++++++++++++++- run.c | 16 ++++++++++++---- run.h | 4 ++-- 6 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 command.c diff --git a/.gitignore b/.gitignore index d163863..01f9cb9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -build/ \ No newline at end of file +build/ +.vscode/ \ No newline at end of file diff --git a/Makefile b/Makefile index a4948e8..cf6e3bb 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,9 @@ run.o: stack.o: $(CC) -c stack.c $(CFLAG) -o $(BUILD_DIR)/$@ +command.o: + $(CC) -c command.c $(CFLAG) -o $(BUILD_DIR)/$@ + clear: rm -rf $(BUILD_DIR) diff --git a/command.c b/command.c new file mode 100644 index 0000000..db93a02 --- /dev/null +++ b/command.c @@ -0,0 +1,6 @@ +#include "./command.h" + +cmd_desc_t cmd_desc[] = { + {PUSH, 1, "PUSH"}, + {POP, 0, "POP"}, + {NONE, 0, "NONE"}} diff --git a/command.h b/command.h index d404053..096839e 100644 --- a/command.h +++ b/command.h @@ -3,9 +3,26 @@ enum command_e { + PUSH, + POP, NONE }; -typedef enum command_e command_t; +typedef enum command_e cmd_code_t; + +typedef struct +{ + cmd_code_t command; + int argc; + char name[10]; +} cmd_desc_t; + +cmd_desc_t cmd_desc[]; + +typedef struct +{ + cmd_code_t code; + int args[]; +} command_t; #endif \ No newline at end of file diff --git a/run.c b/run.c index 875d331..0a2e418 100644 --- a/run.c +++ b/run.c @@ -1,22 +1,30 @@ #include "./run.h" -int run(command_t *buff, stack_t stack) +int run(command_t **buff, stack_t *stack) { int pos = 0; int res = 0; while (buff[pos] != NULL) { - res = exec(buff[pos], stack); + res = exec(*(buff[pos]), stack); + if (res) + break; } return res; } -int exec(command_t cmd, stack_t stack) +int exec(command_t cmd, stack_t *stack) { - switch (cmd) + switch (cmd.code) { + case PUSH: + stack_push(stack, cmd.args[0]); + break; + case POP: + stack_pop(stack); + break; case NONE: break; default: diff --git a/run.h b/run.h index 8d056f2..7ee3f5f 100644 --- a/run.h +++ b/run.h @@ -4,7 +4,7 @@ #include "./stack.h" #include "./command.h" -int run(command_t *buff, stack_t stack); -int exec(command_t cmd, stack_t stack); +int run(command_t **buff, stack_t *stack); +int exec(command_t cmd, stack_t *stack); #endif \ No newline at end of file