public static ITimeable BuildNumNum(string name, List <Argument> args) { if (args.Count != 2) { throw new SyntaxErrorException("ERROR! Function " + name + " has to have 2 numeric arguments."); } INumerable inu1 = NumerableBuilder.Build(args[0].tokens); INumerable inu2 = NumerableBuilder.Build(args[1].tokens); if (inu1.IsNull()) { throw new SyntaxErrorException("ERROR! First argument of function " + name + " cannot be read as number."); } if (inu2.IsNull()) { throw new SyntaxErrorException("ERROR! Second argument of function " + name + " cannot be read as number."); } if (name.Equals("tomorrow")) { return(new FuncTomorrow__2args(inu1, inu2)); } else if (name.Equals("yesterday")) { return(new FuncYesterday__2args(inu1, inu2)); } else if (name.Equals("today")) { return(new FuncToday__2args(inu1, inu2)); } throw new SyntaxErrorException("ERROR! Function " + name + " not identified."); }
// functions are grouped by their arguments // every set of arguments is one method below // exception for substring - has it's own methods public static IStringable BuildNum(string name, List <Argument> args) { if (args.Count != 1) { throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 numeric argument."); } INumerable inu = NumerableBuilder.Build(args[0].tokens); if (inu.IsNull()) { throw new SyntaxErrorException("ERROR! Argument of function " + name + " cannot be read as number."); } if (name.Equals("hex")) { return(new FuncHex(inu)); } else if (name.Equals("binary")) { return(new FuncBinary(inu)); } else if (name.Equals("month")) { return(new FuncMonth(inu)); } else if (name.Equals("weekday")) { return(new FuncWeekday(inu)); } throw new SyntaxErrorException("ERROR! Function " + name + " not identified."); }
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 IStringable BuildStrNumNum(string name, List <Argument> args) { if (args.Count != 3) { throw new SyntaxErrorException("ERROR! Function " + name + " has to have 3 arguments: one text and two numbers."); } IStringable istr = StringableBuilder.Build(args[0].tokens); INumerable inu1 = NumerableBuilder.Build(args[1].tokens); INumerable inu2 = NumerableBuilder.Build(args[2].tokens); if (istr.IsNull()) { throw new SyntaxErrorException("ERROR! First argument of function " + name + " cannot be read as text."); } if (inu1.IsNull()) { throw new SyntaxErrorException("ERROR! Second argument of function " + name + " cannot be read as number."); } if (inu2.IsNull()) { throw new SyntaxErrorException("ERROR! Third argument of function " + name + " cannot be read as number."); } if (name.Equals("substring")) { return(new FuncSubstring__3args(istr, inu1, inu2)); } throw new SyntaxErrorException("ERROR! Function " + name + " not identified."); }
public static IStringable BuildStrNum(string name, List <Argument> args) { if (args.Count != 2) { throw new SyntaxErrorException("ERROR! Function " + name + " has to have 2 arguments: one text and one number."); } IStringable istr = StringableBuilder.Build(args[0].tokens); INumerable inu = NumerableBuilder.Build(args[1].tokens); if (istr.IsNull()) { throw new SyntaxErrorException("ERROR! First argument of function " + name + " cannot be read as text."); } if (inu.IsNull()) { throw new SyntaxErrorException("ERROR! Second argument of function " + name + " cannot be read as number."); } if (name.Equals("filled") || name.Equals("fill")) { return(new FuncFilled(istr, inu)); } else if (name.Equals("repeat") || name.Equals("repeated")) { return(new FuncRepeat(istr, inu)); } else if (name.Equals("substring")) { return(new FuncSubstring__2args(istr, inu)); } throw new SyntaxErrorException("ERROR! Function " + name + " not identified."); }
public static IStringable BuildListElement(List <Token> tokens) { if (Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Square)) { return(null); } string name = tokens[0].GetContent(); tokens.RemoveAt(tokens.Count - 1); tokens.RemoveAt(0); tokens.RemoveAt(0); if (!InterVariables.GetInstance().Contains(name, InterVarType.List)) { throw new SyntaxErrorException("ERROR! List " + name + " not found. Impossible to take element from it."); } INumerable inu = NumerableBuilder.Build(tokens); if (inu.IsNull()) { throw new SyntaxErrorException("ERROR! Impossible to take element from list " + name + ". Index identificator cannot be read as number."); } else { return(new ListElementRefer(name, inu)); } }
public static ITimeable BuildNum(string name, List <Argument> args) { if (args.Count != 1) { throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 numeric argument."); } INumerable inu = NumerableBuilder.Build(args[0].tokens); if (inu.IsNull()) { throw new SyntaxErrorException("ERROR! Argument of function " + name + " cannot be read as number."); } if (name.Equals("newyear")) { return(new FuncNewyear(inu)); } else if (name.Equals("christmas")) { return(new FuncChristmas(inu)); } else if (name.Equals("easter")) { return(new FuncEaster(inu)); } throw new SyntaxErrorException("ERROR! Function " + name + " not identified."); }
public static ISubcommand BuildNumeric(List <Token> tokens, TokenType type) { INumerable inu = NumerableBuilder.Build(tokens); if (inu.IsNull()) { throw new SyntaxErrorException("ERROR! In list declaration there is something wrong with expression: " + GetName(type) + "."); } else { return(new NumericSubcommand(inu, GetNumericType(type))); } }
private static IBoolable BuildBetweenNumbers(INumerable num, List <Token> left, List <Token> right) { INumerable numLeft = NumerableBuilder.Build(left); INumerable numRight = NumerableBuilder.Build(right); if (numLeft.IsNull() || numRight.IsNull()) { return(null); } else { return(new BetweenNumbers(num, numLeft, numRight)); } }
public static INumerable BuildNums(string name, List <Argument> args) { if (args.Count == 0) { throw new SyntaxErrorException("ERROR! Function " + name + " has to have at least one numeric argument."); } List <INumerable> inus = new List <INumerable>(); for (int i = 0; i < args.Count; i++) { INumerable inu = NumerableBuilder.Build(args[i].tokens); if (inu.IsNull()) { throw new SyntaxErrorException("ERROR! Argument " + (i + 1) + " of function " + name + " cannot be read as number."); } else { inus.Add(inu); } } if (name.Equals("max")) { return(new FuncMax(inus)); } else if (name.Equals("min")) { return(new FuncMin(inus)); } else if (name.Equals("average") || name.Equals("avg") || name.Equals("mean")) { return(new FuncAverage(inus)); } else if (name.Equals("sum")) { return(new FuncSum(inus)); } else if (name.Equals("product")) { return(new FuncProduct(inus)); } throw new SyntaxErrorException("ERROR! Function " + name + " not identified."); }
private static IBoolable BuildBetween(List <Token> tokens) { int betweenIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.Between); int andIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.And); if (andIndex < betweenIndex) { return(null); } if (betweenIndex == andIndex - 1) { throw new SyntaxErrorException("ERROR! Expression 'between' do not contain definition of value of left boundary."); } if (betweenIndex == 0) { throw new SyntaxErrorException("ERROR! Expression 'between' starts with keyword 'between' and thus do not contain comparing value."); } if (andIndex == tokens.Count - 1) { throw new SyntaxErrorException("ERROR! Expression 'between' ends with keyword 'and' and thus do not contain definition of value of right boundary."); } List <Token> valueTokens = tokens.Take(betweenIndex).ToList(); List <Token> leftTokens = tokens.GetRange(betweenIndex + 1, andIndex - betweenIndex - 1); List <Token> rightTokens = tokens.Skip(andIndex + 1).ToList(); INumerable inum = NumerableBuilder.Build(valueTokens); if (!inum.IsNull()) { return(BuildBetweenNumbers(inum, leftTokens, rightTokens)); } ITimeable itim = TimeableBuilder.Build(valueTokens); if (!itim.IsNull()) { return(BuildBetweenTimes(itim, leftTokens, rightTokens)); } return(null); }
public static ICommand Build(List <Token> tokens) { tokens.RemoveAt(0); if (tokens.Count == 0) { return(new Sleep(new NumericConstant(1))); } INumerable inum = NumerableBuilder.Build(tokens); if (inum.IsNull()) { throw new SyntaxErrorException("ERROR! Sleeping time cannot be read as number."); } else { return(new Sleep(inum)); } }
public static ICommand Build(List <Token> tokens) { string name = tokens[0].GetContent(); tokens.RemoveAt(0); tokens.RemoveAt(0); List <Token> indexTokens = new List <Token>(); while (tokens.First().GetTokenType() != TokenType.SquareBracketOff) { indexTokens.Add(tokens.First()); tokens.RemoveAt(0); } tokens.RemoveAt(0); INumerable index = NumerableBuilder.Build(indexTokens); if (index.IsNull()) { throw new SyntaxErrorException("ERROR! Index of element of list " + name + " cannot be read as number."); } if (tokens.First().GetTokenType() != TokenType.Equals) { return(null); } tokens.RemoveAt(0); IStringable newValue = StringableBuilder.Build(tokens); if (newValue.IsNull()) { return(null); } return(new ListElementDeclaration(name, newValue, index)); }
public static ICommand Build(List <Token> tokens) { string name = tokens[0].GetContent(); TokenType type = tokens[1].GetTokenType(); tokens.RemoveAt(0); tokens.RemoveAt(0); if (InterVariables.GetInstance().ContainsChangable(name, InterVarType.Number) && !InterVariables.GetInstance().Contains(name, InterVarType.Bool)) { INumerable value = NumerableBuilder.Build(tokens); if (value.IsNull()) { throw new SyntaxErrorException("ERROR! Changing value of variable " + name + " cannot be performed, because expression value is not a number."); } switch (type) { case TokenType.PlusEquals: return(new IncrementBy(name, value)); case TokenType.MinusEquals: return(new DecrementBy(name, value)); case TokenType.MultiplyEquals: return(new MultiplyBy(name, value)); case TokenType.DivideEquals: return(new DivideBy(name, value)); case TokenType.PercentEquals: return(new ModuloBy(name, value)); } } throw new SyntaxErrorException("ERROR! Value of variable " + name + " cannot be changed."); }
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)); } } } }
private static ICommand BuildWithPoint(List <Token> tokens, string name) { if (name.Count(c => c == '.') > 1) { throw new SyntaxErrorException("ERROR! Variable " + name + " contains multiple dot signs and because of that misguides compiler."); } string leftSide = name.Substring(0, name.IndexOf('.')).ToLower(); string rightSide = name.Substring(name.IndexOf('.') + 1).ToLower(); if (leftSide.Length == 0 || rightSide.Length == 0) { return(null); } if (InterVariables.GetInstance().Contains(leftSide, InterVarType.Time)) { if (!InterVariables.GetInstance().ContainsChangable(leftSide, InterVarType.Time)) { throw new SyntaxErrorException("ERROR! Variable " + leftSide + " cannot be modified."); } INumerable inum = NumerableBuilder.Build(tokens); if (inum.IsNull()) { return(null); } switch (rightSide) { case "year": return(new TimeElementDeclaration(leftSide, TimeVariableType.Year, inum)); case "month": return(new TimeElementDeclaration(leftSide, TimeVariableType.Month, inum)); case "day": return(new TimeElementDeclaration(leftSide, TimeVariableType.Day, inum)); case "weekday": return(new TimeElementDeclaration(leftSide, TimeVariableType.WeekDay, inum)); case "hour": return(new TimeElementDeclaration(leftSide, TimeVariableType.Hour, inum)); case "minute": return(new TimeElementDeclaration(leftSide, TimeVariableType.Minute, inum)); case "second": return(new TimeElementDeclaration(leftSide, TimeVariableType.Second, inum)); case "clock": throw new SyntaxErrorException("ERROR! Clock of variable " + leftSide + " can be changed only by actualizing hours, minutes and seconds separately."); case "date": throw new SyntaxErrorException("ERROR! Date of variable " + leftSide + " can be changed only by actualizing year, month and day separately."); default: throw new SyntaxErrorException("ERROR! Property " + rightSide + " of variable " + leftSide + " do not exist."); } } else { throw new SyntaxErrorException("ERROR! Variable " + leftSide + " do not exist."); } }
public static IStringable Build(List <Token> tokens) { // try to build Numerable INumerable inu = NumerableBuilder.Build(tokens); if (!inu.IsNull()) { return(inu as IStringable); } // try to build Timeable ITimeable itim = TimeableBuilder.Build(tokens); if (!itim.IsNull()) { return(itim as IStringable); } // 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 Stringable if (tokens.Count == 1) { if (tokens[0].GetTokenType().Equals(TokenType.Variable)) { string str = tokens[0].GetContent(); if (InterVariables.GetInstance().Contains(str, InterVarType.String)) { return(new StringVariableRefer(str)); } else { // try to build reference to date or clock time IStringable istr = BuildTimeVariableRefer(tokens[0]); if (!istr.IsNull()) { return(istr); } } } if (tokens[0].GetTokenType().Equals(TokenType.StringConstant)) { return(new StringConstant(tokens[0].GetContent())); } } //try to build string function if (Functions.IsPossibleFunction(tokens)) { IStringable istr = StringFunction.Build(tokens); if (!istr.IsNull()) { return(istr); } } // try to build string ternary if (TernaryBuilder.IsPossibleTernary(tokens)) { IStringable istr = TernaryBuilder.BuildStringTernary(tokens); if (!istr.IsNull()) { return(istr); } } // try to build reference to n-th element of list of strings if (tokens.Count > 3 && tokens[0].GetTokenType().Equals(TokenType.Variable) && tokens[1].GetTokenType().Equals(TokenType.SquareBracketOn) && tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.SquareBracketOff)) { IStringable istr = BuildListElement(tokens); if (!istr.IsNull()) { return(istr); } } // try to build concatenated string -> text merged by + if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Plus) && !TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Comma)) { return(BuildConcatenated(tokens)); } else { return(null); } }
private static INumerable BuildExpression(List <Token> tokens) { // turn list of tokens into list of NumericExpressionElements // 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 INumerable // finally build NumericExpression List <INumericExpressionElement> infixList = new List <INumericExpressionElement>(); List <Token> currentTokens = new List <Token>(); bool readingFunction = false; Token previousToken = new Token(TokenType.Null); // first, merge many tokens into fewer number of INumericExpressionElements foreach (Token tok in tokens) { bool actionDone = false; if (TokenGroups.IsArithmeticSign(tok.GetTokenType())) { if (readingFunction) { if (Brackets.AllBracketsClosed(currentTokens)) { INumerable ibo = NumerableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); readingFunction = false; infixList.Add(new NumericExpressionOperator(GetNEOT(tok.GetTokenType()))); } else { currentTokens.Add(tok); } } else { if (currentTokens.Count > 0) { INumerable ibo = NumerableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); } infixList.Add(new NumericExpressionOperator(GetNEOT(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) { INumerable ibo = NumerableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); } infixList.Add(new NumericExpressionOperator(NumericExpressionOperatorType.BracketOn)); } } actionDone = true; } if (tok.GetTokenType().Equals(TokenType.BracketOff)) { if (readingFunction) { if (Brackets.AllBracketsClosed(currentTokens)) { INumerable ibo = NumerableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); readingFunction = false; infixList.Add(new NumericExpressionOperator(NumericExpressionOperatorType.BracketOff)); } else { currentTokens.Add(tok); } } else { if (currentTokens.Count > 0) { INumerable ibo = NumerableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); } infixList.Add(new NumericExpressionOperator(NumericExpressionOperatorType.BracketOff)); } actionDone = true; } if (!actionDone) { currentTokens.Add(tok); } previousToken = tok; } if (currentTokens.Count > 0) { INumerable ibo = NumerableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } } // try to build inversion of one INumerable if (infixList.Count == 2 && (infixList[0] is NumericExpressionOperator) && (infixList[1] is INumerable) && (infixList[0] as NumericExpressionOperator).IsMinus()) { return(BuildNegated(infixList[1] as INumerable)); } // change unary minuses to new type to avoid mistaking them with subtraction sign infixList = IdentifyUnaryMinuses(infixList); // check if value of infixlist can be computed (check order of elements) if (!CheckExpressionComputability(infixList)) { throw new SyntaxErrorException("ERROR! Wrong syntax of numeric expression."); } // if everything is right, finally build NumericExpression in RPN return(new NumericExpression(ReversePolishNotation(infixList))); }
private static ITimeable BuildFromDate(List <Token> tokens) { decimal month = 1; List <Token> leftPart = new List <Token>(); List <Token> rightPart = new List <Token>(); bool pastMonth = false; foreach (Token tok in tokens) { if (IsMonth(tok)) { pastMonth = true; month = StringToMonth(tok.GetContent()); } else { if (pastMonth) { rightPart.Add(tok); } else { leftPart.Add(tok); } } } if (leftPart.Count == 0) { return(null); } INumerable day = NumerableBuilder.Build(leftPart); if (day.IsNull()) { return(null); } // try to build date without year if (rightPart.Count == 0) { if (day is NumericConstant) { return(new Time(new ClockEmpty(), new DayConstant(day.ToNumber()), month, new YearNow())); } else { return(new Time(new ClockEmpty(), new DayNumerable(day), month, new YearNow())); } } INumerable year = NumerableBuilder.Build(rightPart); if (year.IsNull()) { return(null); } if (day is NumericConstant) { if (year is NumericConstant) { return(new Time(new ClockEmpty(), new DayConstant(day.ToNumber()), month, new YearConstant(year.ToNumber()))); } else { return(new Time(new ClockEmpty(), new DayConstant(day.ToNumber()), month, new YearNumerable(year))); } } else { if (year is NumericConstant) { return(new Time(new ClockEmpty(), new DayNumerable(day), month, new YearConstant(year.ToNumber()))); } else { return(new Time(new ClockEmpty(), new DayNumerable(day), month, new YearNumerable(year))); } } }
private static RelativeTimeStruct BuildSingleRTS(List <Token> tokens, Token tok, TimeDirection direction) { if (IsSingleKeyword(tok)) { if (tokens.Count == 1 && tokens[0].GetTokenType().Equals(TokenType.NumericConstant) && tokens[0].GetNumericContent() == 1) { switch (tok.GetContent().ToLower()) { case "century": return(new RelativeTimeStruct(RelativeTimeType.Centuries, new NumericConstant(1), direction)); case "decade": return(new RelativeTimeStruct(RelativeTimeType.Decades, new NumericConstant(1), direction)); case "year": return(new RelativeTimeStruct(RelativeTimeType.Years, new NumericConstant(1), direction)); case "month": return(new RelativeTimeStruct(RelativeTimeType.Months, new NumericConstant(1), direction)); case "week": return(new RelativeTimeStruct(RelativeTimeType.Weeks, new NumericConstant(1), direction)); case "day": return(new RelativeTimeStruct(RelativeTimeType.Days, new NumericConstant(1), direction)); case "hour": return(new RelativeTimeStruct(RelativeTimeType.Hours, new NumericConstant(1), direction)); case "minute": return(new RelativeTimeStruct(RelativeTimeType.Minutes, new NumericConstant(1), direction)); case "second": return(new RelativeTimeStruct(RelativeTimeType.Seconds, new NumericConstant(1), direction)); } } else { return(null); } } else { INumerable inum = NumerableBuilder.Build(tokens); if (inum.IsNull()) { return(null); } else { switch (tok.GetContent().ToLower()) { case "centuries": return(new RelativeTimeStruct(RelativeTimeType.Centuries, inum, direction)); case "decades": return(new RelativeTimeStruct(RelativeTimeType.Decades, inum, direction)); case "years": return(new RelativeTimeStruct(RelativeTimeType.Years, inum, direction)); case "months": return(new RelativeTimeStruct(RelativeTimeType.Months, inum, direction)); case "weeks": return(new RelativeTimeStruct(RelativeTimeType.Weeks, inum, direction)); case "days": return(new RelativeTimeStruct(RelativeTimeType.Days, inum, direction)); case "hours": return(new RelativeTimeStruct(RelativeTimeType.Hours, inum, direction)); case "minutes": return(new RelativeTimeStruct(RelativeTimeType.Minutes, inum, direction)); case "seconds": return(new RelativeTimeStruct(RelativeTimeType.Seconds, inum, direction)); } } } return(null); }
// functions are grouped by their arguments // every set of arguments is one method below public static INumerable BuildNum(string name, List <Argument> args) { if (args.Count != 1) { throw new SyntaxErrorException("ERROR! Function " + name + " has to have 1 numeric argument."); } INumerable inu = NumerableBuilder.Build(args[0].tokens); if (inu.IsNull()) { throw new SyntaxErrorException("ERROR! Argument of function " + name + " cannot be read as number."); } if (name.Equals("round")) { return(new FuncRound(inu)); } else if (name.Equals("floor")) { return(new FuncFloor(inu)); } else if (name.Equals("ceil") || name.Equals("ceiling")) { return(new FuncCeil(inu)); } else if (name.Equals("sqrt")) { return(new FuncSqrt(inu)); } else if (name.Equals("factorial")) { return(new FuncFactorial(inu)); } else if (name.Equals("ln") || name.Equals("log")) { return(new FuncLn(inu)); } else if (name.Equals("log10")) { return(new FuncLog10(inu)); } else if (name.Equals("k")) { return(new Func__SizeUnit(inu, SizeSufix.K)); } else if (name.Equals("kk")) { return(new Func__SizeUnit(inu, SizeSufix.KK)); } else if (name.Equals("kb")) { return(new Func__SizeUnit(inu, SizeSufix.KB)); } else if (name.Equals("mb")) { return(new Func__SizeUnit(inu, SizeSufix.MB)); } else if (name.Equals("gb")) { return(new Func__SizeUnit(inu, SizeSufix.GB)); } else if (name.Equals("tb")) { return(new Func__SizeUnit(inu, SizeSufix.TB)); } else if (name.Equals("pb")) { return(new Func__SizeUnit(inu, SizeSufix.PB)); } throw new SyntaxErrorException("ERROR! Function " + name + " not identified."); }
private static IClock BuildClock(List <Token> tokens) { int semicolons = NumberOfColonsOutsideOfBrackets(tokens); if (semicolons == 1) { List <Token> leftPart = new List <Token>(); List <Token> rightPart = new List <Token>(); int level = 0; bool pastSemicolon = false; foreach (Token tok in tokens) { if (tok.GetTokenType().Equals(TokenType.BracketOn)) { level++; } if (tok.GetTokenType().Equals(TokenType.BracketOff)) { level--; } if (tok.GetTokenType().Equals(TokenType.Colon) && level == 0) { pastSemicolon = true; } else { if (pastSemicolon) { rightPart.Add(tok); } else { leftPart.Add(tok); } } } INumerable inumLeft = NumerableBuilder.Build(leftPart); if (inumLeft.IsNull()) { return(null); } INumerable inumRight = NumerableBuilder.Build(rightPart); if (inumRight.IsNull()) { return(null); } if (inumLeft is NumericConstant && inumRight is NumericConstant) { return(new ClockConstant(inumLeft.ToNumber(), inumRight.ToNumber(), 0)); } return(new ClockWithoutSeconds(inumLeft, inumRight)); } else { List <Token> leftPart = new List <Token>(); List <Token> middlePart = new List <Token>(); List <Token> rightPart = new List <Token>(); int level = 0; int currentPart = 1; foreach (Token tok in tokens) { if (tok.GetTokenType().Equals(TokenType.BracketOn)) { level++; } if (tok.GetTokenType().Equals(TokenType.BracketOff)) { level--; } if (tok.GetTokenType().Equals(TokenType.Colon) && level == 0) { currentPart++; } else { if (currentPart == 1) { leftPart.Add(tok); } else if (currentPart == 2) { middlePart.Add(tok); } else { rightPart.Add(tok); } } } INumerable inumLeft = NumerableBuilder.Build(leftPart); if (inumLeft.IsNull()) { return(null); } INumerable inumMiddle = NumerableBuilder.Build(middlePart); if (inumMiddle.IsNull()) { return(null); } INumerable inumRight = NumerableBuilder.Build(rightPart); if (inumRight.IsNull()) { return(null); } if (inumLeft is NumericConstant && inumMiddle is NumericConstant && inumRight is NumericConstant) { return(new ClockConstant(inumLeft.ToNumber(), inumMiddle.ToNumber(), inumRight.ToNumber())); } return(new ClockNumerables(inumLeft, inumMiddle, inumRight)); } }
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); } }