From 65f6248493eb9a3e9514a7e509572837d633983f Mon Sep 17 00:00:00 2001 From: dm1sh Date: Fri, 16 Apr 2021 18:24:34 +0500 Subject: [PATCH] Created basic makefile, vm command runner and stack --- .gitignore | 1 + Makefile | 24 ++++++++++++++++++++++++ command.h | 11 +++++++++++ main.c | 6 ++++++ run.c | 27 +++++++++++++++++++++++++++ run.h | 10 ++++++++++ stack.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ stack.h | 18 ++++++++++++++++++ 8 files changed, 144 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 command.h create mode 100644 main.c create mode 100644 run.c create mode 100644 run.h create mode 100644 stack.c create mode 100644 stack.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d163863 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a4948e8 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +CFLAG=-Wall +BUILD_DIR=build + +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)/$@ + +main.o: + $(CC) -c main.c $(CFLAG) -o $(BUILD_DIR)/$@ + +run.o: + $(CC) -c run.c $(CFLAG) -o $(BUILD_DIR)/$@ + +stack.o: + $(CC) -c stack.c $(CFLAG) -o $(BUILD_DIR)/$@ + +clear: + rm -rf $(BUILD_DIR) + +.PHONY: all clear \ No newline at end of file diff --git a/command.h b/command.h new file mode 100644 index 0000000..d404053 --- /dev/null +++ b/command.h @@ -0,0 +1,11 @@ +#ifndef COMMAND_H +#define COMMAND_H + +enum command_e +{ + NONE +}; + +typedef enum command_e command_t; + +#endif \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..78fb974 --- /dev/null +++ b/main.c @@ -0,0 +1,6 @@ +#include "./run.h" +#include "./stack.h" + +int main() +{ +} \ No newline at end of file diff --git a/run.c b/run.c new file mode 100644 index 0000000..875d331 --- /dev/null +++ b/run.c @@ -0,0 +1,27 @@ +#include "./run.h" + +int run(command_t *buff, stack_t stack) +{ + int pos = 0; + int res = 0; + + while (buff[pos] != NULL) + { + res = exec(buff[pos], stack); + } + + return res; +} + +int exec(command_t cmd, stack_t stack) +{ + switch (cmd) + { + case NONE: + break; + default: + return -1; + } + + return 0; +} \ No newline at end of file diff --git a/run.h b/run.h new file mode 100644 index 0000000..8d056f2 --- /dev/null +++ b/run.h @@ -0,0 +1,10 @@ +#ifndef RUN_H +#define RUN_H + +#include "./stack.h" +#include "./command.h" + +int run(command_t *buff, stack_t stack); +int exec(command_t cmd, stack_t stack); + +#endif \ No newline at end of file diff --git a/stack.c b/stack.c new file mode 100644 index 0000000..1fb05fa --- /dev/null +++ b/stack.c @@ -0,0 +1,47 @@ +#include "./stack.h" + +stack_t *stack_new(int size) +{ + stack_t *stack = (stack_t *)malloc(sizeof(stack_t)); + if (stack == NULL) + return -1; + + stack->mem = (int *)malloc(sizeof(int) * size); + if (stack->mem == NULL) + return -1; + + stack->size = size; + stack->cursor = -1; + + return stack; +} + +int stack_push(stack_t *stack, int value) +{ + if (stack->cursor > stack->size - 1) + return 1; + + stack->mem[stack->cursor + 1] = value; + stack->cursor += 1; + + return 0; +} + +int stack_pop(stack_t *stack) +{ + if (stack->cursor == -1) + return 1; + + int value = stack->mem[stack->cursor]; + stack->cursor -= 1; + + return value; +} + +int stack_get(stack_t *stack, int pos) +{ + if (pos < 0 || pos >= stack->size) + return 1; + + return stack->mem[pos]; +} \ No newline at end of file diff --git a/stack.h b/stack.h new file mode 100644 index 0000000..408c658 --- /dev/null +++ b/stack.h @@ -0,0 +1,18 @@ +#ifndef STACK_H +#define STACK_H + +#include + +typedef struct stack +{ + int size; + int *mem; + int cursor; +} stack_t; + +stack_t *stack_new(int size); +int stack_push(stack_t *stack, int value); +int stack_pop(stack_t *stack); +int stack_get(stack_t *stack, int pos); + +#endif \ No newline at end of file