Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.util.HashMap;
- public class Main {
- static HashMap<String, Integer> savedVar = new HashMap<String, Integer>(); // hashmap to hold variables and values
- public static void main(String[] args) throws Exception {
- File bareBonesFile = new File("D:\\Users\\Naser Salameh\\Desktop\\BareBones.txt"); //reads file at path
- BufferedReader in = new BufferedReader(new FileReader(bareBonesFile));
- String bareBonesLine;
- StringBuilder bareBonesText = new StringBuilder();
- while ((bareBonesLine = in.readLine()) != null) {
- bareBonesText.append(bareBonesLine.trim()); // append each line to stringBuilder
- }
- String bareBonesCode= bareBonesText.toString(); // convert stringBuilder to String
- System.out.println(bareBonesCode);
- bareBonesInterpreter(bareBonesCode);
- }
- public static void bareBonesInterpreter(String bareBonesCode) { // takes full line of code and seperates them to current line and rest of lines
- int lineCount = bareBonesCode.length() - bareBonesCode.replaceAll(";", "").length(); // counts the number of lines by counting ;
- if (lineCount == 0) { // if no more lines left
- System.out.println("Ended Success");
- System.exit(0);
- }
- if (lineCount > 1){
- String[] linePart = bareBonesCode.split(";", 2); // splits code into current line and rest of code
- System.out.println(linePart[0]); // print each line to track
- if (linePart[0].indexOf("while") != -1) // at while loop initiate while method
- linePart[1] = bareBonesWhile(bareBonesCode);
- bareBonesCommand(linePart[0]); // send current line to command method
- bareBonesInterpreter(linePart[1]); // send rest of code recursively to interpreter method
- }
- else{
- System.out.println(bareBonesCode);
- bareBonesCommand(bareBonesCode);
- }
- }
- public static void bareBonesCommand(String bareBonesLine){ // takes line of code and interprets command
- String[] linePart = bareBonesLine.split(" "); // split to command and variable
- linePart[1] = linePart[1].substring(0,1); // remove ; from variable
- if (savedVar.get(linePart[1]) == null) { // if new variable, create new var in hashmap
- savedVar.put(linePart[1], 0);
- System.out.println("New variable: " + linePart[1] + " Declared at 0.");
- } else {
- switch (linePart[0]) { // switch for different commands
- case "clear":
- savedVar.put(linePart[1], 0);
- break;
- case "incr":
- savedVar.put(linePart[1], savedVar.get(linePart[1]) + 1);
- break;
- case "decr":
- savedVar.put(linePart[1], savedVar.get(linePart[1]) - 1);
- break;
- }
- System.out.println(linePart[1] + ", Value: " + savedVar.get(linePart[1])); // to keep track of change
- }
- }
- public static String bareBonesWhile(String loopCode) { // while method to handle while loops
- String[] loopPart = loopCode.split(";",2); // split loop to interpret while condition
- String loopVar = loopPart[0].substring(6,7); // remove ; from while variable
- boolean nestedCheck = true; // bool to predict nested loops
- int endIndex =0;
- do {
- endIndex = loopPart[1].indexOf("end;",endIndex + 1); // set string from loop start to first END keyword
- int whileCount = (loopPart[1].substring(0, endIndex).length() - loopPart[1].substring(0, endIndex).replaceAll("while", "").length()) /5; // count whiles between current length
- int endCount = (loopPart[1].substring(0, endIndex).length() - loopPart[1].substring(0, endIndex).replaceAll("end", "").length() ) /3; // count ends between current length
- if (whileCount == endCount) { // if there are whiles equal to ends then it is not nested
- nestedCheck = false;
- }
- } while (nestedCheck);
- String loop = loopPart[1].substring(0,endIndex); // set loop codes string
- 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
- bareBonesInterpreter(loop);
- }
- return loopPart[1].substring(endIndex+4); // after loop send remainder of code String
- }
- }
Add Comment
Please, Sign In to add comment