view src/ploki/examples/bf2c.pk @ 7638:25b838ae35f6

<boily> le/rn wat/\xe1\x8a\xa2\xe1\x89\xb5\xe1\x8b\xae\xe1\x8c\xb5\xe1\x8b\xab \xe1\x8b\x8d\xe1\x88\xb5\xe1\x8c\xa5 \xe1\x8b\xa8\xe1\x88\x9a\xe1\x88\xb0\xe1\x88\xab \xe1\x8b\xa8\xe1\x88\x9d\xe1\x8c\x8d\xe1\x89\xa5 \xe1\x8a\xa0\xe1\x8b\xad\xe1\x8a\x90\xe1\x89\xb5 \xe1\x88\xb2\xe1\x88\x86\xe1\x8a\x95\xe1\x8d\xa3 \xe1\x8b\xa8\xe1\x88\x9a\xe1\x88\xb0\xe1\x88\xab\xe1\x8b\x8d\xe1\x88\x9d \xe1\x8a\xa8\xe1\x8c\xa4\xe1\x8d\x8d \xe1\x8a\x90\xe1\x8b\x8d\xe1\x8d\xa2
author HackBot
date Sun, 01 May 2016 22:44:03 +0000
parents ac0403686959
children
line wrap: on
line source

#!/usr/local/bin/ploki
REM brainfuck to c translator

INSERT perl-util.pk HERE

LET tr("[") "while (*p) {\n"
LET tr("]") "}\n"
LET tr("<") "--p;\n"
LET tr(">") "++p;\n"
LET tr("+") "++*p;\n"
LET tr("-") "--*p;\n"
LET tr(".") "putchar(*p);\n"
LET tr(",") "*p = getchar();\n"
LET tr("0") "*p = 0;\n"

"#include <stdio.h>\nint main(void) {\nstatic unsigned char A[30000];\nunsigned char *p = A;\n" _ @s///g #<@clean @balanced @s///g #<@input \ARG:1 "'^[]<>.,+-'+" ""#> "'[]<>.,0+-'" @OMFG tr(\@)#> _ "return 0;\n}\n"
END

FOR clean LEET s \@
	LEET $c$ 0
	10 LET s @s///g #<@s///g #<s "{^|]!}[!'^[]'*]!" @OMFG @countrepl \0#> "{(^|]!)[!'^]'*}[!'^[]'*]!" @OMFG @countrepl \0#>
	IF $c$
		LET $c$ 0
		GOFOR 10
	FI
KTHX @s///g #<s "[!'+-']!" "0"#>

FOR countrepl LET $c$ += 1
KTHX \@

FOR balanced LEET s @s///g #<\@ "'^[]'+" ""#>
	LEET $c$ 0
	10 LET s @s///g #<s "[!]!" @OMFG @countrepl ""#>
	IF $c$
		LET $c$ 0
		GOFOR 10
	FI
	IF s ; ""
		IACS "\V\ARG:0: error: unbalanced brackets
	FI
KTHX \@

FOR input LEET fh
	IF @TYPE OF \@ : "nothing"
		LET fh \EING
	ELSE
		LET fh @LAPERS \@
		IF @NOT fh
			IACS "\V\ARG:0: \V\@: \V\!
		FI
	FI
KTHX fh . @NEG 1