/// <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); }