Fixed errors

This commit is contained in:
Dmitriy Shishkov 2021-04-17 16:50:27 +05:00
parent 073adaa6cf
commit ec82349ce2
No known key found for this signature in database
GPG Key ID: 7CAE12ED13853CAC
7 changed files with 22 additions and 10 deletions

View File

@ -3,8 +3,9 @@ TARGET_EXEC ?= stack_vm
BUILD_DIR ?= ./build BUILD_DIR ?= ./build
SRC_DIRS ?= ./src SRC_DIRS ?= ./src
SRCS := $(shell find $(SRC_DIRS) -name *.c) SRCS := $(shell find $(SRC_DIRS) -name "*.c")
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
INC_DIRS := $(shell find $(SRC_DIRS) -type d) INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS)) INC_FLAGS := $(addprefix -I,$(INC_DIRS))
@ -12,6 +13,7 @@ INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP -D IO_OPERATIONS CPPFLAGS ?= $(INC_FLAGS) -MMD -MP -D IO_OPERATIONS
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS) $(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
echo $(SRC_DIRS)
$(CC) -ggdb3 $(OBJS) -o $@ $(LDFLAGS) $(CC) -ggdb3 $(OBJS) -o $@ $(LDFLAGS)
$(BUILD_DIR)/%.c.o: %.c $(BUILD_DIR)/%.c.o: %.c
@ -25,4 +27,6 @@ all: $(BUILD_DIR)/$(TARGET_EXEC)
clean: clean:
$(RM) -r $(BUILD_DIR) $(RM) -r $(BUILD_DIR)
-include $(DEPS)
MKDIR_P ?= mkdir -p MKDIR_P ?= mkdir -p

View File

@ -4,6 +4,7 @@ Stack based virtual machine and assembly code compiler
## Features ## Features
- Binary code execution (assembly language compilation coming soon)
- Assembly - Assembly
- Stack operations (push & pop) - Stack operations (push & pop)
- Arithmetics operations - Arithmetics operations
@ -36,7 +37,6 @@ Stack based virtual machine and assembly code compiler
## TODO ## TODO
- VM running
- Assembly compiler - Assembly compiler
- Variables - Variables
- Arithmetics operators - Arithmetics operators

View File

@ -67,7 +67,10 @@ int main(int argc, char *argv[])
command_t **commands = read_bin_file(argv[2], res); command_t **commands = read_bin_file(argv[2], res);
if (commands == NULL) if (commands == NULL)
{
fprintf(stderr, "Couldn't read file\n");
return 1; return 1;
}
return run(commands, stack); return run(commands, stack);
} }

View File

@ -9,7 +9,7 @@ command_t **read_bin_file(char src[], int *res)
int size = -1; int size = -1;
fread(&size, sizeof(int), 1, file); fread(&size, sizeof(int), 1, file);
command_t **buffer = malloc(sizeof(command_t *) * size); command_t **buffer = malloc(sizeof(command_t *) * (size + 1));
if (buffer == NULL) if (buffer == NULL)
{ {
*res = -1; *res = -1;
@ -20,11 +20,16 @@ command_t **read_bin_file(char src[], int *res)
{ {
buffer[i] = malloc(sizeof(command_t)); buffer[i] = malloc(sizeof(command_t));
fread(buffer[i]->code, sizeof(int), 1, file); fread(&buffer[i]->code, sizeof(int), 1, file);
buffer[i]->arg_v = malloc(sizeof(int) * cmd_desc[buffer[i]->code].argc); if (cmd_desc[buffer[i]->code].argc)
fread(buffer[i]->arg_v, sizeof(int), cmd_desc[buffer[i]->code].argc, file); {
buffer[i]->arg_v = malloc(sizeof(int) * cmd_desc[buffer[i]->code].argc);
fread(buffer[i]->arg_v, sizeof(int), cmd_desc[buffer[i]->code].argc, file);
}
} }
buffer[size] = NULL;
return buffer; return buffer;
} }

View File

@ -151,7 +151,7 @@ int exec(command_t cmd, stack_t *stack)
if (res != NULL) if (res != NULL)
return *res; return *res;
while (ch != NULL) while (ch != '\0')
{ {
putchar(ch); putchar(ch);
ch = stack_pop(stack, res); ch = stack_pop(stack, res);

View File

@ -6,14 +6,14 @@ stack_t *stack_new(int size, int *result)
if (stack == NULL) if (stack == NULL)
{ {
*result = -1; *result = -1;
return *result; return NULL;
} }
stack->mem = (int *)malloc(sizeof(int) * size); stack->mem = (int *)malloc(sizeof(int) * size);
if (stack->mem == NULL) if (stack->mem == NULL)
{ {
*result = -1; *result = -1;
return *result; return NULL;
} }
stack->size = size; stack->size = size;

View File

@ -4,7 +4,7 @@ int str_len(char str[])
{ {
int pos = 0; int pos = 0;
while (str[pos] != NULL) while (str[pos] != '\0')
pos++; pos++;
return pos; return pos;