private HybInstance RunBinaryExpression(BinaryExpressionSyntax node) { var op = node.OperatorToken.ValueText; if (op == "is") { return(RunIs(node)); } var left = RunExpression(node.Left); if (op == "||") { if (IsTrueOrEquivalent(left)) { return(HybInstance.Bool(true)); } } else if (op == "&&") { if (IsTrueOrEquivalent(left) == false) { return(HybInstance.Bool(false)); } } var right = RunExpression(node.Right); return(MadMath.Op(left, right, op)); }
private HybInstance RunIs(BinaryExpressionSyntax node) { var left = RunExpression(node.Left); var type = Resolver.GetType($"{node.Right}"); if (type == null) { throw new SemanticViolationException($"Unrecognized type: {node.Right}"); } return(left.Is(type) ? HybInstance.Bool(true) : HybInstance.Bool(false)); }
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 LE(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.Bool(a.As <Int32>() <= b.As <Int32>())); } if (a.Is <Int64>()) { return(HybInstance.Bool(a.As <Int64>() <= b.As <Int64>())); } if (a.Is <Single>()) { return(HybInstance.Bool(a.As <Single>() <= b.As <Single>())); } if (a.Is <Double>()) { return(HybInstance.Bool(a.As <Double>() <= b.As <Double>())); } } var lessEqualMethod = GetLessEqualMethod(a); if (lessEqualMethod != null) { return(lessEqualMethod.Target.Invoke(null, new HybInstance[] { a, b })); } throw new NotImplementedException(); }
public static HybInstance Neq(HybInstance a, HybInstance b) { if (a.IsCompiledType && b.IsCompiledType) { var aType = a.GetHybType(); if (aType.IsValueType) { if (aType.IsPrimitive) { (a, b) = Promote(a, b); } return(HybInstance.Bool(!a.InnerObject.Equals(b.InnerObject))); } else if (aType.CompiledType == typeof(string)) { return(HybInstance.Bool(!a.InnerObject.Equals(b.InnerObject))); } return(HybInstance.Bool(a.InnerObject != b.InnerObject)); } throw new NotImplementedException(); }
public static HybInstance Xor(HybInstance a, HybInstance b) { if (a.IsCompiledType && b.IsCompiledType) { if (a.Is <bool>() && b.Is <bool>()) { return(HybInstance.Bool(a.As <bool>() ^ b.As <bool>())); } if (a.GetHybType().IsPrimitive) { 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 <Int32>())); } } } throw new NotImplementedException(); }