codebrothers.net :: Home  >  Peter Schneider
User: Guest  Login

News:

Advertisment:

Powered by Smart|MicroBlog.NET
Blog search:  
Brainfuck Interpreter and Compiler in Powershell

Surley some of you know the Brainfuck programming language... there are many compilers and interpreters around, yet I haven’t found some for powershell... so here we go:

Powershell Brainfuck Interpreter:

param ($i)

$t = @{ ’>’=’$p++;’;
’<’=’$p--;’;
’+’=’$m[$p]++’;
’-’=’$m[$p]--’;
’.’=’write-host $([char]$m[$p]) -n ’;
’,’=’$m[$p]=$host.ui.ReadLine() ’;
’[’=’while ($m[$p] -ne 0) {’;
’]’=’}’;
}

$c = ’$p=0;$m=new-object "byte[]" 32768’+"`n" gc $i -Enc Byte -r 1 | % {$c+=$t["$([char]$_)"]+"`n"}

invoke-expression $c

 

Powershell Brainfuck Compiler:

param ( [string] $infile = $(throw "Please specify input file (.b)"),
[string] $outfile = $(throw "Please specify output file (.cs)"),
[switch] $run = $false
)

$csc = (join-path ($env:windir) Microsoft.NET\Framework\v2.0.50727\csc.exe)

$transpose = @{’>’ = ’p++;’;
’<’ = ’p--;’;
’+’ = ’m[p]++;’;
’-’ = ’m[p]--;’;
’.’ = ’Console.Write(m[p]);’;
’,’ = ’m[p]=Console.ReadLine();’;
’[’ = "while (m[p]!=0) {"
’]’ = ’}’;
}

$header = @"
using System;
public class Program {
public static void Main() {
int p=0;
char[] m=new char[32768];
"@

if ($(test-path $outfile)) { rm $outfile | out-null }
$header | out-file $outfile -append

get-content $infile -encoding Byte -readcount 1 |
% { $transpose["$([char]$_)"] } | out-file $outfile -append

"}}" | out-file $outfile -append

& $csc `/target:exe $outfile | out-null

if ($run) {
$outfile = $outfile.Replace(".cs",".exe")
& .`/$outfile
}


Have fun!

Share this post: Email it!
Posted:  10.04.2007 08:25:50  by  pschneider
Tags:  Powershell
[10] comments:
Nikolet said:
Nice site!
18.03.2008 20:46:48
Nice Site! http://google.com
30.06.2008 05:59:38
fico scores said:
Nice Site! http://excellent-credit-card.blogspot.com
01.07.2008 21:13:44
flower1800 said:
PEkhzj fdasjhgadghi hdagh iao hiasoh fgiash isafohsa
22.07.2008 08:46:01
google said:
Here is google. He is nice. Thanks.
18.01.2009 13:06:19
google said:
Here is google. He is nice. Thanks.
18.01.2009 13:26:17
Kaylyn said:
I was really confused, and this answered all my qusteonis.
25.12.2011 10:55:50
mnakbftcon said:
HFskdv , [url=http://egiqpguskyce.com/]egiqpguskyce[/url], [link=http://ricisadvwivb.com/]ricisadvwivb[/link], http://ghprclnhsunv.com/
26.12.2011 14:13:55
cheapest home insurance >:-OO cheap auto insurance 5241 california health insurance >:-D affordable health insurance qbnd
19.01.2012 05:48:27
ko0k33z said:
cheap auto insurance znbcfl private health insurance 06878 cheap health insurance 1360
21.01.2012 16:07:55
Leave a Comment

Name required

Your URL

Comments (required)