-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbcc.cpp
More file actions
86 lines (74 loc) · 2.26 KB
/
bcc.cpp
File metadata and controls
86 lines (74 loc) · 2.26 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
// For printf and file used below
#include <inttypes.h>
#include <stdlib.h>
#include <iostream>
#include <BCC.h>
char program[BCC_MAX_PROGRAM_SIZE];
BCC compiler;
void error_callback(uint16_t line, const char *position, const char *string) {
printf("| State | \033[31mError: %s Line: %d", string, line);
if(position != NULL) {
printf("\nCode: ");
for(uint16_t i = 0; *position != 0 && *position != BP_LF; position++)
printf("%c", *position);
}
printf("\033[m\n\n");
exit(EXIT_FAILURE);
};
int main(int argc, char* argv[]) {
printf("\n| BCC | BIP Compiler Collection by Giovanni Blu Mitolo\n");
FILE * p_file;
long p_size;
size_t result;
// Open file
p_file = fopen(argv[1], "r");
if(p_file == NULL) {
printf("| State | \033[31mError: Unable to open source file.\033[m\n\n");
exit(2);
}
// Obtain file size:
fseek(p_file, 0, SEEK_END);
p_size = ftell(p_file);
rewind(p_file);
if((sizeof(char) * p_size) >= BCC_MAX_PROGRAM_SIZE) {
printf("| State | \033[31mError: ");
printf(
"Source too big, BCC_MAX_PROGRAM_SIZE is set to %dB.\033[m\n\n",
BCC_MAX_PROGRAM_SIZE
);
exit(3);
}
// Copy the file into the buffer:
result = fread(program, 1, p_size, p_file);
if(result != p_size) {
printf("| State | \033[31mError: Unable to read source file.\033[m\n\n");
exit(4);
}
fclose(p_file);
printf("| Source | %s (%ldB)\n", argv[1], p_size);
// Open target file
FILE *o_file = fopen(argv[2], "w");
if(o_file == NULL) {
printf("| State | \033[31mError: Unable to open the target file.\033[m\n\n");
exit(5);
}
// Compile program
compiler.error_callback = error_callback;
uint32_t t = BPM_MICROS();
if(!compiler.run(program)) {
printf("| State | \033[31mError: Compilation failed.\033[m\n\n");
exit(EXIT_FAILURE);
}
t = BPM_MICROS() - t;
// Save program in target file
fwrite(program, sizeof(char), strlen(program), o_file);
// Obtain file size:
fseek(o_file, 0, SEEK_END);
int o_size = ftell(o_file);
rewind(o_file);
printf("| Target | %s (%dB)\n", argv[2], o_size);
printf("| Duration | %.2f milliseconds \n", (float)(t) / 1000);
fclose(o_file);
printf("| State | \033[32mSuccess\033[m\n\n");
exit(EXIT_SUCCESS);
};