Added basic assembly compilation

This commit is contained in:
Dmitriy Shishkov 2021-04-17 18:28:18 +05:00
parent ec82349ce2
commit 027f16d72b
No known key found for this signature in database
GPG Key ID: 7CAE12ED13853CAC
8 changed files with 128 additions and 14 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ build/
vgcore*
test.*
a.out
out.bin

View File

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

View File

@ -3,6 +3,8 @@
#include <stdlib.h>
#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

View File

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

View File

@ -1,5 +1,11 @@
#ifndef COMPILE_H
#include <stdio.h>
#include <string.h>
#include "./utils.h"
#include "./command.h"
int compile(char input[], char output[]);
#endif

View File

@ -2,24 +2,20 @@
#include "./stack.h"
#include "./compile.h"
#include "./read_bin.h"
#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)
@ -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;
}
}

View File

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

View File

@ -1,7 +1,10 @@
#ifndef UTILS_H
#include <stdlib.h>
#include <stdio.h>
int str_len(char str[]);
int str_eq(char a[], char b[]);
int read_line(FILE *file, char *buff);
#endif