public static INumerable BuildNumericTernary(List <Token> tokens) { IBoolable condition = BoolableBuilder.Build(GetTernaryCondition(tokens)); if (condition.IsNull()) { return(null); } INumerable confirmationCase = NumerableBuilder.Build(GetTernaryConfirmation(tokens)); if (confirmationCase.IsNull()) { return(null); } INumerable negationCase = NumerableBuilder.Build(GetTernaryNegation(tokens)); if (negationCase.IsNull()) { return(null); } return(new NumericTernary(condition, confirmationCase, negationCase)); }
public static ISubcommand BuildWhere(List <Token> tokens) { IBoolable iboo = BoolableBuilder.Build(tokens); if (iboo.IsNull()) { throw new SyntaxErrorException("ERROR! In list declaration there is something wrong with expression: where."); } else { return(new Where(iboo)); } }
public NegatedBoolable(IBoolable content) { this.content = content; }
public Where(IBoolable condition) { this.condition = condition; }
public IfOpenning(IBoolable condition, int commandNumber) { this.condition = condition; this.commandNumber = commandNumber; }
public StringTernary(IBoolable condition, IStringable leftValue, IStringable rightValue) { this.condition = condition; this.leftValue = leftValue; this.rightValue = rightValue; }
public static List <ICommand> Build(List <Token> tokens) { List <Token> currentTokens = new List <Token>(); List <ICommand> commands = new List <ICommand>(); int waitingArrowsEndings = 0; int arrowDepth = 0; foreach (Token tok in tokens) { if (tok.GetTokenType().Equals(TokenType.Semicolon)) { if (currentTokens.Count > 0) { commands.Add(SingleCommandFactory.Build(currentTokens)); currentTokens.Clear(); } if (arrowDepth == 0 && waitingArrowsEndings > 0) { waitingArrowsEndings.Times(() => commands.Add(new BracketOff())); waitingArrowsEndings = 0; } } else if (tok.GetTokenType().Equals(TokenType.CurlyBracketOff)) { if (currentTokens.Count > 0) { commands.Add(SingleCommandFactory.Build(currentTokens)); currentTokens.Clear(); } commands.Add(new BracketOff()); InterVariables.GetInstance().BracketsDown(); if (waitingArrowsEndings > 0) { arrowDepth--; } if (arrowDepth == 0 && waitingArrowsEndings > 0) { waitingArrowsEndings.Times(() => commands.Add(new BracketOff())); waitingArrowsEndings = 0; } } else if (tok.GetTokenType().Equals(TokenType.CurlyBracketOn)) { if (currentTokens.Count == 0) { commands.Add(new EmptyOpenning()); } else { TokenType first = currentTokens.First().GetTokenType(); if (first.Equals(TokenType.If)) { currentTokens.RemoveAt(0); if (currentTokens.Count == 0) { throw new SyntaxErrorException("ERROR! Expression 'if' is empty."); } IBoolable iboo = BoolableBuilder.Build(currentTokens); if (iboo.IsNull()) { throw new SyntaxErrorException("ERROR! There is something wrong with expression 'if'."); } commands.Add(new IfOpenning(iboo, commands.Count)); currentTokens.Clear(); } else if (first.Equals(TokenType.Else)) { currentTokens.RemoveAt(0); if (currentTokens.Count == 0) { commands.Add(new ElseOpenning()); } else { throw new SyntaxErrorException("ERROR! Expression 'else' contains not necessary code."); } } else if (first.Equals(TokenType.While)) { currentTokens.RemoveAt(0); if (currentTokens.Count == 0) { throw new SyntaxErrorException("ERROR! Expression 'while' is empty."); } IBoolable iboo = BoolableBuilder.Build(currentTokens); if (iboo.IsNull()) { throw new SyntaxErrorException("ERROR! There is something wrong with expression 'while'."); } commands.Add(new WhileOpenning(iboo, commands.Count)); currentTokens.Clear(); } else if (first.Equals(TokenType.Inside)) { currentTokens.RemoveAt(0); if (currentTokens.Count == 0) { throw new SyntaxErrorException("ERROR! Expression 'inside' is empty."); } IListable ilist = ListableBuilder.Build(currentTokens); if (ilist.IsNull()) { throw new SyntaxErrorException("ERROR! There is something wrong with expression 'inside'."); } commands.Add(new InsideOpenning(ilist, commands.Count)); currentTokens.Clear(); } else { IListable ilist = ListableBuilder.Build(currentTokens); if (ilist.IsNull()) { throw new SyntaxErrorException("ERROR! There is something wrong with List Loop / Numeric Loop."); } if (ilist is INumerable) { commands.Add(new NumericLoopOpenning(ilist as INumerable, commands.Count)); } else { commands.Add(new ListLoopOpenning(ilist, commands.Count)); } currentTokens.Clear(); } } InterVariables.GetInstance().BracketsUp(); if (waitingArrowsEndings > 0) { arrowDepth++; } } else if (tok.GetTokenType().Equals(TokenType.BigArrow)) { if (currentTokens.Count == 0) { throw new SyntaxErrorException("ERROR! Left side of Big Arrow Function is empty."); } else { IListable ilist = ListableBuilder.Build(currentTokens); if (ilist.IsNull()) { throw new SyntaxErrorException("ERROR! There is something wrong with List Loop / Numeric Loop."); } if (ilist is INumerable) { commands.Add(new NumericLoopOpenning(ilist as INumerable, commands.Count)); } else { commands.Add(new ListLoopOpenning(ilist, commands.Count)); } currentTokens.Clear(); waitingArrowsEndings++; } } else { currentTokens.Add(tok); } } if (currentTokens.Count > 0) { commands.Add(SingleCommandFactory.Build(currentTokens)); } if (waitingArrowsEndings > 0) { waitingArrowsEndings.Times(() => commands.Add(new BracketOff())); } return(commands); }
public NumericTernary(IBoolable condition, INumerable leftValue, INumerable rightValue) { this.condition = condition; this.leftValue = leftValue; this.rightValue = rightValue; }
private static IBoolable BuildExpression(List <Token> tokens) { // turn list of tokens into list of BoolExpressionElements // they are in usual infix notation // when this is done, their order is changed to Reverse Polish Notation // meanwhile check, if it all can be represented as simple one negated IBoolable // finally build BoolExpression List <IBoolExpressionElement> infixList = new List <IBoolExpressionElement>(); List <Token> currentTokens = new List <Token>(); bool readingFunction = false; Token previousToken = new Token(TokenType.Null); // first, merge many tokens into fewer number of IBoolables foreach (Token tok in tokens) { bool actionDone = false; if (TokenGroups.IsLogicSign(tok.GetTokenType())) { if (readingFunction) { if (Brackets.AllBracketsClosed(currentTokens)) { IBoolable ibo = BoolableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); readingFunction = false; infixList.Add(new BoolExpressionOperator(GetBEOT(tok.GetTokenType()))); } else { currentTokens.Add(tok); } } else { if (currentTokens.Count > 0) { IBoolable ibo = BoolableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); } infixList.Add(new BoolExpressionOperator(GetBEOT(tok.GetTokenType()))); } actionDone = true; } if (tok.GetTokenType().Equals(TokenType.BracketOn)) { if (readingFunction) { currentTokens.Add(tok); } else { if (currentTokens.Count == 1 && previousToken.GetTokenType().Equals(TokenType.Variable)) { currentTokens.Add(tok); readingFunction = true; } else { if (currentTokens.Count > 0) { IBoolable ibo = BoolableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); } infixList.Add(new BoolExpressionOperator(BoolExpressionOperatorType.BracketOn)); } } actionDone = true; } if (tok.GetTokenType().Equals(TokenType.BracketOff)) { if (readingFunction) { if (Brackets.AllBracketsClosed(currentTokens)) { IBoolable ibo = BoolableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); readingFunction = false; infixList.Add(new BoolExpressionOperator(BoolExpressionOperatorType.BracketOff)); } else { currentTokens.Add(tok); } } else { if (currentTokens.Count > 0) { IBoolable ibo = BoolableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); } infixList.Add(new BoolExpressionOperator(BoolExpressionOperatorType.BracketOff)); } actionDone = true; } if (!actionDone) { currentTokens.Add(tok); } previousToken = tok; } if (currentTokens.Count > 0) { IBoolable ibo = BoolableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } } // try to build negation of one boolable if (infixList.Count == 2 && (infixList[0] is BoolExpressionOperator) && (infixList[1] is IBoolable) && (infixList[0] as BoolExpressionOperator).GetOperatorType().Equals(BoolExpressionOperatorType.Not)) { return(new NegatedBoolable(infixList[1] as IBoolable)); } // check if value of infixlist can be computed (check order of elements) if (!CheckExpressionComputability(infixList)) { throw new SyntaxErrorException("ERROR! Wrong syntax of logic expression."); } // if everything is right, finally build BoolExpression in RPN return(new BoolExpression(ReversePolishNotation(infixList))); }
public static IBoolable Build(List <Token> tokens) { /// INITIAL CHECKING // check is is empty if (tokens.Count == 0) { throw new SyntaxErrorException("ERROR! Variable declaration is empty."); } // check if contains not allowed tokens Token wwtok = TokenGroups.WrongTokenInExpression(tokens); if (!wwtok.GetTokenType().Equals(TokenType.Null)) { return(null); } // check brackets if (!Brackets.CheckCorrectness(tokens)) { return(null); } // remove first and last bracket if it is there while (tokens[0].GetTokenType().Equals(TokenType.BracketOn) && tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.BracketOff) && !Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal)) { List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList(); tokensCopy.RemoveAt(tokens.Count - 1); tokensCopy.RemoveAt(0); tokens = tokensCopy; } // check is is empty again after removing brackets if (tokens.Count == 0) { throw new SyntaxErrorException("ERROR! Variable declaration is empty."); } /// BOOL BUILDING // try to build simple one-element Boolable if (tokens.Count == 1) { if (tokens[0].GetTokenType().Equals(TokenType.Variable)) { string str = tokens[0].GetContent(); if (InterVariables.GetInstance().Contains(str, InterVarType.Bool)) { return(new BoolVariableRefer(str)); } else { return(null); } } if (tokens[0].GetTokenType().Equals(TokenType.BoolConstant)) { if (tokens[0].GetContent().Equals("true")) { return(new BoolConstant(true)); } else { return(new BoolConstant(false)); } } } // try to build IN function if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.In)) { IBoolable iboo = BuildIn(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build LIKE function if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Like)) { IBoolable iboo = BuildLike(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build BETWEEN function if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.And) && TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Between)) { IBoolable iboo = BuildBetween(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build time comparison IS AFTER/IS BEFORE if (ContainsOneTimeComparingToken(tokens)) { IBoolable iboo = BuildTimeComparison(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build comparison = != > < >= <= if (ContainsOneComparingToken(tokens)) { IBoolable iboo = BuildComparison(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build bool ternary if (TernaryBuilder.IsPossibleTernary(tokens)) { IBoolable iboo = TernaryBuilder.BuildBoolTernary(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build bool function if (Functions.IsPossibleFunction(tokens)) { IBoolable iboo = BoolFunction.Build(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build expression: many elements with operators or, and, xor, not if (ContainsLogicTokens(tokens)) { return(BuildExpression(tokens)); } else { return(null); } }
public static ICommand Build(List <Token> tokens) { string name = tokens[0].GetContent(); tokens.RemoveAt(0); tokens.RemoveAt(0); if (tokens.Count == 0) { throw new SyntaxErrorException("ERROR! Variable " + name + " has no assigned value."); } if (name.Contains('.')) { return(BuildWithPoint(tokens, name)); } if (!InterVariables.GetInstance().Contains(name)) { IListable value = ListableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! There are is something wrong with assigning value to variable " + name + "."); } if (value is IBoolable) { InterVariables.GetInstance().Add(name, InterVarType.Bool); return(new BoolDeclaration(name, (IBoolable)value)); } else if (value is INumerable) { InterVariables.GetInstance().Add(name, InterVarType.Number); return(new NumericDeclaration(name, (INumerable)value)); } else if (value is ITimeable) { InterVariables.GetInstance().Add(name, InterVarType.Time); return(new TimeDeclaration(name, (ITimeable)value)); } else if (value is IStringable) { InterVariables.GetInstance().Add(name, InterVarType.String); return(new StringDeclaration(name, (IStringable)value)); } else { InterVariables.GetInstance().Add(name, InterVarType.List); return(new ListDeclaration(name, value)); } } else { InterVar ivar = InterVariables.GetInstance().GetVar(name); if (ivar.IsBool()) { IBoolable value = BoolableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be logical."); } return(new BoolDeclaration(name, value)); } else { if (ivar.IsNumber()) { INumerable value = NumerableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be numeric."); } return(new NumericDeclaration(name, value)); } else if (ivar.IsTime()) { ITimeable value = TimeableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be time."); } return(new TimeDeclaration(name, value)); } else if (ivar.IsString()) { IStringable value = StringableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be text."); } return(new StringDeclaration(name, value)); } else { IListable value = ListableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! Value assigned to variable " + name + " must be list."); } return(new ListDeclaration(name, value)); } } } }
public TimeTernary(IBoolable condition, ITimeable leftValue, ITimeable rightValue) { this.condition = condition; this.leftValue = leftValue; this.rightValue = rightValue; }
public BoolDeclaration(string name, IBoolable value) { this.name = name; this.value = value; }
public While(IBoolable condition, int commandNumber) { this.condition = condition; this.commandNumber = commandNumber; }
public ListTernary(IBoolable condition, IListable leftValue, IListable rightValue) { this.condition = condition; this.leftValue = leftValue; this.rightValue = rightValue; }
public static INumerable Build(List <Token> tokens) { // try to build Boolable IBoolable ibo = BoolableBuilder.Build(tokens); if (!ibo.IsNull()) { return(ibo as INumerable); } // remove first and last bracket if it is there while (tokens[0].GetTokenType().Equals(TokenType.BracketOn) && tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.BracketOff) && !Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal)) { List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList(); tokensCopy.RemoveAt(tokens.Count - 1); tokensCopy.RemoveAt(0); tokens = tokensCopy; } // try to build simple one-token Numerable if (tokens.Count == 1) { if (tokens[0].GetTokenType().Equals(TokenType.Variable)) { string str = tokens[0].GetContent(); if (InterVariables.GetInstance().Contains(str, InterVarType.Number)) { return(new NumericVariableRefer(str)); } else { // try to build reference to element of time variable INumerable inum = BuildTimeVariableRefer(tokens[0]); if (!inum.IsNull()) { return(inum); } } } if (tokens[0].GetTokenType().Equals(TokenType.NumericConstant)) { return(new NumericConstant(tokens[0].GetNumericContent())); } } // try to build numeric ternary if (TernaryBuilder.IsPossibleTernary(tokens)) { INumerable inum = TernaryBuilder.BuildNumericTernary(tokens); if (!inum.IsNull()) { return(inum); } } // try to build "count" and "count inside" if (tokens[0].GetTokenType().Equals(TokenType.Count)) { INumerable inum = BuildCount(tokens); if (!inum.IsNull()) { return(inum); } } // try to build numeric function if (Functions.IsPossibleFunction(tokens)) { INumerable inu = NumericFunction.Build(tokens); if (!inu.IsNull()) { return(inu); } } // try to build expression: many elements with operators +, -, *, /, % if (TokenGroups.ContainsArithmeticTokensOutsideBrackets(tokens)) { return(BuildExpression(tokens)); } else { return(null); } }