-
Notifications
You must be signed in to change notification settings - Fork 0
/
transpiler.c
150 lines (132 loc) · 3.6 KB
/
transpiler.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "eli.h"
#include "parser.h"
// Tables of instruction symbols for each supported language.
const char brainfuck_symbols[8] = {'+', '-', '.', ',', '>', '<', '[', ']'};
const char ook_symbols_f[8] = {'.', '!', '!', '.', '.', '?', '!', '?'};
const char ook_symbols_s[8] = {'.', '!', '.', '!', '?', '.', '?', '!'};
// Generate Ook! code and send it into given file.
void generateOok(Instruction* inst, FILE* out) {
while (*inst != EXIT) {
fprintf(out, "Ook%c Ook%c ", ook_symbols_f[*inst], ook_symbols_s[*inst]);
inst++;
}
fprintf(out, "\n");
}
// Generate compressed Ook! code and send it into given file.
void generateCompressedOok(Instruction* inst, FILE* out) {
while (*inst != EXIT) {
fprintf(out, "%c%c ", ook_symbols_f[*inst], ook_symbols_s[*inst]);
inst++;
}
fprintf(out, "\n");
}
// Generate brainf*ck code and send it into given file.
void generateBrainfuck(Instruction* inst, FILE* out) {
while (*inst != EXIT) {
fprintf(out, "%c", brainfuck_symbols[*inst]);
inst++;
}
fprintf(out, "\n");
}
// Dump sequence of instructions to stdout.
void dump_instructions(Instruction* inst) {
int i;
printf("Instruction dump:\n");
for (i = 0; *inst != EXIT; i++, inst++) {
printf("%s ", InstructionTable[*inst]);
if (i == 30) {
i = 0;
printf("\n");
}
}
printf("\n\n");
}
// Print help block into stdout.
void help(char* name) {
printf("Esoteric language interpreter - Transpiler (ELI/T)\n");
printf("Usage:\n");
printf(" %s -ilm [FILE]\n\n", name);
printf(" i flag dumps used instructions.\n");
printf(" l flag shows input language.\n\n");
printf(" b - transpile into Brainfuck language.\n");
printf(" o - transpile into Ook! language.\n");
printf(" c - transpile into compressed Ook! language.\n");
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[]) {
char c;
bool t_brainfuck, t_cook, t_ook;
FILE *out;
t_brainfuck = false;
t_cook = false;
t_ook = false;
if (argc < 2) help(argv[0]);
while ((c = getopt(argc, argv, "ilbco")) != -1) {
switch (c) {
case 'i':
r_instr_dump = true;
break;
case 'l':
r_lang_dump = true;
break;
case 'b':
t_brainfuck = true;
break;
case 'c':
t_cook = true;
break;
case 'o':
t_ook = true;
break;
default:
printf("Unknown flag.\n");
help(argv[0]);
break;
}
}
Instruction* inst = parseFile(argv[optind]);
if (r_instr_dump)
dump_instructions(inst);
if (!(t_cook || t_ook || t_brainfuck )) {
printf("No language was selected - transpiling into all supported languages\n");
t_brainfuck = true;
t_cook = true;
t_ook = true;
}
if (t_brainfuck) {
out = fopen("output.bf", "w");
if (out != NULL) {
printf("Generating Brainfuck code\n");
generateBrainfuck(inst, out);
fclose(out);
} else {
printf("Cannot create output file for Brainfuck code\n");
}
}
if (t_cook) {
out = fopen("output.cook", "w");
if (out != NULL) {
printf("Generating compressed Ook! code\n");
generateCompressedOok(inst, out);
fclose(out);
} else {
printf("Cannot create output file for compressed Ook! code\n");
}
}
if (t_ook) {
out = fopen("output.ook", "w");
if (out != NULL) {
printf("Generating Ook! code\n");
generateOok(inst, out);
fclose(out);
} else {
printf("Cannot create output file for Ook! code\n");
}
}
free(inst);
return EXIT_SUCCESS;
}