private void AddToConTable(ref string token, ref int i) { if (OutputTokenTable.Table.Count > 1) { string penultimateToken = OutputTokenTable.Table[OutputTokenTable.Table.Count - 2].Name; if (OutputTokenTable.Table.Last().Name.Equals("=") && IdnTable.Contains(penultimateToken)) { if (Checker.GetType(token) == IdnTable.GetType(penultimateToken)) { ConTable.Add(token, IdnTable.GetType(penultimateToken)); } else { throw new Exception($"Error on {i + 1} line!\tInvalid type of constant. Unable to cast " + $"'{Checker.GetType(token)}' to '{IdnTable.GetType(penultimateToken)}'"); } } else if (!ConTable.Contains(token)) { ConTable.Add(token); } } else if (!ConTable.Contains(token)) { ConTable.Add(token); } }
private void AddToIdnTable(ref string token, ref int i) { if (OutputTokenTable.Table.Count != 0 && Checker.IsType(OutputTokenTable.Table.Last().Name)) { if (IdnTable.Contains(token)) { throw new Exception($"Error on {i + 1} line!\tThe variable '{token}' is already declared."); } else { IdnTable.Add(token); } } else if (OutputTokenTable.Table.Count != 0 && OutputTokenTable.Table.Last().Name == ",") { if (IdnTable.Contains(token)) { throw new Exception($"Error on {i + 1} line!\tThe variable '{token}' is already declared."); } else { IdnTable.Add(token); } } else if (!IdnTable.Contains(token) && !TokenTable.Contains(token)) { throw new Exception($"Error on {i + 1} line!\tThe variable '{token}' wasn't declared."); } }
private bool VariablesList(ref int i)// TODO: by daclarations { if (IdnTable.Contains(outputTokens[i].Name)) { if (!Increment(ref i)) { return(false); } if (outputTokens[i].Name == ",") { if (!Increment(ref i)) { return(false); } return(VariablesList(ref i)); } return(true); } else { errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExepted name of variable!"); return(false); } }
private bool Assignment(ref int i) { if (IdnTable.Contains(outputTokens[i].Name)) { if (!Increment(ref i)) { return(false); } if (outputTokens[i].Name == "=") { if (!Increment(ref i)) { return(false); } if (Expression(ref i)) { return(true); } } } return(false); }
public static bool IsAbleToCast(string token) { //output tokens is exist and last token in table is "=" return(OutputTokenTable.Table.Count != 0 && OutputTokenTable.Table.Last().Name == "=" //penultimate token is identifier && IdnTable.Contains(OutputTokenTable.Table[OutputTokenTable.Table.Count - 2].Name) //current token is identifier && IdnTable.Contains(token) //types of tokens on both sides of "=" are equal && (IdnTable.GetType(token) == IdnTable.GetType(OutputTokenTable.Table[OutputTokenTable.Table.Count - 2].Name))); }
private string GetLabelOfToken(string token) { if (IdnTable.Contains(token)) { return("id"); } else if (ConTable.Contains(token)) { return("con"); } else { return(token); } }
private bool Factor(ref int i) { if (outputTokens[i].Name == "(") { if (!Increment(ref i)) { return(false); } if (Expression(ref i)) { if (outputTokens[i].Name == ")") { if (!Increment(ref i)) { return(false); } return(true); } else { errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected ')'"); } } } if (IdnTable.Contains(outputTokens[i].Name)) { if (!Increment(ref i)) { return(false); } return(true); } if (ConTable.Contains(outputTokens[i].Name)) { if (!Increment(ref i)) { return(false); } return(true); } return(false); }
private bool Output(ref int i) { if (outputTokens[i].Name == "cout") { if (!Increment(ref i)) { return(false); } if (outputTokens[i].Name == ">>") { if (!Increment(ref i)) { return(false); } if (IdnTable.Contains(outputTokens[i].Name)) { if (!Increment(ref i)) { return(false); } while ((outputTokens[i].Name == ">>" && outputTokens[i - 1].Name != ">>") || IdnTable.Contains(outputTokens[i].Name)) { if (!Increment(ref i)) { return(false); } } return(true); } else { errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected variable!"); } } else { errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected '>>'!"); } } return(false); }
private double GetOperand() { if (IdnTable.Contains(stack.Peek())) { return((double)IdnTable.GetValue(stack.Pop())); } else if (Double.TryParse(stack.Peek(), NumberStyles.Any, CultureInfo.InvariantCulture, out double operand)) { stack.Pop(); return(operand); } else if (new Regex("r\\d+").IsMatch(stack.Peek())) { return(cycleDesignations[stack.Pop()]); } throw new NotImplementedException(); }
private void ExecuteIO(string operand, string @operator) { switch (@operator) { case ">>": string outputValue = string.Empty; if (IdnTable.Contains(operand)) { outputValue = IdnTable.GetValue(operand).ToString(); } else { outputValue = "undefined"; } consoleWindow.consoleContent.ConsoleOutput.Add(outputValue); break; case "<<": consoleWindow.Focus(); var inputWindow = new InputWindow(operand); inputWindow.ShowDialog(); if (Double.TryParse(inputWindow.inputField.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out double value)) { IdnTable.SetValue(operand, value); } else { MessageBox.Show("Invalid value!"); } break; default: throw new InvalidOperationException(); } }
public void Execute() { double secondOperand, firstOperand; string @operator, lastLabel = string.Empty; bool jumpFlag = false; Regex labelRegex = new Regex("l\\d+"); for (int i = 0; i < Generator.Polish.Count; i++) { string token = Generator.Polish[i]; if (jumpFlag) { if (token == lastLabel) { jumpFlag = false; } continue; } if ((token != "true" && token != "false") && (Checker.IsUnaryOperator(token) || Checker.IsBinaryOperator(token))) { if (Checker.IsBinaryOperator(token)) { @operator = token; if (Checker.IsOperator(@operator)) { secondOperand = GetOperand(); firstOperand = GetOperand(); ExecuteExpression(firstOperand, secondOperand, @operator); AddToViewTable(token, $"{firstOperand} {@operator} {secondOperand}"); } else if (Checker.IsRelation(@operator)) { secondOperand = GetOperand(); firstOperand = GetOperand(); ExecuteRelation(firstOperand, secondOperand, @operator); AddToViewTable(token, $"{firstOperand} {@operator} {secondOperand}"); } else if (Checker.IsIO(@operator)) { ExecuteIO(stack.Pop(), @operator); } else if (@operator == "=") { secondOperand = GetOperand(); string identifierName = stack.Pop(); if (new Regex("r\\d+").IsMatch(identifierName)) { if (cycleDesignations.FirstOrDefault(r => r.Key == identifierName).Key is null) { cycleDesignations.Add(identifierName, secondOperand); } else { cycleDesignations[identifierName] = secondOperand; } } else { IdnTable.SetValue(identifierName, secondOperand); } AddToViewTable(token, $"{identifierName} {@operator} {secondOperand}"); } } else if (Checker.IsUnaryOperator(token)) { if (token == "@") { string operand = stack.Pop(); if (IdnTable.Contains(operand) && IdnTable.GetValue(operand).HasValue) { IdnTable.SetValue(operand, -IdnTable.GetValue(operand).Value); } stack.Push($"-{operand}"); AddToViewTable(token, $"Change sign of {operand}"); } else { string identifier; while (stack.Count > 0) { identifier = stack.Pop(); if (IdnTable.GetType(identifier) != token) { IdnTable.SetType(identifier, token); } } } } } else if (token.Contains("JNE")) { lastLabel = $"{labelRegex.Match(token).Groups[0].Value}:"; bool condition = Boolean.Parse(stack.Pop()); jumpFlag = condition ? false : true; AddToViewTable(token, $"Jump to label {lastLabel} if last condition is false."); } else if (token.Contains("JMP")) { string label = labelRegex.Match(token).Groups[0].Value; i = Generator.LabelsTable[label]; AddToViewTable(token, $"Jump to label {label}:"); } else if (!token.Contains(":")) { stack.Push(token); AddToViewTable(token, "Add input token to stack"); } } }
public bool Parse(string[] programCode) { try { string token = string.Empty; for (int i = 0; i < programCode.Length; i++) { int j = 0; foreach (char ch in programCode[i]) { if (ch.Equals('$')) { OutputTokenTable.Add(numRow: i, ch.ToString()); j++; continue; } if (Checker.IsConstant(ch) || Checker.IsLetter(ch) || (Checker.IsSign(ch) && !string.IsNullOrEmpty(token) && Checker.IsExponent(token.Last()))) { token += ch; } else if (Checker.IsSingleCharacterSeparator(ch) || Checker.IsWhiteSeparator(ch) || Checker.IsPossibleDoubleSeparator(ch)) { AddToInternalTable(ref token, ref i); if (ConTable.Contains(token) || IdnTable.Contains(token) || (TokenTable.Contains(token) && !Checker.IsDoubleSeparator(token + ch))) { OutputTokenTable.Add(numRow: i + 1, token); token = string.Empty; } if (Checker.IsPossibleDoubleSeparator(ch)) { if (!Checker.IsDoubleSeparator(token) && Checker.IsDoubleSeparator(ch.ToString() + ((programCode[i].Length - 1 != j) ? programCode[i][j + 1].ToString() : string.Empty))) { token += ch; j++; continue; } else { OutputTokenTable.Add(numRow: i + 1, token + ch); token = string.Empty; } } else if (Checker.IsSingleCharacterSeparator(ch)) { OutputTokenTable.Add(numRow: i + 1, token + ch); } } else { throw new Exception($"Error on {i + 1} line!\tUndefined exeption."); } j++; } } return(true); } catch (Exception ex) { MessageBox.Show(ex.Message, "Alert", MessageBoxButton.OK); return(false); } }
private bool Cycle(ref int i) { if (outputTokens[i].Name == "for") { if (!Increment(ref i)) { return(false); } if (outputTokens[i].Name == "(") { if (!Increment(ref i)) { return(false); } if (IdnTable.Contains(outputTokens[i].Name)) { if (!Increment(ref i)) { return(false); } if (outputTokens[i].Name == "=") { if (!Increment(ref i)) { return(false); } if (Expression(ref i)) { if (outputTokens[i].Name == ";") { if (!Increment(ref i)) { return(false); } if (Relation(ref i)) { if (outputTokens[i].Name == ";") { if (!Increment(ref i)) { return(false); } if (Expression(ref i)) { if (outputTokens[i].Name == ")") { if (!Increment(ref i)) { return(false); } if (OperatorBlock(ref i)) { return(true); } return(false); } else { errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected ')'!"); } } } else { errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected ';'!"); } } } else { errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected ';'!"); } } } else { errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected '='!"); } } else { errors.Add(" > Error on " + outputTokens[i].Row + " line!\tnNot initiated variable!"); } } else { errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected '('!"); } } return(false); }