private HybInstance ResolveLiteral(LiteralExpressionSyntax node) { var cache = OptCache.GetOrCreate(node, () => { var optNode = new OptLiteralNode(); if (node.Token.Value == null) { optNode.value = HybInstance.Null(); } else if (node.Token.Value is char c) { optNode.value = HybInstance.Char(c); } else if (node.Token.Value is string str) { optNode.value = HybInstance.String(str); } else if (node.Token.Value is bool b) { optNode.value = HybInstance.Bool(b); } else if (node.Token.Value is int i) { if (int.TryParse(node.Token.Text, out _) == false) { throw new SemanticViolationException($"Integer literal out of range"); } optNode.value = HybInstance.Int(i); } else if (node.Token.Value is float f) { optNode.value = HybInstance.Float(f); } else if (node.Token.Value is double d) { optNode.value = HybInstance.Double(d); } else { throw new InvalidOperationException(); } return(optNode); }); return(cache.value); }
public static HybInstance PrefixMinus(HybInstance a) { if (a.IsCompiledType) { if (a.GetHybType().IsValueType) { if (a.Is <Decimal>()) { return(HybInstance.Decimal(-a.As <Decimal>())); } if (a.Is <Double>()) { return(HybInstance.Double(-a.As <Double>())); } if (a.Is <Single>()) { return(HybInstance.Float(-a.As <Single>())); } if (a.Is <Int64>()) { return(HybInstance.Int64(-a.As <Int64>())); } if (a.Is <Int32>()) { return(HybInstance.Int(-a.As <Int32>())); } if (a.Is <short>()) { return(HybInstance.Short(-a.As <short>())); } if (a.Is <sbyte>()) { return(HybInstance.Byte(-a.As <sbyte>())); } } } var negationMethod = GetUnaryNegationMethod(a); if (negationMethod != null) { return(negationMethod.Target.Invoke(null, new HybInstance[] { a })); } throw new NotImplementedException(); }
public static HybInstance Add(HybInstance a, HybInstance b) { if (a.IsNull()) { throw new NullReferenceException(a.Id); } if (a.GetHybType().IsPrimitive) { (a, b) = Promote(a, b); if (a.Is <Int32>()) { return(HybInstance.Int(a.As <Int32>() + b.As <Int32>())); } if (a.Is <Int64>()) { return(HybInstance.Int64(a.As <Int64>() + b.As <Int64>())); } if (a.Is <Single>()) { return(HybInstance.Float(a.As <Single>() + b.As <Single>())); } if (a.Is <Double>()) { return(HybInstance.Double(a.As <Double>() + b.As <Double>())); } } else if (a.Is <String>()) { return(HybInstance.String(a.As <String>() + b.ToString())); } var addMethod = GetAddMethod(a); if (addMethod != null) { return(addMethod.Target.Invoke(null, new HybInstance[] { a, b })); } throw new NotImplementedException(); }