public ICLS_Expression Compiler_Expression_Value(Token value, int pos) { if (value.type == TokenType.VALUE) { if (value.text.StartsWith("-0x") || value.text.StartsWith("-0X")) { long lv = -Convert.ToInt64(value.text.Substring(1), 16); return CreateValueExpression(lv); } else if (value.text.StartsWith("0x") || value.text.StartsWith("0X")) { long lv = Convert.ToInt64(value.text, 16); return CreateValueExpression(lv); } else if (value.text[value.text.Length - 1] == 'f') { CLS_Expression_Value<float> number = new CLS_Expression_Value<float>(); number.value = float.Parse(value.text.Substring(0, value.text.Length - 1)); return number; } else if (value.text.Contains(".")) { CLS_Expression_Value<double> number = new CLS_Expression_Value<double>(); number.value = double.Parse(value.text); return number; } else if (value.text.Contains("'")) { CLS_Expression_Value<char> number = new CLS_Expression_Value<char>(); number.value = (char)value.text[1]; return number; } else if (value.text.StartsWith("-")) { long lv = long.Parse(value.text); return CreateValueExpression(lv); } else { long lv = long.Parse(value.text); return CreateValueExpression(lv); } } else if (value.type == TokenType.STRING) { CLS_Expression_Value<string> str = new CLS_Expression_Value<string>(); str.value = value.text.Substring(1, value.text.Length - 2); return str; } else if (value.type == TokenType.IDENTIFIER) { CLS_Expression_GetValue getvalue = new CLS_Expression_GetValue(pos, pos, value.line, value.line); getvalue.value_name = value.text; return getvalue; } else if (value.type == TokenType.TYPE) { CLS_Expression_GetValue getvalue = new CLS_Expression_GetValue(pos, pos, value.line, value.line); int l = value.text.LastIndexOf('.'); if (l >= 0) { getvalue.value_name = value.text.Substring(l + 1); } else getvalue.value_name = value.text; return getvalue; } logger.Log_Error("无法识别的简单表达式" + value); return null; }
ICLS_Expression OptimizeSingle(ICLS_Expression expr, CLS_Content content) { if (expr is CLS_Expression_Math2Value || expr is CLS_Expression_Math2ValueAndOr || expr is CLS_Expression_Math2ValueLogic) { if (expr.listParam[0] is ICLS_Value && expr.listParam[1] is ICLS_Value) { CLS_Content.Value result = expr.ComputeValue(content); if ((Type)result.type == typeof(bool)) { CLS_Expression_Value<bool> value = new CLS_Expression_Value<bool>(); value.value = (bool)result.value; value.tokenBegin = expr.listParam[0].tokenBegin; value.tokenEnd = expr.listParam[1].tokenEnd; value.lineBegin = expr.listParam[0].lineBegin; value.lineEnd = expr.listParam[1].lineEnd; return value; } else { ICLS_Type v = content.environment.GetType(result.type); ICLS_Value value = v.MakeValue(result.value); value.tokenBegin = expr.listParam[0].tokenBegin; value.tokenEnd = expr.listParam[1].tokenEnd; value.lineBegin = expr.listParam[0].lineBegin; value.lineEnd = expr.listParam[1].lineEnd; return value as ICLS_Expression; } } } if (expr is CLS_Expression_Math3Value) { CLS_Content.Value result = expr.listParam[0].ComputeValue(content); if ((Type)result.type == typeof(bool)) { bool bv = (bool)result.value; if (bv) return expr.listParam[1]; else return expr.listParam[2]; } } return expr; }
ICLS_Expression CreateValueExpression(long val) { if (val > uint.MaxValue || val < int.MinValue) { CLS_Expression_Value<long> number = new CLS_Expression_Value<long>(); number.value = val; return number; } else if (val > int.MaxValue) { CLS_Expression_Value<uint> number = new CLS_Expression_Value<uint>(); number.value = (uint)val; return number; } else { CLS_Expression_Value<int> number = new CLS_Expression_Value<int>(); number.value = (int)val; return number; } }