예제 #1
0
 private void logFloatTooBig(LiteralContext litContext)
 {
     log.error(
         new DiagnosticPosition(litContext.Start.Line, litContext.Start.Column),
         messages.floatLiteratTooBig, litContext.GetText()
         );
 }
예제 #2
0
        public override Tree VisitLiteral(LiteralContext context)
        {
            IToken  symbol;
            String  text;
            Object  value;
            TypeTag type;

            switch (context.literalType)
            {
            case INT:
                symbol = context.IntegerLiteral().Symbol;
                text   = symbol.Text;
                if (Int32.TryParse(text, out var intLit))
                {
                    type  = TypeTag.INT;
                    value = intLit;
                }
                else if (Int64.TryParse(text, out var longLit))
                {
                    type  = TypeTag.LONG;
                    value = longLit;
                }
                else
                {
                    log.error(
                        new DiagnosticPosition(context.Start.Line, context.Start.Column),
                        messages.intLiteratTooBig, context.GetText()
                        );
                    type  = TypeTag.ERROR;
                    value = 0;
                }
                break;

            case FLOAT:
                symbol = context.FloatingPointLiteral().Symbol;
                text   = symbol.Text;
                if (text.EndsWith("f") || text.EndsWith("F"))
                {
                    text = text.Substring(0, text.Length - 1);
                    type = TypeTag.FLOAT;
                    if (Single.TryParse(text, out var floatLit))
                    {
                        value = floatLit;
                    }
                    else
                    {
                        value = 0;
                        logFloatTooBig(context);
                    }
                }
                else if (text.EndsWith("d") || text.EndsWith("D"))
                {
                    type = TypeTag.DOUBLE;
                    if (Double.TryParse(text, out var doubleLit))
                    {
                        value = doubleLit;
                    }
                    else
                    {
                        value = 0;
                        logFloatTooBig(context);
                    }
                }
                else if (Single.TryParse(text, out var floatLit))
                {
                    type  = TypeTag.FLOAT;
                    value = floatLit;
                }
                else if (Double.TryParse(text, out var doubleLit))
                {
                    type  = TypeTag.DOUBLE;
                    value = doubleLit;
                }
                else
                {
                    type  = TypeTag.ERROR;
                    value = 0;
                    logFloatTooBig(context);
                }
                break;

            case BOOLEAN:
                symbol = context.BooleanLiteral().Symbol;
                text   = symbol.Text;
                type   = TypeTag.BOOLEAN;
                value  = Boolean.Parse(text);
                break;

            case CHAR:
                symbol = context.CharLiteral().Symbol;
                text   = symbol.Text;
                type   = TypeTag.CHAR;
                value  = parseCharLiteral(text);
                break;

            case CSTRING:
                symbol = context.CStringLiteral().Symbol;
                text   = symbol.Text;
                type   = TypeTag.C_STRING;
                value  = parseStringLiteral(text);
                break;

            case NULL:
                value  = null;
                symbol = context.NULL().Symbol;
                type   = TypeTag.NULL;
                text   = "null";
                break;

            default:
                throw new InvalidOperationException();
            }

            int line        = symbol.Line;
            int startColumn = symbol.Column;
            int endColumn   = startColumn + text.Length;

            return(new LiteralExpression(line, startColumn, line, endColumn, type, value));
        }