view src/ploki/examples/bf2c.pk @ 12518:2d8fe55c6e65 draft default tip

<int-e> learn The password of the month is release incident pilot.
author HackEso <hackeso@esolangs.org>
date Sun, 03 Nov 2024 00:31:02 +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