-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
83 lines (80 loc) · 2.1 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include "./util/options.h"
#include "./util/token.h"
#include "./util/AST.h"
#include "./util/symbol.h"
#include "./backend/built-in-functions.h"
#include "y.tab.h"
// The current line number.
// This is from tokenizer.l.
extern int lineno;
extern FILE *yyin;
// Current token's lexeme
extern char *yytext;
// The source file name
extern char *currentFileName;
// From lex.yy.c, returns the next token.
// Ends with EOF
int yylex();
// Parses the source (from parser.y)
int yyparse();
// Prints all tokens frrom yylex()
void testTokenizer()
{
int token;
printf(ANSI_COLOR_MAGENTA ANSI_COLOR_BOLD "%-10s\t\t %-18s\t\t %-18s\t\n" ANSI_COLOR_RESET, "Line number", "Lexeme", "Token Type");
while ((token = yylex()) != EOF_TOKEN && token != 0)
{
// printf("%s: %s = %f\n", tokenStr(token), yytext, yylval.fValue);
printToken(token);
}
printf("%-10d\t\t %-18s\t\t %-18s\t\n", lineno, "EOF", "EOF");
}
/**
* @brief Run the compiler
*
* This calls `yyparse()` which calls `backend` (backend/backend.c) when the start
* production rule is reduced.
*
* The AST is passed to `backend` which generates the IR (as three address code)
* and then calls the `VM` (virtual machine) to execute the IR.
*
* @param argc
* @param argv
* @return int
*/
int main(int argc, char *argv[])
{
// Options like --dump-ast are parsed and flags set here
char *filename = parseOptions(argc, argv);
// Open fresh
yyin = fopen(filename, "r");
if (!yyin)
{
fprintf(stderr, "Failed to open file %s\n", filename);
return 1;
}
currentFileName = filename;
if (dumpTokens)
testTokenizer();
// On testing the tokenizer the file is read till the end,
// so re-initialize the file pointer.
yyin = fopen(filename, "r");
lineno = 1;
// Starting the parser
// Initialise the symbol tables
initSymbolTable();
// Add the built in functions
// to the global symbol table
installBuiltInFunctions(globals);
// Run the parser. All further operations will be
// from parser.y
int error = yyparse();
if (error)
{
return 1;
}
// printf("Successful\n");
return 0;
}