aboutsummaryrefslogtreecommitdiff
path: root/pbfi/pbfi-1.1/src/Main.java
blob: 597ae2a99838f780cede43d7f348e1acb7074c3c (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Copyright 2009 Sebastiano Tronto <sebastiano@luganega.org>

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;
			}
		}
	}
}

Generated with cgit - Back to sebastiano.tronto.net