Example #1
0
        private void _setFlag(CommandContext context, CompilerFlags flags,
                              Expression <Func <CompilerFlags, object> > prop, string switchName)
        {
            Switch s;

            if ((s = context.GetSwitch(switchName)) != null)
            {
                s.AssertValue();

                var lambda     = prop as LambdaExpression;
                var memberExpr = lambda.Body as MemberExpression;
                if (memberExpr == null)
                {
                    throw new InvalidOperationException();
                }
                var propName = memberExpr.Member.Name;

                var propInfo = flags.GetType().GetProperty(propName);

                if (propInfo.PropertyType == typeof(bool))
                {
                    StatementHelpers.TryParseBooleanFromString(s.Value, out var value);
                    propInfo.SetValue(flags, value);
                }
                else
                {
                    propInfo.SetValue(flags, Convert.ChangeType(s.Value, propInfo.PropertyType));
                }
            }
        }
        public override string FormatConstantExpression(ExpressionBuilderParams p, TypeDescriptor typeDescriptor,
                                                        string expression,
                                                        EvaluationStatement template)
        {
            if (template is ConstantValueStatement constantValueStatement)
            {
                if (typeDescriptor.IsBoolean())
                {
                    if (!StatementHelpers.TryParseBooleanFromString(constantValueStatement.Value, out var boolResult))
                    {
                        throw new InvalidStatementStructureCompilerException(template, template.Info);
                    }

                    return(boolResult ? "1" : "0");
                }

                if (constantValueStatement.IsString() || constantValueStatement.IsDelegate())
                {
                    if (template.ParentStatement is ArithmeticEvaluationStatement arithmeticEvaluationStatement)
                    {
                        if (arithmeticEvaluationStatement.Operator is AdditionOperator)
                        {
                            if (p.FormatString)
                            {
                                return(BashTranspilerHelpers.ToBashString(constantValueStatement.Value, true, false));
                            }
                        }
                        else
                        {
                            throw new InvalidStatementStructureCompilerException(arithmeticEvaluationStatement,
                                                                                 arithmeticEvaluationStatement.Info);
                        }
                    }

                    return(base.FormatConstantExpression(p, typeDescriptor, expression, template));
                }
            }

            return(base.FormatConstantExpression(p, typeDescriptor, expression, template));
        }
Example #3
0
        private bool CalculatePreProcessorCondition(Token token, IPeekingEnumerator <Token> enumerator,
                                                    ParserContext context, bool parseCondition)
        {
            if (!enumerator.MoveNext())
            {
                throw EndOfFile(token, context);
            }

            token = enumerator.Current;
            if (token.Type != TokenType.OpenParenthesis)
            {
                throw UnexpectedToken(token, context, true);
            }

            if (!enumerator.MoveNext()) //read the first eval token
            {
                throw EndOfFile(token, context);
            }

            token = enumerator.Current;

            var evalStatement = Parser.ReadEvaluationStatement(token, enumerator, context);

            if (!enumerator.MoveNext()) //read the close parenthesis
            {
                throw EndOfFile(token, context);
            }

            token = enumerator.Current;
            if (token.Type != TokenType.CloseParenthesis)
            {
                throw UnexpectedToken(token, context);
            }

            if (parseCondition)
            {
                try
                {
                    var stt = EvaluationStatementTranspilerBase.ProcessEvaluation(Context, Context.GeneralScope,
                                                                                  evalStatement);

                    if (stt is ConstantValueStatement constantValueStatement)
                    {
                        if (StatementHelpers.TryParseBooleanFromString(constantValueStatement.Value, out var boolVal))
                        {
                            return(boolVal);
                        }

                        throw UnexpectedToken(token, context);
                    }
                }
                catch (IdentifierNotFoundCompilerException)
                {
                    return(false);
                }

                throw UnexpectedToken(token, context);
            }

            return(false);
        }