-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
108 lines (87 loc) · 2.48 KB
/
main.c
File metadata and controls
108 lines (87 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/tokens/tokens.h"
#include "include/lexer/lexer.h"
#include "include/parser/ast.h"
#include "include/parser/parser.h"
#include "include/asm/asm.h"
#include "include/context/context.h"
#define FLAG 0
#define MAX_LINE_LENGTH 1024
void usage(char *programName)
{
printf("Usage: %s [options]\n", programName);
printf("Options:\n");
printf(" -f <inputfile.xt> provide file to compiler\n");
printf(" -o <outputfile.o> provide file to compiler\n");
// Add more options as needed
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
/* if args not provide show usage menu*/
if (argc < 2)
{
usage(argv[0]);
}
char *inputfile;
char *outputfile;
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-f") == 0 && i + 1 < argc)
{
inputfile = argv[i + 1];
i++;
// Skip the next argument
}
else if (strcmp(argv[i], "-o") == 0 && i + 1 < argc)
{
outputfile = argv[i + 1];
i++;
}
else
{
printf("Unknown option: %s\n", argv[i]);
usage(argv[0]);
}
}
int tokenCount = 0;
// Open file for reading
Context *context = initContext();
context->ip = fopen(inputfile, "r");
if (context->ip == NULL)
{
printf("\nERROR: %s file not found\n", inputfile);
exit(1);
}
// printTokens(tokens, tokenCount);
#ifdef FLAG
SymbolTable *symbolTable = initSymbolTable();
pushSymbolTable(context->symbolTableStack, symbolTable);
FILE *op;
char *asmFile = malloc(50 * sizeof(char));
snprintf(asmFile, 50, "%s.asm", outputfile);
op = fopen(asmFile, "w+");
parseProgram(context, op);
fclose(op);
char *asmCommand = malloc(50 * sizeof(char));
char *linkCommand = malloc(50 * sizeof(char));
// Execute the command using system()
snprintf(asmCommand, 50, "nasm -f elf32 %s.asm -o %s.o", outputfile, outputfile);
snprintf(linkCommand, 50, "ld -m elf_i386 -o %s %s.o", outputfile, outputfile);
int asmResult = system(asmCommand);
int linkResult = system(linkCommand);
if (asmResult != 0)
{
fprintf(stderr, "Assembly failed with error code %d\n", asmResult);
return 1;
}
if (linkResult != 0)
{
fprintf(stderr, "Linking failed with error code %d\n", linkResult);
return 1;
}
#endif
return 0;
}