Main.java (7608B)
1 /* 2 Copyright 2009 Sebastiano Tronto <sebastiano@luganega.org> 3 4 This file is part of SlackYouInterpreter. 5 6 SlackYouInterpreter 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 SlackYouInterpreter 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 SlackYouInterpreter; 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 int i, j; 28 public static byte a[][]; 29 public static void main( String args[] ) { 30 System.out.println(""); 31 Frame frame = new Frame(); 32 frame.setSize( 200, 300 ); 33 frame.setVisible( true ); 34 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 35 } 36 public static void run( String code ) { 37 scanner = new Scanner( System.in ); 38 a = new byte[1024][1024]; 39 for ( int countX = 0; countX < a[0].length; countX++ ) 40 for ( int countY = 0; countY < a.length; countY++ ) 41 a[countX][countY] = 0; 42 chars = 'A'; 43 int z = 0; 44 totalCode = code.toCharArray(); 45 charCodeAux = new char[totalCode.length*2]; 46 for ( int k = 0; k < totalCode.length; k++ ) { 47 switch ( totalCode[k] ) { 48 case '>': 49 case '<': 50 case '+': 51 case '-': 52 case '.': 53 case ',': 54 case '^': 55 case 'v': 56 case 'y': 57 case 'g': 58 case 'h': 59 case 'j': 60 case '*': 61 case '/': 62 case '%': 63 case '=': 64 case 'r': 65 case 'w': 66 case 'a': 67 case 'b': 68 case 'c': 69 case 'd': 70 case 'e': 71 case 'f': 72 case '0': 73 case '1': 74 case '2': 75 case '3': 76 case '4': 77 case '5': 78 case '6': 79 case '7': 80 case '8': 81 case '9': 82 charCodeAux[z] = totalCode[k]; 83 z++; 84 break; 85 case '?': 86 case '!': 87 charCodeAux[z] = totalCode[k]; 88 z++; 89 charCodeAux[z] = chars; 90 chars++; 91 z++; 92 break; 93 case ';': 94 case ':': 95 chars--; 96 charCodeAux[z] = chars; 97 z++; 98 charCodeAux[z] = totalCode[k]; 99 z++; 100 break; 101 } 102 } 103 charCode = new char[z]; 104 for ( int k = 0; k < charCode.length; k++ ) 105 charCode[k] = charCodeAux[k]; 106 i = 0; 107 j = 0; 108 execute( scanner, charCode, a, i, j, 0, charCode.length ); 109 } 110 public static void execute( Scanner scanner, char charCode[], byte a[][], int i, int j, int start, int stop ) { 111 112 for (int count = start; count < stop; count++ ) { 113 switch ( charCode[count] ) { 114 case '>': 115 i++; 116 break; 117 case '<': 118 i--; 119 break; 120 case 'v': 121 j++; 122 break; 123 case '^': 124 j--; 125 break; 126 case '.': 127 a[j][i]++; 128 break; 129 case ',': 130 a[j][i]--; 131 break; 132 case 'y': 133 a[j-1][i] = a[j][i]; 134 j--; 135 break; 136 case 'h': 137 a[j+1][i] = a[j][i]; 138 j++; 139 break; 140 case 'g': 141 a[j][i-1] = a[j][i]; 142 i--; 143 break; 144 case 'j': 145 a[j][i+1] = a[j][i]; 146 i++; 147 break; 148 case '+': 149 a[j][i] += a[j][i+1]; 150 break; 151 case '-': 152 a[j][i] -= a[j][i+1]; 153 break; 154 case '*': 155 a[j][i] *= a[j][i+1]; 156 break; 157 case '/': 158 a[j][i] /= a[j][i+1]; 159 break; 160 case '%': 161 a[j][i] %= a[j][i+1]; 162 break; 163 case 'w': 164 System.out.print( (char) a[j][i] ); 165 break; 166 case 'r': 167 String stringa = scanner.nextLine(); 168 char character[] = stringa.toCharArray(); 169 switch ( character.length ) { 170 case 1: 171 a[j][i] = (byte) character[0]; 172 break; 173 default: 174 count --; 175 break; 176 } 177 break; 178 case '=': 179 if ( (charCode[count+1] == '0' || charCode[count+1] == '1' || charCode[count+1] == '2' || charCode[count+1] == '3' || 180 charCode[count+1] == '4' || charCode[count+1] == '5' || charCode[count+1] == '6' || charCode[count+1] == '7' || 181 charCode[count+1] == '8' || charCode[count+1] == '9' || charCode[count+1] == 'a' || charCode[count+1] == 'b' || 182 charCode[count+1] == 'c' || charCode[count+1] == 'd' || charCode[count+1] == 'e' || charCode[count+1] == 'f' ) && 183 (charCode[count+2] == '0' || charCode[count+2] == '1' || charCode[count+2] == '2' || charCode[count+2] == '3' || 184 charCode[count+2] == '4' || charCode[count+2] == '5' || charCode[count+2] == '6' || charCode[count+2] == '7' || 185 charCode[count+2] == '8' || charCode[count+2] == '9' || charCode[count+2] == 'a' || charCode[count+2] == 'b' || 186 charCode[count+2] == 'c' || charCode[count+2] == 'd' || charCode[count+2] == 'e' || charCode[count+2] == 'f' ) ) { 187 a[j][i] = 0; 188 switch ( charCode[count+1] ) { 189 case '0': 190 break; 191 case '1': 192 a[j][i] += 1 * 16; 193 break; 194 case '2': 195 a[j][i] += 2 * 16; 196 break; 197 case '3': 198 a[j][i] += 3 * 16; 199 break; 200 case '4': 201 a[j][i] += 4 * 16; 202 break; 203 case '5': 204 a[j][i] += 5 * 16; 205 break; 206 case '6': 207 a[j][i] += 6 * 16; 208 break; 209 case '7': 210 a[j][i] += 7 * 16; 211 break; 212 case '8': 213 a[j][i] += 8 * 16; 214 break; 215 case '9': 216 a[j][i] += 9 * 16; 217 break; 218 case 'a': 219 a[j][i] += 10 * 16; 220 break; 221 case 'b': 222 a[j][i] += 11 * 16; 223 break; 224 case 'c': 225 a[j][i] += 12 * 16; 226 break; 227 case 'd': 228 a[j][i] += 13 * 16; 229 break; 230 case 'e': 231 a[j][i] += 14 * 16; 232 break; 233 case 'f': 234 a[j][i] += 15 * 16; 235 break; 236 } 237 switch ( charCode[count+2] ) { 238 case '0': 239 break; 240 case '1': 241 a[j][i] += 1; 242 break; 243 case '2': 244 a[j][i] += 2; 245 break; 246 case '3': 247 a[j][i] += 3; 248 break; 249 case '4': 250 a[j][i] += 4; 251 break; 252 case '5': 253 a[j][i] += 5; 254 break; 255 case '6': 256 a[j][i] += 6; 257 break; 258 case '7': 259 a[j][i] += 7; 260 break; 261 case '8': 262 a[j][i] += 8; 263 break; 264 case '9': 265 a[j][i] += 9; 266 break; 267 case 'a': 268 a[j][i] += 10; 269 break; 270 case 'b': 271 a[j][i] += 11; 272 break; 273 case 'c': 274 a[j][i] += 12; 275 break; 276 case 'd': 277 a[j][i] += 13; 278 break; 279 case 'e': 280 a[j][i] += 14; 281 break; 282 case 'f': 283 a[j][i] += 15; 284 break; 285 } 286 } 287 break; 288 case '?': 289 if ( a[j][i] == 0 ) { 290 count++; 291 char key = charCode[count]; 292 count++; 293 for ( ; charCode[count] != key; count++ ) ; 294 count++; 295 } 296 break; 297 case '!': 298 if ( a[j][i] != 0 ) { 299 count ++; 300 char key = charCode[count]; 301 count ++; 302 for ( ; charCode[count] != key; count++ ) ; 303 count++; 304 } 305 break; 306 case ';': 307 if ( a[j][i] != 0 ) { 308 count --; 309 char key = charCode[count]; 310 count --; 311 for ( ; charCode[count] != key; count-- ) ; 312 } 313 break; 314 case ':': 315 if ( a[j][i] == 0 ) { 316 count --; 317 char key = charCode[count]; 318 count --; 319 for ( ; charCode[count] != key; count-- ) ; 320 } 321 break; 322 } 323 } 324 } 325 }