/// <summary>Builds the Jump Table</summary> /// <param name="instructions">All program instructions.</param> /// <returns>Dictionary object representing the Jump table</returns> /// <remarks>Line numbers start at zero</remarks> private Dictionary<string, int> buildJumpTable(Instruction[] instructions) { Dictionary<string, int> temp = new Dictionary<string, int>(); for (int i = 0; i < instructions.Length; i++) { if (instructions[i].getOpcode() == "label") { temp.Add(instructions[i].getOperand(), i); } } return temp; }
/// <summary>Builds the Symbol Table</summary> /// <returns>Dictionary object representing the Symbol table</returns> /// <remarks>If opcode = one of the instructions that can generate symbols, test and see if it is a symbol /// or a number. If it's a symbol and not already present, add it to the symbol table.</remarks> private Dictionary<string, int> buildSymbolTable(Instruction[] instructions) { Dictionary<string, int> temp = new Dictionary<string, int>(); for (int i = 0; i < instructions.Length; i++) { string op = instructions[i].getOpcode(); string oper = instructions[i].getOperand(); if (op == "get" || op == "put" || op == "push" || op == "pop") { int test = 0; Boolean num = Int32.TryParse(oper, out test); if (!num) { if (!temp.ContainsKey(oper)) temp.Add(oper, 0); } } } return temp; }
/// <summary>Creates instructions Instruction array.</summary> /// <param name="lines">String array of all lines of code.</param> /// <returns>Array of instructions representing entire program code.</returns> /// <remarks>Uses RegEx to allow for more WIC formatting options.</remarks> private Instruction[] convertToInstructions(string[] lines) { Instruction[] instructions = new Instruction[lines.Length]; for (int i = 0; i < lines.Length; i++) { // Regular expression to capture the Opcode/Operand and test the formatting of the input line. Regex testInput = new Regex("\\|.*|[\t\x20]*([a-zA-Z0-9]+)[\t\x20]*([a-zA-Z0-9]*)\\s*[^\\S]*\\|?.*|\\s*"); Match m = testInput.Match(lines[i]); if (m.Success) { // Case the operation and operand to lowercase and trim any whitespace. string op = m.Groups[1].Value.ToLower().Trim(); string oper = m.Groups[2].Value.ToLower().Trim(); if (op == "") op = "nop"; // Attempt to create an instruction from the operation/operand. instructions[i] = createInstructionObject(op, oper, i); } else { // If invalid format, exit. Console.WriteLine("Invalid instruction format entered!"); Console.WriteLine("*** Exiting Interpreter ***"); Environment.Exit(1); } } return instructions; }