Guest User

Untitled

a guest
Oct 15th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.util.HashMap;
  5.  
  6. public class Main {
  7.  
  8. static HashMap<String, Integer> savedVar = new HashMap<String, Integer>(); // hashmap to hold variables and values
  9.  
  10. public static void main(String[] args) throws Exception {
  11. File bareBonesFile = new File("D:\\Users\\Naser Salameh\\Desktop\\BareBones.txt"); //reads file at path
  12. BufferedReader in = new BufferedReader(new FileReader(bareBonesFile));
  13. String bareBonesLine;
  14. StringBuilder bareBonesText = new StringBuilder();
  15. while ((bareBonesLine = in.readLine()) != null) {
  16. bareBonesText.append(bareBonesLine.trim()); // append each line to stringBuilder
  17. }
  18. String bareBonesCode= bareBonesText.toString(); // convert stringBuilder to String
  19. System.out.println(bareBonesCode);
  20. bareBonesInterpreter(bareBonesCode);
  21. }
  22.  
  23. public static void bareBonesInterpreter(String bareBonesCode) { // takes full line of code and seperates them to current line and rest of lines
  24. int lineCount = bareBonesCode.length() - bareBonesCode.replaceAll(";", "").length(); // counts the number of lines by counting ;
  25. if (lineCount == 0) { // if no more lines left
  26. System.out.println("Ended Success");
  27. System.exit(0);
  28. }
  29. if (lineCount > 1){
  30. String[] linePart = bareBonesCode.split(";", 2); // splits code into current line and rest of code
  31. System.out.println(linePart[0]); // print each line to track
  32. if (linePart[0].indexOf("while") != -1) // at while loop initiate while method
  33. linePart[1] = bareBonesWhile(bareBonesCode);
  34. bareBonesCommand(linePart[0]); // send current line to command method
  35. bareBonesInterpreter(linePart[1]); // send rest of code recursively to interpreter method
  36. }
  37. else{
  38. System.out.println(bareBonesCode);
  39. bareBonesCommand(bareBonesCode);
  40. }
  41. }
  42.  
  43. public static void bareBonesCommand(String bareBonesLine){ // takes line of code and interprets command
  44. String[] linePart = bareBonesLine.split(" "); // split to command and variable
  45. linePart[1] = linePart[1].substring(0,1); // remove ; from variable
  46. if (savedVar.get(linePart[1]) == null) { // if new variable, create new var in hashmap
  47. savedVar.put(linePart[1], 0);
  48. System.out.println("New variable: " + linePart[1] + " Declared at 0.");
  49. } else {
  50. switch (linePart[0]) { // switch for different commands
  51. case "clear":
  52. savedVar.put(linePart[1], 0);
  53. break;
  54. case "incr":
  55. savedVar.put(linePart[1], savedVar.get(linePart[1]) + 1);
  56. break;
  57. case "decr":
  58. savedVar.put(linePart[1], savedVar.get(linePart[1]) - 1);
  59. break;
  60. }
  61. System.out.println(linePart[1] + ", Value: " + savedVar.get(linePart[1])); // to keep track of change
  62. }
  63. }
  64.  
  65. public static String bareBonesWhile(String loopCode) { // while method to handle while loops
  66. String[] loopPart = loopCode.split(";",2); // split loop to interpret while condition
  67. String loopVar = loopPart[0].substring(6,7); // remove ; from while variable
  68. boolean nestedCheck = true; // bool to predict nested loops
  69. int endIndex =0;
  70. do {
  71. endIndex = loopPart[1].indexOf("end;",endIndex + 1); // set string from loop start to first END keyword
  72. int whileCount = (loopPart[1].substring(0, endIndex).length() - loopPart[1].substring(0, endIndex).replaceAll("while", "").length()) /5; // count whiles between current length
  73. int endCount = (loopPart[1].substring(0, endIndex).length() - loopPart[1].substring(0, endIndex).replaceAll("end", "").length() ) /3; // count ends between current length
  74. if (whileCount == endCount) { // if there are whiles equal to ends then it is not nested
  75. nestedCheck = false;
  76. }
  77. } while (nestedCheck);
  78. String loop = loopPart[1].substring(0,endIndex); // set loop codes string
  79. while (savedVar.get(loopVar) > 0){ // loop conditon ASSUMPTION: Bare Bones only accepts while loops when var is NOT 0 -- IDEA to fix in WEEK 3 Challenge
  80. bareBonesInterpreter(loop);
  81. }
  82. return loopPart[1].substring(endIndex+4); // after loop send remainder of code String
  83. }
  84. }
Add Comment
Please, Sign In to add comment