Added cli interface for vm, and bin file running

This commit is contained in:
Dmitriy Shishkov 2021-04-17 10:02:30 +05:00
parent ecbd89c1dd
commit 073adaa6cf
No known key found for this signature in database
GPG Key ID: 7CAE12ED13853CAC
7 changed files with 131 additions and 4 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
build/
.vscode/
vgcore*
test.*
test.*
a.out

View File

@ -9,7 +9,7 @@ OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP -D IO_OPERATIONS
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
$(CC) -ggdb3 $(OBJS) -o $@ $(LDFLAGS)

5
src/compile.c Normal file
View File

@ -0,0 +1,5 @@
#include "./compile.h"
int compile(char input[], char output[])
{
}

5
src/compile.h Normal file
View File

@ -0,0 +1,5 @@
#ifndef COMPILE_H
int compile(char input[], char output[]);
#endif

View File

@ -1,6 +1,82 @@
#include "./run.h"
#include "./stack.h"
#include "./compile.h"
#include "./read_bin.h"
int main()
#ifdef IO_OPERATIONS
#include <stdio.h>
void print_help()
{
}
printf("*HELP*\n");
}
#endif
int main(int argc, char *argv[])
{
if (argc < 2)
{
#ifdef IO_OPERATIONS
fprintf(stderr, "Illegal input: command must be specified\n");
print_help();
#endif
return 2;
}
if (strcmp(argv[1], "build") == 0)
{
if (argc == 3)
compile(argv[2], "out.bin");
else if (argc == 4)
compile(argv[2], argv[3]);
else
{
#ifdef IO_OPERATIONS
fprintf(stderr, "Illegal input for build:\n");
print_help();
#endif
return 2;
}
}
else if (strcmp(argv[1], "run") == 0)
{
stack_t *stack;
int *res = NULL;
if (argc == 4)
stack = stack_new(atoi(argv[3]), res);
else if (argc == 3)
stack = stack_new(1024, res);
else
{
#ifdef IO_OPERATIONS
fprintf(stderr, "Illegal input for run:\n");
print_help();
#endif
return 2;
}
if (res != NULL && *res == -1)
{
#ifdef IO_OPERATIONS
fprintf(stderr, "Stack creation error: check memory usage.\n");
print_help();
#endif
return 1;
}
command_t **commands = read_bin_file(argv[2], res);
if (commands == NULL)
return 1;
return run(commands, stack);
}
else
{
#ifdef IO_OPERATIONS
fprintf(stderr, "Illegal input: no such command\n");
print_help();
#endif
return 2;
}
}

30
src/read_bin.c Normal file
View File

@ -0,0 +1,30 @@
#include "./read_bin.h"
command_t **read_bin_file(char src[], int *res)
{
FILE *file = fopen(src, "rb");
if (file == NULL)
return NULL;
int size = -1;
fread(&size, sizeof(int), 1, file);
command_t **buffer = malloc(sizeof(command_t *) * size);
if (buffer == NULL)
{
*res = -1;
return NULL;
}
for (int i = 0; i < size; i++)
{
buffer[i] = malloc(sizeof(command_t));
fread(buffer[i]->code, sizeof(int), 1, 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);
}
return buffer;
}

10
src/read_bin.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef READ_BIN_H
#include <stdio.h>
#include <string.h>
#include "./command.h"
command_t **read_bin_file(char src[], int *res);
#endif