public override bool Equals(Object x) { if (!(x is SeqValue)) { return(false); } IteratorValue e1 = GetIterator(); IteratorValue e2 = (x as SeqValue).GetIterator(); bool b1 = e1.MoveNext(); bool b2 = e2.MoveNext(); // While both lists have data. while (b1 && b2) { HeronValue v1 = e1.GetValue(); HeronValue v2 = e2.GetValue(); if (!v1.Equals(v2)) { return(false); } b1 = e1.MoveNext(); b2 = e2.MoveNext(); } // If one of b1 or b2 is true, then we didn't get to the end of list // so we have different sized lists. if (b1 || b2) { return(false); } return(true); }
public override HeronValue Eval(VM vm) { HeronValue a = operand1.Eval(vm); HeronValue b = operand2.Eval(vm); switch (opcode) { case OpCode.opEq: return(new BoolValue(a.Equals(b))); case OpCode.opNEq: return(new BoolValue(!a.Equals(b))); case OpCode.opIs: { TypeValue tv = b as TypeValue; if (tv == null) { throw new Exception("The second argument of the 'is' operator must be a type"); } return(new BoolValue(a.Is(tv.Type))); } case OpCode.opAs: { HeronType t = b as HeronType; if (t == null) { throw new Exception("The 'as' operator expects a type as a right hand argument"); } HeronValue r = a.As(t); if (r != null) { return(r); } if (t is InterfaceDefn && a is ClassInstance) { DuckValue dv = new DuckValue(a as ClassInstance, t as InterfaceDefn); return(dv); } throw new Exception("Failed to convert " + a.Type.name + " to a " + t.name); }; } return(a.InvokeBinaryOperator(vm, opcode, b)); }
public override void Eval(VM vm) { HeronValue o = condition.Eval(vm); foreach (CaseStatement c in cases) { HeronValue cond = vm.Eval(c.condition); if (o.Equals(cond)) { vm.Eval(c.statement); return; } } if (ondefault != null) { vm.Eval(ondefault); } }
static public void TestCompareValues(string sInput, string sOutput) { Console.WriteLine("testing evaluation of " + sInput); Console.WriteLine("expecting result of " + sOutput); try { HeronValue input = vm.EvalString(sInput); HeronValue output = vm.EvalString(sOutput); Console.WriteLine("test input was " + input.ToString()); Console.WriteLine("test output was " + output.ToString()); if (!input.Equals(output)) { throw new Exception("Result was different than expected"); } } catch (Exception e) { Console.WriteLine("test failed with exception " + e.Message); } }