Add optimisations
This commit is contained in:
parent
2eae55e08b
commit
aedccb1d30
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
7
.idea/discord.xml
Normal file
7
.idea/discord.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="PROJECT_FILES" />
|
||||
<option name="description" value="" />
|
||||
</component>
|
||||
</project>
|
18
.idea/misc.xml
Normal file
18
.idea/misc.xml
Normal file
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MakefileSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<MakefileProjectSettings>
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
</set>
|
||||
</option>
|
||||
<option name="version" value="2" />
|
||||
</MakefileProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
<component name="MakefileWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
4
Makefile
4
Makefile
@ -2,9 +2,9 @@ sourcefiles := $(shell find src/impl/ -name *.c)
|
||||
|
||||
|
||||
all:
|
||||
make build-main
|
||||
make build
|
||||
|
||||
build-main:
|
||||
build:
|
||||
mkdir -p dist/ && \
|
||||
gcc -o dist/bf2c -I src/intf/ ${sourcefiles}
|
||||
|
||||
|
106
src/impl/main.c
106
src/impl/main.c
@ -4,6 +4,9 @@
|
||||
#include "popen2.h"
|
||||
|
||||
int is_arg(int ac, char **argv, char *arg) {
|
||||
|
||||
|
||||
|
||||
if (ac < 2) return 0;
|
||||
for(int x=1; x < ac; x++)
|
||||
if (0 == strcmp(argv[x], arg))
|
||||
@ -70,61 +73,70 @@ int main(int argc, char **argv) {
|
||||
out = fdopen(*fwd, "w");
|
||||
}
|
||||
|
||||
fprintf(out,
|
||||
"#include <stdio.h>\n"
|
||||
"#include <stdlib.h>\n\n"
|
||||
"int main(int argc, char **argv) {\n"
|
||||
"unsigned char *cell = calloc(%d, 1);\n"
|
||||
"unsigned char *cells = cell;\n"
|
||||
"if (!cell) {\n"
|
||||
"fprintf(stderr, \"Error allocating memory.\\n\");\n"
|
||||
"return 1;\n"
|
||||
"}\n\n", cellsize
|
||||
);
|
||||
fprintf(out,
|
||||
"#include <stdio.h>\n"
|
||||
"#include <stdlib.h>\n\n"
|
||||
"int main(int argc, char **argv) {\n"
|
||||
"unsigned char *cell = calloc(%d, 1);\n"
|
||||
"unsigned char *cells = cell;\n"
|
||||
"if (!cell) {\n"
|
||||
"fprintf(stderr, \"Error allocating memory.\\n\");\n"
|
||||
"return 1;\n"
|
||||
"}\n\n", cellsize
|
||||
);
|
||||
|
||||
a=1;
|
||||
a = 1;
|
||||
|
||||
while (b!=EOF) {
|
||||
c = getc(in);
|
||||
switch (c) {
|
||||
case '>':
|
||||
case '<':
|
||||
case '+':
|
||||
case '-':
|
||||
case '.':
|
||||
case ',':
|
||||
case '[':
|
||||
case ']':
|
||||
case EOF:
|
||||
if (c!=EOF && b==c && optimise) a++;
|
||||
else {
|
||||
switch (b) {
|
||||
case '>': addValueChange(out, "cell", true, a); break;
|
||||
case '<': addValueChange(out, "cell", false, a); break;
|
||||
case '+': addValueChange(out, "*cell", true, a); break;
|
||||
case '-': addValueChange(out, "*cell", false, a); break;
|
||||
while (b != EOF) {
|
||||
c = getc(in);
|
||||
switch (c) {
|
||||
case '>':
|
||||
case '<':
|
||||
case '+':
|
||||
case '-':
|
||||
case '.':
|
||||
for (int i = 0; i < a; i++) fprintf(out, "putchar(*cell);\n");
|
||||
break;
|
||||
case ',':
|
||||
for (int i = 0; i < a; i++) fprintf(out, "*cell = getchar();\n");
|
||||
break;
|
||||
case '[':
|
||||
for (int i = 0; i < a; i++) fprintf(out, "while (*cell) {\n");
|
||||
break;
|
||||
case ']':
|
||||
for (int i = 0; i < a; i++) fprintf(out, "}\n");
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
case EOF:
|
||||
if (c != EOF && b == c && optimise) a++;
|
||||
else {
|
||||
switch (b) {
|
||||
case '>':
|
||||
addValueChange(out, "cell", true, a);
|
||||
break;
|
||||
case '<':
|
||||
addValueChange(out, "cell", false, a);
|
||||
break;
|
||||
case '+':
|
||||
addValueChange(out, "*cell", true, a);
|
||||
break;
|
||||
case '-':
|
||||
addValueChange(out, "*cell", false, a);
|
||||
break;
|
||||
case '.':
|
||||
for (int i = 0; i < a; i++) fprintf(out, "putchar(*cell);\n");
|
||||
break;
|
||||
case ',':
|
||||
for (int i = 0; i < a; i++) fprintf(out, "*cell = getchar();\n");
|
||||
break;
|
||||
case '[':
|
||||
for (int i = 0; i < a; i++) fprintf(out, "while (*cell) {\n");
|
||||
break;
|
||||
case ']':
|
||||
for (int i = 0; i < a; i++) fprintf(out, "}\n");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Reset for next loop
|
||||
a = 1;
|
||||
b = c;
|
||||
// Reset for next loop
|
||||
a = 1;
|
||||
b = c;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(out, "\nfree(cells);\nreturn 0;\n}\n");
|
||||
fprintf(out, "\nfree(cells);\nreturn 0;\n}\n");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user