예제 #1
0
/*
 *  public override void OnCodeAnalysis(CodeAnalysisArgs args) {
 *    switch (args.Phase) {
 *      case CodeAnalysisPhase.Binding:
 *        _dispatcher = args.Context.Runtime.GetDispatcher(Op);
 *        if (_dispatcher == null)
 *          args.Context.ReportError(this.Location, "Operator " + Op + " not defined.");
 *        break;
 *    }//switch
 *    base.OnCodeAnalysis(args);
 *  }
 */

        protected override void DoEvaluate(EvaluationContext context)
        {
            try {
                Left.Evaluate(context);
                object arg1 = context.CurrentResult;
                Right.Evaluate(context);
                context.Arg2 = context.CurrentResult;
                context.Arg1 = arg1;
                _dispatcher.Evaluate(context);
            } catch (RuntimeException e) {
                e.Location = this.Location;
                throw;
            }
        }
예제 #2
0
 private void EvaluateOther(EvaluationContext context)
 {
     Arg.Evaluate(context);
     context.Arg1 = context.CurrentResult;
     _dispatcher.Evaluate(context);
 }
        public void TestOperatorDispatchPlus()
        {
            LanguageRuntime runtime    = new LanguageRuntime();
            CallDispatcher  dispatcher = runtime.GetDispatcher("+");
            object          v;

            v = dispatcher.Evaluate(2, 3);
            CheckResult(v, 5, "int + int");

            v = dispatcher.Evaluate(2, 3.0);
            CheckResult(v, 5.0, "int + double");

            v = dispatcher.Evaluate(2, "3");
            CheckResult(v, "23", "int + string");

            v = dispatcher.Evaluate("2", "3");
            CheckResult(v, "23", "string + string");

            //Note that for all operations on bytes/sbytes/int16 types our implementation returns int32
            v = dispatcher.Evaluate((sbyte)2, (sbyte)3);
            CheckResult(v, 5, "sbyte + sbyte");

            v = dispatcher.Evaluate((byte)2, (byte)3);
            CheckResult(v, 5, "byte + byte");

            v = dispatcher.Evaluate((Int16)2, (Int16)3);
            CheckResult(v, 5, "Int16 + Int16");

            v = dispatcher.Evaluate((byte)2, 3);
            CheckResult(v, 5, "byte + int");

            v = dispatcher.Evaluate(int.MaxValue, 10); //we get overflow here, and switch to Int64
            CheckResult(v, (Int64)int.MaxValue + 10, "Int32 overflow");

            BigInteger xBig = BigInteger.Create(1000000000); //one billion

            xBig = xBig * xBig * xBig;
            xBig = xBig * xBig * xBig - 1; //that makes it really big
            v    = dispatcher.Evaluate(xBig, 2);
            Assert.IsTrue(v != null && v.GetType() == typeof(BigInteger) && v.ToString().EndsWith("0001"), "Test [BigInteger + int] failed");

            v = dispatcher.Evaluate(new Complex(1, 2), new Complex(2, 3));
            CheckResult(v, new Complex(3, 5), "complex + complex");
        }
 private void DispatchMultiple(CallDispatcher dispatcher, object x, object y) {
   int count = 1000000; // one million
   int index = count / 10; //do it in 10-packs to reduce the impact of the loop counting
   Stopwatch sw = new Stopwatch();
   sw.Start();
   while (index-- > 0) {
     dispatcher.Evaluate(x, y);
     dispatcher.Evaluate(x, y);
     dispatcher.Evaluate(x, y);
     dispatcher.Evaluate(x, y);
     dispatcher.Evaluate(x, y);
     dispatcher.Evaluate(x, y);
     dispatcher.Evaluate(x, y);
     dispatcher.Evaluate(x, y);
     dispatcher.Evaluate(x, y);
     dispatcher.Evaluate(x, y);
   }
   sw.Stop();
   long timeNsPerOper = sw.ElapsedMilliseconds * 1000000 / count; //timeMs * (one million ns per ms) divided by number of runs
   string msg = x.GetType().Name +  " + " + y.GetType().Name;
   Trace.WriteLine(msg.PadRight(20) + ": " + timeNsPerOper);
 }