ancient-projects

My earliest programs (2006-2010)
git clone https://git.tronto.net/ancient-projects
Download | Log | Files | Refs | README

Main.java (3063B)


      1 /*
      2 Copyright 2009 Sebastiano Tronto <sebastiano@luganega.org>
      3 
      4 This file is part of PBFI (Porky's Brain F*** Interpreter).
      5 
      6     PBFI is free software; you can redistribute it and/or modify
      7     it under the terms of the GNU General Public License as published by
      8     the Free Software Foundation; either version 2 of the License, or
      9     (at your option) any later version.
     10 
     11     PBFI is distributed in the hope that it will be useful,
     12     but WITHOUT ANY WARRANTY; without even the implied warranty of
     13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14     GNU General Public License for more details.
     15 
     16     You should have received a copy of the GNU General Public License
     17     along with PBFI; if not, write to the Free Software
     18     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     19 
     20 */
     21 
     22 import javax.swing.JFrame;
     23 import java.util.Scanner;
     24 public class Main {
     25 	public static Scanner scanner;
     26 	public static char totalCode[], charCodeAux[], charCode[], chars;
     27 	public static byte a[];
     28 	public static int i;
     29 	public static void main( String args[] ) {
     30 		Frame frame = new Frame();
     31 		frame.setSize( 200, 300 );
     32 		frame.setVisible( true );
     33 		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     34 	}
     35 	public static void run( String code ) {
     36 		chars = 'a';
     37 		int j = 0;
     38 		totalCode = code.toCharArray();
     39 		charCodeAux = new char[totalCode.length*2];
     40 		for ( int k = 0; k < totalCode.length; k++ ) {
     41 			switch ( totalCode[k] ) {
     42 				case '>':
     43 				case '<':
     44 				case '+':
     45 				case '-':
     46 				case '.':
     47 				case ',':
     48 					charCodeAux[j] = totalCode[k];
     49 					j++;
     50 					break;
     51 				case '[':
     52 					charCodeAux[j] = totalCode[k];
     53 					j++;
     54 					charCodeAux[j] = chars;
     55 					j++;
     56 					chars++;
     57 					break;
     58 				case ']':
     59 					chars--;
     60 					charCodeAux[j] = chars;
     61 					j++;
     62 					charCodeAux[j] = totalCode[k];
     63 					j++;
     64 					break;
     65 			}
     66 		}
     67 		charCode = new char[j];
     68 		for ( int k = 0; k < charCode.length; k++ )
     69 			charCode[k] = charCodeAux[k];
     70 		scanner = new Scanner( System.in );
     71 		a = new byte[30000];
     72 		for ( int count = 0; count < a.length; count++ )
     73 			a[count] = 0;
     74 		i = 0;
     75 		execute( scanner, charCode, a, i, 0, charCode.length );
     76 	}
     77 	public static void execute( Scanner scanner, char charCode[], byte a[], int i, int start, int stop ) {
     78 		for (int count = start; count < stop; count++ ) {
     79 			switch ( charCode[count] ) {
     80 				case '>':
     81 					i++;
     82 					break;
     83 				case '<':
     84 					i--;
     85 					break;
     86 				case '+':
     87 					a[i]++;
     88 					break;
     89 				case '-':
     90 					a[i]--;
     91 					break;
     92 				case '.':
     93 					System.out.print( (char) a[i] );
     94 					break;
     95 				case ',':
     96 					String stringa = scanner.nextLine();
     97 					a[i] = (byte) ( ( stringa.toCharArray() )[0] );
     98 					break;
     99 				case '[':
    100 					if ( a[i] == 0 ) {
    101 						count++;
    102 						char key = charCode[count];
    103 						count++;
    104 						for ( ; charCode[count] != key; count++ ) ;
    105 						count++;
    106 					}
    107 					break;
    108 				case ']':
    109 					if ( a[i] != 0 ) {
    110 						count --;
    111 						char key = charCode[count];
    112 						count --;
    113 						for ( ; charCode[count] != key; count-- ) ;
    114 					}
    115 					break;
    116 			}
    117 		}
    118 	}
    119 }