From 027f16d72b324f34f1a9e061b24a37ae4a918c1e Mon Sep 17 00:00:00 2001 From: dm1sh Date: Sat, 17 Apr 2021 18:28:18 +0500 Subject: [PATCH] Added basic assembly compilation --- .gitignore | 1 + src/command.c | 9 +++++++ src/command.h | 3 +++ src/compile.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/compile.h | 6 +++++ src/main.c | 18 ++++---------- src/utils.c | 36 ++++++++++++++++++++++++++++ src/utils.h | 3 +++ 8 files changed, 128 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index d35d3fa..5f210e0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build/ vgcore* test.* a.out +out.bin \ No newline at end of file diff --git a/src/command.c b/src/command.c index b11c784..dfc9144 100644 --- a/src/command.c +++ b/src/command.c @@ -25,4 +25,13 @@ command_t *command_new(cmd_code_t type, int args[]) cmd->arg_v[i] = args[i]; return cmd; +} + +cmd_desc_t *get_desc(char name[]) +{ + for (int i = 0; i < sizeof(cmd_desc) / sizeof(cmd_desc_t); i++) + if (str_eq(name, cmd_desc[i].name)) + return &cmd_desc[i]; + + return NULL; } \ No newline at end of file diff --git a/src/command.h b/src/command.h index 7c16606..42492c7 100644 --- a/src/command.h +++ b/src/command.h @@ -3,6 +3,8 @@ #include +#include "./utils.h" + enum command_e { PUSH, @@ -37,5 +39,6 @@ typedef struct } command_t; command_t *command_new(cmd_code_t type, int args[]); +cmd_desc_t *get_desc(char name[]); #endif \ No newline at end of file diff --git a/src/compile.c b/src/compile.c index 40bf4d3..0b3ac21 100644 --- a/src/compile.c +++ b/src/compile.c @@ -1,5 +1,69 @@ #include "./compile.h" -int compile(char input[], char output[]) +int compile(char input_src[], char output_src[]) { + FILE *input = fopen(input_src, "r"); + FILE *output = fopen(output_src, "wb"); + if (input == NULL) + { + fprintf(stderr, "Unable to open input file\n"); + return 1; + } + if (output == NULL) + { + fprintf(stderr, "Unable to open output file\n"); + return -1; + } + + int size = -1; + fwrite(&size, sizeof(int), 1, output); + size++; + + char buffer[512]; + int len = 0; + + while ((len = read_line(input, buffer)), len >= 0) + { + if (len == 0) + continue; + + char *pch = strtok(buffer, " "); + + cmd_desc_t *description = get_desc(pch); + if (description == NULL) + { + fprintf(stderr, "No such command %s\n", pch); + return 1; + } + + fwrite(&description->command, sizeof(int), 1, output); + + for (int i = 0; i < description->argc; i++) + { + pch = strtok(NULL, " "); + if (pch == NULL) + { + fprintf(stderr, "Not enough arguments for %s\n", description->name); + return 1; + } + + int arg = atoi(pch); + + if (arg == 0 && pch[0] != '0') + { + fprintf(stderr, "Unsupported argument for %s\n", description->name); + return 1; + } + + fwrite(&arg, sizeof(int), 1, output); + } + + size++; + } + + fseek(output, 0, SEEK_SET); + fwrite(&size, sizeof(int), 1, output); + + fclose(input); + fclose(output); } \ No newline at end of file diff --git a/src/compile.h b/src/compile.h index d85c224..b2593ca 100644 --- a/src/compile.h +++ b/src/compile.h @@ -1,5 +1,11 @@ #ifndef COMPILE_H +#include +#include + +#include "./utils.h" +#include "./command.h" + int compile(char input[], char output[]); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index f47155d..42d6145 100644 --- a/src/main.c +++ b/src/main.c @@ -2,24 +2,20 @@ #include "./stack.h" #include "./compile.h" #include "./read_bin.h" - -#ifdef IO_OPERATIONS #include 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) @@ -30,10 +26,9 @@ int main(int argc, char *argv[]) compile(argv[2], argv[3]); else { -#ifdef IO_OPERATIONS fprintf(stderr, "Illegal input for build:\n"); print_help(); -#endif + return 2; } } @@ -49,19 +44,17 @@ int main(int argc, char *argv[]) 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; } @@ -76,10 +69,9 @@ int main(int argc, char *argv[]) } else { -#ifdef IO_OPERATIONS fprintf(stderr, "Illegal input: no such command\n"); print_help(); -#endif + return 2; } } diff --git a/src/utils.c b/src/utils.c index c8eb7bb..0122f89 100644 --- a/src/utils.c +++ b/src/utils.c @@ -7,5 +7,41 @@ int str_len(char str[]) while (str[pos] != '\0') pos++; + return pos; +} + +int str_eq(char a[], char b[]) +{ + int i = 0; + while (a[i] != '\0' && b[i] != '\0') + { + if (a[i] != b[i]) + return 0; + + i++; + } + + if (a[i] == '\0' && b[i] == '\0') + return 1; + else + return 0; +} + +int read_line(FILE *file, char *buff) +{ + int pos = -1; + char ch; + + while ((ch = getc(file)), ch != EOF && ch != '\n') + { + if (pos == -1) + pos = 0; + + buff[pos] = ch; + pos++; + } + + buff[pos] = '\0'; + return pos; } \ No newline at end of file diff --git a/src/utils.h b/src/utils.h index a733f8b..6000f3b 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,7 +1,10 @@ #ifndef UTILS_H #include +#include int str_len(char str[]); +int str_eq(char a[], char b[]); +int read_line(FILE *file, char *buff); #endif \ No newline at end of file