From e59754b20611f41b1cca40159fcf2d67f0009989 Mon Sep 17 00:00:00 2001 From: dm1sh Date: Fri, 16 Apr 2021 23:53:23 +0500 Subject: [PATCH] Created function for command struct initialization, fixed a couple of issues --- .gitignore | 3 ++- Makefile | 10 +++++----- command.c | 15 ++++++++++++++- command.h | 8 ++++++-- run.c | 5 +++-- stack.c | 8 -------- 6 files changed, 30 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 01f9cb9..b969127 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ -.vscode/ \ No newline at end of file +.vscode/ +vgcore* \ No newline at end of file diff --git a/Makefile b/Makefile index cf6e3bb..045e399 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAG=-Wall +CFLAG=-Wall -ggdb3 BUILD_DIR=build all: build_dir stack_vm @@ -6,8 +6,8 @@ all: build_dir stack_vm build_dir: mkdir -p $(BUILD_DIR) -stack_vm: main.o run.o stack.o - $(CC) $(BUILD_DIR)/main.o $(BUILD_DIR)/run.o $(BUILD_DIR)/stack.o -o $(BUILD_DIR)/$@ +stack_vm: main.o run.o stack.o command.o + $(CC) $(BUILD_DIR)/main.o $(BUILD_DIR)/run.o $(BUILD_DIR)/stack.o $(BUILD_DIR)/command.o -o $(BUILD_DIR)/$@ main.o: $(CC) -c main.c $(CFLAG) -o $(BUILD_DIR)/$@ @@ -21,7 +21,7 @@ stack.o: command.o: $(CC) -c command.c $(CFLAG) -o $(BUILD_DIR)/$@ -clear: +clean: rm -rf $(BUILD_DIR) -.PHONY: all clear \ No newline at end of file +.PHONY: all clean \ No newline at end of file diff --git a/command.c b/command.c index f364f6c..b11c784 100644 --- a/command.c +++ b/command.c @@ -12,4 +12,17 @@ cmd_desc_t cmd_desc[] = { {IN, 0, "IN"}, {OUT, 0, "OUT"}, #endif - {NONE, 0, "NONE"}} + {NONE, 0, "NONE"}}; + +command_t *command_new(cmd_code_t type, int args[]) +{ + command_t *cmd = (command_t *)malloc(sizeof(command_t)); + + cmd->code = type; + cmd->arg_v = malloc(sizeof(int) * cmd_desc[type].argc); + + for (int i = 0; i < cmd_desc[type].argc; i++) + cmd->arg_v[i] = args[i]; + + return cmd; +} \ No newline at end of file diff --git a/command.h b/command.h index 1b0738c..7c16606 100644 --- a/command.h +++ b/command.h @@ -1,6 +1,8 @@ #ifndef COMMAND_H #define COMMAND_H +#include + enum command_e { PUSH, @@ -26,12 +28,14 @@ typedef struct char name[10]; } cmd_desc_t; -cmd_desc_t cmd_desc[]; +extern cmd_desc_t cmd_desc[]; typedef struct { cmd_code_t code; - int args[]; + int *arg_v; } command_t; +command_t *command_new(cmd_code_t type, int args[]); + #endif \ No newline at end of file diff --git a/run.c b/run.c index e843d1d..40b503b 100644 --- a/run.c +++ b/run.c @@ -10,6 +10,7 @@ int run(command_t **buff, stack_t *stack) res = exec(*(buff[pos]), stack); if (res) break; + pos++; } return res; @@ -17,7 +18,7 @@ int run(command_t **buff, stack_t *stack) int exec(command_t cmd, stack_t *stack) { - int *res; + int *res = NULL; switch (cmd.code) { @@ -31,7 +32,7 @@ int exec(command_t cmd, stack_t *stack) */ case PUSH: { - stack_push(stack, cmd.args[0], res); + stack_push(stack, cmd.arg_v[0], res); if (res != NULL) return *res; diff --git a/stack.c b/stack.c index 3c72d6c..e5713f3 100644 --- a/stack.c +++ b/stack.c @@ -2,8 +2,6 @@ stack_t *stack_new(int size, int *result) { - result = NULL; - stack_t *stack = (stack_t *)malloc(sizeof(stack_t)); if (stack == NULL) { @@ -26,8 +24,6 @@ stack_t *stack_new(int size, int *result) int stack_push(stack_t *stack, int value, int *result) { - result = NULL; - if (stack->cursor > stack->size - 1) { *result = 2; @@ -42,8 +38,6 @@ int stack_push(stack_t *stack, int value, int *result) int stack_pop(stack_t *stack, int *result) { - result = NULL; - if (stack->cursor == -1) { *result = 1; @@ -58,8 +52,6 @@ int stack_pop(stack_t *stack, int *result) int stack_get(stack_t *stack, int pos, int *result) { - result = NULL; - if (pos < 0 || pos >= stack->size) { *result = 1;