Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Brainf*** Compiler and Interpreter in Powershell

Closed Thread
 
Thread Tools Display Modes
Old 04-08-2007   #1 (permalink)
Peter Schneider [MVP]
Guest
 
Posts: n/a

Brainf*** Compiler and Interpreter in Powershell

Hi there!

Perhaps some of you know the brainf*** programming language... there are
many interpreters
and compilers around, yet I haven't found one for powershell... so here we
go.

# Powershell Brainf*** Interpreter
# File: bf2ps.ps1
# Author: Peter Schneider <ps.at.ugwa.net>
# Date: 08 April 2007
# Usage: ./bf2ps <file.b>
#
# Sample Hello World Program:
# ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>
# +.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

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


And here's the script for the compiler:

# Powershell Brainf*** Compiler
# File: bf2cs.ps1
# Author: Peter Schneider <ps.at.ugwa.net>
# Date: 08 April 2007
# Usage: ./bf2cs <file.b> <file.c> [-run]
#
# Sample Hello World Program:
# ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>
# +.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

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
}


You can download the both script files also on
http://weblogs.asp.net/pschneider

Have fun,

Peter Schneider
MVP Visual Developer - Visual C#
MCT, MCSD.NET, MCAD.NET, MCDBA

 
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Miracle C Compiler - Linking issues? Sunny Vista General 1 05-18-2008 11:14 AM
Real nested Powershell interpreter ivo.looser PowerShell 0 09-25-2007 03:47 AM
Brain**** Compiler & Interpreter in Powershell Peter Schneider [MVP] PowerShell 0 04-08-2007 04:14 AM
[ANN] Liogo, a Logo compiler for WPF Lionel LASKE Avalon 0 08-16-2006 03:12 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49