Added basic assembly compilation
This commit is contained in:
parent
ec82349ce2
commit
027f16d72b
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@ build/
|
|||||||
vgcore*
|
vgcore*
|
||||||
test.*
|
test.*
|
||||||
a.out
|
a.out
|
||||||
|
out.bin
|
@ -25,4 +25,13 @@ command_t *command_new(cmd_code_t type, int args[])
|
|||||||
cmd->arg_v[i] = args[i];
|
cmd->arg_v[i] = args[i];
|
||||||
|
|
||||||
return cmd;
|
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;
|
||||||
}
|
}
|
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "./utils.h"
|
||||||
|
|
||||||
enum command_e
|
enum command_e
|
||||||
{
|
{
|
||||||
PUSH,
|
PUSH,
|
||||||
@ -37,5 +39,6 @@ typedef struct
|
|||||||
} command_t;
|
} command_t;
|
||||||
|
|
||||||
command_t *command_new(cmd_code_t type, int args[]);
|
command_t *command_new(cmd_code_t type, int args[]);
|
||||||
|
cmd_desc_t *get_desc(char name[]);
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,5 +1,69 @@
|
|||||||
#include "./compile.h"
|
#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);
|
||||||
}
|
}
|
@ -1,5 +1,11 @@
|
|||||||
#ifndef COMPILE_H
|
#ifndef COMPILE_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "./utils.h"
|
||||||
|
#include "./command.h"
|
||||||
|
|
||||||
int compile(char input[], char output[]);
|
int compile(char input[], char output[]);
|
||||||
|
|
||||||
#endif
|
#endif
|
18
src/main.c
18
src/main.c
@ -2,24 +2,20 @@
|
|||||||
#include "./stack.h"
|
#include "./stack.h"
|
||||||
#include "./compile.h"
|
#include "./compile.h"
|
||||||
#include "./read_bin.h"
|
#include "./read_bin.h"
|
||||||
|
|
||||||
#ifdef IO_OPERATIONS
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void print_help()
|
void print_help()
|
||||||
{
|
{
|
||||||
printf("*HELP*\n");
|
printf("*HELP*\n");
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
{
|
{
|
||||||
#ifdef IO_OPERATIONS
|
|
||||||
fprintf(stderr, "Illegal input: command must be specified\n");
|
fprintf(stderr, "Illegal input: command must be specified\n");
|
||||||
print_help();
|
print_help();
|
||||||
#endif
|
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
if (strcmp(argv[1], "build") == 0)
|
if (strcmp(argv[1], "build") == 0)
|
||||||
@ -30,10 +26,9 @@ int main(int argc, char *argv[])
|
|||||||
compile(argv[2], argv[3]);
|
compile(argv[2], argv[3]);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
#ifdef IO_OPERATIONS
|
|
||||||
fprintf(stderr, "Illegal input for build:\n");
|
fprintf(stderr, "Illegal input for build:\n");
|
||||||
print_help();
|
print_help();
|
||||||
#endif
|
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,19 +44,17 @@ int main(int argc, char *argv[])
|
|||||||
stack = stack_new(1024, res);
|
stack = stack_new(1024, res);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
#ifdef IO_OPERATIONS
|
|
||||||
fprintf(stderr, "Illegal input for run:\n");
|
fprintf(stderr, "Illegal input for run:\n");
|
||||||
print_help();
|
print_help();
|
||||||
#endif
|
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res != NULL && *res == -1)
|
if (res != NULL && *res == -1)
|
||||||
{
|
{
|
||||||
#ifdef IO_OPERATIONS
|
|
||||||
fprintf(stderr, "Stack creation error: check memory usage.\n");
|
fprintf(stderr, "Stack creation error: check memory usage.\n");
|
||||||
print_help();
|
print_help();
|
||||||
#endif
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,10 +69,9 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
#ifdef IO_OPERATIONS
|
|
||||||
fprintf(stderr, "Illegal input: no such command\n");
|
fprintf(stderr, "Illegal input: no such command\n");
|
||||||
print_help();
|
print_help();
|
||||||
#endif
|
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
36
src/utils.c
36
src/utils.c
@ -7,5 +7,41 @@ int str_len(char str[])
|
|||||||
while (str[pos] != '\0')
|
while (str[pos] != '\0')
|
||||||
pos++;
|
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;
|
return pos;
|
||||||
}
|
}
|
@ -1,7 +1,10 @@
|
|||||||
#ifndef UTILS_H
|
#ifndef UTILS_H
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
int str_len(char str[]);
|
int str_len(char str[]);
|
||||||
|
int str_eq(char a[], char b[]);
|
||||||
|
int read_line(FILE *file, char *buff);
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
x
Reference in New Issue
Block a user