Esempio n. 1
0
 public override ExitResult Run(NameContext context)
 {
     return new ExitResult
     {
         ExitMode = ExitMode.Break
     };
 }
Esempio n. 2
0
 public override Instance Eval(NameContext context)
 {
     var leftValue = LeftArg.EvalScalar(context);
     var rightValue = RightArg.EvalScalar(context);
     var retValue = ComputeBinary(leftValue, rightValue);
     return new Instance(retValue);
 }
Esempio n. 3
0
 public override Instance Eval(NameContext context)
 {
     var indexValue = Index.EvalScalar(context);
     var arrayInst = Array.Eval(context);
     var member = arrayInst.GetField(indexValue);
     return member;
 }
Esempio n. 4
0
 public override ExitResult Run(NameContext context)
 {
     if (Condition.EvalBool(context))
         return Then != null ? Then.Run(context.NewChild()) : null;
     else
         return Else != null ? Else.Run(context.NewChild()) : null;
 }
Esempio n. 5
0
 public override ExitResult Run(NameContext context)
 {
     if (ObjectRef is DotExpr)
     {
         var dot = ObjectRef as DotExpr;
         dot.LeftArg.Eval(context).DeleteField(dot.MemberName);
     }
     else if (ObjectRef is NamedExpr)
     {
         var named = ObjectRef as NamedExpr;
         while (context != null)
         {
             if (context.DeleteField(named.Name))
                 return null;
             context = context.ParentContext;
         }
     }
     else if (ObjectRef is IndexExpr)
     {
         var idx = ObjectRef as IndexExpr;
         var i = idx.Index.EvalScalar(context);
         idx.Array.Eval(context).DeleteField(i);
     }
     else
         throw new Exception("invalid");
     return null;
 }
Esempio n. 6
0
 public override Instance Eval(NameContext context)
 {
     var inst = context.GetFieldUpwards(this.Name);
     if (inst == null)
         throw new Exception(string.Format("undefined '{0}'", this.Name));
     return inst;
 }
Esempio n. 7
0
 public override ExitResult Run(NameContext context)
 {
     var loopContext = context.NewChild();
     NameContext codeContext = null;
     var setInst = SetExpr.Eval(context);
     var instList = setInst.GetFields();
     if (instList != null)
         foreach (var inst in instList)
             if (Code != null)
             {
                 loopContext.SetField(VarName, inst);
                 if (codeContext == null)
                     codeContext = loopContext.NewChild();
                 var result = Code.Run(codeContext);
                 if (result != null)
                     switch (result.ExitMode)
                     {
                         case ExitMode.Break: break;
                         case ExitMode.Return: return result;
                         case ExitMode.Continue: continue;
                         case ExitMode.Except: return result;
                     }
             }
     return null;
 }
Esempio n. 8
0
 public override ExitResult Run(NameContext context)
 {
     return new ExitResult
     {
         ExitMode = ExitMode.Except,
         ExitValue = Arg != null ? Arg.Eval(context) : null
     };
 }
Esempio n. 9
0
 public override Instance Eval(NameContext context)
 {
     var argValue = Arg.EvalScalar(context);
     bool updateArg;
     var retValue = ComputeUnary(ref argValue, out updateArg);
     if (updateArg)
         Arg.Update(context, new Instance(argValue));
     return new Instance(retValue);
 }
Esempio n. 10
0
 public Instance[] ToInstances(NameContext context)
 {
     if (Params == null)
         return null;
     var instances = new Instance[Params.Length];
     for (var i = 0; i < Params.Length; i++)
         instances[i] = Params[i].Eval(context);
     return instances;
 }
Esempio n. 11
0
 public override Instance Eval(NameContext context)
 {
     var inst = new Instance(null);
     var len = Items != null ? Items.Length : 0;
     for (var i = 0; i < len; i++)
         inst.SetField(i, Items[i].Eval(context));
     inst.SetField("length", new Instance(len));
     return inst;
 }
Esempio n. 12
0
 public static dynamic Run(string code, string eval, NameContext context)
 {
     var compiler = new JSCompiler();
     var byteCode = compiler.Parse(code);
     var exitState = byteCode.Run(context);
     if (eval != null)
     {
         var a = context.GetField(eval);
         return a != null ? a.Scalar : null;
     }
     return null;
 }
Esempio n. 13
0
 public bool EvalBool(NameContext context)
 {
     var inst = Eval(context);
     if (inst == null)
         return false;
     var v = inst.Scalar;
     if (v == null)
         return false;
     if (v is string)
         return v.Length > 0;
     Type t = v.GetType();
     if (t.IsAssignableFrom(typeof(byte)))
         return v != 0;
     return v;
 }
Esempio n. 14
0
 public ExitResult Call(NameContext context, Instance owner, Instance[] args)
 {
     if (NativeCode != null)
         return new ExitResult
         {
             ExitMode = Models.ExitMode.Normal,
             ExitValue = NativeCode(owner, args)
         };
     // nesse callContext serao guardados os parametros e o simbolo this=owner
     var callContext = context.NewChild();
     if (Params != null)
         for (var i = 0; i < Params.Length; i++)
             callContext.SetField(Params[i], args != null && i < args.Length ? args[i] : null);
     if (owner != null)
         callContext.SetField("this", owner);
     return Code.Run(callContext);
 }
Esempio n. 15
0
 public override ExitResult Run(NameContext context)
 {
     var result = Code.Run(context.NewChild());
     if (result != null && result.ExitMode == ExitMode.Except)
         if (Catch != null)
         {
             var catchResult = Catch.Run(context.NewChild());
             if (catchResult != null && catchResult.ExitMode != ExitMode.Normal)
                 return catchResult;
             result = null;
         }
     if (Finally != null)
     {
         var finalResult = Finally.Run(context.NewChild());
         if (finalResult != null && finalResult.ExitMode != ExitMode.Normal)
             return finalResult;
     }
     return result;
 }
Esempio n. 16
0
 public override ExitResult Run(NameContext context)
 {
     NameContext codeContext = null;
     while (Condition.EvalBool(context))
         if (Code != null)
         {
             if (codeContext == null)
                 codeContext = context.NewChild();
             var result = Code.Run(codeContext);
             if (result != null)
                 switch (result.ExitMode)
                 {
                     case ExitMode.Break: break;
                     case ExitMode.Return: return result;
                     case ExitMode.Continue: continue;
                     case ExitMode.Except: return result;
                 }
         }
     return null;
 }
Esempio n. 17
0
 public override Instance Eval(NameContext context)
 {
     Instance[] args = null;
     ExprPiece funcExpr;
     if (Creator is ParamsExpr)
     {
         var paramExpr = Creator as ParamsExpr;
         if (paramExpr.Params != null)
         {
             args = new Instance[paramExpr.Params.Length];
             for (var i = 0; i < paramExpr.Params.Length; i++)
                 args[i] = paramExpr.Params[i].Eval(context);
         }
         funcExpr = paramExpr.FuncExpr;
     }
     else
         funcExpr = Creator;
     var funcEval = funcExpr.Eval(context);
     if (funcEval.Prototype != null)
         return funcEval.Prototype.New(args);
     throw new Exception("no constructor");
 }
Esempio n. 18
0
        static void Clock()
        {
            CodePiece c = null;
            var p = new JSCompiler();

            Thread.Sleep(1000);

            var h0 = DateTime.Now;
            for (int i = 0; i < 90; i++)
                c = p.Parse("1+1");
            var h1 = DateTime.Now;
            System.Console.WriteLine("compiled 90 times in: " + (int)((h1 - h0).TotalMilliseconds) + "ms");

            var ctx = new NameContext();
            var hh0 = DateTime.Now;
            for (int i = 0; i < 50000; i++)
                c.Run(ctx);
            var hh1 = DateTime.Now;
            System.Console.WriteLine("ran 50000 times in: " + (int)((hh1 - hh0).TotalMilliseconds) + "ms");

            System.Console.ReadLine();
        }
Esempio n. 19
0
 public override Instance Eval(NameContext context)
 {
     // se for uma expressao no formato obj.proc(), o obj serah o "this" no contexto
     Instance ownerInst, funcInst;
     if (FuncExpr is DotExpr)
     {
         var dotExpr = FuncExpr as DotExpr;
         dotExpr.GetMethod(context, out ownerInst, out funcInst);
     }
     else
     {
         ownerInst = null;
         funcInst = FuncExpr.Eval(context);
     }
     var args = ToInstances(context);
     if (funcInst.Prototype != null)
     {
         var ret = funcInst.Prototype.Run(ownerInst, args);
         return ret != null ? ret.ExitValue : null;
     }
     throw new Exception("no method");
 }
Esempio n. 20
0
 public override ExitResult Run(NameContext context)
 {
     var switchContext = context.NewChild();
     var argInst = Arg.Eval(switchContext);
     var found = false;
     foreach (var someCase in Cases)
         if (someCase.Match(switchContext, argInst))
         {
             found = true;
             var caseResult = someCase.Run(switchContext);
             if (caseResult != null)
                 switch (caseResult.ExitMode)
                 {
                     case ExitMode.Break: return null;
                     case ExitMode.Return: return caseResult;
                     case ExitMode.Continue: return caseResult;
                     case ExitMode.Except: return caseResult;
                 }
         }
     if (!found && Default != null)
         return Default.Run(switchContext);
     return null;
 }
Esempio n. 21
0
 public override ExitResult Run(NameContext context)
 {
     if (Codes == null || Codes.Count == 0)
         return null;
     return Codes.Select(code => code.Run(context)).FirstOrDefault(result => result != null && result.ExitMode != ExitMode.Normal);
 }
Esempio n. 22
0
 public override Instance Eval(NameContext context)
 {
     return Instance.DefineType(this.Function, context);
 }
Esempio n. 23
0
 public abstract ExitResult Run(NameContext context);
Esempio n. 24
0
 public override void Update(NameContext context, Instance inst)
 {
     var indexValue = Index.EvalScalar(context);
     var arrayInst = Array.Eval(context);
     arrayInst.SetField(indexValue, inst);
 }
Esempio n. 25
0
 public override Instance Eval(NameContext context)
 {
     var obj = (object)Expr.EvalScalar(context);
     return new Instance(obj.GetType());
 }
Esempio n. 26
0
 public override ExitResult Run(NameContext context)
 {
     Arg.Eval(context);
     return null;
 }
Esempio n. 27
0
 public override Instance Eval(NameContext context)
 {
     return null;/// base.Eval(context);///TODO
 }
Esempio n. 28
0
 public override ExitResult Run(NameContext context)
 {
     context.SetField(this.Name, Value != null ? Value.Eval(context) : null);
     return null;
 }
Esempio n. 29
0
 public override void Update(NameContext context, Instance inst)
 {
     context.SetFieldUpwards(this.Name, inst);
 }
Esempio n. 30
0
 public override ExitResult Run(NameContext context)
 {
     context.SetField(this.Name, Instance.DefineType(this, context));
     return null;
 }