Created basic makefile, vm command runner and stack

This commit is contained in:
Dmitriy Shishkov 2021-04-16 18:24:34 +05:00
commit 65f6248493
No known key found for this signature in database
GPG Key ID: 7CAE12ED13853CAC
8 changed files with 144 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build/

24
Makefile Normal file
View File

@ -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

11
command.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef COMMAND_H
#define COMMAND_H
enum command_e
{
NONE
};
typedef enum command_e command_t;
#endif

6
main.c Normal file
View File

@ -0,0 +1,6 @@
#include "./run.h"
#include "./stack.h"
int main()
{
}

27
run.c Normal file
View File

@ -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;
}

10
run.h Normal file
View File

@ -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

47
stack.c Normal file
View File

@ -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];
}

18
stack.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef STACK_H
#define STACK_H
#include <stdlib.h>
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