bool FindStartingToken(TokenizedWord firstWord, Statement statment) { bool foundStartingToken = false; foreach (var token in firstWord.Tokens) if (token is Generic) { var generictoken = (Generic)token; foundStartingToken = statment.startingTokens.Any(t => t == generictoken.genericType); if (foundStartingToken) { var type = generictoken.genericType; if (statment.statementType != StatementType.Assignment) { if (type == GenericType.Intvariable || type == GenericType.StringVariable || type == GenericType.RealVariable) { if (!handler.IsDeclared(firstWord.Value)) return false; else return true; } return foundStartingToken; } return foundStartingToken; } } return foundStartingToken; }
public bool HandleStatement(Statement statement, TokenizedWord[] sentence) { currentStatement = statement; switch (statement.statementType) { case StatementType.Arithmatic: return HandleArithmatic(sentence); case StatementType.Assignment: return HandleAssignment(sentence); case StatementType.Comment: return HandleComment(sentence); case StatementType.ConditionalExpression: return HandleLogical(sentence) || HandleRelational(sentence); case StatementType.End: if (Ended != null) Ended(); return true; case StatementType.IFThen: break; case StatementType.Print: return HandlePrint(sentence); case StatementType.Read: return HandleRead(sentence); } return false; }
public bool Tokenize(string[] sentence) { Token[] identifiedTokensInWord = null; foreach (var word in sentence) { string curString = string.Empty; for (int i = 0; i < word.Length; i++) { curString += word[i]; identifiedTokensInWord = tokens.Verify(curString); } if (identifiedTokensInWord != null && identifiedTokensInWord.Length > 0) { var tokenizedWord = new TokenizedWord(word, identifiedTokensInWord); TokenizedSentence.Add(tokenizedWord); } } if (SentenceTokenized != null && TokenizedSentence.Count > 0) { SentenceTokenized(TokenizedSentence.ToArray()); TokenizedSentence = new List<TokenizedWord>(); return true; } return false; }
public Statement[] Verify(TokenizedWord[] tokenizedSentence) { var returnedStatements = new List<Statement>(); foreach (var statement in statements) { if (FindStartingToken(tokenizedSentence[0], statement)) { if (handler.HandleStatement(statement, tokenizedSentence)) { Logger.Log("Found STATEMENT: " + statement.statementType.ToString()); } } } return returnedStatements.ToArray(); }
private void OnSentenceTockenized(TokenizedWord[] sentence) { if (modeType == Mode.TokenVerifier) { foreach (var word in sentence) { Console.WriteLine("Identified " + word.Value + " as: "); foreach (var token in word.Tokens) { Console.WriteLine("Token type:" + token.type); if (token is Generic) Console.WriteLine("Generic type:" + ((Generic)token).genericType); if (token is Operator) Console.WriteLine("Operator type:" + ((Operator)token).operatorType); } } }else statementIdentifier.Verify(sentence); }
bool CheckTokens(TokenizedWord word, Statement statment) { var tokens = word.Tokens.Where(t => t is Generic).Select(t => (Generic)t).ToArray(); return tokens.Any(t => statment.followUpTokens.Any(fT => t.genericType == fT)); }
bool HandleLogical(TokenizedWord[] sentence) { Variable firstParameter = null; Variable secondParameter = null; if (sentence.Length >= 3) { var isOperator = sentence[1].Tokens.Any(t => t is Operator) && sentence[1].Tokens.FirstOrDefault(t => t is Operator) is ArithmaticOperator; if (!isOperator) return false; var startingTokens = sentence[0].GenericTypes; if (sentence[0].VariableTypes.Length > 0) { firstParameter = variables.FirstOrDefault(v => v.VariableName == sentence[0].Value); if (firstParameter == null) { Logger.LogError("Variable: " + sentence[0].Value + " is not Declated."); return false; } } var opToken = sentence[1].Tokens.FirstOrDefault(t => t is Operator); if (opToken is LogicalOperator) { var logicalToken = (LogicalOperator)opToken; var thirdTokenTypes = sentence[2].GenericTypes; if (thirdTokenTypes.Any(t => types.Any(ty => ty == t))) { if (sentence[2].VariableTypes.Length > 0) { secondParameter = variables.FirstOrDefault(v => v.VariableName == sentence[2].Value); if (secondParameter == null) { Logger.LogError("Variable: " + sentence[2].Value + " not declared."); return false; } } var param1 = firstParameter == null ? sentence[0].Value : firstParameter.Value; var param2 = secondParameter == null ? sentence[2].Value : secondParameter.Value; string answer = string.Empty; switch (logicalToken.opType) { case LogicalOperatorType.And: answer = (double.Parse(param1) + double.Parse(param2)).ToString(); break; case LogicalOperatorType.Or: answer = (double.Parse(param1) / double.Parse(param2)).ToString(); break; case LogicalOperatorType.Not: answer = (double.Parse(param1) * double.Parse(param2)).ToString(); break; } Logger.LogWarning("Answer: " + answer); return true; } else { Logger.LogError("Did not find expecting token. Verify second value."); return false; } } } return false; }
bool HandleArithmatic(TokenizedWord[] sentence) { Result result; Variable firstParameter = null; Variable secondParameter = null; if (sentence.Length >= 3) { var isOperator = sentence[1].Tokens.Any(t => t is Operator) && sentence[1].Tokens.FirstOrDefault(t => t is Operator) is ArithmaticOperator; if (!isOperator) return false; if (sentence[0].VariableTypes.Length > 0) { firstParameter = variables.FirstOrDefault(v => v.VariableName == sentence[0].Value); if (firstParameter == null) { Logger.LogError("Variable: " + sentence[0].Value + " is not Declared."); return false; } } var opToken = sentence[1].Tokens.FirstOrDefault(t => t is Operator); if (opToken is ArithmaticOperator) { var arithToken = (ArithmaticOperator)opToken; var thirdTokenTypes = sentence[2].GenericTypes; if (thirdTokenTypes.Any(t => numberTypes.Any(ty => ty == t))) { if (sentence[2].VariableTypes.Length > 0) { secondParameter = variables.FirstOrDefault(v => v.VariableName == sentence[2].Value); if (secondParameter == null) { Logger.LogError("Variable: " + sentence[2].Value + " not declared."); return false; } } var param1 = firstParameter == null ? sentence[0].Value : firstParameter.Value; var param2 = secondParameter == null ? sentence[2].Value : secondParameter.Value; var firstIsString = sentence[0].VariableTypes.Length > 0 && (sentence[0].VariableTypes.Contains(GenericType.StringVariable) || sentence[0].GenericTypes.Contains(GenericType.Strng)); string answer = string.Empty; switch (arithToken.opType) { case ArithmaticOperatorType.Add: if (!firstIsString) answer = (double.Parse(param1) + double.Parse(param2)).ToString(); else answer = param1 + param2; break; case ArithmaticOperatorType.Divide: if (!firstIsString) answer = (double.Parse(param1) / double.Parse(param2)).ToString(); else { Logger.LogError("String cannot be devided."); return false; } break; case ArithmaticOperatorType.Multiply: if(!firstIsString) answer = (double.Parse(param1) * double.Parse(param2)).ToString(); else{ Logger.LogError("String cannot be multiplied."); return false; } break; case ArithmaticOperatorType.Substract: if(!firstIsString) answer = (double.Parse(param1) - double.Parse(param2)).ToString(); else { Logger.LogError("String cannot be subsctracted."); return false; } break; } Logger.LogWarning("Answer: " + answer); return true; } else { Logger.LogError("Did not find expecting token. Verify second value."); return false; } } } return false; }
bool HandlePrint(TokenizedWord[] sentence) { var followUpTypes = currentStatement.followUpTokens; if (sentence.Length == 1) { Logger.Log(""); return true; } var tokenTypes = sentence[1].GenericTypes; if(tokenTypes.Any(t => t == GenericType.Intvariable || t == GenericType.StringVariable || t == GenericType.RealVariable)) { var variable = variables.FirstOrDefault(v => v.VariableName == sentence[1].Value); if(variable != null) Logger.Log(variable.Value); else Logger.LogError("Variable not declared."); return true; }else { if (tokenTypes.Any(tT => followUpTypes.Any(fT => tT == fT))) { Logger.Log(sentence[0].Value); return true; } } return false; }
bool HandleRead(TokenizedWord[] sentence) { if (sentence.Length > 1 && sentence[1].VariableTypes.Length > 0) { var variable = variables.FirstOrDefault(v => v.VariableName == sentence[1].Value); var type = sentence[1].VariableTypes[0]; Console.WriteLine("Enter Value: "); var value = Console.ReadLine(); switch (type) { case GenericType.Intvariable: int number; var isInt = int.TryParse(value, out number); if (isInt) { if (variable != null) variable.SetValue(number.ToString()); else { variable = new Variable(sentence[1].Value, number.ToString(), GenericType.Integer); variables.Add(variable); } return true; } else { Logger.LogError("Value is not an integer."); return false; } case GenericType.RealVariable: double real; var isReal = double.TryParse(value, out real); if (isReal) { if (variable != null) variable.SetValue(real.ToString()); else { variable = new Variable(sentence[1].Value, real.ToString(), GenericType.Real); variables.Add(variable); } return true; } else { Logger.LogError("Value is not a real number."); return false; } case GenericType.StringVariable: if (value[0].Equals('"') && value[value.Length - 1].Equals('"')) { var strng = value.Trim(new Char[]{ '"'}); if (variable != null) variable.SetValue(strng); else { variable = new Variable(sentence[1].Value, strng, GenericType.Strng); variables.Add(variable); } return true; } else { Logger.LogError("Value is not a string."); return false; } default: Logger.LogError("Did not find a variable."); return false; } } else { Logger.LogError("Did not find a variable."); return false; } }
bool HandleComment(TokenizedWord[] sentence) { var types = sentence[0].GenericTypes; if(types.Any(t => t == GenericType.Comment)) { var nextWords = sentence.Where(w => w != sentence[0]).ToArray(); var tokens = nextWords.Select(t => t.GenericTypes).ToArray(); return tokens.All(t => t.Any(type => type == GenericType.AnyString)); } return false; }
bool HandleAssignment(TokenizedWord[] sentence) { if (sentence.Length >= 3) { var firstTokenTypes = sentence[0].GenericTypes; var secondTokenTypes = sentence[1].GenericTypes; if (secondTokenTypes.Any(t => t == GenericType.Assignment)) foreach (var type in firstTokenTypes) if (type == GenericType.Intvariable || type == GenericType.RealVariable || type == GenericType.StringVariable) switch (type) { case GenericType.StringVariable: if(sentence[2].GenericTypes.Any(t => t == GenericType.Strng || t == type)) { Variable secondVariable = null; if(sentence[2].VariableTypes.Length > 0) { secondVariable = variables.FirstOrDefault(v => v.VariableName == sentence[2].Value); if(secondVariable == null) { Logger.LogError("Variable: " + sentence[2].Value + " is not declared."); return false; } } var variable = variables.FirstOrDefault(v => v.VariableName == sentence[0].Value); var value = secondVariable == null ? sentence[2].Value : secondVariable.Value; if(variable == null) variables.Add(new Variable(sentence[0].Value, value, GenericType.Strng)); else variable.SetValue(value); return true; } break; case GenericType.RealVariable: if (sentence[2].GenericTypes.Any(t => t == GenericType.Real || t == type)) { var variable = variables.FirstOrDefault(v => v.VariableName == sentence[0].Value); if (variable == null) variables.Add(new Variable(sentence[0].Value, sentence[2].Value, GenericType.Real)); else variable.SetValue(sentence[2].Value); return true; } break; case GenericType.Intvariable: if (sentence[2].GenericTypes.Any(t => t == GenericType.Integer || t == type)) { var variable = variables.FirstOrDefault(v => v.VariableName == sentence[0].Value); if (variable == null) variables.Add(new Variable(sentence[0].Value, sentence[2].Value, GenericType.Integer)); else variable.SetValue(sentence[2].Value); return true; } break; } } return false; }
bool HandleRelational(TokenizedWord[] sentence) { Variable firstParameter = null; Variable secondParameter = null; if (sentence.Length >= 3) { var isOperator = sentence[1].Tokens.Any(t => t is Operator) && sentence[1].Tokens.FirstOrDefault(t => t is Operator) is ArithmaticOperator; if (!isOperator) return false; var startingTokens = sentence[0].GenericTypes; if (sentence[0].VariableTypes.Length > 0) { firstParameter = variables.FirstOrDefault(v => v.VariableName == sentence[0].Value); if (firstParameter == null) { Logger.LogError("Variable: " + sentence[0].Value + " is not Declated."); return false; } } var opToken = sentence[1].Tokens.FirstOrDefault(t => t is Operator); if (opToken is RelationalOperator) { var relationalToken = (RelationalOperator)opToken; var thirdTokenTypes = sentence[2].GenericTypes; if (thirdTokenTypes.Any(t => types.Any(ty => ty == t))) { if (sentence[2].VariableTypes.Length > 0) { secondParameter = variables.FirstOrDefault(v => v.VariableName == sentence[2].Value); if (secondParameter == null) { Logger.LogError("Variable: " + sentence[2].Value + " not declared."); return false; } } var param1 = firstParameter == null ? sentence[0].Value : firstParameter.Value; var param2 = secondParameter == null ? sentence[2].Value : secondParameter.Value; string answer = string.Empty; var firstIsString = sentence[0].GenericTypes.Any(t => t == GenericType.StringVariable || t == GenericType.Strng); var secondIsString = sentence[2].GenericTypes.Any(t => t == GenericType.StringVariable || t == GenericType.Strng); var isString = firstIsString || secondIsString; switch (relationalToken.opType) { case RelationalOperatorType.Equal: if (!isString) answer = (double.Parse(param1) == double.Parse(param2)).ToString(); else answer = (param1.Equals(param2)).ToString(); break; case RelationalOperatorType.Greaterorequal: if (!isString) answer = (double.Parse(param1) >= double.Parse(param2)).ToString(); else { Logger.LogError("Cannot apply Operator " + relationalToken.opType + " to strings."); return false; } break; case RelationalOperatorType.GreaterThan: if (!isString) answer = (double.Parse(param1) > double.Parse(param2)).ToString(); else { Logger.LogError("Cannot apply Operator " + relationalToken.opType + " to strings."); return false; } break; case RelationalOperatorType.Lessererequal: if(!isString) answer = (double.Parse(param1) <= double.Parse(param2)).ToString(); else { Logger.LogError("Cannot apply Operator " + relationalToken.opType + " to strings."); return false; } break; case RelationalOperatorType.LessThan: if (!isString) answer = (double.Parse(param1) < double.Parse(param2)).ToString(); else { Logger.LogError("Cannot apply Operator " + relationalToken.opType +" to strings."); return false; } break; case RelationalOperatorType.Notequal: if (!isString) answer = (double.Parse(param1) != double.Parse(param2)).ToString(); else answer = (!param1.Equals(param2)).ToString(); break; } Logger.LogWarning("Answer: " + answer); return true; } else { Logger.LogError("Did not find expecting token. Verify second value."); return false; } } } return false; }