Advertisement
makerimagesGames

Compiler3

Jun 11th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package tests;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.File;
  9. import java.io.FileReader;
  10. import java.io.IOException;
  11.  
  12. /**
  13.  *
  14.  * @author Makerimages
  15.  */
  16. public class Compilertest3
  17. {
  18.     String S;
  19.     String SClean;
  20.     public Compilertest3(File f) throws IOException
  21.     {
  22.          StringBuffer fileData = new StringBuffer();
  23.         BufferedReader reader = new BufferedReader(
  24.                 new FileReader(f));
  25.         char[] buf = new char[1024];
  26.         int numRead=0;
  27.         while((numRead=reader.read(buf)) != -1){
  28.             String readData = String.valueOf(buf, 0, numRead);
  29.             fileData.append(readData);
  30.         }
  31.         reader.close();
  32.        S=fileData.toString();
  33.     }
  34.  
  35.    
  36.    
  37.    
  38.     public void cleanFile(String input)
  39.     {
  40.         String split[]=input.split("\n");
  41.         input="\n"+input;
  42.         split[0]=" "+split[0];
  43.          int count = 0;
  44.          int varCount = 0;//change this for starting vX value (default: 0)
  45.          int funcCount=0;
  46.                 while (count < split.length) {
  47.                         if (split[count].startsWith(" define")) {
  48.                            
  49.                                 split[count] = split[count].substring(8);//remove define and space
  50.                                
  51.                                 input = input.replaceAll(split[count].split(" ")[0], split[count].substring(split[count].split(" ")[0].length()));
  52.                                 //the above replaces the first argument with all the last arguments in the define function, in the input string
  53.                               //  System.err.print(input);
  54.                         }  if (split[count].startsWith(" var")) {
  55.                                //System.out.println("int debug:");
  56.                               //System.out.println(split[count]);
  57.                                 split[count] = split[count].substring(1);//remove "int" and space
  58.                                 input = input.replaceAll(split[count], " v" + varCount + " ");
  59.                                 input = input.replaceAll(split[count], " v" + varCount + " ");//just in case
  60.                                 varCount++;
  61.                         }
  62.                         if (split[count].startsWith("func "))
  63.                         {
  64.                             split[count] = split[count].substring(0);
  65.                             input = input.replaceAll(split[count], "~" + funcCount + " ");
  66.                            // input = input.replaceAll(split[count], " ~" + funcCount + " ");//just in case
  67.                             funcCount++;
  68.                         }
  69.                          if (split[count].contains("="))
  70.                         {
  71.                             split[count] = split[count].substring(0);
  72.                             input = input.replaceAll(".*."+"=" ,"v"+ varCount + "=");
  73.                            // input = input.replaceAll(split[count], " ~" + funcCount + " ");//just in case
  74.                             varCount++;
  75.                         }
  76.                          
  77.                         count++;
  78.                 }
  79.                
  80.                 input = input.replaceAll("\n int.*.\n", "\n");//this is where the added \n at start is
  81.                 input = input.replaceAll("\nfunc.*.\n", "\n");
  82.                  input = input.replaceAll(" #.*.", "");
  83.                    input = input.replaceAll("\\{", "");
  84.                 while(input.contains(" int ")==true)
  85.                 {
  86.                         input = input.replaceAll("\n int.*.\n", "\n");
  87.                 }
  88.                
  89.                          while(input.contains(" func ")==true)
  90.                 {
  91.                         input = input.replaceAll("\n func.*.\n", "\n");
  92.                 }
  93.                 input=input.replaceAll("(?m)^[ \t]*\r?\n", "");//clear all empty lines
  94.                 input=input.substring(1);//gets rid of the problem caused by the adding \n thing
  95.               //  input = cleanInput(input);//cleans the input for the last time
  96.                 //System.err.print(input);
  97.                 SClean=input;
  98.                 System.err.println(SClean);
  99.                 }
  100.  
  101.    
  102.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement