Пример #1
0
        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.");
        }
Пример #2
0
        // 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.");
        }
Пример #3
0
        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.");
        }
Пример #4
0
        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.");
        }
Пример #5
0
        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.");
        }
Пример #6
0
        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.");
        }
Пример #7
0
        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));
            }
        }
Пример #8
0
        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.");
            }
        }
Пример #12
0
        // 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.");
        }