Example #1
0
        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 ICommand Build(List <Token> tokens)
        {
            bool   isPlusPlus = tokens[1].GetTokenType().Equals(TokenType.PlusPlus);
            string name       = tokens[0].GetContent();
            string type       = isPlusPlus ? "increment" : "decrement";

            if (tokens.Count > 2)
            {
                throw new SyntaxErrorException("ERROR! Variable " + name + " " + type + "ation operation contains unnecessary code.");
            }

            if (InterVariables.GetInstance().ContainsChangable(name, InterVarType.Number) &&
                !InterVariables.GetInstance().Contains(name, InterVarType.Bool))
            {
                if (isPlusPlus)
                {
                    return(new PlusPlus(name));
                }
                else
                {
                    return(new MinusMinus(name));
                }
            }
            throw new SyntaxErrorException("ERROR! Variable " + name + " cannot be " + type + "ed.");
        }
Example #3
0
        public static ICommand Build(List <Token> tokens)
        {
            if (tokens.Count == 1)
            {
                throw new SyntaxErrorException("ERROR! Reverse command do not contain variable name.");
            }
            if (tokens.Count > 2)
            {
                throw new SyntaxErrorException("ERROR! Reverse command is too long.");
            }
            if (!tokens[1].GetTokenType().Equals(TokenType.Variable))
            {
                throw new SyntaxErrorException("ERROR! Variable name of reverse command cannot be read.");
            }

            string str = tokens[1].GetContent();

            if (!InterVariables.GetInstance().ContainsChangable(str, InterVarType.String) && InterVariables.GetInstance().ContainsChangable(str, InterVarType.List))
            {
                return(new Reverse(str));
            }
            else
            {
                throw new SyntaxErrorException("ERROR! In reverse command variable " + str + " do not exist, cannnot be read as list or cannot be modified.");
            }
        }
Example #4
0
        private static INumerable BuildTimeVariableRefer(Token token)
        {
            string name  = token.GetContent();
            int    count = name.Count(c => c == '.');

            if (count == 0)
            {
                return(null);
            }

            if (count > 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))
            {
                switch (rightSide)
                {
                case "year":
                    return(new TimeElementRefer(leftSide, TimeVariableType.Year));

                case "month":
                    return(new TimeElementRefer(leftSide, TimeVariableType.Month));

                case "day":
                    return(new TimeElementRefer(leftSide, TimeVariableType.Day));

                case "weekday":
                    return(new TimeElementRefer(leftSide, TimeVariableType.WeekDay));

                case "hour":
                    return(new TimeElementRefer(leftSide, TimeVariableType.Hour));

                case "minute":
                    return(new TimeElementRefer(leftSide, TimeVariableType.Minute));

                case "second":
                    return(new TimeElementRefer(leftSide, TimeVariableType.Second));
                }
                return(null);
            }
            else
            {
                throw new SyntaxErrorException("ERROR! Time variable " + leftSide + " do not exist.");
            }
        }
Example #5
0
        public static ICommand Build(List <Token> tokens)
        {
            tokens.RemoveAt(0);

            if (tokens.Count() > 3)
            {
                throw new SyntaxErrorException("ERROR! Command 'swap' is too long.");
            }

            if (tokens.Count() < 3)
            {
                throw new SyntaxErrorException("ERROR! Command 'swap' do not contain all necessary information.");
            }

            if (!tokens[1].GetTokenType().Equals(TokenType.With) && !tokens[1].GetTokenType().Equals(TokenType.And))
            {
                throw new SyntaxErrorException("ERROR! Command 'swap' do not have variables separated by keyword 'with' or 'and'.");
            }

            if (!tokens[0].GetTokenType().Equals(TokenType.Variable))
            {
                throw new SyntaxErrorException("ERROR! Command 'swap' do not have name of first variable to swap.");
            }

            if (!tokens[2].GetTokenType().Equals(TokenType.Variable))
            {
                throw new SyntaxErrorException("ERROR! Command 'swap' do not have name of second variable to swap.");
            }

            string leftVariable  = tokens[2].GetContent();
            string rightVariable = tokens[0].GetContent();

            if (!InterVariables.GetInstance().Contains(leftVariable))
            {
                throw new SyntaxErrorException("ERROR! In command 'swap' variable " + leftVariable + " do not exist.");
            }

            if (!InterVariables.GetInstance().Contains(rightVariable))
            {
                throw new SyntaxErrorException("ERROR! In command 'swap' variable " + rightVariable + " do not exist.");
            }

            InterVarType leftType  = InterVariables.GetInstance().GetVarType(leftVariable);
            InterVarType rightType = InterVariables.GetInstance().GetVarType(rightVariable);

            if (!leftType.Equals(rightType))
            {
                throw new SyntaxErrorException("ERROR! Variables " + leftVariable + " and " + rightVariable + " cannot be swapped, because they are of different type.");
            }

            return(new Swap(leftVariable, rightVariable));
        }
Example #6
0
        public static ICommand Build(List <Token> tokens)
        {
            if (!tokens.Any(t => t.GetTokenType().Equals(TokenType.By)))
            {
                throw new SyntaxErrorException("ERROR! Order command do not contain definition of sorting variables.");
            }

            TokenType type = tokens[0].GetTokenType();

            tokens.RemoveAt(0);

            if (tokens.Count < 3)
            {
                throw new SyntaxErrorException("ERROR! Order command is too short.");
            }
            if (tokens.Where(t => t.GetTokenType().Equals(TokenType.By)).Count() > 1)
            {
                throw new SyntaxErrorException("ERROR! In order command keyword 'by' occurs too many times.");
            }
            if (!tokens[0].GetTokenType().Equals(TokenType.Variable))
            {
                throw new SyntaxErrorException("ERROR! Second word of order command must be name of list to order.");
            }
            if (!tokens[1].GetTokenType().Equals(TokenType.By))
            {
                throw new SyntaxErrorException("ERROR! In order command name of list contains spaces or other unallowed symbols.");
            }

            string name = tokens[0].GetContent();

            if (!InterVariables.GetInstance().ContainsChangable(name, InterVarType.String) && InterVariables.GetInstance().ContainsChangable(name, InterVarType.List))
            {
                tokens.RemoveAt(0);
                tokens.RemoveAt(0);

                ISubcommand ord = SubcommandBuilder.Build(tokens, TokenType.OrderBy);
                if (ord is OrderBy)
                {
                    return(new Order(name, ord as OrderBy));
                }
                else
                {
                    throw new SyntaxErrorException("ERROR! In order command there is something wrong with variables.");
                }
            }
            else
            {
                throw new SyntaxErrorException("ERROR! In order command variable " + name + " do not exist, cannnot be read as list or cannot be modified.");
            }
        }
Example #7
0
        public static void Run(string code, string location)
        {
            Logger.GetInstance().LogOn();
            InterVariables.GetInstance().Clear();
            RuntimeVariables.GetInstance().InitializeInnerVariables();
            RuntimeVariables.GetInstance().Actualize("location", location);

            try
            {
                List <Token>    tokens   = Reader.CreateTokenlist(code);
                List <ICommand> commands = CommandListFactory.Build(tokens);
                RunCommands(commands);
            }
            catch (Uroboros.syntax.SyntaxErrorException te)
            {
                Logger.GetInstance().LogSyntaxError(te.GetMessage());
            }
        }
Example #8
0
        private static IStringable BuildTimeVariableRefer(Token token)
        {
            string name  = token.GetContent();
            int    count = name.Count(c => c == '.');

            if (count == 0)
            {
                return(null);
            }

            if (count > 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))
            {
                switch (rightSide)
                {
                case "date":
                    return(new TimeDateRefer(leftSide));

                case "clock":
                    return(new TimeClockRefer(leftSide));
                }
                return(null);
            }
            else
            {
                throw new SyntaxErrorException("ERROR! Time variable " + leftSide + " do not exist.");
            }
        }
        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.");
        }
        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.");
            }
        }
Example #11
0
        public static ITimeable Build(List <Token> tokens)
        {
            // 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 Timeable
            if (tokens.Count == 1)
            {
                if (tokens[0].GetTokenType().Equals(TokenType.Variable))
                {
                    string str = tokens[0].GetContent();
                    if (InterVariables.GetInstance().Contains(str, InterVarType.Time))
                    {
                        return(new TimeVariableRefer(str));
                    }
                }
            }

            //try to build time function
            if (Functions.IsPossibleFunction(tokens))
            {
                ITimeable itim = TimeFunction.Build(tokens);
                if (!itim.IsNull())
                {
                    return(itim);
                }
            }

            // try to build time ternary
            if (TernaryBuilder.IsPossibleTernary(tokens))
            {
                ITimeable itim = TernaryBuilder.BuildTimeTernary(tokens);
                if (!itim.IsNull())
                {
                    return(itim);
                }
            }

            // try to build relative time expression
            if (tokens.Where(t => IsTimeDirection(t)).Count() > 0)
            {
                ITimeable itim = BuildRelativeTime(tokens);
                if (!itim.IsNull())
                {
                    return(itim);
                }
            }

            if (HasOneComma(tokens))
            {
                // try to build Timeable from date and clock
                ITimeable itim = BuildFromDateAndClock(tokens);
                if (!itim.IsNull())
                {
                    return(itim);
                }
            }
            else
            {
                // try to build Timeable from date only
                if (ContainMonth(tokens))
                {
                    ITimeable itim = BuildFromDate(tokens);
                    if (!itim.IsNull())
                    {
                        return(itim);
                    }
                }

                // try to build Timeable from clock only
                if (ContainSemicolons(tokens))
                {
                    ITimeable itim = BuildFromClock(tokens);
                    if (!itim.IsNull())
                    {
                        return(itim);
                    }
                }
            }

            return(null);
        }
Example #12
0
        public static ICommand Build(List <Token> tokens)
        {
            TokenType type = tokens[0].GetTokenType();

            tokens.RemoveAt(0);

            if (!tokens.Any(t => t.GetTokenType().Equals(TokenType.To)))
            {
                throw new SyntaxErrorException("ERROR! Command 'add' do not contain keyword 'to'.");
            }
            if (tokens.Where(t => t.GetTokenType().Equals(TokenType.To)).Count() > 1)
            {
                throw new SyntaxErrorException("ERROR! In command 'add' keyword 'to' occurs too many times.");
            }

            List <Token> part1  = new List <Token>();
            List <Token> part2  = new List <Token>();
            bool         pastTo = false;

            foreach (Token tok in tokens)
            {
                if (tok.GetTokenType().Equals(TokenType.To))
                {
                    pastTo = true;
                }
                else
                {
                    if (pastTo)
                    {
                        part2.Add(tok);
                    }
                    else
                    {
                        part1.Add(tok);
                    }
                }
            }

            if (part2.Count == 0)
            {
                throw new SyntaxErrorException("ERROR! Command 'add' do not contain definition for target variable.");
            }
            if (part2.Count > 2 || !part2[0].GetTokenType().Equals(TokenType.Variable))
            {
                throw new SyntaxErrorException("ERROR! Target variable in command 'add' cannot be read.");
            }

            string name = part2[0].GetContent();

            if (!InterVariables.GetInstance().ContainsChangable(name, InterVarType.List))
            {
                throw new SyntaxErrorException("ERROR! In command 'add' variable " + name + " do not exist or cannot be read as list.");
            }

            // add this
            if (part1.Count == 0)
            {
                return(new AddString(name, new StringVariableRefer("this") as IStringable));
            }

            IListable ilist = ListableBuilder.Build(part1);

            if (ilist.IsNull())
            {
                throw new SyntaxErrorException("ERROR! In command 'add' definition for elements to add cannot be read as list.");
            }

            // turn variable to list if it was string
            if (InterVariables.GetInstance().ContainsChangable(name, InterVarType.String))
            {
                InterVariables.GetInstance().TurnToList(name);
            }

            if (ilist is IStringable)
            {
                return(new AddString(name, ilist as IStringable));
            }
            else
            {
                return(new AddList(name, ilist));
            }
        }
Example #13
0
        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);
        }
Example #14
0
        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));
                    }
                }
            }
        }
Example #16
0
        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);
            }
        }
Example #17
0
        public static IListable Build(List <Token> tokens)
        {
            // try to build Stringable
            IStringable ist = StringableBuilder.Build(tokens);

            if (!ist.IsNull())
            {
                return(ist as IListable);
            }

            // 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 'empty list'
            if (tokens.Count == 2 && tokens[0].GetTokenType().Equals(TokenType.Variable) && tokens[1].GetTokenType().Equals(TokenType.Variable) &&
                tokens[0].GetContent().ToLower().Equals("empty") && tokens[1].GetContent().ToLower().Equals("list"))
            {
                return(new EmptyList());
            }

            // try to build small arrow function
            if (tokens.Where(t => t.GetTokenType().Equals(TokenType.SmallArrow)).Count() == 1)
            {
                IListable smallArrow = BuildSmallArrowFunction(tokens);
                if (!smallArrow.IsNull())
                {
                    return(smallArrow);
                }
            }

            string str = tokens[0].GetContent();

            // try to build list variable reference - just one word and it is a name
            if (tokens.Count == 1 && tokens[0].GetTokenType().Equals(TokenType.Variable) && InterVariables.GetInstance().Contains(str, InterVarType.List))
            {
                return(new ListVariableRefer(str));
            }

            // try to build list expression
            if (InterVariables.GetInstance().Contains(str, InterVarType.List))
            {
                IListable listEx = BuildListExpression(tokens, str);
                if (!listEx.IsNull())
                {
                    return(listEx);
                }
            }

            // try to build list ternary
            if (TernaryBuilder.IsPossibleTernary(tokens))
            {
                IListable ilist = TernaryBuilder.BuildListTernary(tokens);
                if (!ilist.IsNull())
                {
                    return(ilist);
                }
            }

            // try to build listed lists/strings: many Listables/Stringables divided by commas
            if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Comma))
            {
                IListable listed = BuildListed(tokens);
                if (!listed.IsNull())
                {
                    return(listed);
                }
            }

            throw new SyntaxErrorException("ERROR! Unknown error in code syntax.");
        }
Example #18
0
        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);
            }
        }