Пример #1
0
        public override void ExtendDeclaration(IDeclarationModel declaration, CompilationModel model)
        {
            // this only applies to properties
            var propDecl = declaration as PropertyDeclaration;
            if (propDecl == null || propDecl.Container == null)
                return;

            var container = propDecl.Container;
            container.RemoveMember(propDecl);

            var field = new FieldDeclaration
            {
                Container = container,
                OriginatingNode = propDecl.OriginatingNode,
                OriginatingTree = propDecl.OriginatingTree,
                Definition = new FieldDefinition
                {
                    Name = propDecl.Name,
                    ContainingType = propDecl.Definition.ContainingType,
                    Kind = DefinitionKind.Member,
                    MemberKind = MemberDefinitionKind.Field,
                    Type = propDecl.Definition.Type
                }
            };

            field.Definition.Modifiers.Apply(propDecl.Definition.Modifiers);
            container.AddMember(field);
        }
        public override void ExtendDeclaration(IDeclarationModel declaration, CompilationModel model)
        {
            var classDecl = declaration as ClassDeclaration;

            if (classDecl.Definition == null)
                throw new CompilationException("A script object literal attribute may only be applied to a class.", declaration);

            ValidateClass(classDecl);

            // remove from the model, this produces no declaration
            model.RemoveType(classDecl);
        }
Пример #3
0
        public override void ExtendDeclaration(IDeclarationModel declaration, CompilationModel model)
        {
            // this only applies to methods
            var methodDecl = declaration as MethodDeclaration;
            if (methodDecl == null || methodDecl.Container == null)
                return;

            if (!methodDecl.Definition.Symbol.IsExtensionMethod)
                throw new CompilationException("The ScriptMixin attribute can only be applied to extension methods.", declaration);

            // remove the function from the container
            methodDecl.Container.RemoveMember(methodDecl);

            // get the extension target info
            var extTarget = methodDecl.Parameters[0];
            var extType = extTarget.Definition.Type;

            // rewrite first param as a local declaration to this context
            var thisDecl = new LocalDeclarationStatement { VariableDeclaration = new VariableDeclaration() };
            thisDecl.VariableDeclaration.Variables.Add(new VariableDeclarator
            {
                Definition = new LocalDefinition
                {
                    Name = extTarget.Name,
                    Type = extType
                },
                EqualsValueExpression = new LiteralExpression { Text = "this" }
            });

            // add the declaration to the method body
            methodDecl.Body.Statements.Insert(0, thisDecl);

            // create a lambda using the method body.
            var lambdaExpression = new LambdaExpression();
            lambdaExpression.Body = methodDecl.Body;

            for (int i = 1; i < methodDecl.Parameters.Count; i++)
                lambdaExpression.Parameters.Add(methodDecl.Parameters[i]);

            // create a global statement to set the prototype value
            var target = extType.GetFullName() + ".prototype." + methodDecl.Definition.Name;

            model.GlobalStatements.Add(new ExpressionStatement
            {
                Expression = new BinaryExpression
                {
                    LeftExpression = new LiteralExpression { Text = target },
                    RightExpression = lambdaExpression,
                    Operator = "="
                }
            });
        }
        /// <summary>
        /// Validates the generated model.
        /// </summary>
        /// <param name="model">The compilation model.</param>
        /// <param name="result">The compilation result.</param>
        public void Validate(CompilationModel model, CompilationResult result)
        {
            var instanceMethods = new List<string>();
            var staticMethdos = new List<string>();

            foreach (var item in model.Classes)
            {
                instanceMethods.Clear();
                staticMethdos.Clear();

                foreach (var method in item.Methods)
                {
                    ValidateMethod(method, (method.IsStatic ? staticMethdos : instanceMethods), result);
                }
            }
        }
Пример #5
0
        public override List <InstructionModel> GetInstructions(CompilationModel model)
        {
            var instructions = new List <InstructionModel>();
            var @class       = FindParent <Class>();
            var classModel   = model.GetClass(@class.Name);
            var index        = classModel.GetFieldIndex(Property).ToString(CultureInfo.InvariantCulture);

            instructions.Add(new InstructionModel(Instructions.LoadPointerInstruction, "0")
            {
                Comment = model.GetComment(this)
            });
            instructions.Add(new InstructionModel(Instructions.GetFieldInstruction, index)
            {
                Comment = model.GetComment(this)
            });

            return(instructions);
        }
Пример #6
0
        public override void ExtendDeclaration(IDeclarationModel declaration, CompilationModel model)
        {
            if (declaration.Definition == null)
                return;

            switch (declaration.Definition.Kind)
            {
                case DefinitionKind.Type:
                    var typeDecl = declaration as ITypeDeclarationModel;
                    if (typeDecl != null)
                        model.RemoveType(typeDecl);
                    break;

                case DefinitionKind.Member:
                    var memberDecl = declaration as IMemberDeclarationModel;
                    if (memberDecl != null && memberDecl.Container != null)
                        memberDecl.Container.RemoveMember(memberDecl);
                    break;
            }
        }
Пример #7
0
        public override List <InstructionModel> GetInstructions(CompilationModel model)
        {
            var method = FindParent <Method>();
            var @class = (Class)method.Parent;

            var methodModel = model.GetClass(@class.Name).GetMethod(method.FullName);
            var index       = methodModel.GetVariableIndex(Variable).ToString(CultureInfo.InvariantCulture);
            var type        = methodModel.GetVariableType(Variable);


            var instructions = new List <InstructionModel>();

            instructions.AddRange(Expression.GetInstructions(model));            // get instructions of expression
            instructions.Add(new InstructionModel(type == BuiltinTypes.Integer ? // store result into local variable
                                                  Instructions.StoreIntInstruction :
                                                  Instructions.StorePointerInstruction, index));

            instructions.Latest().Comment = model.GetComment(this);

            return(instructions);
        }
        public override List <InstructionModel> GetInstructions(CompilationModel model)
        {
            var instructions = new List <InstructionModel>();
            var method       = FindParent <Method>();
            var @classs      = (Class)method.Parent;
            var methodModel  = model.GetClass(@classs.Name).GetMethod(method.FullName);
            var type         = methodModel.GetVariableType(Variable);
            var index        = methodModel.GetVariableIndex(Variable).ToString(CultureInfo.InvariantCulture);

            if (type == BuiltinTypes.Integer)
            {
                instructions.Add(new InstructionModel(Instructions.LoadIntInstruction, index));
            }
            else
            {
                instructions.Add(new InstructionModel(Instructions.LoadPointerInstruction, index));
            }

            instructions.Latest().Comment = model.GetComment(this);

            return(instructions);
        }
        public override List <InstructionModel> GetInstructions(CompilationModel model)
        {
            var instructions = new List <InstructionModel>();

            if (Expression != null)
            {
                instructions.AddRange(Expression.GetInstructions(model));
                var type        = Expression.GetExpressionType(model);
                var instruction =
                    new InstructionModel(type == BuiltinTypes.Integer
                                             ? Instructions.ReturnIntInstruction
                                             : Instructions.ReturnPointerInstruction);
                instruction.Comment = model.GetComment(this);
                instructions.Add(instruction);
            }
            else
            {
                var instruction = new InstructionModel(Instructions.ReturnInstruction);
                instruction.Comment = model.GetComment(this);
                instructions.Add(instruction);
            }

            return(instructions);
        }
 public override List <InstructionModel> GetInstructions(CompilationModel model)
 {
     return(Literal.GetInstructions(model));
 }
 public override string GetExpressionType(CompilationModel model)
 {
     return(Literal.GetLiteralType());
 }
Пример #12
0
        public override void ExtendDeclaration(IDeclarationModel declaration, CompilationModel model)
        {
            // this only applies to methods
            var methodDecl = declaration as MethodDeclaration;

            if (methodDecl == null || methodDecl.Container == null)
            {
                return;
            }

            if (!methodDecl.Definition.Symbol.IsExtensionMethod)
            {
                throw new CompilationException("The ScriptMixin attribute can only be applied to extension methods.", declaration);
            }

            // remove the function from the container
            methodDecl.Container.RemoveMember(methodDecl);

            // get the extension target info
            var extTarget = methodDecl.Parameters[0];
            var extType   = extTarget.Definition.Type;

            // rewrite first param as a local declaration to this context
            var thisDecl = new LocalDeclarationStatement {
                VariableDeclaration = new VariableDeclaration()
            };

            thisDecl.VariableDeclaration.Variables.Add(new VariableDeclarator
            {
                Definition = new LocalDefinition
                {
                    Name = extTarget.Name,
                    Type = extType
                },
                EqualsValueExpression = new LiteralExpression {
                    Text = "this"
                }
            });

            // add the declaration to the method body
            methodDecl.Body.Statements.Insert(0, thisDecl);

            // create a lambda using the method body.
            var lambdaExpression = new LambdaExpression();

            lambdaExpression.Body = methodDecl.Body;

            for (int i = 1; i < methodDecl.Parameters.Count; i++)
            {
                lambdaExpression.Parameters.Add(methodDecl.Parameters[i]);
            }

            // create a global statement to set the prototype value
            var target = extType.GetFullName() + ".prototype." + methodDecl.Definition.Name;

            model.GlobalStatements.Add(new ExpressionStatement
            {
                Expression = new BinaryExpression
                {
                    LeftExpression = new LiteralExpression {
                        Text = target
                    },
                    RightExpression = lambdaExpression,
                    Operator        = "="
                }
            });
        }
Пример #13
0
 public override void ExtendCompilation(CompilationModel model)
 {
     model.Clear();
 }
Пример #14
0
 public virtual List <InstructionModel> GetInstructions(CompilationModel model)
 {
     return(new List <InstructionModel>());
 }
Пример #15
0
 /// <summary>
 /// Extends a declaration.
 /// </summary>
 /// <param name="declaration">The target declaration.</param>
 /// <param name="model">The containing compilation model.</param>
 public virtual void ExtendDeclaration(IDeclarationModel declaration, CompilationModel model)
 {
 }
Пример #16
0
 public override string GetExpressionType(CompilationModel model)
 {
     return(Type.Name);
 }
Пример #17
0
 public override void ExtendCompilation(CompilationModel model)
 {
     model.Clear();
 }
        public override List <InstructionModel> GetInstructions(CompilationModel model)
        {
            var    instructions = new List <InstructionModel>();
            bool   isStaticCall = false;
            string tmpIndex     = null;

            if (Expression is VariableExpression) // calling static method
            {
                var variable = (VariableExpression)Expression;
                var method   = FindParent <Method>();
                var @class   = (Class)method.Parent;
                // first try to find it in locals in that case it would be local variable
                tmpIndex = model.GetClass(@class.Name).GetMethod(method.FullName).GetVariableIndex(LocalModel.TempVariable).ToString(CultureInfo.InvariantCulture);
                try
                {
                    model.GetClass(@class.Name).GetMethod(method.FullName).GetVariableIndex(variable.Variable);
                }
                catch (Exception)
                {
                    isStaticCall = true;
                }
            }

            // push args
            foreach (var param in Parameters)
            {
                instructions.AddRange(param.GetInstructions(model));
            }

            // find on which object it will be called
            if (isStaticCall)
            {
                var @class = (VariableExpression)Expression;
                instructions.Add(new InstructionModel(Instructions.NewInstruction, @class.Variable)
                {
                    Comment = model.GetComment(this) + " - creating new instance"
                });
                instructions.Add(new InstructionModel(Instructions.StorePointerInstruction, tmpIndex)
                {
                    Comment = model.GetComment(this) + " - storing in tmp variable"
                });
                instructions.Add(new InstructionModel(Instructions.LoadPointerInstruction, tmpIndex)
                {
                    Comment = model.GetComment(this) + " - loading from tmp variable"
                });
            }
            else if (Expression != null)
            {
                instructions.AddRange(Expression.GetInstructions(model));
            }
            else
            {
                instructions.Add(new InstructionModel(Instructions.LoadPointerInstruction, "0")
                {
                    Comment = model.GetComment(this) + " - loading self"
                });
            }

            try
            {
                instructions.Add(new InstructionModel(Instructions.CallInstruction, string.Format("::{0}::{1}", Method, Parameters.Count()))
                {
                    Comment = model.GetComment(this) + " - doing method call"
                });
                if (isStaticCall)
                {
                    instructions.Add(new InstructionModel(Instructions.LoadPointerInstruction, tmpIndex)
                    {
                        Comment = model.GetComment(this) + " - loading from tmp variable"
                    });
                }
            }
            catch (Exception)
            {
                if (Method != "New" && Parameters.Count() != 0)
                {
                    throw;
                }
            }

            return(instructions);
        }
        public override string GetExpressionType(CompilationModel model)
        {
            var fullName = string.Format("{0}:{1}", Method, String.Join(":", Parameters.Select(e => e.GetExpressionType(model))));

            return(GetCorrectParent(model).GetMethod(fullName).Type);
        }
Пример #20
0
 /// <summary>
 /// Extends the completed compilation model.
 /// </summary>
 public virtual void ExtendCompilation(CompilationModel model)
 {
 }
Пример #21
0
 public abstract string GetExpressionType(CompilationModel model);
Пример #22
0
 /// <summary>
 /// Extends a declaration.
 /// </summary>
 /// <param name="declaration">The target declaration.</param>
 /// <param name="model">The containing compilation model.</param>
 public virtual void ExtendDeclaration(IDeclarationModel declaration, CompilationModel model) { }
Пример #23
0
 public virtual void PrepareModel(CompilationModel model)
 {
 }
Пример #24
0
 /// <summary>
 /// Extends the completed compilation model.
 /// </summary>
 public virtual void ExtendCompilation(CompilationModel model) { }
Пример #25
0
 public virtual void Compile(CompilationModel model)
 {
 }
 public override string GetExpressionType(CompilationModel model)
 {
     return(BuiltinTypes.Integer);
 }
Пример #27
0
 public override void Compile(CompilationModel model)
 {
     model.Classes.Latest().Fields.Add(new FieldModel(Name, Type.Name));
 }
 public override List <InstructionModel> GetInstructions(CompilationModel model)
 {
     return(Expression.GetInstructions(model));
 }
        public override string GetExpressionType(CompilationModel model)
        {
            var @class = model.GetClass(Expression.GetExpressionType(model));

            return(@class.GetFieldType(Property));
        }