-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
98 lines (81 loc) · 2.56 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
main.c - Main file of brainfuck compiler
Copyright (C) 2022 kotleni
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <main.h>
#include <argsparser.h>
#if defined(__APPLE__)
#include <malloc/malloc.h>
#elif defined(__linux__)
#include <malloc.h>
#endif
// for result codes
int result;
int main(int argc, char *argv[]) {
Args* args = (Args*) malloc(sizeof(Args));
result = parse_args(args, argc, argv);
if(result != 0) {
printf("Arguments error!");
return result;
}
FILE* in_file = fopen(args->input_path, "r");
FILE* out_file = fopen("output.c", "w+");
printf("Generating sources...\n");
// add start of c code
fprintf(out_file, PROG_START);
char ch;
do {
ch = fgetc(in_file);
switch(ch) {
case '>': // next cell
fprintf(out_file, PROG_NEXT);
break;
case '<': // prev cell
fprintf(out_file, PROG_PREV);
break;
case '+': // inc cell
fprintf(out_file, PROG_ADD);
break;
case '-': // dec cell
fprintf(out_file, PROG_SUB);
break;
case '.': // out cell value
fprintf(out_file, PROG_OUT);
break;
case ',': // in value to cell
fprintf(out_file, PROG_IN);
break;
case '[': // start cycle
fprintf(out_file, PROG_CBEGIN);
break;
case ']': // end of cycle
fprintf(out_file, PROG_CEND);
break;
}
} while(ch != EOF);
// add end of c code
fprintf(out_file, PROG_END);
fclose(in_file);
fclose(out_file);
printf("Compiling sources...\n");
// make gcc command
char* cmd = (char*) malloc(sizeof(char) * 32);
sprintf(cmd, "gcc -o %s output.c", args->output_path);
// invoke compiler
result = system(cmd);
if(result != 0) {
printf("Compiling error!\n");
return result;
}
return 0;
}