Added push and pop commands

This commit is contained in:
Dmitriy Shishkov 2021-04-16 20:33:24 +05:00
parent 253bc13ce3
commit 0e5fa6c854
No known key found for this signature in database
GPG Key ID: 7CAE12ED13853CAC
6 changed files with 43 additions and 8 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
build/ build/
.vscode/

View File

@ -18,6 +18,9 @@ run.o:
stack.o: stack.o:
$(CC) -c stack.c $(CFLAG) -o $(BUILD_DIR)/$@ $(CC) -c stack.c $(CFLAG) -o $(BUILD_DIR)/$@
command.o:
$(CC) -c command.c $(CFLAG) -o $(BUILD_DIR)/$@
clear: clear:
rm -rf $(BUILD_DIR) rm -rf $(BUILD_DIR)

6
command.c Normal file
View File

@ -0,0 +1,6 @@
#include "./command.h"
cmd_desc_t cmd_desc[] = {
{PUSH, 1, "PUSH"},
{POP, 0, "POP"},
{NONE, 0, "NONE"}}

View File

@ -3,9 +3,26 @@
enum command_e enum command_e
{ {
PUSH,
POP,
NONE 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 #endif

16
run.c
View File

@ -1,22 +1,30 @@
#include "./run.h" #include "./run.h"
int run(command_t *buff, stack_t stack) int run(command_t **buff, stack_t *stack)
{ {
int pos = 0; int pos = 0;
int res = 0; int res = 0;
while (buff[pos] != NULL) while (buff[pos] != NULL)
{ {
res = exec(buff[pos], stack); res = exec(*(buff[pos]), stack);
if (res)
break;
} }
return res; 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: case NONE:
break; break;
default: default:

4
run.h
View File

@ -4,7 +4,7 @@
#include "./stack.h" #include "./stack.h"
#include "./command.h" #include "./command.h"
int run(command_t *buff, stack_t stack); int run(command_t **buff, stack_t *stack);
int exec(command_t cmd, stack_t stack); int exec(command_t cmd, stack_t *stack);
#endif #endif