-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.c
54 lines (48 loc) · 1.54 KB
/
compiler.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
#include "compiler.h"
#include <stdarg.h>
#include <stdlib.h>
struct lex_process_functions compiler_lex_functions = {
.next_char = compile_process_next_char,
.peek_char = compile_process_peek_char,
.push_char = compile_process_push_char};
void compiler_error(struct compile_process *compiler, const char *msg, ...)
{
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, " on line %i, col %i in file %s\n", compiler->pos.line, compiler->pos.col, compiler->pos.filename);
exit(-1);
}
void compiler_warning(struct compile_process *compiler, const char *msg, ...)
{
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, " on line %i, col %i in file %s\n", compiler->pos.line, compiler->pos.col, compiler->pos.filename);
}
int compile_file(const char *filename, const char *out_filename, int flags)
{
struct compile_process *process = compiler_process_create(filename, out_filename, flags);
if (!process)
return COMPILER_FAILED_WITH_ERRORS;
// Perform lexical analysis
struct lex_process *lex_process = lex_process_create(process, &compiler_lex_functions, NULL);
if (!lex_process)
{
return COMPILER_FAILED_WITH_ERRORS;
}
if (lex(lex_process) != LEXICAL_ANALYSIS_ALL_OK)
{
return COMPILER_FAILED_WITH_ERRORS;
}
process->token_vec = lex_process->token_vec;
// Perform parsing
if (parse(process) != PARSE_ALL_OK)
{
return COMPILER_FAILED_WITH_ERRORS;
}
// Perform code generation..
return COMPILER_FILE_COMPILED_OK;
}