示例#1
0
 public override object VisitFactor([NotNull] SLangGrammarParser.FactorContext context)
 {
     if (context.IntValue() != null)
     {
         cppText.Write(context.IntValue().GetText());
     }
     else if (context.RealValue() != null)
     {
         cppText.Write(context.RealValue().GetText());
     }
     else if (context.BoolValue() != null)
     {
         cppText.Write(context.BoolValue().GetText());
     }
     else if (context.SingleCharacter() != null)
     {
         cppText.Write(context.SingleCharacter().GetText());
     }
     else if (context.StringLiteral() != null)
     {
         cppText.Write($"std::string({context.StringLiteral().GetText()})");
     }
     else if (context.BoolNot() != null)
     {
         cppText.Write(context.BoolNot());
         Visit(context.factor());
     }
     else if (context.exp() != null)
     {
         cppText.Write('(');
         Visit(context.exp());
         cppText.Write(')');
     }
     else if (context.designator() != null)
     {
         Visit(context.designator());
     }
     else if (context.newC() != null)
     {
         var t = Visit(context.newC().customType()) as SlangCustomType;
         cppText.Write($"std::shared_ptr<{GetStringFromType(t)}>(new({GetStringFromType(t)}))");
     }
     else if (context.Nil() != null)
     {
         cppText.Write("nullptr");
     }
     return(null);
 }
示例#2
0
        public override object VisitFactor([NotNull] SLangGrammarParser.FactorContext context)
        {
            if (context.IntValue() != null)
            {
                return(new ExpressionResult(SlangSimpleType.Int, ExpressionValueType.Value));
            }

            if (context.RealValue() != null)
            {
                return(new ExpressionResult(SlangSimpleType.Real, ExpressionValueType.Value));
            }

            if (context.BoolValue() != null)
            {
                return(new ExpressionResult(SlangSimpleType.Boolean, ExpressionValueType.Value));
            }

            if (context.SingleCharacter() != null)
            {
                return(new ExpressionResult(SlangSimpleType.Character, ExpressionValueType.Value));
            }

            if (context.StringLiteral() != null)
            {
                return(new ExpressionResult(SlangSimpleType.String, ExpressionValueType.Value));
            }

            if (context.BoolNot() != null)
            {
                var type = (Visit(context.factor()) as ExpressionResult)?.Type;
                if (type is SlangSimpleType && type.Equals(SlangSimpleType.Boolean))
                {
                    return(new ExpressionResult(SlangSimpleType.Boolean, ExpressionValueType.Value));
                }

                ThrowInvalidTypesForUnaryOperationException(context.BoolNot(), file, type);
            }
            else if (context.exp() != null)
            {
                return(Visit(context.exp()) as ExpressionResult);
            }
            else if (context.designator() != null)
            {
                return(Visit(context.designator()) as ExpressionResult);
            }
            else if (context.newC() != null)
            {
                var type = Visit(context.newC().customType()) as SlangCustomType;
                if (Table.FindClass(type).IsAbstract())
                {
                    ThrowCannotInitializeAbstractClassException(type, file, context.newC().Start.Line, context.newC().Start.Column);
                }
                return(new ExpressionResult(new SlangPointerType(type), ExpressionValueType.Value)); // value or var? hmm
            }
            else if (context.Nil() != null)
            {
                return(new ExpressionResult(new SlangNullType(), ExpressionValueType.Value));
            }

            return(null);
        }