-
Notifications
You must be signed in to change notification settings - Fork 2
/
erplag.c
132 lines (115 loc) · 3 KB
/
erplag.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "typeChecker.h"
#include "codegen.h"
#include "error.h"
FILE *fpOut ;
void checkCommand (int argc, char **argv, char **outputFile)
{
if (argc == 1)
{
errFatal () ;
printf ("No source file\n") ;
exit (1) ;
}
if (argc != 2 && argc != 3)
{
errFatal () ;
printf ("Incorrect number of arguments\n") ;
exit (1) ;
}
int len = strlen (argv[1]) ;
if (!(len > 4 && argv[1][len-1] == 'p' && argv[1][len-2] == 'r' && argv[1][len-3] == 'e' && argv[1][len-4] == '.'))
{
errFatal () ;
#if defined __linux__ || defined __MACH__
printf ("Unsupported source format. Use file with " ANSI_BOLD ".erp" ANSI_RESET " extension\n") ;
#endif
#ifdef _WIN64
printf ("Unsupported source format. Use file with .erp extension\n") ;
#endif
exit (1) ;
}
if (argc == 2)
{
*outputFile = (char *) malloc (sizeof(char) * (len + 1)) ;
strcpy (*outputFile, argv[1]) ;
(*outputFile)[len - 1] = 'm' ;
(*outputFile)[len - 2] = 's' ;
(*outputFile)[len - 3] = 'a' ;
}
else if (argc == 3)
{
len = strlen (argv[2]) ;
if (!(len > 4 && argv[2][len-1] == 'm' && argv[2][len-2] == 's' && argv[2][len-3] == 'a' && argv[2][len-4] == '.'))
{
errFatal () ;
#if defined __linux__ || defined __MACH__
printf ("Unsupported output format. Use " ANSI_BOLD ".asm" ANSI_RESET " extension\n") ;
#endif
#ifdef _WIN64
printf ("Unsupported output format. Use .asm extension\n") ;
#endif
exit (1) ;
}
*outputFile = argv[2] ;
}
}
int main(int argc, char **argv)
{
FILE *fpIn ;
char *outputFile ;
checkCommand (argc, argv, &outputFile) ;
if (!(fpIn = fopen (argv[1], "r")))
{
errFatal () ;
#if defined __linux__ || defined __MACH__
printf ("Source file " ANSI_BOLD "%s" ANSI_RESET " does not exist\n", argv[1]) ;
#endif
#ifdef _WIN64
printf ("Source file %s does not exist\n", argv[1]) ;
#endif
exit (1) ;
}
treeNode *root = parse (fpIn) ;
if (root->syntax_error)
exit (1) ;
astNode *astRoot ;
astRoot = applyASTRule (root) ;
deletePT (root) ;
realBase = fillSymbolTable (astRoot,0);
if (!realBase->semanticError)
{
if (realPresent)
{
errCodegen () ;
#if defined __linux__ || defined __MACH__
printf ("Variables or static values of type " ANSI_BOLD ANSI_GREEN "real" ANSI_RESET " found in source file, which is not handled by the present version of the compiler.\n") ;
#endif
#ifdef _WIN64
printf ("Variables or static values of type \"real\" found in source file, which is not handled by the present version of the compiler.\n") ;
#endif
exit (1) ;
}
int len ;
if (!(fpOut = fopen (outputFile, "w")))
{
errFatal () ;
#if defined __linux__ || defined __MACH__
printf ("Error creating asm file " ANSI_BOLD "%s" ANSI_RESET "\n", outputFile) ;
#endif
#ifdef _WIN64
printf ("Error creating asm file %s\n", outputFile) ;
#endif
exit (1) ;
}
preamble () ;
codeGeneration (astRoot) ;
postamble () ;
}
else
exit (1) ;
return 0;
}