コード例 #1
0
ファイル: ConstantEncoder.cs プロジェクト: djlw78/crayon
 public static void CompileBoolean(ParserContext parser, ByteBuffer buffer, BooleanConstant boolConstant, bool outputUsed)
 {
     if (!outputUsed)
     {
         throw new ParserException(boolConstant, "This expression does nothing.");
     }
     buffer.Add(boolConstant.FirstToken, OpCode.LITERAL, parser.GetBoolConstant(boolConstant.Value));
 }
コード例 #2
0
 /// <summary>
 /// Creates an intrinsic attribute that encodes an integer spec.
 /// </summary>
 /// <param name="specification">An integer specification.</param>
 /// <returns>An intrinsic attribute.</returns>
 public static IntrinsicAttribute Create(IntegerSpec specification)
 {
     return(new IntrinsicAttribute(
                AttributeName,
                new Constant[]
     {
         new IntegerConstant(specification.Size),
         BooleanConstant.Create(specification.IsSigned)
     }));
 }
コード例 #3
0
 protected abstract void TranslateBooleanConstant(List <string> output, BooleanConstant booleanConstant);
コード例 #4
0
 /// <summary>
 /// Encodes a Boolean constant.
 /// </summary>
 /// <param name="value">A Boolean constant to encode.</param>
 /// <returns>
 /// The encoded Boolean constant.
 /// </returns>
 public LNode Encode(bool value)
 {
     return(Encode(BooleanConstant.Create(value)));
 }
コード例 #5
0
 protected override void TranslateBooleanConstant(List <string> output, BooleanConstant booleanConstant)
 {
     output.Add(booleanConstant.Value ? "true" : "false");
 }
コード例 #6
0
        /// <summary>
        /// Decodes an LNode as a constant value.
        /// </summary>
        /// <param name="node">The node to decode.</param>
        /// <param name="state">The decoder state to use.</param>
        /// <returns>A decoded constant.</returns>
        public override Constant Decode(LNode node, DecoderState state)
        {
            // Default-value constants.
            if (node.IsIdNamed(CodeSymbols.Default))
            {
                return(DefaultConstant.Instance);
            }

            // Type/field/method token constants.
            if (node.Calls(CodeSymbols.Typeof, 1))
            {
                return(new TypeTokenConstant(state.DecodeType(node.Args[0])));
            }
            else if (node.Calls(fieldofSymbol, 1))
            {
                return(new FieldTokenConstant(state.DecodeField(node.Args[0])));
            }
            else if (node.Calls(methodofSymbol, 1))
            {
                return(new MethodTokenConstant(state.DecodeMethod(node.Args[0])));
            }

            if (!FeedbackHelpers.AssertIsLiteral(node, state.Log))
            {
                return(null);
            }

            object value;
            Symbol typeMarker;

            // Custom literals.
            if (TryDecomposeCustomLiteral(node, out value, out typeMarker))
            {
                // Arbitrary-width integer literals.
                IntegerSpec spec;
                if (IntegerSpec.TryParse(typeMarker.Name, out spec))
                {
                    BigInteger integerVal;
                    if (BigInteger.TryParse(value.ToString(), out integerVal))
                    {
                        return(new IntegerConstant(integerVal, spec));
                    }
                    else
                    {
                        FeedbackHelpers.LogSyntaxError(
                            state.Log,
                            node,
                            FeedbackHelpers.QuoteEven(
                                "cannot parse ",
                                value.ToString(),
                                " as an integer."));
                        return(null);
                    }
                }
                else
                {
                    FeedbackHelpers.LogSyntaxError(
                        state.Log,
                        node,
                        FeedbackHelpers.QuoteEven(
                            "unknown custom literal type ",
                            typeMarker.Name,
                            "."));
                    return(null);
                }
            }

            value = node.Value;

            // Miscellaneous constants: null, strings, Booleans.
            if (value == null)
            {
                return(NullConstant.Instance);
            }
            else if (value is string)
            {
                return(new StringConstant((string)value));
            }
            else if (value is bool)
            {
                return(BooleanConstant.Create((bool)value));
            }
            // Floating-point numbers.
            else if (value is float)
            {
                return(new Float32Constant((float)value));
            }
            else if (value is double)
            {
                return(new Float64Constant((double)value));
            }
            // Fixed-width integer constants and characters.
            else if (value is char)
            {
                return(new IntegerConstant((char)value));
            }
            else if (value is sbyte)
            {
                return(new IntegerConstant((sbyte)value));
            }
            else if (value is short)
            {
                return(new IntegerConstant((short)value));
            }
            else if (value is int)
            {
                return(new IntegerConstant((int)value));
            }
            else if (value is long)
            {
                return(new IntegerConstant((long)value));
            }
            else if (value is byte)
            {
                return(new IntegerConstant((byte)value));
            }
            else if (value is ushort)
            {
                return(new IntegerConstant((ushort)value));
            }
            else if (value is uint)
            {
                return(new IntegerConstant((uint)value));
            }
            else if (value is ulong)
            {
                return(new IntegerConstant((ulong)value));
            }

            FeedbackHelpers.LogSyntaxError(
                state.Log,
                node,
                new Text("unknown literal type."));
            return(null);
        }