コード例 #1
0
        public override void EnterUsingExpression([NotNull] SBP.UsingExpressionContext context)
        {
            var child = context.GetChild(0) as Antlr4.Runtime.RuleContext;

            if (child.RuleIndex == SBP.RULE_simpleVariableDeclaration)
            {
            }
            else if (child.RuleIndex == SBP.RULE_expression)
            {
                m_expressionData.PushStackLevel("UsingStatement");
            }
            else
            {
                throw new NotImplementedException(String.Format("What? Unknown using expression type (rule = {0}).", child.RuleIndex));
            }
        }
コード例 #2
0
        public override void ExitUsingExpression([NotNull] SBP.UsingExpressionContext context)
        {
            //var usingVariable = m_scopeStack.Peek().AddVariable("usingVariable_" + context.start.Line.ToString(), typeof(IDisposable), null, EntryModifiers.Private);

            Expression usingExpression = null;

            var child = context.GetChild(0) as Antlr4.Runtime.RuleContext;

            if (child.RuleIndex == SBP.RULE_simpleVariableDeclaration)
            {
                if (m_variableInitializer.IsConstant && m_variableInitializer.Value == null)
                {
                    // Convert the null value to the type of the variable
                    if (m_variableType.Type == typeof(string))
                    {
                        m_variableInitializer = new SBExpressionData(TypeReference.TypeString, null);
                    }
                }
                if (m_variableType == null)
                {
                    throw new NotImplementedException();
                }
                else if (m_variableType.Type != typeof(VarSpecifiedType))
                {
                    if (m_variableInitializer.IsValueType &&
                        m_variableInitializer.IsConstant &&
                        m_variableInitializer.Value == null)
                    {
                        m_variableInitializer.NarrowGetValueType(m_variableType);
                    }
                    else if (m_variableType != m_variableInitializer.DataType && !m_variableType.Type.IsAssignableFrom(m_variableInitializer.DataType.Type))
                    {
                        throw new NotImplementedException("Convertion of variable initializer is not implemented.");
                    }
                }
                if (m_variableType.Type == typeof(VarSpecifiedType))
                {
                    m_variableType = m_variableInitializer.DataType;
                }

                var scope = m_scopeStack.Peek();
                var v     = scope.AddVariable(m_variableName, m_variableType, null, EntryModifiers.Private);
                usingExpression = Expression.Assign(v.VariableExpression, m_variableInitializer.ExpressionCode);

                m_variableName        = null;
                m_variableInitializer = null;
            }
            else if (child.RuleIndex == SBP.RULE_expression)
            {
                var stack = m_expressionData.PopStackLevel();
                var exp   = stack.Pop();

                exp = this.ResolveForGetOperation(exp);

                if (!exp.IsValueType)
                {
                    throw new NotImplementedException("Something wrong with the using expression; it is not an IDisposable type.");
                }

                usingExpression = exp.ExpressionCode;
            }
            else
            {
                throw new NotImplementedException(String.Format("What? Unknown using expression type (rule = {0}).", child.RuleIndex));
            }

            if ((!usingExpression.Type.IsClass && !usingExpression.Type.IsInterface) || !typeof(IDisposable).IsAssignableFrom(usingExpression.Type))
            {
                throw new NotImplementedException("Something wrong with the using expression; it is not an IDisposable type.");
            }
            m_scopeStack.Peek().UsingVariableAssignment = usingExpression;  // Save for later
        }