/* Copyright 2009 Sebastiano Tronto This file is part of PBFI (Porky's Brain F*** Interpreter). PBFI is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. PBFI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with PBFI; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import javax.swing.JFrame; import java.util.Scanner; public class Main { public static Scanner scanner; public static char totalCode[], charCodeAux[], charCode[], chars; public static byte a[]; public static int i; public static void main( String args[] ) { Frame frame = new Frame(); frame.setSize( 200, 300 ); frame.setVisible( true ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void run( String code ) { chars = 'a'; int j = 0; totalCode = code.toCharArray(); charCodeAux = new char[totalCode.length*2]; for ( int k = 0; k < totalCode.length; k++ ) { switch ( totalCode[k] ) { case '>': case '<': case '+': case '-': case '.': case ',': charCodeAux[j] = totalCode[k]; j++; break; case '[': charCodeAux[j] = totalCode[k]; j++; charCodeAux[j] = chars; chars++; j++; break; case ']': chars--; charCodeAux[j] = chars; j++; charCodeAux[j] = totalCode[k]; j++; break; } } charCode = new char[j]; for ( int k = 0; k < charCode.length; k++ ) charCode[k] = charCodeAux[k]; scanner = new Scanner( System.in ); a = new byte[30000]; for ( int count = 0; count < a.length; count++ ) a[count] = 0; i = 0; execute( scanner, charCode, a, i, 0, charCode.length ); } public static void execute( Scanner scanner, char charCode[], byte a[], int i, int start, int stop ) { for ( int count = start; count < stop; count++ ) { switch ( charCode[count] ) { case '>': i++; break; case '<': i--; break; case '+': a[i]++; break; case '-': a[i]--; break; case '.': System.out.print( (char) a[i] ); break; case ',': String stringa = scanner.nextLine(); a[i] = (byte) ( ( stringa.toCharArray() )[0] ); break; case '[': if ( a[i] == 0 ) { count++; char key = charCode[count]; count++; for ( ; charCode[count] != key; count++ ) ; count++; } break; case ']': if ( a[i] != 0 ) { count --; char key = charCode[count]; count --; for ( ; charCode[count] != key; count-- ) ; } break; } } } }