Add optimisations

This commit is contained in:
Melon 2022-01-20 00:14:01 +00:00
parent 2eae55e08b
commit aedccb1d30
Signed by: melon
GPG Key ID: B0ADD5395BCDAAB6
6 changed files with 100 additions and 49 deletions

8
.idea/.gitignore vendored Normal file
View 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
View 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
View 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
View 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>

View File

@ -2,9 +2,9 @@ sourcefiles := $(shell find src/impl/ -name *.c)
all: all:
make build-main make build
build-main: build:
mkdir -p dist/ && \ mkdir -p dist/ && \
gcc -o dist/bf2c -I src/intf/ ${sourcefiles} gcc -o dist/bf2c -I src/intf/ ${sourcefiles}

View File

@ -4,6 +4,9 @@
#include "popen2.h" #include "popen2.h"
int is_arg(int ac, char **argv, char *arg) { int is_arg(int ac, char **argv, char *arg) {
if (ac < 2) return 0; if (ac < 2) return 0;
for(int x=1; x < ac; x++) for(int x=1; x < ac; x++)
if (0 == strcmp(argv[x], arg)) if (0 == strcmp(argv[x], arg))
@ -70,61 +73,70 @@ int main(int argc, char **argv) {
out = fdopen(*fwd, "w"); out = fdopen(*fwd, "w");
} }
fprintf(out, fprintf(out,
"#include <stdio.h>\n" "#include <stdio.h>\n"
"#include <stdlib.h>\n\n" "#include <stdlib.h>\n\n"
"int main(int argc, char **argv) {\n" "int main(int argc, char **argv) {\n"
"unsigned char *cell = calloc(%d, 1);\n" "unsigned char *cell = calloc(%d, 1);\n"
"unsigned char *cells = cell;\n" "unsigned char *cells = cell;\n"
"if (!cell) {\n" "if (!cell) {\n"
"fprintf(stderr, \"Error allocating memory.\\n\");\n" "fprintf(stderr, \"Error allocating memory.\\n\");\n"
"return 1;\n" "return 1;\n"
"}\n\n", cellsize "}\n\n", cellsize
); );
a=1; a = 1;
while (b!=EOF) { while (b != EOF) {
c = getc(in); c = getc(in);
switch (c) { switch (c) {
case '>': case '>':
case '<': case '<':
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;
case '.': case '.':
for (int i = 0; i < a; i++) fprintf(out, "putchar(*cell);\n");
break;
case ',': case ',':
for (int i = 0; i < a; i++) fprintf(out, "*cell = getchar();\n");
break;
case '[': case '[':
for (int i = 0; i < a; i++) fprintf(out, "while (*cell) {\n");
break;
case ']': case ']':
for (int i = 0; i < a; i++) fprintf(out, "}\n"); case EOF:
break; if (c != EOF && b == c && optimise) a++;
default: break; 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 // Reset for next loop
a = 1; a = 1;
b = c; b = c;
}
break;
} }
break;
} }
}
fprintf(out, "\nfree(cells);\nreturn 0;\n}\n"); fprintf(out, "\nfree(cells);\nreturn 0;\n}\n");
} }