Exemple #1
0
 public override Et Walk(EtGenerator etgen)
 {
     etgen.FunctionScope.EnterLabelScope(Label, true);
     var et = LoopWalk(etgen);
     etgen.FunctionScope.ExitLabelScope();
     return et;
 }
Exemple #2
0
 public override Et Walk(EtGenerator etgen)
 {
     return Et.Block(
         Target.Walk(etgen),
         Undefined.Expr
     );
 }
Exemple #3
0
        public override Expression Walk(EtGenerator etgen)
        {
            var tmp = Et.Parameter(typeof(object), "#tmp");

            return Et.Block(
                new[] { tmp },

                Et.Assign(
                    tmp,
                    EtUtils.Cast<object>(Left.Walk(etgen))
                ),

                Et.Condition(
                    Et.Dynamic(
                        etgen.Context.CreateConvertBinder(typeof(bool)),
                        typeof(bool),
                        tmp
                    ),

                    EtUtils.Cast<object>(
                        Op == ExpressionType.AndAlso
                               ? Right.Walk(etgen) // &&
                               : tmp               // ||
                    ),

                    EtUtils.Cast<object>(
                        Op == ExpressionType.AndAlso
                               ? tmp               // &&
                               : Right.Walk(etgen) // ||
                    )
                )
            );
        }
Exemple #4
0
        public override Et Walk(EtGenerator etgen)
        {
            if (Label == null)
                return Et.Continue(etgen.FunctionScope.LabelScope.Continue());

            return Et.Continue(etgen.FunctionScope.LabelScope.Continue(Label));
        }
Exemple #5
0
 public override System.Linq.Expressions.Expression Walk(EtGenerator etgen)
 {
     return Et.Call(
         typeof(Operators).GetMethod("TypeOf"),
         Target.Walk(etgen)
     );
 }
Exemple #6
0
 public override Et Walk(EtGenerator etgen)
 {
     return Et.Call(
         typeof(Operators).GetMethod("TypeOf"),
         Target.Walk(etgen)
     );
 }
        public override Et Walk(EtGenerator etgen)
        {
            //TODO: to much boxing/conversion going on
            return EtUtils.Box(
                Et.Convert(
                    Et.Call(
                        typeof(Operators).GetMethod("UnsignedRightShift"),

                        Et.Convert(
                            Et.Dynamic(
                                etgen.Context.CreateConvertBinder(typeof(double)),
                                typeof(double),
                                Left.Walk(etgen)
                            ),
                            typeof(int)
                        ),

                        Et.Convert(
                            Et.Dynamic(
                                etgen.Context.CreateConvertBinder(typeof(double)),
                                typeof(double),
                                Right.Walk(etgen)
                            ),
                            typeof(int)
                        )

                    ),
                    typeof(double)
                )
            );
        }
Exemple #8
0
        public override Et Walk(EtGenerator etgen)
        {
            if (Label == null)
                return Et.Break(etgen.FunctionScope.LabelScope.Break());

            return Et.Break(etgen.FunctionScope.LabelScope.Break(Label));
        }
Exemple #9
0
 public override Et Walk(EtGenerator etgen)
 {
     return etgen.GenerateAssign(
         Target,
         Value.Walk(etgen)
     );
 }
Exemple #10
0
 public override Et Walk(EtGenerator etgen)
 {
     return IObjUtils.EtHasProperty(
         EtUtils.Cast<IObj>(Target.Walk(etgen)),
         Property.Walk(etgen)
     );
 }
Exemple #11
0
        public override Et Walk(EtGenerator etgen)
        {
            etgen.EnterFunctionScope();

            etgen.LambdaTuples.Add(
                Tuple.Create(
                    Et.Lambda<LambdaType>(
                        Et.Block(
                            // lambda body
                            Body.Walk(etgen),
                            Et.Label(
                                etgen.FunctionScope.ReturnLabel,
                                Undefined.Expr // 12.9
                            )
                        ),
                        etgen.FunctionScope.ScopeExpr
                    ),
                    // parameter names
                    Args.Select(x => x.Name).ToList()
                )
            );

            etgen.ExitFunctionScope();

            return Context.EtCreateFunction(
                etgen.Context,
                etgen.FunctionScope.ScopeExpr,
                FunctionTable.EtPull(
                    etgen.FuncTableExpr,
                    etgen.LambdaId
                )
            );
        }
Exemple #12
0
 public override Et Walk(EtGenerator etgen)
 {
     return Et.Call(
         EtUtils.Cast<IObj>(Target.Walk(etgen)),
         IObjMethods.MiHasProperty,
         Property.Walk(etgen)
     );
 }
Exemple #13
0
 public override Et Walk(EtGenerator etgen)
 {
     return Et.Call(
         etgen.FunctionScope.ScopeExpr,
         Scope.MiPull,
         Et.Constant(Name, typeof(object))
     );
 }
Exemple #14
0
 public override Et Walk(EtGenerator etgen)
 {
     return Et.Return(
         etgen.FunctionScope.ReturnLabel,
         EtUtils.Cast<object>(Value.Walk(etgen)),
         typeof(object)
     );
 }
 public override Et Walk(EtGenerator etgen)
 {
     return Et.Call(
         typeof(Operators).GetMethod("InstanceOf"),
         Target.Walk(etgen),
         Function.Walk(etgen)
     );
 }
Exemple #16
0
 public override Et Walk(EtGenerator etgen)
 {
     return Et.Dynamic(
         etgen.Context.CreateUnaryOpBinder(Op),
         typeof(object),
         Target.Walk(etgen)
     );
 }
Exemple #17
0
        public override Et Walk(EtGenerator etgen)
        {
            if (Nodes.Count == 0)
                return Et.Default(typeof(object));

            return Et.Block(
                Nodes.Select(x => x.Walk(etgen))
            );
        }
 public override Et Walk(EtGenerator etgen)
 {
     return Et.Dynamic(
         etgen.Context.CreateGetIndexBinder(new CallInfo(1)),
         typeof(object),
         Target.Walk(etgen),
         Index.Walk(etgen)
     );
 }
Exemple #19
0
 public override Et Walk(EtGenerator etgen)
 {
     return Et.Call(
         Et.Constant(etgen.Context),
         Context.MiCreateRegExp,
         Et.Constant(Regex, typeof(object)),
         Et.Constant(Modifiers, typeof(object))
     );
 }
Exemple #20
0
 public override Et Walk(EtGenerator etgen)
 {
     return Et.Throw(
         AstUtils.SimpleNewHelper(
             JsRuntimeError.Ctor,
             etgen.GenerateConvertToObject(
                 Target.Walk(etgen)
             )
         )
     );
 }
Exemple #21
0
        public override Et Walk(EtGenerator etgen)
        {
            // try ... finally
            if (Catch == null)
            {
                return Et.TryFinally(
                    Body.Walk(etgen),
                    Finally.Walk(etgen)
                );
            }
            else
            {
                var catchParam = Et.Parameter(typeof(JsRuntimeError), "#catch");

                var catchBody = Et.Block(
                    etgen.GenerateAssign(
                        Catch.Target,
                        Et.Property(
                            catchParam,
                            "JsObj"
                        )
                    ),
                    Et.Block(
                        Catch.Body.Walk(etgen)
                    )
                );

                var catchBlock = Et.Catch(
                    catchParam,
                    EtUtils.Cast<object>(catchBody)
                );

                var tryBody = EtUtils.Box(Body.Walk(etgen));

                // try ... catch
                if (Finally == null)
                {
                    return Et.TryCatch(
                        tryBody,
                        catchBlock
                    );
                }
                // try ... catch ... finally
                else
                {
                    return Et.TryCatchFinally(
                        tryBody,
                        Finally.Walk(etgen),
                        catchBlock
                    );
                }
            }
        }
Exemple #22
0
 public override Et Walk(EtGenerator etgen)
 {
     return Et.Dynamic(
         etgen.Context.CreateGetMemberBinder(Name),
         typeof(object),
         Et.Dynamic(
             etgen.Context.CreateConvertBinder(typeof(IObj)),
             typeof(object),
             Target.Walk(etgen)
         )
     );
 }
Exemple #23
0
        public override Et Walk(EtGenerator etgen)
        {
            var trueBranch = TrueBranch.Walk(etgen);
            var elseBranch = etgen.WalkIfNotNull(ElseBranch);

            return Et.Condition(
                Et.Dynamic(
                    etgen.Context.CreateConvertBinder(typeof(bool)),
                    typeof(bool),
                    Test.Walk(etgen)
                ),
                EtUtils.Cast<object>(trueBranch),
                EtUtils.Cast<object>(elseBranch)
            );
        }
Exemple #24
0
        public override Expression Walk(EtGenerator etgen)
        {
            // for both
            Et expr = Et.Call(
                typeof(Operators).GetMethod("StrictEquality"),
                EtUtils.Cast<object>(Left.Walk(etgen)),
                EtUtils.Cast<object>(Right.Walk(etgen))
            );

            // specific to 11.9.5
            if (Op == ExpressionType.NotEqual)
                expr = Et.Not(Et.Convert(expr, typeof(bool)));

            return expr;
        }
Exemple #25
0
        public override Et LoopWalk(EtGenerator etgen)
        {
            Et loop = null;

            var test = Et.Dynamic(
                etgen.Context.CreateConvertBinder(typeof(bool)),
                typeof(bool),
                Test.Walk(etgen)
            );

            // while
            if (Loop == Ast.WhileType.While)
            {
                var body = Body.Walk(etgen);

                loop = AstUtils.While(
                    test,
                    body,
                    null,
                    etgen.FunctionScope.LabelScope.Break(),
                    etgen.FunctionScope.LabelScope.Continue()
                );
            }
            // do ... while
            else if (Loop == Ast.WhileType.DoWhile)
            {
                var bodyExprs = new List<Et>();

                bodyExprs.Add(Body.Walk(etgen));

                // test last, instead of first
                bodyExprs.Add(
                    Et.IfThenElse(
                        test,
                        Et.Continue(etgen.FunctionScope.LabelScope.Continue()),
                        Et.Break(etgen.FunctionScope.LabelScope.Break())
                    )
                );

                loop = Et.Loop(
                    Et.Block(bodyExprs),
                    etgen.FunctionScope.LabelScope.Break(),
                    etgen.FunctionScope.LabelScope.Continue()
                );
            }

            return loop;
        }
Exemple #26
0
        public override Et Walk(EtGenerator etgen)
        {
            etgen.FunctionScope.EnterLabelScope(Label, false);

            var tmp = Et.Variable(typeof(object), "#switch-tmp");
            var hasMatched = Et.Variable(typeof(bool), "#switch-has-matched");

            var et = Et.Block(
                new[] { tmp, hasMatched },
                Et.Assign(
                    hasMatched,
                    Et.Constant(false)
                ),
                Et.Assign(
                    tmp,
                    Target.Walk(etgen)
                ),
                Et.Block(
                    Cases.Select(x =>
                        Et.IfThen(
                            Et.MakeBinary(
                                ExpressionType.OrElse,
                                hasMatched,
                                Et.Call(
                                    typeof(Operators).GetMethod("StrictEquality"),
                                    tmp,
                                    x.Item1.Walk(etgen)
                                )
                            ),
                            Et.Block(
                                x.Item2.Walk(etgen),
                                Et.Assign(
                                    hasMatched,
                                    Et.Constant(true)
                                )
                            )
                        )
                    )
                ),
                Default.Walk(etgen),
                Et.Label(etgen.FunctionScope.LabelScope.Break(Label))
            );

            etgen.FunctionScope.ExitLabelScope();

            return et;
        }
Exemple #27
0
        public override Et Walk(EtGenerator etgen)
        {
            var target = Target.Walk(etgen);
            var args = Args.Select(x => x.Walk(etgen)).ToArray();
            var tmp = Et.Variable(typeof(IObj), "#tmp");
            var exprs = new List<Et>();

            // this handles properties defined
            // in the shorthand json-style object
            // expression: { foo: 1, bar: 2 }
            if (HasProperties)
            {
                foreach (var prop in Properties)
                {
                    exprs.Add(
                        Et.Call(
                            tmp,
                            IObjMethods.MiPut,
                            Et.Constant(prop.Name, typeof(object)),
                            EtUtils.Box(prop.Value.Walk(etgen))
                        )
                    );
                }
            }

            return Et.Block(
                new[] { tmp },
                Et.Assign(
                    tmp,
                    EtUtils.Cast<IObj>(
                        Et.Dynamic(
                            etgen.Context.CreateInstanceBinder(
                                new CallInfo(args.Length)
                            ),
                            typeof(object),
                            ArrayUtils.Insert(
                                target,
                                args
                            )
                        )
                    )
                ),
                EtUtils.CreateBlockIfNotEmpty(exprs),
                EtUtils.Box(tmp)
            );
        }
Exemple #28
0
 public override Et LoopWalk(EtGenerator etgen)
 {
     return Et.Block(
         Setup.Walk(etgen),
         AstUtils.Loop(
             Et.Dynamic(
                 etgen.Context.CreateConvertBinder(typeof(bool)),
                 typeof(bool),
                 Test.Walk(etgen)
             ),
             Incr.Walk(etgen),
             Body.Walk(etgen),
             null,
             etgen.FunctionScope.LabelScope.Break(),
             etgen.FunctionScope.LabelScope.Continue()
         )
     );
 }
Exemple #29
0
        public override Et Walk(EtGenerator etgen)
        {
            if (Target is MemberAccessNode)
            {
                var maNode = (MemberAccessNode)Target;

                return EtUtils.Box(
                    Et.Dynamic(
                        etgen.Context.CreateDeleteMemberBinder(maNode.Name),
                        typeof(void),
                        maNode.Target.Walk(etgen)
                    )
                );
            }

            if (Target is IndexAccessNode)
            {
                var iaNode = (IndexAccessNode)Target;

                return EtUtils.Box(
                    Et.Dynamic(
                        etgen.Context.CreateDeleteIndexBinder(new CallInfo(1)),
                        typeof(void),
                        iaNode.Target.Walk(etgen),
                        iaNode.Index.Walk(etgen)
                    )
                );
            }

            if (Target is IdentifierNode)
            {
                var idNode = (IdentifierNode)Target;

                return Et.Call(
                    etgen.FunctionScope.ScopeExpr,
                    Scope.MiDelete,
                    Et.Constant(idNode.Name, typeof(object))
                );
            }

            throw new NotImplementedException();
        }
Exemple #30
0
        public override Et Walk(EtGenerator etgen)
        {
            etgen.EnterWith();
            var body = Body.Walk(etgen);
            etgen.ExitWith();

            return Et.Block(
                Et.Assign(etgen.FunctionScope.ScopeExpr,
                    AstUtils.SimpleNewHelper(
                        Scope.Ctor2Args,
                        etgen.FunctionScope.ScopeExpr,
                        Target.Walk(etgen)
                    )
                ),
                body,
                Et.Assign(
                    etgen.FunctionScope.ScopeExpr,
                    Et.Property(
                        etgen.FunctionScope.ScopeExpr,
                        Scope.PiParentScope
                    )
                )
            );
        }
Exemple #31
0
    public void ReadJsFile(string in_filename)
    {
        var            context     = Context.Create();
        var            astBuilder  = new AstGenerator();
        var            etGenerator = new EtGenerator();
        var            astNodes    = astBuilder.Build(in_filename, Encoding.UTF8);
        Action <Scope> compiled    = etGenerator.Build(astNodes, context);

        var globals = Scope.CreateGlobal(context);

        context.SetupGlobals(globals);

        // set up 'puts' function
        Action <object> emit = (obj) => { Console.WriteLine(JsTypeConverter.ToString(obj)); };

        globals.Global("puts", emit);

        // Forms the `net" namespace
        net netObj = new net(context);

        globals.Global("net", netObj);

        compiled(globals);
    }