-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ed3a5a
commit f9ac4dd
Showing
5 changed files
with
100 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module Pages.Projects.Brainfuck where | ||
|
||
import IHP.HSX.QQ (hsx) | ||
import Text.Blaze.Html (Html) | ||
import Data.List (intercalate) | ||
|
||
import Helpers.CodeBlock (codeBlock) | ||
|
||
convertSymbols :: String -> String | ||
convertSymbols ('+':xs) = "(*ptr)++;\n"++convertSymbols xs | ||
convertSymbols ('-':xs) = "(*ptr)--;\n"++convertSymbols xs | ||
convertSymbols ('>':xs) = "ptr++;\n"++convertSymbols xs | ||
convertSymbols ('<':xs) = "ptr--;\n"++convertSymbols xs | ||
convertSymbols ('[':xs) = "while(*ptr) {\n"++convertSymbols xs | ||
convertSymbols (']':xs) = "}\n"++convertSymbols xs | ||
convertSymbols ('.':xs) = "printf(\"%c\", (*ptr));\n"++convertSymbols xs | ||
convertSymbols (',':xs) = "scanf(\"%c\", ptr);\n"++convertSymbols xs | ||
convertSymbols (x:xs) = convertSymbols xs | ||
convertSymbols [] = [] | ||
|
||
code :: String -> String | ||
code input = intercalate "\n" [ | ||
"#include <stdio.h>", | ||
"#include <stdint.h>", | ||
"#include <inttypes.h>", | ||
"char buffer[30000] = {0};", | ||
"char* ptr = buffer;", | ||
"int main () {", | ||
convertSymbols input, | ||
"}" | ||
] | ||
|
||
brainfuck :: Html | ||
brainfuck = [hsx| | ||
Write some brainfuck code, and you will receive the equivalent in C code.<br> | ||
<script> | ||
function download(file, text) { | ||
var element = document.createElement("a") | ||
element.setAttribute('href', 'data:text/plain;charset=utf-8, ' + encodeURIComponent(text)) | ||
element.setAttribute('download', file) | ||
document.body.appendChild(element) | ||
element.click() | ||
document.body.removeChild(element) | ||
} | ||
function submit() { | ||
var source = document.getElementById("code").value | ||
fetch("/api/brainfuck", { | ||
method: "post", | ||
body: source | ||
}).then(response => response.text().then(text => { | ||
download("brainfuck.c", text) | ||
})) | ||
|
||
} | ||
</script> | ||
<textarea type="text" id="code" style="min-width: 600px; min-height: 200px;"></textarea> | ||
<br> | ||
<button id="submit" onclick="submit()">Submit</button> | ||
<br> | ||
Example: | ||
{codeBlock "txt" "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."} | ||
|] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters