commit 65f6248493eb9a3e9514a7e509572837d633983f Author: dm1sh Date: Fri Apr 16 18:24:34 2021 +0500 Created basic makefile, vm command runner and stack 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