Example #1
0
        protected override object CompileUnaryExpression(UnaryExpression expression)
        {
            object            value     = CompileExpression(expression.Expression);
            UnaryOperatorType @operator = expression.Operator;

            if (expression.Expression is IdentifierExpression identifierExpression)
            {
                if (CompilerContext.ContainsPropety(identifierExpression.Name) == false)
                {
                    CompilerContext.ErrorHandler.ThrowError(new SyntaxError($"Identifier '{identifierExpression.Name}' is not defined", null));

                    return(null);
                }

                CompilerContext.DeleteProperty(identifierExpression);

                return(null);
            }

            if (expression.Expression is Function function)
            {
                if (CompilerContext.ContainsFunction(function.Name) == false)
                {
                    CompilerContext.ErrorHandler.ThrowError(new SyntaxError($"Function '{function.Name}' is not defined", null));

                    return(null);
                }

                CompilerContext.DeleteFunction(function);

                return(null);
            }

            if (DustType.GetDustType(value) == DustType.Number)
            {
                switch (@operator)
                {
                case UnaryOperatorType.PLUS_PLUS:
                    if (DustType.GetDustType(value) == DustType.Int)
                    {
                        return((int)value + 1);
                    }

                    return(Convert.ToSingle(value) + 1);

                case UnaryOperatorType.MINUS_MINUS:
                    if (DustType.GetDustType(value) == DustType.Int)
                    {
                        return((int)value - 1);
                    }

                    return(Convert.ToSingle(value) - 1);

                case UnaryOperatorType.TIMES_TIMES:
                    if (DustType.GetDustType(value) == DustType.Int)
                    {
                        return(Math.Pow((int)value, 2));
                    }

                    return(Math.Pow(Convert.ToSingle(value), 2));

                case UnaryOperatorType.DIVIDE_DIVIDE:
                    if (DustType.GetDustType(value) == DustType.Int)
                    {
                        return(Math.Sqrt((int)value));
                    }

                    return(Math.Sqrt(Convert.ToSingle(value)));
                }
            }

            if (DustType.GetDustType(value) == DustType.Bool)
            {
                switch (@operator)
                {
                case UnaryOperatorType.BANG:
                    if (DustType.GetDustType(value) == DustType.Bool)
                    {
                        return(!(bool)value);
                    }

                    break;
                }
            }

            return(null);
        }