Пример #1
0
 public CodeFragment(Base parent, object code)
 {
     this.Parent = parent;
     this.Code = code;
     if (this.Parent != null)
         this.Type = this.Parent.GetType();
 }
Пример #2
0
        //public ExpressionCompiler()
        //{
        //    var an = new AssemblyName("DynamicAssembly");
        //    var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
        //    var moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");
        //    var tb = moduleBuilder.DefineType("MyDynamicType"
        //                        , TypeAttributes.Public |
        //                        TypeAttributes.Class |
        //                        TypeAttributes.AutoClass |
        //                        TypeAttributes.AnsiClass |
        //                        TypeAttributes.BeforeFieldInit |
        //                        TypeAttributes.AutoLayout
        //                        , null);
        //    //return tb;
        //}
        public static Expression Compile(Base node, Scope scope)
        {
            if (node == null)
                return null;

            var block = node as Block;
            if (block != null)
                return CompileBlock(block, scope);

            var assign = node as Assign;
            if (assign != null)
                return CompileAssign(assign, scope);

            var value = node as Value;
            if (value != null)
                return CompileValue(value, scope);

            var literal = node as Literal;
            if (literal != null)
                return CompileLiteral(literal, scope);

            var parens = node as Parens;
            if (parens != null)
                return CompileParens(parens, scope);

            var arr = node as Arr;
            if (arr != null)
                return CompileArr(arr, scope);

            var iff = node as If;
            if (iff != null)
                return CompileIf(iff, scope);

            var existence = node as Existence;
            if (existence != null)
                return CompileExistence(existence, scope);

            var boo = node as Bool;
            if (boo != null)
                return CompileBool(boo, scope);

            var call = node as Call;
            if (call != null)
                return CompileCall(call, scope);

            var code = node as Code;
            if (code != null)
                return CompileCode(code, scope);

            var nil = node as Null;
            if (nil != null)
                return CompileNull(nil, scope);

            var op = node as Op;
            if (op != null)
                return CompileOp(op, scope);

            var @for = node as For;
            if (@for != null)
                return CompileFor(@for, scope);

            var obj = node as Obj;
            if (obj != null)
                return CompileObj(obj, scope);

            if (node is Return)
            {

            }
            else if (node is Extends)
            {

            }
            else if (node is Access)
            {

            }
            else if (node is Index)
            {

            }
            else if (node is Param)
            {

            }

            throw new NotImplementedException("The node type is not implemented: " + node.GetType().Name);
        }
Пример #3
0
        private static Expression GetMemberInvocaton(Base v, Scope scope, MemberBinder binder, SimpleBinder simple)
        {
            var value = v as Value;

            Expression memberObject;
            if (value != null && value.Properties.Count > 0)
            {
                var last = value.Properties.Last() as Access;
                Helper.IsNotNull(last);
                var nameLit = last.Name as Literal;
                Helper.IsNotNull(nameLit);

                value.Properties.Remove(last);

                memberObject = Compile(value, scope);

                return binder(memberObject, nameLit.Value);
            }

            memberObject = Compile(v, scope);

            return simple(memberObject);
        }
Пример #4
0
        private static Expression CompileObjectAssignment(Obj variable, Base value, Scope scope)
        {
            var tempName = scope.FreeVariableName("ref");

            var expressions1 = new List<Base>
            {
                new Assign {Variable = NewValue(tempName), Value = value}
            };

            var propNames = CompileToNames(variable);

            var assignments = propNames.Select(prop => new Assign {Variable = NewValue(prop), Value = NewValue(tempName, prop)});

            expressions1.AddRange(assignments);
            var expressions = expressions1.Select(expr => Compile(expr, scope));

            return Expression.Block(expressions);
        }
Пример #5
0
 private Base Wrap(Base root)
 {
     return new Code
     {
         Body = root,
         Parameters = new List<Base>
         {
                 new Param{Name = new Literal{Value = "exports"}},
                 new Param{Name = new Literal{Value = "require"}},
                 new Param{Name = new Literal{Value = "module"}},
                 new Param{Name = new Literal{Value = "__filename"}},
                 new Param{Name = new Literal{Value = "__dirname"}},
         }
     };
 }
Пример #6
0
 public static Base Unsoak(Base node)
 {
     Helper.Break();
     return node;
 }