Esempio n. 1
0
 private void SplitFunctionDefinitionNode(FunctionDefinitionNode node)
 {
     if (!node.IsExternLib)
     {
         node.FunctionBody.Body = SplitMulitpleAssignment(node.FunctionBody.Body);
     }
 }
Esempio n. 2
0
        protected virtual void EmitClassDeclNode(ref ProtoCore.AST.AssociativeAST.ClassDeclNode classDeclNode)
        {
            //EmitCode(classDeclNode.ToString());
            //EmitCode("class ");
            //EmitCode(classDeclNode.className);
            //EmitCode("\n{\n");
            List <ProtoCore.AST.AssociativeAST.AssociativeNode> varList = classDeclNode.varlist;

            foreach (ProtoCore.AST.AssociativeAST.AssociativeNode varMember in varList)
            {
                //how is var member stored?
                if (varMember is ProtoCore.AST.AssociativeAST.VarDeclNode)
                {
                    AST.AssociativeAST.VarDeclNode varDecl = varMember as ProtoCore.AST.AssociativeAST.VarDeclNode;
                    EmitVarDeclNode(ref varDecl);
                }
            }
            List <ProtoCore.AST.AssociativeAST.AssociativeNode> funcList = classDeclNode.funclist;

            foreach (ProtoCore.AST.AssociativeAST.AssociativeNode funcMember in funcList)
            {
                if (funcMember is ProtoCore.AST.AssociativeAST.FunctionDefinitionNode)
                {
                    AST.AssociativeAST.FunctionDefinitionNode funcDefNode = funcMember as ProtoCore.AST.AssociativeAST.FunctionDefinitionNode;
                    EmitFunctionDefNode(ref funcDefNode);
                }
            }
            //EmitCode("}\n");
        }
Esempio n. 3
0
        private static void InsertBinaryOperationMethod(Core core, CodeBlockNode root, Operator op, PrimitiveType r, PrimitiveType op1, PrimitiveType op2, int retRank = 0, int op1rank = 0, int op2rank = 0)
        {
            FunctionDefinitionNode funcDefNode = new FunctionDefinitionNode();
            funcDefNode.access = CompilerDefinitions.AccessModifier.kPublic;
            funcDefNode.IsAssocOperator = true;
            funcDefNode.IsBuiltIn = true;
            funcDefNode.Name = Op.GetOpFunction(op);
            funcDefNode.ReturnType = new Type() { Name = core.TypeSystem.GetType((int)r), UID = (int)r, rank = retRank };
            ArgumentSignatureNode args = new ArgumentSignatureNode();
            args.AddArgument(new VarDeclNode()
            {
                Access = CompilerDefinitions.AccessModifier.kPublic,
                NameNode = AstFactory.BuildIdentifier(DSASM.Constants.kLHS),
                ArgumentType = new Type { Name = core.TypeSystem.GetType((int)op1), UID = (int)op1, rank = op1rank }
            });
            args.AddArgument(new VarDeclNode()
            {
                Access = CompilerDefinitions.AccessModifier.kPublic,
                NameNode = AstFactory.BuildIdentifier(DSASM.Constants.kRHS),
                ArgumentType = new Type { Name = core.TypeSystem.GetType((int)op2), UID = (int)op2, rank = op2rank }
            });
            funcDefNode.Signature = args;

            CodeBlockNode body = new CodeBlockNode();

            var lhs = AstFactory.BuildIdentifier(DSASM.Constants.kLHS);
            var rhs = AstFactory.BuildIdentifier(DSASM.Constants.kRHS);
            var binaryExpr = AstFactory.BuildBinaryExpression(lhs, rhs, op);
            body.Body.Add(AstFactory.BuildReturnStatement(binaryExpr));

            funcDefNode.FunctionBody = body;
            root.Body.Add(funcDefNode);
        }
Esempio n. 4
0
    private static void InsertBinaryOperationMethod(Core core, ProtoCore.AST.Node root, Operator op, PrimitiveType r, PrimitiveType op1, PrimitiveType op2, int retRank = 0, int op1rank = 0, int op2rank = 0)
    {
        ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
        funcDefNode.access = ProtoCore.DSASM.AccessSpecifier.kPublic;
        funcDefNode.IsAssocOperator = true;
        funcDefNode.IsBuiltIn = true;
        funcDefNode.Name = Op.GetOpFunction(op);
        funcDefNode.ReturnType = new ProtoCore.Type() { Name = core.TypeSystem.GetType((int)r), UID = (int)r, rank = retRank};
        ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
        args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
        {
            memregion = ProtoCore.DSASM.MemoryRegion.kMemStack,
            access = ProtoCore.DSASM.AccessSpecifier.kPublic,
            NameNode = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kLHS),
            ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)op1), UID = (int)op1, rank = op1rank}
        });
        args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
        {
            memregion = ProtoCore.DSASM.MemoryRegion.kMemStack,
            access = ProtoCore.DSASM.AccessSpecifier.kPublic,
            NameNode = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kRHS),
            ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)op2), UID = (int)op2, rank = op2rank}
        });
        funcDefNode.Signature = args;

        ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
        ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(core, ProtoCore.DSDefinitions.Keyword.Return, ProtoCore.PrimitiveType.kTypeReturn);

        ProtoCore.AST.AssociativeAST.IdentifierNode lhs = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kLHS);
        ProtoCore.AST.AssociativeAST.IdentifierNode rhs = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kRHS);
        body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = _return, Optr = ProtoCore.DSASM.Operator.assign, RightNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = lhs, RightNode = rhs, Optr = op } });
        funcDefNode.FunctionBody = body;
        (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode);
    }
Esempio n. 5
0
        private ProtoCore.AST.AssociativeAST.AssociativeNode ParseMethod(MethodInfo method)
        {
            ProtoCore.Type retype       = CLRModuleType.GetProtoCoreType(method.ReturnType, Module);
            bool           propaccessor = isPropertyAccessor(method);
            bool           isOperator   = isOverloadedOperator(method);

            FFIMethodAttributes mattrs = new FFIMethodAttributes(method);

            if (method.IsStatic &&
                method.DeclaringType == method.ReturnType &&
                !propaccessor &&
                !isOperator)
            {
                //case for named constructor. Must return a pointer type
                if (!Object.Equals(method.ReturnType, CLRType))
                {
                    throw new InvalidOperationException("Unexpected type for constructor {0D28FC00-F8F4-4049-AD1F-BBC34A68073F}");
                }

                retype = ProtoCoreType;
                ConstructorDefinitionNode node = ParsedNamedConstructor(method, method.Name, retype);
                node.MethodAttributes = mattrs;
                return(node);
            }

            //Need to hide property accessor from design script users, prefix with %
            string prefix = (isOperator || propaccessor) ? "%" : "";
            var    func   = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();

            if (isOperator)
            {
                func.Name = string.Format("{0}{1}", prefix, GetDSOperatorName(method.Name));
            }
            else
            {
                func.Name = string.Format("{0}{1}", prefix, method.Name);
            }
            func.Pattern   = null;
            func.Signature = ParseArgumentSignature(method);

            if ((retype.IsIndexable && mattrs.AllowRankReduction) ||
                (typeof(object).Equals(method.ReturnType)))
            {
                retype.rank = Constants.kArbitraryRank;
            }
            func.ReturnType       = retype;
            func.FunctionBody     = null;
            func.access           = ProtoCore.Compiler.AccessSpecifier.kPublic;
            func.IsDNI            = false;
            func.IsExternLib      = true;
            func.ExternLibName    = Module.Name;
            func.IsStatic         = method.IsStatic;
            func.MethodAttributes = mattrs;

            return(func);
        }
        private static void InsertInlineConditionOperationMethod(Core core, ProtoCore.AST.Node root, PrimitiveType condition, PrimitiveType r)
        {
            ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
            funcDefNode.access     = ProtoCore.Compiler.AccessSpecifier.kPublic;
            funcDefNode.Name       = ProtoCore.DSASM.Constants.kInlineCondition;
            funcDefNode.ReturnType = new ProtoCore.Type()
            {
                Name = core.TypeSystem.GetType((int)r), UID = (int)r
            };
            ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
            args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
            {
                memregion    = ProtoCore.DSASM.MemoryRegion.kMemStack,
                access       = ProtoCore.Compiler.AccessSpecifier.kPublic,
                NameNode     = BuildAssocIdentifier(core, "%condition"),
                ArgumentType = new ProtoCore.Type {
                    Name = core.TypeSystem.GetType((int)condition), UID = (int)condition
                }
            });
            args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
            {
                memregion    = ProtoCore.DSASM.MemoryRegion.kMemStack,
                access       = ProtoCore.Compiler.AccessSpecifier.kPublic,
                NameNode     = BuildAssocIdentifier(core, "%trueExp"),
                ArgumentType = new ProtoCore.Type {
                    Name = core.TypeSystem.GetType((int)r), UID = (int)r
                }
            });
            args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
            {
                memregion    = ProtoCore.DSASM.MemoryRegion.kMemStack,
                access       = ProtoCore.Compiler.AccessSpecifier.kPublic,
                NameNode     = BuildAssocIdentifier(core, "%falseExp"),
                ArgumentType = new ProtoCore.Type {
                    Name = core.TypeSystem.GetType((int)r), UID = (int)r
                }
            });
            funcDefNode.Signature = args;

            ProtoCore.AST.AssociativeAST.CodeBlockNode  body    = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
            ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(core, ProtoCore.DSDefinitions.Keyword.Return, ProtoCore.PrimitiveType.kTypeReturn);
            ProtoCore.AST.AssociativeAST.IdentifierNode con     = BuildAssocIdentifier(core, "%condition");
            ProtoCore.AST.AssociativeAST.IdentifierNode t       = BuildAssocIdentifier(core, "%trueExp");
            ProtoCore.AST.AssociativeAST.IdentifierNode f       = BuildAssocIdentifier(core, "%falseExp");

            body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode()
            {
                LeftNode = _return, Optr = Operator.assign, RightNode = new ProtoCore.AST.AssociativeAST.InlineConditionalNode()
                {
                    ConditionExpression = con, TrueExpression = t, FalseExpression = f
                }
            });
            funcDefNode.FunctionBody = body;
            (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode);
        }
        /// <summary>
        /// Gets the has id of a function signature given the name and argument types
        /// </summary>
        /// <param name="functionDef"></param>
        /// <returns></returns>
        public static int GetFunctionHash(ProtoCore.AST.AssociativeAST.FunctionDefinitionNode functionDef)
        {
            Validity.Assert(null != functionDef);
            string functionDescription = functionDef.Name;

            foreach (ProtoCore.AST.AssociativeAST.VarDeclNode argNode in functionDef.Signature.Arguments)
            {
                functionDescription += argNode.ArgumentType.ToString();
            }
            return(functionDescription.GetHashCode());
        }
        private static void InsertBinaryOperationMethod(Core core, ProtoCore.AST.Node root, Operator op, PrimitiveType r, PrimitiveType op1, PrimitiveType op2, int retRank = 0, int op1rank = 0, int op2rank = 0)
        {
            ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
            funcDefNode.access          = ProtoCore.Compiler.AccessSpecifier.kPublic;
            funcDefNode.IsAssocOperator = true;
            funcDefNode.IsBuiltIn       = true;
            funcDefNode.Name            = Op.GetOpFunction(op);
            funcDefNode.ReturnType      = new ProtoCore.Type()
            {
                Name = core.TypeSystem.GetType((int)r), UID = (int)r, rank = retRank
            };
            ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
            args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
            {
                memregion    = ProtoCore.DSASM.MemoryRegion.kMemStack,
                access       = ProtoCore.Compiler.AccessSpecifier.kPublic,
                NameNode     = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kLHS),
                ArgumentType = new ProtoCore.Type {
                    Name = core.TypeSystem.GetType((int)op1), UID = (int)op1, rank = op1rank
                }
            });
            args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
            {
                memregion    = ProtoCore.DSASM.MemoryRegion.kMemStack,
                access       = ProtoCore.Compiler.AccessSpecifier.kPublic,
                NameNode     = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kRHS),
                ArgumentType = new ProtoCore.Type {
                    Name = core.TypeSystem.GetType((int)op2), UID = (int)op2, rank = op2rank
                }
            });
            funcDefNode.Signature = args;

            ProtoCore.AST.AssociativeAST.CodeBlockNode  body    = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
            ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(core, ProtoCore.DSDefinitions.Keyword.Return, ProtoCore.PrimitiveType.kTypeReturn);

            ProtoCore.AST.AssociativeAST.IdentifierNode lhs = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kLHS);
            ProtoCore.AST.AssociativeAST.IdentifierNode rhs = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kRHS);
            body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode()
            {
                LeftNode = _return, Optr = ProtoCore.DSASM.Operator.assign, RightNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode()
                {
                    LeftNode = lhs, RightNode = rhs, Optr = op
                }
            });
            funcDefNode.FunctionBody = body;
            (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode);
        }
Esempio n. 9
0
    private static ProtoCore.AST.AssociativeAST.FunctionDefinitionNode GenerateBuiltInMethodSignatureNode(ProtoCore.Lang.BuiltInMethods.BuiltInMethod method)
    {
        ProtoCore.AST.AssociativeAST.FunctionDefinitionNode fDef = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
        fDef.Name = ProtoCore.Lang.BuiltInMethods.GetMethodName(method.ID);
        fDef.ReturnType = method.ReturnType;
        fDef.IsExternLib = true;
        fDef.IsBuiltIn = true;
        fDef.BuiltInMethodId = method.ID;
        fDef.Signature = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
        fDef.MethodAttributes = method.MethodAttributes;

        foreach (KeyValuePair<string, ProtoCore.Type> param in method.Parameters)
        {
            ProtoCore.AST.AssociativeAST.VarDeclNode arg = new ProtoCore.AST.AssociativeAST.VarDeclNode();
            arg.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode { Name = param.Key, Value = param.Key };
            arg.ArgumentType = param.Value;
            fDef.Signature.AddArgument(arg);
        }

        return fDef;
    }
Esempio n. 10
0
        private ProtoCore.AST.AssociativeAST.FunctionDefinitionNode ParseFieldAccessor(FieldInfo f)
        {
            if (null == f || !IsBrowsable(f))
            {
                return(null);
            }

            ProtoCore.AST.AssociativeAST.FunctionDefinitionNode func = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
            func.Name          = string.Format("%get_{0}", f.Name);
            func.Pattern       = null;
            func.Singnature    = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
            func.ReturnType    = CLRModuleType.GetProtoCoreType(f.FieldType, Module);
            func.FunctionBody  = null;
            func.access        = ProtoCore.DSASM.AccessSpecifier.kPublic;
            func.IsDNI         = false;
            func.IsExternLib   = true;
            func.ExternLibName = Module.Name;
            func.IsStatic      = f.IsStatic;

            return(func);
        }
Esempio n. 11
0
        private ProtoCore.AST.AssociativeAST.AssociativeNode ParseMethod(MethodInfo method)
        {
            ProtoCore.Type retype       = CLRModuleType.GetProtoCoreType(method.ReturnType, Module);
            bool           propaccessor = isPropertyAccessor(method);

            if (method.IsStatic && method.DeclaringType == method.ReturnType && !propaccessor)
            {
                //case for named constructor. Must return a pointer type
                if (!Object.Equals(method.ReturnType, CLRType))
                {
                    throw new InvalidOperationException("Unexpected type for constructor {0D28FC00-F8F4-4049-AD1F-BBC34A68073F}");
                }

                retype = ProtoCoreType;
                return(ParsedNamedConstructor(method, method.Name, retype));
            }

            //Need to hide property accessor from design script users, prefix with %
            string prefix = propaccessor ? "%" : "";

            ProtoCore.AST.AssociativeAST.FunctionDefinitionNode func = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
            func.Name       = string.Format("{0}{1}", prefix, method.Name);
            func.Pattern    = null;
            func.Singnature = ParseArgumentSignature(method);
            if (retype.IsIndexable && AllowsRankReduction(method))
            {
                retype.rank = -1;
            }
            func.ReturnType    = retype;
            func.FunctionBody  = null;
            func.access        = ProtoCore.DSASM.AccessSpecifier.kPublic;
            func.IsDNI         = false;
            func.IsExternLib   = true;
            func.ExternLibName = Module.Name;
            func.IsStatic      = method.IsStatic;

            return(func);
        }
        private static ProtoCore.AST.AssociativeAST.FunctionDefinitionNode GenerateBuiltInMethodSignatureNode(ProtoCore.Lang.BuiltInMethods.BuiltInMethod method)
        {
            ProtoCore.AST.AssociativeAST.FunctionDefinitionNode fDef = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
            fDef.Name             = ProtoCore.Lang.BuiltInMethods.GetMethodName(method.ID);
            fDef.ReturnType       = method.ReturnType;
            fDef.IsExternLib      = true;
            fDef.IsBuiltIn        = true;
            fDef.BuiltInMethodId  = method.ID;
            fDef.Signature        = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
            fDef.MethodAttributes = method.MethodAttributes;

            foreach (KeyValuePair <string, ProtoCore.Type> param in method.Parameters)
            {
                ProtoCore.AST.AssociativeAST.VarDeclNode arg = new ProtoCore.AST.AssociativeAST.VarDeclNode();
                arg.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode {
                    Name = param.Key, Value = param.Key
                };
                arg.ArgumentType = param.Value;
                fDef.Signature.AddArgument(arg);
            }

            return(fDef);
        }
Esempio n. 13
0
        // The following methods are used to insert methods to the bottom of the AST and convert operator to these method calls
        // to support replication on operators
        private static void InsertUnaryOperationMethod(Core core, ProtoCore.AST.Node root, UnaryOperator op, PrimitiveType r, PrimitiveType operand)
        {
            ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
            funcDefNode.access          = ProtoCore.CompilerDefinitions.AccessModifier.kPublic;
            funcDefNode.IsAssocOperator = true;
            funcDefNode.IsBuiltIn       = true;
            funcDefNode.Name            = Op.GetUnaryOpFunction(op);
            funcDefNode.ReturnType      = new ProtoCore.Type()
            {
                Name = core.TypeSystem.GetType((int)r), UID = (int)r
            };
            ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
            args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
            {
                memregion    = ProtoCore.DSASM.MemoryRegion.kMemStack,
                access       = ProtoCore.CompilerDefinitions.AccessModifier.kPublic,
                NameNode     = BuildAssocIdentifier(core, "%param"),
                ArgumentType = new ProtoCore.Type {
                    Name = core.TypeSystem.GetType((int)operand), UID = (int)operand
                }
            });
            funcDefNode.Signature = args;

            ProtoCore.AST.AssociativeAST.CodeBlockNode  body    = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
            ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(core, ProtoCore.DSDefinitions.Keyword.Return, ProtoCore.PrimitiveType.kTypeReturn);
            ProtoCore.AST.AssociativeAST.IdentifierNode param   = BuildAssocIdentifier(core, "%param");
            body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode()
            {
                LeftNode = _return, Optr = ProtoCore.DSASM.Operator.assign, RightNode = new ProtoCore.AST.AssociativeAST.UnaryExpressionNode()
                {
                    Expression = param, Operator = op
                }
            });
            funcDefNode.FunctionBody = body;
            (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode);
        }
Esempio n. 14
0
        private void EmitClassDeclNode(AssociativeNode node, ref ProtoCore.Type inferedType, ProtoCore.DSASM.AssociativeSubCompilePass subPass = ProtoCore.DSASM.AssociativeSubCompilePass.kNone)
        {
            ClassDeclNode classDecl = node as ClassDeclNode;

            // Handling n-pass on class declaration
            if (ProtoCore.DSASM.AssociativeCompilePass.kClassName == compilePass)
            {
                ProtoCore.DSASM.ClassNode cnode = new ProtoCore.DSASM.ClassNode();
                cnode.name = classDecl.className;
                cnode.size = classDecl.varlist.Count;
                cnode.IsImportedClass = classDecl.IsImportedClass;
                cnode.typeSystem = core.TypeSystem;
                globalClassIndex = core.ClassTable.Append(cnode);
                CodeRangeTable.AddClassBlockRangeEntry(globalClassIndex,
                    classDecl.line,
                    classDecl.col,
                    classDecl.endLine,
                    classDecl.endCol,
                    core.CurrentDSFileName);
            }
            else if (ProtoCore.DSASM.AssociativeCompilePass.kClassHeirarchy == compilePass)
            {
                // Class heirarchy pass
                // Populating each class entry with their respective base classes
                globalClassIndex = core.ClassTable.IndexOf(classDecl.className);

                ProtoCore.DSASM.ClassNode cnode = core.ClassTable.ClassNodes[globalClassIndex];

                if (null != classDecl.superClass)
                {
                    for (int n = 0; n < classDecl.superClass.Count; ++n)
                    {
                        int baseClass = core.ClassTable.IndexOf(classDecl.superClass[n]);
                        if (ProtoCore.DSASM.Constants.kInvalidIndex != baseClass)
                        {
                            cnode.baseList.Add(baseClass);
                            cnode.coerceTypes.Add(baseClass, (int)ProtoCore.DSASM.ProcedureDistance.kCoerceBaseClass);
                        }
                    }
                }

            }
            else if (ProtoCore.DSASM.AssociativeCompilePass.kClassMemVar == compilePass)
            {
                // Class member variable pass
                // Populating each class entry symbols with their respective member variables

                globalClassIndex = core.ClassTable.IndexOf(classDecl.className);
                ProtoCore.DSASM.ClassNode cnode = core.ClassTable.ClassNodes[globalClassIndex];

                // Handle member vars from base class
                if (null != classDecl.superClass)
                {
                    for (int n = 0; n < classDecl.superClass.Count; ++n)
                    {
                        int baseClass = core.ClassTable.IndexOf(classDecl.superClass[n]);
                        if (ProtoCore.DSASM.Constants.kInvalidIndex != baseClass)
                        {
                            // Append the members variables of every class that this class inherits from
                            foreach (ProtoCore.DSASM.SymbolNode symnode in core.ClassTable.ClassNodes[baseClass].symbols.symbolList.Values)
                            {
                                // It is a member variables
                                if (ProtoCore.DSASM.Constants.kGlobalScope == symnode.functionIndex)
                                {
                                    Debug.Assert(!symnode.isArgument);
                                    int symbolIndex = AllocateMemberVariable(globalClassIndex, symnode.isStatic ? symnode.classScope : baseClass, symnode.name, symnode.datatype, symnode.access, symnode.isStatic);

                                    if (symbolIndex != ProtoCore.DSASM.Constants.kInvalidIndex)
                                        cnode.size += ProtoCore.DSASM.Constants.kPointerSize;
                                }
                            }
                        }
                        else
                        {
                            Debug.Assert(false, "n-pass compile error, fixme Jun....");
                        }
                    }
                }

                // This list will store all static properties initialization
                // expression (say x = 1).
                List<BinaryExpressionNode> staticPropertyInitList = new List<BinaryExpressionNode>();

                foreach (VarDeclNode vardecl in classDecl.varlist)
                {
                    IdentifierNode varIdent = null;
                    if (vardecl.NameNode is IdentifierNode)
                    {
                        varIdent = vardecl.NameNode as IdentifierNode;
                    }
                    else if (vardecl.NameNode is BinaryExpressionNode)
                    {
                        BinaryExpressionNode bNode = vardecl.NameNode as BinaryExpressionNode;
                        varIdent = bNode.LeftNode as IdentifierNode;
                        if (vardecl.IsStatic)
                            staticPropertyInitList.Add(bNode);
                        else
                            cnode.defaultArgExprList.Add(bNode);
                    }
                    else
                    {
                        Debug.Assert(false, "Check generated AST");
                    }

                    // It is possible that fail to allocate variable. In that
                    // case we should remove initializing expression from
                    // cnode's defaultArgExprList
                    ProtoCore.Type datatype = vardecl.ArgumentType;
                    if (string.IsNullOrEmpty(vardecl.ArgumentType.Name))
                        datatype.UID = (int)PrimitiveType.kTypeVar;
                    else
                    {
                        datatype.UID = core.TypeSystem.GetType(vardecl.ArgumentType.Name);
                        if (datatype.UID == ProtoCore.DSASM.Constants.kInvalidIndex)
                            datatype.UID = (int)PrimitiveType.kTypeVar;
                    }
                    datatype.Name = core.TypeSystem.GetType(datatype.UID);
                    int symbolIndex = ProtoCore.DSASM.Constants.kInvalidIndex;

                    if (varIdent is ProtoCore.AST.AssociativeAST.TypedIdentifierNode)
                        symbolIndex = AllocateTypedMemberVariable(globalClassIndex, globalClassIndex, varIdent.Value, datatype, vardecl.access, vardecl.IsStatic);
                    else
                        symbolIndex = AllocateMemberVariable(globalClassIndex, globalClassIndex, varIdent.Value, datatype, vardecl.access, vardecl.IsStatic);

                    if (symbolIndex != ProtoCore.DSASM.Constants.kInvalidIndex &&
                        !classDecl.IsExternLib)
                    {
                        ProtoCore.DSASM.SymbolNode memVar =
                            vardecl.IsStatic
                            ? core.CodeBlockList[0].symbolTable.symbolList[symbolIndex]
                            : core.ClassTable.ClassNodes[globalClassIndex].symbols.symbolList[symbolIndex];

                        EmitGetterForMemVar(classDecl, memVar);
                        EmitSetterForMemVar(classDecl, memVar);
                    }
                }
                classOffset = 0;

                // Now we are going to create a static function __init_static_properties()
                // which will initialize all static properties. We will emit a
                // call to this function after all classes have been compiled.
                if (staticPropertyInitList.Count > 0)
                {
                    FunctionDefinitionNode initFunc = new FunctionDefinitionNode()
                    {
                        Name = ProtoCore.DSASM.Constants.kStaticPropertiesInitializer,
                        Singnature = new ArgumentSignatureNode(),
                        Pattern = null,
                        ReturnType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)PrimitiveType.kTypeNull), UID = (int)PrimitiveType.kTypeNull },
                        FunctionBody = new CodeBlockNode(),
                        IsExternLib = false,
                        IsDNI = false,
                        ExternLibName = null,
                        access = ProtoCore.DSASM.AccessSpecifier.kPublic,
                        IsStatic = true
                    };
                    classDecl.funclist.Add(initFunc);

                    staticPropertyInitList.ForEach(bNode => initFunc.FunctionBody.Body.Add(bNode));
                    initFunc.FunctionBody.Body.Add(new BinaryExpressionNode()
                    {
                        LeftNode = new IdentifierNode()
                        {
                            Value = ProtoCore.DSDefinitions.Kw.kw_return,
                            Name = ProtoCore.DSDefinitions.Kw.kw_return,
                            datatype = core.TypeSystem.BuildTypeObject(PrimitiveType.kTypeReturn, false)
                        },
                        Optr = ProtoCore.DSASM.Operator.assign,
                        RightNode = new NullNode()
                    });
                }
            }
            else if (ProtoCore.DSASM.AssociativeCompilePass.kClassMemFuncSig == compilePass)
            {
                // Class member variable pass
                // Populating each class entry vtables with their respective member variables signatures

                globalClassIndex = core.ClassTable.IndexOf(classDecl.className);
                foreach (AssociativeNode funcdecl in classDecl.funclist)
                {
                    DfsTraverse(funcdecl, ref inferedType);
                }

                if (!classDecl.IsExternLib)
                {
                    ProtoCore.DSASM.ProcedureTable vtable = core.ClassTable.ClassNodes[globalClassIndex].vtable;
                    if (vtable.IndexOfExact(classDecl.className, new List<ProtoCore.Type>()) == ProtoCore.DSASM.Constants.kInvalidIndex)
                    {
                        ConstructorDefinitionNode defaultConstructor = new ConstructorDefinitionNode();
                        defaultConstructor.Name = classDecl.className;
                        defaultConstructor.localVars = 0;
                        defaultConstructor.Signature = new ArgumentSignatureNode();
                        defaultConstructor.Pattern = null;
                        defaultConstructor.ReturnType = new ProtoCore.Type { Name = "var", UID = 0 };
                        defaultConstructor.FunctionBody = new CodeBlockNode();
                        defaultConstructor.baseConstr = null;
                        defaultConstructor.access = ProtoCore.DSASM.AccessSpecifier.kPublic;
                        defaultConstructor.IsExternLib = false;
                        defaultConstructor.ExternLibName = null;
                        DfsTraverse(defaultConstructor, ref inferedType);
                        classDecl.funclist.Add(defaultConstructor);
                    }
                }
            }
            else if (ProtoCore.DSASM.AssociativeCompilePass.kClassMemFuncBody == compilePass)
            {
                // Class member variable pass
                // Populating the function body of each member function defined in the class vtables

                globalClassIndex = core.ClassTable.IndexOf(classDecl.className);

                foreach (AssociativeNode funcdecl in classDecl.funclist)
                {
                    // reset the inferedtype between functions
                    inferedType = new ProtoCore.Type();
                    DfsTraverse(funcdecl, ref inferedType, false, null, subPass);
                }
            }

            // Reset the class index
            core.ClassIndex = globalClassIndex = ProtoCore.DSASM.Constants.kGlobalScope;
        }
Esempio n. 15
0
        private void EmitMemberVariables(ClassDeclNode classDecl, GraphNode graphNode = null)
        {
            // Class member variable pass
            // Populating each class entry symbols with their respective member variables

            int thisClassIndex = core.ClassTable.GetClassId(classDecl.ClassName);
            globalClassIndex = thisClassIndex;

            if (!unPopulatedClasses.ContainsKey(thisClassIndex))
            {
                return;
            }
            ProtoCore.DSASM.ClassNode thisClass = core.ClassTable.ClassNodes[thisClassIndex];

            // Handle member vars from base class
            if (null != classDecl.BaseClasses)
            {
                for (int n = 0; n < classDecl.BaseClasses.Count; ++n)
                {
                    int baseClassIndex = core.ClassTable.GetClassId(classDecl.BaseClasses[n]);
                    if (ProtoCore.DSASM.Constants.kInvalidIndex != baseClassIndex)
                    {
                        // To handle the case that a base class is defined after
                        // this class.
                        if (unPopulatedClasses.ContainsKey(baseClassIndex))
                        {
                            EmitMemberVariables(unPopulatedClasses[baseClassIndex], graphNode);
                            globalClassIndex = thisClassIndex;
                        }

                        ClassNode baseClass = core.ClassTable.ClassNodes[baseClassIndex];
                        // Append the members variables of every class that this class inherits from
                        foreach (ProtoCore.DSASM.SymbolNode symnode in baseClass.Symbols.symbolList.Values)
                        {
                            // It is a member variables
                            if (ProtoCore.DSASM.Constants.kGlobalScope == symnode.functionIndex)
                            {
                                Validity.Assert(!symnode.isArgument);
                                int symbolIndex = AllocateMemberVariable(thisClassIndex, symnode.isStatic ? symnode.classScope : baseClassIndex, symnode.name, symnode.datatype.rank, symnode.access, symnode.isStatic);

                                if (symbolIndex != ProtoCore.DSASM.Constants.kInvalidIndex)
                                    thisClass.Size += ProtoCore.DSASM.Constants.kPointerSize;
                            }
                        }
                    }
                    else
                    {
                        Validity.Assert(false, "n-pass compile error, fixme Jun....");
                    }
                }
            }

            // This list will store all static properties initialization
            // expression (say x = 1).
            List<BinaryExpressionNode> staticPropertyInitList = new List<BinaryExpressionNode>();

            foreach (VarDeclNode vardecl in classDecl.Variables)
            {
                IdentifierNode varIdent = null;
                if (vardecl.NameNode is IdentifierNode)
                {   
                    varIdent = vardecl.NameNode as IdentifierNode;

                    BinaryExpressionNode bNode = new BinaryExpressionNode();

                    var thisNode =AstFactory.BuildIdentifier(ProtoCore.DSDefinitions.Keyword.This);
                    var propNode =AstFactory.BuildIdentifier(varIdent.Value);
                    bNode.LeftNode = AstFactory.BuildIdentList(thisNode, propNode);

                    NodeUtils.CopyNodeLocation(bNode, vardecl);
                    bNode.Optr = ProtoCore.DSASM.Operator.assign;

                    bool skipInitialization = false;

                    // Initialize it to default value by manually add the right hand side node
                    if (vardecl.ArgumentType.rank == 0)
                    {
                        switch (vardecl.ArgumentType.Name)
                        {
                            case "double": bNode.RightNode = new DoubleNode(0); break;
                            case "int": bNode.RightNode = new IntNode(0); break;
                            case "bool": bNode.RightNode = new BooleanNode(false); break;
                            default: skipInitialization = true; break;
                        }
                    }
                    else if (vardecl.ArgumentType.rank > 0)
                    {
                        if (!vardecl.ArgumentType.Name.Equals("var"))
                            bNode.RightNode = new ExprListNode();
                        else
                            skipInitialization = true;
                    }
                    else if(vardecl.ArgumentType.rank.Equals(ProtoCore.DSASM.Constants.kArbitraryRank))
                    {
                        if (!vardecl.ArgumentType.Name.Equals("var"))
                            bNode.RightNode = new NullNode();
                        else
                            skipInitialization = true;
                    }

                    if (!skipInitialization)
                    {
                        if (vardecl.IsStatic)
                            staticPropertyInitList.Add(bNode);
                        else
                            thisClass.DefaultArgExprList.Add(bNode);
                    }
                }
                else if (vardecl.NameNode is BinaryExpressionNode)
                {
                    BinaryExpressionNode bNode = vardecl.NameNode as BinaryExpressionNode;
                    varIdent = bNode.LeftNode as IdentifierNode;

                    bNode.endCol = vardecl.endCol;
                    bNode.endLine = vardecl.endLine;

                    if (vardecl.IsStatic)
                        staticPropertyInitList.Add(bNode);
                    else
                        thisClass.DefaultArgExprList.Add(bNode);
                }
                else
                {
                    Validity.Assert(false, "Check generated AST");
                }

                // It is possible that fail to allocate variable. In that 
                // case we should remove initializing expression from 
                // cnode's defaultArgExprList
                int symbolIndex = AllocateMemberVariable(thisClassIndex, thisClassIndex, varIdent.Value, vardecl.ArgumentType.rank, vardecl.Access, vardecl.IsStatic);
                if (symbolIndex == ProtoCore.DSASM.Constants.kInvalidIndex)
                {
                    Validity.Assert(thisClass.DefaultArgExprList.Count > 0);
                    thisClass.DefaultArgExprList.RemoveAt(thisClass.DefaultArgExprList.Count - 1);
                }
                // Only generate getter/setter for non-ffi class
                else if (!classDecl.IsExternLib)
                {
                    ProtoCore.DSASM.SymbolNode prop =
                        vardecl.IsStatic
                        ? core.CodeBlockList[0].symbolTable.symbolList[symbolIndex]
                        : core.ClassTable.ClassNodes[thisClassIndex].Symbols.symbolList[symbolIndex];
                    string typeName = vardecl.ArgumentType.Name;
                    if (String.IsNullOrEmpty(typeName))
                    {
                        prop.datatype = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.kTypeVar, Constants.kArbitraryRank);
                    }
                    else
                    {
                        int type = core.TypeSystem.GetType(typeName);
                        if (type == (int)PrimitiveType.kInvalidType)
                        {
                            string message = String.Format(ProtoCore.Properties.Resources.kTypeUndefined, typeName);
                            core.BuildStatus.LogWarning(ProtoCore.BuildData.WarningID.kTypeUndefined, message, core.CurrentDSFileName, vardecl.line, vardecl.col, graphNode);
                            prop.datatype = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.kTypeVar, 0);
                        }
                        else
                        {
                            int rank = vardecl.ArgumentType.rank;
                            prop.datatype = core.TypeSystem.BuildTypeObject(type, rank);
                            if (type != (int)PrimitiveType.kTypeVar || prop.datatype.IsIndexable)
                            {
                                prop.staticType = prop.datatype;
                            }
                        }
                    }

                    EmitGetterForProperty(classDecl, prop);
                    EmitSetterForProperty(classDecl, prop);
                }
            }
            classOffset = 0;

            // Now we are going to create a static function __init_static_properties()
            // which will initialize all static properties. We will emit a 
            // call to this function after all classes have been compiled.
            if (staticPropertyInitList.Count > 0 && !classDecl.IsExternLib)
            {
                FunctionDefinitionNode initFunc = new FunctionDefinitionNode
                {
                    Name = ProtoCore.DSASM.Constants.kStaticPropertiesInitializer,
                    Signature = new ArgumentSignatureNode(),
                    Pattern = null,
                    ReturnType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)PrimitiveType.kTypeNull), UID = (int)PrimitiveType.kTypeNull },
                    FunctionBody = new CodeBlockNode(),
                    IsExternLib = false,
                    IsDNI = false,
                    ExternLibName = null,
                    Access = ProtoCore.CompilerDefinitions.AccessModifier.kPublic,
                    IsStatic = true
                };
                classDecl.Procedures.Add(initFunc);

                staticPropertyInitList.ForEach(bNode => initFunc.FunctionBody.Body.Add(bNode));
                initFunc.FunctionBody.Body.Add(AstFactory.BuildReturnStatement(new NullNode()));
            }

            unPopulatedClasses.Remove(thisClassIndex);
        }
Esempio n. 16
0
        private void EmitGetterForProperty(ProtoCore.AST.AssociativeAST.ClassDeclNode cnode, ProtoCore.DSASM.SymbolNode prop)
        {
            FunctionDefinitionNode getter = new FunctionDefinitionNode
            {
                Name = ProtoCore.DSASM.Constants.kGetterPrefix + prop.name,
                Signature = new ArgumentSignatureNode(),
                Pattern = null,
                ReturnType = prop.datatype,
                FunctionBody = new CodeBlockNode(),
                IsExternLib = false,
                IsDNI = false,
                ExternLibName = null,
                Access = prop.access,
                IsStatic = prop.isStatic,
                IsAutoGenerated = true
            };

            var ret = AstFactory.BuildIdentifier(ProtoCore.DSDefinitions.Keyword.Return); 
            var ident =AstFactory.BuildIdentifier(prop.name);
            var retStatement = AstFactory.BuildBinaryExpression(ret, ident, Operator.assign); 
            getter.FunctionBody.Body.Add(retStatement);
            cnode.Procedures.Add(getter);
        }
Esempio n. 17
0
        private List<AssociativeNode> GetASTNodesDependentOnFunctionList(FunctionDefinitionNode functionNode)
        {
            // Determine if the modified function was used in any of the current nodes
            List<AssociativeNode> modifiedNodes = new List<AssociativeNode>();

            // Iterate through the vm graphnodes at the global scope that contain a function call
            //foreach (ProtoCore.AssociativeGraph.GraphNode gnode in runnerCore.DSExecutable.instrStreamList[0].dependencyGraph.GraphList)
            Validity.Assert(null != core.GraphNodeCallList);
            foreach (ProtoCore.AssociativeGraph.GraphNode gnode in core.GraphNodeCallList)
            {
                if (gnode.isActive)
                {
                    // Iterate through the current ast nodes 
                    foreach (KeyValuePair<System.Guid, Subtree> kvp in currentSubTreeList)
                    {
                        foreach (AssociativeNode assocNode in kvp.Value.AstNodes)
                        {
                            if (assocNode is BinaryExpressionNode)
                            {
                                if (gnode.exprUID == (assocNode as BinaryExpressionNode).ExpressionUID)
                                {
                                    // Check if the procedure associatied with this graphnode matches thename and arg count of the modified proc
                                    if (null != gnode.firstProc)
                                    {
                                        if (gnode.firstProc.Name == functionNode.Name
                                            && gnode.firstProc.ArgumentInfos.Count == functionNode.Signature.Arguments.Count)
                                        {
                                            // If it does, create a new ast tree for this graphnode and append it to deltaAstList
                                            modifiedNodes.Add(assocNode);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return modifiedNodes;
        }
Esempio n. 18
0
        private ProtoCore.AST.AssociativeAST.FunctionDefinitionNode ParseFieldAccessor(FieldInfo f)
        {
            if (null == f || SupressesImport(f))
                return null;

            ProtoCore.AST.AssociativeAST.FunctionDefinitionNode func = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
            func.Name = string.Format("%get_{0}", f.Name);
            func.Pattern = null;
            func.Signature = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
            func.ReturnType = CLRModuleType.GetProtoCoreType(f.FieldType, Module);
            func.FunctionBody = null;
            func.access = ProtoCore.Compiler.AccessSpecifier.kPublic;
            func.IsDNI = false;
            func.IsExternLib = true;
            func.ExternLibName = Module.Name;
            func.IsStatic = f.IsStatic;

            return func;
        }
Esempio n. 19
0
        public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes)
        {
            Func<string, string[], object[], object> backingMethod = DSCore.Formula.Evaluate;

            // Format input names to be used as function parameters
            var inputs = InPorts.Select(x => x.PortName.Replace(' ', '_')).ToList();


            /*  def formula_partial(<params>) {
             *    return = DSCore.Formula.Evaluate(<FormulaString>, <InPortData Names>, <params>);
             *  }
             */

            var functionDef = new FunctionDefinitionNode
            {
                Name = "__formula_" + AstIdentifierGuid, 
                Signature =
                    new ArgumentSignatureNode
                    {
                        Arguments = inputs.Select(AstFactory.BuildParamNode).ToList()
                    },
                ReturnType = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.Var),
                FunctionBody =
                    new CodeBlockNode
                    {
                        Body =
                            new List<AssociativeNode>
                                {
                                    AstFactory.BuildReturnStatement(
                                        AstFactory.BuildFunctionCall(
                                            backingMethod,
                                            new List<AssociativeNode>
                                            {
                                                AstFactory.BuildStringNode(FormulaString),
                                                AstFactory.BuildExprList(
                                                    InPorts.Select(
                                                        x =>
                                                            AstFactory.BuildStringNode(x.PortName) as
                                                            AssociativeNode).ToList()),
                                                AstFactory.BuildExprList(
                                                    inputs.Select(AstFactory.BuildIdentifier)
                                                        .Cast<AssociativeNode>()
                                                        .ToList())
                                            }))
                                }
                    }
            };

            if (IsPartiallyApplied)
            {
                return new AssociativeNode[]
                {
                    functionDef,
                    AstFactory.BuildAssignment(
                        GetAstIdentifierForOutputIndex(0),
                        AstFactory.BuildFunctionObject(
                            functionDef.Name,
                            InPorts.Count,
                            Enumerable.Range(0, InPorts.Count).Where(index=>InPorts[index].IsConnected),
                            inputAstNodes))
                };
            }
            else
            {
                UseLevelAndReplicationGuide(inputAstNodes);

                return new AssociativeNode[]
                {
                    functionDef,
                    AstFactory.BuildAssignment(
                        GetAstIdentifierForOutputIndex(0),
                        AstFactory.BuildFunctionCall(
                            functionDef.Name,
                            inputAstNodes))
                };
            }
        }
Esempio n. 20
0
	void Associative_functiondecl(out ProtoCore.AST.AssociativeAST.AssociativeNode node, List<ProtoCore.AST.AssociativeAST.AssociativeNode> attrs = null, ProtoCore.CompilerDefinitions.AccessModifier access = ProtoCore.CompilerDefinitions.AccessModifier.Public, bool isStatic = false) {
		ProtoCore.AST.AssociativeAST.FunctionDefinitionNode f = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); 
		string methodName;  
		ProtoCore.AST.AssociativeAST.AssociativeNode argumentSignature; 
		ProtoCore.Type returnType;  
		ProtoCore.AST.AssociativeAST.AssociativeNode pattern; 
		string externLibName = ""; 
		bool isExternLib = false; 
		bool isDNI = false;                                 
		
		Expect(27);
		NodeUtils.SetNodeLocation(f, t); 
		Associative_MethodSignature(out methodName, out argumentSignature, out pattern, out returnType);
		if (isExternLib &&  "var" == returnType.Name){
		   errors.Warning(String.Format("External function {0} does not have a return type defined. Defaulting to var.", methodName));
		}
		
		
		f.IsExternLib = isExternLib; 
		f.IsDNI = isDNI; 
		f.ExternLibName = externLibName; 
		f.Name = methodName; 
		f.Pattern = pattern; 
		f.ReturnType = returnType; 
		f.Access = access;
		f.Attributes = attrs;
		f.Signature = argumentSignature as ProtoCore.AST.AssociativeAST.ArgumentSignatureNode; 
		f.IsStatic = isStatic;
		ProtoCore.AST.AssociativeAST.AssociativeNode functionBody = null; 
		
		if (la.kind == 23) {
			Get();
		} else if (la.kind == 51) {
			Get();
			Associative_FunctionalMethodBodySingleLine(out functionBody);
		} else if (la.kind == 45) {
			Associative_FunctionalMethodBodyMultiLine(out functionBody);
		} else SynErr(72);
		f.FunctionBody = functionBody as ProtoCore.AST.AssociativeAST.CodeBlockNode; 
		node = f;   
		
	}
Esempio n. 21
0
 private void Initialize(FunctionDefinitionNode functionDefinitionNode)
 {
 }
Esempio n. 22
0
 public GlobalInstanceProc()
 {
     classIndex = ProtoCore.DSASM.Constants.kInvalidIndex;
     procNode = null;
 }
Esempio n. 23
0
 public override AssociativeNode VisitFunctionDefinitionNode(FunctionDefinitionNode node)
 {
     // Not applying renaming to function defintion node, otherwise
     // function defintion would depend on variables that defined in
     // code block node, and there are implicit dependency between
     // code block node that uses this funciton and code block node
     // that defines this function.
     return node;
 }
Esempio n. 24
0
 public ThisPointerProcOverload()
 {
     classIndex = ProtoCore.DSASM.Constants.kInvalidIndex;
     procNode = null;
 }
Esempio n. 25
0
        private void EmitGetterForMemVar(ProtoCore.AST.AssociativeAST.ClassDeclNode cnode, ProtoCore.DSASM.SymbolNode memVar)
        {
            FunctionDefinitionNode getter = new FunctionDefinitionNode()
            {
                Name = ProtoCore.DSASM.Constants.kGetterPrefix + memVar.name,
                Singnature = new ArgumentSignatureNode(),
                Pattern = null,
                ReturnType = core.TypeSystem.BuildTypeObject((int)PrimitiveType.kTypeVar, false),
                FunctionBody = new CodeBlockNode(),
                IsExternLib = false,
                IsDNI = false,
                ExternLibName = null,
                access = memVar.access,
                IsStatic = memVar.isStatic
            };

            getter.FunctionBody.Body.Add(new BinaryExpressionNode()
            {
                // return = property;
                LeftNode = new IdentifierNode()
                {
                    Value = ProtoCore.DSDefinitions.Kw.kw_return,
                    Name = ProtoCore.DSDefinitions.Kw.kw_return,
                    datatype = core.TypeSystem.BuildTypeObject(PrimitiveType.kTypeReturn, false)
                },
                Optr = ProtoCore.DSASM.Operator.assign,
                RightNode = new IdentifierNode()
                {
                    Value = memVar.name,
                    Name = memVar.name,
                    datatype = core.TypeSystem.BuildTypeObject(PrimitiveType.kTypeVar, false)
                }
            });

            cnode.funclist.Add(getter);
        }
Esempio n. 26
0
        private static void InsertDotMemVarMethod(Core core, ProtoCore.AST.Node root)
        {
            ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
            funcDefNode.access = ProtoCore.DSASM.AccessSpecifier.kPublic;
            funcDefNode.Name = ProtoCore.DSASM.Constants.kDotArgMethodName;
            funcDefNode.ReturnType = new ProtoCore.Type() { Name = core.TypeSystem.GetType((int)PrimitiveType.kTypeVar), UID = (int)PrimitiveType.kTypeVar };
            ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
            args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
            {
            memregion = ProtoCore.DSASM.MemoryRegion.kMemStack,
            access = ProtoCore.DSASM.AccessSpecifier.kPublic,
            NameNode = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kLHS),
            ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)PrimitiveType.kTypeVar), UID = (int)PrimitiveType.kTypeVar }
            });
            args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
            {
            memregion = ProtoCore.DSASM.MemoryRegion.kMemStack,
            access = ProtoCore.DSASM.AccessSpecifier.kPublic,
            NameNode = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kRHS),
            ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)PrimitiveType.kTypeInt), UID = (int)PrimitiveType.kTypeInt }
            });
            args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
            {
            memregion = ProtoCore.DSASM.MemoryRegion.kMemStack,
            access = ProtoCore.DSASM.AccessSpecifier.kPublic,
            NameNode = BuildAssocIdentifier(core, "%rhsDimExprList"),
            ArgumentType = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.kTypeInt, 1)
            });
            args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
            {
            memregion = ProtoCore.DSASM.MemoryRegion.kMemStack,
            access = ProtoCore.DSASM.AccessSpecifier.kPublic,
            NameNode = BuildAssocIdentifier(core, "%rhsDim"),
            ArgumentType = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.kTypeInt, 0)
            });
            funcDefNode.Signature = args;

            ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
            ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(core, ProtoCore.DSDefinitions.Keyword.Return, ProtoCore.PrimitiveType.kTypeReturn);

            ProtoCore.AST.AssociativeAST.DotFunctionBodyNode dotNode = new ProtoCore.AST.AssociativeAST.DotFunctionBodyNode(args.Arguments[0].NameNode, args.Arguments[1].NameNode, args.Arguments[2].NameNode, args.Arguments[3].NameNode);
            body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = _return, Optr = ProtoCore.DSASM.Operator.assign, RightNode = dotNode});
            funcDefNode.FunctionBody = body;
            (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode);
        }
Esempio n. 27
0
        private void EmitSetterForMemVar(ProtoCore.AST.AssociativeAST.ClassDeclNode cnode, ProtoCore.DSASM.SymbolNode memVar)
        {
            var argument = new ProtoCore.AST.AssociativeAST.VarDeclNode()
            {
                memregion = ProtoCore.DSASM.MemoryRegion.kMemStack,
                access = ProtoCore.DSASM.AccessSpecifier.kPublic,
                NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode()
                {
                    Value = ProtoCore.DSASM.Constants.kTempArg,
                    Name = ProtoCore.DSASM.Constants.kTempArg,
                    datatype = core.TypeSystem.BuildTypeObject(PrimitiveType.kTypeVar, false)
                },
                ArgumentType = new ProtoCore.Type
                {
                    Name = core.TypeSystem.GetType((int)PrimitiveType.kTypeVar),
                    UID = (int)PrimitiveType.kTypeVar
                }
            };

            var argumentSingature = new ArgumentSignatureNode();
            argumentSingature.AddArgument(argument);

            FunctionDefinitionNode setter = new FunctionDefinitionNode()
            {
                Name = ProtoCore.DSASM.Constants.kSetterPrefix + memVar.name,
                Singnature = argumentSingature,
                Pattern = null,
                ReturnType = core.TypeSystem.BuildTypeObject((int)PrimitiveType.kTypeNull, false),
                FunctionBody = new CodeBlockNode(),
                IsExternLib = false,
                IsDNI = false,
                ExternLibName = null,
                access = memVar.access,
                IsStatic = memVar.isStatic
            };

            // property = %tmpArg
            setter.FunctionBody.Body.Add(new BinaryExpressionNode()
            {
                // return = property;
                LeftNode = new IdentifierNode()
                {
                    Value = memVar.name,
                    Name = memVar.name,
                    datatype = core.TypeSystem.BuildTypeObject(PrimitiveType.kTypeVar, false)
                },
                Optr = ProtoCore.DSASM.Operator.assign,
                RightNode = new IdentifierNode()
                {
                    Value = ProtoCore.DSASM.Constants.kTempArg,
                    Name = ProtoCore.DSASM.Constants.kTempArg,
                    datatype = core.TypeSystem.BuildTypeObject(PrimitiveType.kTypeVar, false)
                }
            });

            // return = null;
            setter.FunctionBody.Body.Add(new BinaryExpressionNode()
            {
                LeftNode = new IdentifierNode()
                {
                    Value = ProtoCore.DSDefinitions.Kw.kw_return,
                    Name = ProtoCore.DSDefinitions.Kw.kw_return,
                    datatype = core.TypeSystem.BuildTypeObject(PrimitiveType.kTypeReturn, false)
                },
                Optr = ProtoCore.DSASM.Operator.assign,
                RightNode = new NullNode()
            });

            cnode.funclist.Add(setter);
        }
Esempio n. 28
0
	void Associative_functiondecl(out ProtoCore.AST.AssociativeAST.AssociativeNode node, ProtoCore.CompilerDefinitions.AccessModifier access = ProtoCore.CompilerDefinitions.AccessModifier.Public, bool isStatic = false) {
		ProtoCore.AST.AssociativeAST.FunctionDefinitionNode f = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); 
		string methodName;  
		ProtoCore.AST.AssociativeAST.AssociativeNode argumentSignature; 
		ProtoCore.Type returnType;  
		string externLibName = ""; 
		bool isExternLib = false; 
		
		Expect(27);
		NodeUtils.SetNodeLocation(f, t); 
		Associative_MethodSignature(out methodName, out argumentSignature, out returnType);
		if (isExternLib &&  "var" == returnType.Name){
		   errors.Warning(String.Format("External function {0} does not have a return type defined. Defaulting to var.", methodName));
		}
		
		
		f.IsExternLib = isExternLib; 
		f.ExternLibName = externLibName; 
		f.Name = methodName; 
		f.ReturnType = returnType; 
		f.Access = access;
		f.Signature = argumentSignature as ProtoCore.AST.AssociativeAST.ArgumentSignatureNode; 
		f.IsStatic = isStatic;
		ProtoCore.AST.AssociativeAST.AssociativeNode functionBody = null; 
		
		Associative_FunctionDefinitionBody(out functionBody);
		f.FunctionBody = functionBody as ProtoCore.AST.AssociativeAST.CodeBlockNode; 
		node = f;   
		
	}
Esempio n. 29
0
        /// <summary>
        /// Sets the function to an inactive state where it can no longer be used by the front-end and backend
        /// </summary>
        /// <param name="functionDef"></param>
        public void SetFunctionInactive(FunctionDefinitionNode functionDef)
        {
            // DS language only supports function definition on the global and first language block scope 
            // TODO Jun: Determine if it is still required to combine function tables in the codeblocks and callsite

            // Update the functiond definition in the codeblocks
            int hash = CoreUtils.GetFunctionHash(functionDef);

            ProcedureNode procNode = null;

            foreach (CodeBlock block in CodeBlockList)
            {
                // Update the current function definition in the current block
                procNode = block.procedureTable.Procedures.FirstOrDefault(p => p.HashID == hash);
                int index = procNode == null ? Constants.kInvalidIndex: procNode.ID; 
                if (Constants.kInvalidIndex == index)
                    continue;

                procNode.IsActive = false;

                // Remove staled graph nodes
                var graph = block.instrStream.dependencyGraph;
                graph.GraphList.RemoveAll(g => g.classIndex == ClassIndex && 
                                               g.procIndex == index);
                graph.RemoveNodesFromScope(Constants.kGlobalScope, index);

                // Make a copy of all symbols defined in this function
                var localSymbols = block.symbolTable.symbolList.Values
                                        .Where(n => 
                                                n.classScope == Constants.kGlobalScope 
                                             && n.functionIndex == index)
                                        .ToList();

                foreach (var symbol in localSymbols)
                {
                    block.symbolTable.UndefineSymbol(symbol);
                }

                break;
            }

            if (null != procNode)
            {
                // Remove codeblock defined in procNode from CodeBlockList and CompleteCodeBlockList
                foreach (int cbID in procNode.ChildCodeBlocks)
                {
                    CompleteCodeBlockList.RemoveAll(x => x.codeBlockId == cbID);

                    foreach (CodeBlock cb in CodeBlockList)
                    {
                        cb.children.RemoveAll(x => x.codeBlockId == cbID);
                    }
                }
            }


            // Update the function definition in global function tables
            foreach (KeyValuePair<int, Dictionary<string, FunctionGroup>> functionGroupList in DSExecutable.FunctionTable.GlobalFuncTable)
            {
                foreach (KeyValuePair<string, FunctionGroup> functionGroup in functionGroupList.Value)
                {
                    functionGroup.Value.FunctionEndPoints.RemoveAll(func => func.procedureNode.HashID == hash);
                }
            }
        }
Esempio n. 30
0
        /// <summary>
        ///     Compiles a collection of Dynamo nodes into a function definition for a custom node.
        /// </summary>
        /// <param name="def"></param>
        /// <param name="funcBody"></param>
        /// <param name="outputs"></param>
        /// <param name="parameters"></param>
        public void CompileCustomNodeDefinition(
            CustomNodeDefinition def, IEnumerable<NodeModel> funcBody, List<AssociativeNode> outputs,
            IEnumerable<string> parameters)
        {
            OnAstNodeBuilding(def.FunctionId);

            var functionBody = new CodeBlockNode();
            functionBody.Body.AddRange(CompileToAstNodes(funcBody, false));

            if (outputs.Count > 1)
            {
                /* rtn_array = {};
                 * rtn_array[key0] = out0;
                 * rtn_array[key1] = out1;
                 * ...
                 * return = rtn_array;
                 */

                // return array, holds all outputs
                string rtnName = "__temp_rtn_" + def.FunctionId.ToString().Replace("-", String.Empty); 
                functionBody.Body.Add(
                    AstFactory.BuildAssignment(
                        AstFactory.BuildIdentifier(rtnName),
                        AstFactory.BuildExprList(new List<string>())));

                // indexers for each output
                IEnumerable<AssociativeNode> indexers = def.ReturnKeys != null
                    ? def.ReturnKeys.Select(AstFactory.BuildStringNode) as IEnumerable<AssociativeNode>
                    : Enumerable.Range(0, outputs.Count).Select(AstFactory.BuildIntNode);

                functionBody.Body.AddRange(
                    outputs.Zip(
                        indexers,
                        (outputId, indexer) => // for each outputId and return key
                            // pack the output into the return array
                            AstFactory.BuildAssignment(AstFactory.BuildIdentifier(rtnName, indexer), outputId)));

                // finally, return the return array
                functionBody.Body.Add(AstFactory.BuildReturnStatement(AstFactory.BuildIdentifier(rtnName)));
            }
            else
            {
                // For single output, directly return that identifier or null.
                AssociativeNode returnValue = outputs.Count == 1 ? outputs[0] : new NullNode();
                functionBody.Body.Add(AstFactory.BuildReturnStatement(returnValue));
            }

            Type allTypes = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.kTypeVar);

            //Create a new function definition
            var functionDef = new FunctionDefinitionNode
            {
                Name = def.FunctionName.Replace("-", string.Empty),
                Signature =
                    new ArgumentSignatureNode
                    {
                        Arguments =
                            parameters.Select(param => AstFactory.BuildParamNode(param, allTypes)).ToList()
                    },
                FunctionBody = functionBody,
                ReturnType = allTypes
            };

            OnAstNodeBuilt(def.FunctionId, new[] { functionDef });
        }
Esempio n. 31
0
        private ProtoCore.AST.AssociativeAST.AssociativeNode ParseMethod(MethodInfo method)
        {
            ProtoCore.Type retype = CLRModuleType.GetProtoCoreType(method.ReturnType, Module);
            bool propaccessor = isPropertyAccessor(method);
            bool isOperator = isOverloadedOperator(method);

            FFIMethodAttributes mattrs = new FFIMethodAttributes(method);
            if (method.IsStatic &&
                method.DeclaringType == method.ReturnType && 
                !propaccessor &&
                !isOperator)
            {
                //case for named constructor. Must return a pointer type
                if (!Object.Equals(method.ReturnType, CLRType))
                    throw new InvalidOperationException("Unexpected type for constructor {0D28FC00-F8F4-4049-AD1F-BBC34A68073F}");

                retype = ProtoCoreType;
                ConstructorDefinitionNode node = ParsedNamedConstructor(method, method.Name, retype);
                node.MethodAttributes = mattrs;
                return node;
            }

            //Need to hide property accessor from design script users, prefix with %
            string prefix = (isOperator || propaccessor) ? "%" : "";
            var func = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();

            if (isOperator)
            {
                func.Name = string.Format("{0}{1}", prefix, GetDSOperatorName(method.Name));
            }
            else
            {
                func.Name = string.Format("{0}{1}", prefix, method.Name);
            }
            func.Pattern = null;
            func.Signature = ParseArgumentSignature(method);

            if ((retype.IsIndexable && mattrs.AllowRankReduction) 
                || (typeof(object).Equals(method.ReturnType)))
            {
                retype.rank = Constants.kArbitraryRank;
            }
            func.ReturnType = retype;
            func.FunctionBody = null;
            func.access = ProtoCore.Compiler.AccessSpecifier.kPublic;
            func.IsDNI = false;
            func.IsExternLib = true;
            func.ExternLibName = Module.Name;
            func.IsStatic = method.IsStatic;
            func.MethodAttributes = mattrs;

            return func;
        }
Esempio n. 32
0
        // The following methods are used to insert methods to the bottom of the AST and convert operator to these method calls 
        // to support replication on operators 
        private static void InsertUnaryOperationMethod(Core core, CodeBlockNode root, UnaryOperator op, PrimitiveType r, PrimitiveType operand)
        {
            FunctionDefinitionNode funcDefNode = new FunctionDefinitionNode();
            funcDefNode.access = CompilerDefinitions.AccessModifier.kPublic;
            funcDefNode.IsAssocOperator = true;
            funcDefNode.IsBuiltIn = true;
            funcDefNode.Name = Op.GetUnaryOpFunction(op);
            funcDefNode.ReturnType = new Type() { Name = core.TypeSystem.GetType((int)r), UID = (int)r };
            ArgumentSignatureNode args = new ArgumentSignatureNode();
            args.AddArgument(new VarDeclNode()
            {
                Access = CompilerDefinitions.AccessModifier.kPublic,
                NameNode = AstFactory.BuildIdentifier("%param"),
                ArgumentType = new Type { Name = core.TypeSystem.GetType((int)operand), UID = (int)operand }
            });
            funcDefNode.Signature = args;

            CodeBlockNode body = new CodeBlockNode();
            IdentifierNode param = AstFactory.BuildIdentifier("%param");
            body.Body.Add(AstFactory.BuildReturnStatement(new UnaryExpressionNode() { Expression = param, Operator = op }));
            funcDefNode.FunctionBody = body;
            root.Body.Add(funcDefNode);
        }
Esempio n. 33
0
 public ThisPointerProcOverload()
 {
     classIndex = Constants.kInvalidIndex;
     procNode = null;
 }
Esempio n. 34
0
        private void EmitClassDeclNode(AssociativeNode node, ref ProtoCore.Type inferedType, ProtoCore.CompilerDefinitions.SubCompilePass subPass = ProtoCore.CompilerDefinitions.SubCompilePass.None,
            GraphNode graphNode = null)
        {
            ClassDeclNode classDecl = node as ClassDeclNode;

            // Handling n-pass on class declaration
            if (ProtoCore.CompilerDefinitions.CompilePass.ClassName == compilePass)
            {
                // Class name pass
                // Populating the class tables with the class names
                if (null != codeBlock.parent)
                {
                    buildStatus.LogSemanticError(Resources.ClassCannotBeDefinedInsideLanguageBlock, core.CurrentDSFileName, classDecl.line, classDecl.col);
                }


                ProtoCore.DSASM.ClassNode thisClass = new ProtoCore.DSASM.ClassNode();
                thisClass.Name = classDecl.ClassName;
                thisClass.Size = classDecl.Variables.Count;
                thisClass.IsImportedClass = classDecl.IsImportedClass;
                thisClass.TypeSystem = core.TypeSystem;
                thisClass.ClassAttributes = classDecl.ClassAttributes;
                
                if (classDecl.ExternLibName != null)
                    thisClass.ExternLib = classDecl.ExternLibName;
                else
                    thisClass.ExternLib = Path.GetFileName(core.CurrentDSFileName);

                globalClassIndex = core.ClassTable.Append(thisClass);
                if (ProtoCore.DSASM.Constants.kInvalidIndex == globalClassIndex)
                {
                    string message = string.Format("Class redefinition '{0}' (BE1C3285).\n", classDecl.ClassName);
                    buildStatus.LogSemanticError(message, core.CurrentDSFileName, classDecl.line, classDecl.col);
                    throw new BuildHaltException(message);
                }

                unPopulatedClasses.Add(globalClassIndex, classDecl);

                //Always allow us to convert a class to a bool
                thisClass.CoerceTypes.Add((int)PrimitiveType.Bool, (int)ProtoCore.DSASM.ProcedureDistance.CoerceScore);
            }
            else if (ProtoCore.CompilerDefinitions.CompilePass.ClassBaseClass == compilePass)
            { 
                // Base class pass
                // Populating each class entry with their immediate baseclass
                globalClassIndex = core.ClassTable.GetClassId(classDecl.ClassName);

                ProtoCore.DSASM.ClassNode thisClass = core.ClassTable.ClassNodes[globalClassIndex];

                // Verify and store the list of classes it inherits from 
                if (!string.IsNullOrEmpty(classDecl.BaseClass))
                {
                    int baseClass = core.ClassTable.GetClassId(classDecl.BaseClass);
                    if (baseClass == globalClassIndex)
                    {
                        string message = string.Format("Class '{0}' cannot derive from itself (DED0A61F).\n", classDecl.ClassName);
                        buildStatus.LogSemanticError(message, core.CurrentDSFileName, classDecl.line, classDecl.col);
                        throw new BuildHaltException(message);
                    }

                    if (ProtoCore.DSASM.Constants.kInvalidIndex != baseClass)
                    {
                        if (core.ClassTable.ClassNodes[baseClass].IsImportedClass && !thisClass.IsImportedClass)
                        {
                            string message = string.Format("Cannot derive from FFI class {0} (DA87AC4D).\n",
                                core.ClassTable.ClassNodes[baseClass].Name);

                            buildStatus.LogSemanticError(message, core.CurrentDSFileName, classDecl.line, classDecl.col);
                            throw new BuildHaltException(message);
                        }

                        thisClass.Base = baseClass;
                        thisClass.CoerceTypes.Add(baseClass, (int)ProtoCore.DSASM.ProcedureDistance.CoerceBaseClass);
                    }
                    else
                    {
                        string message = string.Format("Unknown base class '{0}' (9E44FFB3).\n", classDecl.BaseClass);
                        buildStatus.LogSemanticError(message, core.CurrentDSFileName, classDecl.line, classDecl.col);
                        throw new BuildHaltException(message);
                    }
                }
            }
            else if (ProtoCore.CompilerDefinitions.CompilePass.ClassHierarchy == compilePass)
            {
                // Class hierarchy pass
                // Populating each class entry with all sub classes in the hierarchy
                globalClassIndex = core.ClassTable.GetClassId(classDecl.ClassName);

                ProtoCore.DSASM.ClassNode thisClass = core.ClassTable.ClassNodes[globalClassIndex];

                // Verify and store the list of classes it inherits from 
                if (!string.IsNullOrEmpty(classDecl.BaseClass))
                {
                    int baseClass = core.ClassTable.GetClassId(classDecl.BaseClass);

                    // Iterate through all the base classes until the the root class
                    // For every base class, add the coercion score
                    ProtoCore.DSASM.ClassNode tmpCNode = core.ClassTable.ClassNodes[baseClass];
                    if (tmpCNode.Base != Constants.kInvalidIndex)
                    {
                        baseClass = tmpCNode.Base;
                        while (ProtoCore.DSASM.Constants.kInvalidIndex != baseClass)
                        {
                            thisClass.CoerceTypes.Add(baseClass, (int)ProtoCore.DSASM.ProcedureDistance.CoerceBaseClass);
                            tmpCNode = core.ClassTable.ClassNodes[baseClass];

                            baseClass = ProtoCore.DSASM.Constants.kInvalidIndex;
                            if (tmpCNode.Base != Constants.kInvalidIndex)
                            {
                                baseClass = tmpCNode.Base;
                            }
                        }
                    }
                }
            }
            else if (ProtoCore.CompilerDefinitions.CompilePass.ClassMemVar == compilePass)
            {
                EmitMemberVariables(classDecl, graphNode);
            }
            else if (ProtoCore.CompilerDefinitions.CompilePass.ClassMemFuncSig == compilePass)
            {
                // Class member variable pass
                // Populating each class entry vtables with their respective
                // member variables signatures
                globalClassIndex = core.ClassTable.GetClassId(classDecl.ClassName);

                List<AssociativeNode> thisPtrOverloadList = new List<AssociativeNode>();
                foreach (AssociativeNode funcdecl in classDecl.Procedures)
                {
                    DfsTraverse(funcdecl, ref inferedType);

                    var funcDef = funcdecl as FunctionDefinitionNode;
                    if (funcDef == null || funcDef.IsStatic || funcDef.Name == ProtoCore.DSDefinitions.Keyword.Dispose)
                        continue;

                    bool isGetterSetter = CoreUtils.IsGetterSetter(funcDef.Name);

                    var thisPtrArgName = Constants.kThisPointerArgName;
                    if (!isGetterSetter)
                    {
                        var classsShortName = classDecl.ClassName.Split('.').Last();
                        var typeDepName = classsShortName.ToLower();
                        if (typeDepName != classsShortName && funcDef.Signature.Arguments.All(a => a.NameNode.Name != typeDepName))
                            thisPtrArgName = typeDepName;
                    }

                    // This is a function, create its parameterized this pointer overload
                    var procNode = new FunctionDefinitionNode(funcDef);
                    var thisPtrArg = new VarDeclNode()
                    {
                        Access = ProtoCore.CompilerDefinitions.AccessModifier.Public,
                        NameNode = AstFactory.BuildIdentifier(thisPtrArgName),
                        ArgumentType = new ProtoCore.Type { Name = classDecl.ClassName, UID = globalClassIndex, rank = 0 }
                    };
                    procNode.Signature.Arguments.Insert(0, thisPtrArg);
                    procNode.IsAutoGeneratedThisProc = true;

                    if (CoreUtils.IsGetterSetter(funcDef.Name))
                    {
                        for (int n = 0; n < procNode.FunctionBody.Body.Count; ++n)
                        {
                            if (procNode.FunctionBody.Body[n] is BinaryExpressionNode)
                            {
                                var assocNode = procNode.FunctionBody.Body[n] as BinaryExpressionNode;
                                AssociativeNode outLNode = null;
                                AssociativeNode outRNode = null;

                                // This is the first traversal of this statement and is therefore the assignment op
                                Validity.Assert(Operator.assign == assocNode.Optr);
                                TraverseAndAppendThisPtrArg(assocNode.LeftNode, ref outLNode);
                                TraverseAndAppendThisPtrArg(assocNode.RightNode, ref outRNode);

                                assocNode.LeftNode = outLNode;
                                assocNode.RightNode = outRNode;
                            }
                        }
                    }
                    else
                    {
                        // Generate a static function for member function f():
                        //     satic def f(a: A)
                        //     { 
                        //        return = a.f()
                        //     }
                        procNode.IsStatic = true;
                        var args = procNode.Signature.Arguments.Select(a => a.NameNode).ToList();
                        var fcall = AstFactory.BuildFunctionCall(procNode.Name, args) as FunctionCallNode;

                        // Build the dotcall node
                        var lhs = procNode.Signature.Arguments[0].NameNode;
                        var right = CoreUtils.GenerateCallDotNode(lhs, fcall, core);
                        var body = AstFactory.BuildReturnStatement(right);
                        procNode.FunctionBody.Body = new List<AssociativeNode> { body };
                    }

                    thisPtrOverloadList.Add(procNode);
                }

                foreach (var overloadFunc in thisPtrOverloadList)
                {
                    // Emit the newly defined overloads
                    DfsTraverse(overloadFunc, ref inferedType);
                }

                classDecl.Procedures.AddRange(thisPtrOverloadList);

                if (!classDecl.IsExternLib)
                {
                    ProtoCore.DSASM.ProcedureTable vtable = core.ClassTable.ClassNodes[globalClassIndex].ProcTable;
                    if (vtable.GetFunctionBySignature(classDecl.ClassName, new List<ProtoCore.Type>()) == null)
                    {
                        ConstructorDefinitionNode defaultConstructor = new ConstructorDefinitionNode();
                        defaultConstructor.Name = classDecl.ClassName;
                        defaultConstructor.LocalVariableCount = 0;
                        defaultConstructor.Signature = new ArgumentSignatureNode();
                        defaultConstructor.ReturnType = new ProtoCore.Type { Name = "var", UID = 0 };
                        defaultConstructor.FunctionBody = new CodeBlockNode();
                        defaultConstructor.BaseConstructor = null;
                        defaultConstructor.Access = ProtoCore.CompilerDefinitions.AccessModifier.Public;
                        defaultConstructor.IsExternLib = false;
                        defaultConstructor.ExternLibName = null;
                        DfsTraverse(defaultConstructor, ref inferedType);
                        classDecl.Procedures.Add(defaultConstructor);
                    }
                }
            }
            else if (ProtoCore.CompilerDefinitions.CompilePass.GlobalScope == compilePass)
            {
                // before populate the attributes, we must know the attribute class constructor signatures
                // in order to check the parameter 
                // populate the attributes for the class and class member variable 
                globalClassIndex = core.ClassTable.GetClassId(classDecl.ClassName);
                if (globalClassIndex != ProtoCore.DSASM.Constants.kInvalidIndex)
                {
                    ProtoCore.DSASM.ClassNode thisClass = core.ClassTable.ClassNodes[globalClassIndex];
                    // class
                    if (classDecl.Attributes != null)
                    {
                        thisClass.Attributes = PopulateAttributes(classDecl.Attributes);
                    }

                    // member variable
                    int ix = -1;
                    int currentClassScope = -1;
                    foreach (ProtoCore.DSASM.SymbolNode sn in thisClass.Symbols.symbolList.Values)
                    {
                        // only populate the attributes for member variabls
                        if (sn.functionIndex != ProtoCore.DSASM.Constants.kInvalidIndex)
                            continue;
                        if (sn.classScope != globalClassIndex)
                        {
                            if (currentClassScope != sn.classScope)
                            {
                                currentClassScope = sn.classScope;
                                ix = 0;
                            }
                        }
                        else
                        {
                            if (currentClassScope != globalClassIndex)
                            {
                                currentClassScope = globalClassIndex;
                                ix = 0;
                            }
                        }
                    }
                }
            }
            else if (ProtoCore.CompilerDefinitions.CompilePass.ClassMemFuncBody == compilePass)
            {
                // Class member variable pass
                // Populating the function body of each member function defined in the class vtables

                globalClassIndex = core.ClassTable.GetClassId(classDecl.ClassName);

                // Initialize the global function table for this class
                // 'classIndexAtCallsite' is the class index as it is stored at the callsite function tables
                int classIndexAtCallsite = globalClassIndex + 1;
                core.FunctionTable.InitGlobalFunctionEntry(classIndexAtCallsite);

                foreach (AssociativeNode funcdecl in classDecl.Procedures)
                {
                    // reset the inferedtype between functions
                    inferedType = new ProtoCore.Type();
                    DfsTraverse(funcdecl, ref inferedType, false, null, subPass);
                }
            }

            // Reset the class index
            core.ClassIndex = globalClassIndex = ProtoCore.DSASM.Constants.kGlobalScope;
        }
Esempio n. 35
0
        private FunctionDefinitionNode EmitSetterFunction(ProtoCore.DSASM.SymbolNode prop, ProtoCore.Type argType)
        {
            var argument = new ProtoCore.AST.AssociativeAST.VarDeclNode()
            {
                Access = ProtoCore.CompilerDefinitions.AccessModifier.kPublic,
                NameNode =AstFactory.BuildIdentifier(Constants.kTempArg),
                ArgumentType = argType
            };
            var argumentSingature = new ArgumentSignatureNode();
            argumentSingature.AddArgument(argument);

            FunctionDefinitionNode setter = new FunctionDefinitionNode
            {
                Name = ProtoCore.DSASM.Constants.kSetterPrefix + prop.name,
                Signature = argumentSingature,
                Pattern = null,
                ReturnType = TypeSystem.BuildPrimitiveTypeObject(PrimitiveType.kTypeNull, 0),
                FunctionBody = new CodeBlockNode(),
                IsExternLib = false,
                IsDNI = false,
                ExternLibName = null,
                Access = prop.access,
                IsStatic = prop.isStatic,
                IsAutoGenerated = true
            };

            // property = %tmpArg
            var propIdent = new TypedIdentifierNode();
            propIdent.Name = propIdent.Value = prop.name;
            propIdent.datatype = prop.datatype;

            var tmpArg =AstFactory.BuildIdentifier(Constants.kTempArg);
            var assignment = AstFactory.BuildBinaryExpression(propIdent, tmpArg, Operator.assign);
            setter.FunctionBody.Body.Add(assignment);

            // return = null;
            var returnNull = AstFactory.BuildReturnStatement(new NullNode()); 
            setter.FunctionBody.Body.Add(returnNull);

            return setter;
        }
Esempio n. 36
0
	// The following methods are used to insert methods to the bottom of the AST and convert operator to these method calls 
	// to support replication on operators 
	private static void InsertUnaryOperationMethod(Core core, ProtoCore.AST.Node root, UnaryOperator op, PrimitiveType r, PrimitiveType operand)
    {
        ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
        funcDefNode.access = ProtoCore.CompilerDefinitions.AccessModifier.kPublic;
        funcDefNode.IsAssocOperator = true;
        funcDefNode.IsBuiltIn = true;
        funcDefNode.Name = Op.GetUnaryOpFunction(op);
        funcDefNode.ReturnType = new ProtoCore.Type() { Name = core.TypeSystem.GetType((int)r), UID = (int)r };
        ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
        args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
        {
            memregion = ProtoCore.DSASM.MemoryRegion.kMemStack,
            access = ProtoCore.CompilerDefinitions.AccessModifier.kPublic,
            NameNode = BuildAssocIdentifier(core, "%param"),
            ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)operand), UID = (int)operand }
        });
        funcDefNode.Signature = args;

        ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
        ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(core, ProtoCore.DSDefinitions.Keyword.Return, ProtoCore.PrimitiveType.kTypeReturn);
        ProtoCore.AST.AssociativeAST.IdentifierNode param = BuildAssocIdentifier(core, "%param");
        body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = _return, Optr = ProtoCore.DSASM.Operator.assign, RightNode = new ProtoCore.AST.AssociativeAST.UnaryExpressionNode() { Expression = param, Operator = op } });
        funcDefNode.FunctionBody = body;
        (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode);
    }
Esempio n. 37
0
	private static void InsertInlineConditionOperationMethod(Core core, ProtoCore.AST.Node root, PrimitiveType condition, PrimitiveType r)
    {
        ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
        funcDefNode.access = ProtoCore.DSASM.AccessSpecifier.kPublic;
        funcDefNode.Name = ProtoCore.DSASM.Constants.kInlineCondition; 
        funcDefNode.ReturnType = new ProtoCore.Type() { Name = core.TypeSystem.GetType((int)r), UID = (int)r };
        ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
        args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
        {
            memregion = ProtoCore.DSASM.MemoryRegion.kMemStack,
            access = ProtoCore.DSASM.AccessSpecifier.kPublic,
            NameNode = BuildAssocIdentifier(core, "%condition"),
            ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)condition), UID = (int)condition }
        });
        args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
        {
            memregion = ProtoCore.DSASM.MemoryRegion.kMemStack,
            access = ProtoCore.DSASM.AccessSpecifier.kPublic,
            NameNode = BuildAssocIdentifier(core, "%trueExp"),
            ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)r), UID = (int)r }
        });
        args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode()
        {
            memregion = ProtoCore.DSASM.MemoryRegion.kMemStack,
            access = ProtoCore.DSASM.AccessSpecifier.kPublic,
            NameNode = BuildAssocIdentifier(core, "%falseExp"),
            ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)r), UID = (int)r }
        });
        funcDefNode.Signature = args;

        ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
        ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(core, ProtoCore.DSDefinitions.Keyword.Return, ProtoCore.PrimitiveType.kTypeReturn);
        ProtoCore.AST.AssociativeAST.IdentifierNode con = BuildAssocIdentifier(core, "%condition");
        ProtoCore.AST.AssociativeAST.IdentifierNode t = BuildAssocIdentifier(core, "%trueExp");
        ProtoCore.AST.AssociativeAST.IdentifierNode f = BuildAssocIdentifier(core, "%falseExp");

        body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = _return, Optr = Operator.assign, RightNode = new ProtoCore.AST.AssociativeAST.InlineConditionalNode() { ConditionExpression = con, TrueExpression = t, FalseExpression = f } });
        funcDefNode.FunctionBody = body;
        (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode);
    }
Esempio n. 38
0
        public FunctionDefinitionNode(FunctionDefinitionNode rhs)
        {
            this.Name = rhs.Name;
            if (null != rhs.FunctionBody)
            {
                this.FunctionBody = new CodeBlockNode(rhs.FunctionBody);
            }
            else
            {
                this.FunctionBody = new CodeBlockNode();
            }

            this.ReturnType = rhs.ReturnType;

            this.Attributes = rhs.Attributes;
            this.Singnature = new ArgumentSignatureNode(rhs.Singnature);
            this.Pattern = rhs.Pattern;
            this.IsExternLib = rhs.IsExternLib;
            this.BuiltInMethodId = rhs.BuiltInMethodId;
            this.IsDNI = rhs.IsDNI;
            this.ExternLibName = rhs.ExternLibName;
            this.access = rhs.access;
            this.IsStatic = rhs.IsStatic;
            this.IsAutoGenerated = rhs.IsAutoGenerated;
            this.IsAssocOperator = rhs.IsAssocOperator;
            this.IsAutoGeneratedThisProc = IsAutoGeneratedThisProc;
            this.IsBuiltIn = rhs.IsBuiltIn;
        }
Esempio n. 39
0
 public void DFSTraverse(ref AST.AssociativeAST.AssociativeNode node)
 {
     if (node is AST.AssociativeAST.IdentifierNode)
     {
         EmitIdentifierNode(ref node);
     }
     else if (node is ProtoCore.AST.AssociativeAST.IdentifierListNode)
     {
         AST.AssociativeAST.IdentifierListNode identList = node as ProtoCore.AST.AssociativeAST.IdentifierListNode;
         EmitIdentifierListNode(ref identList);
     }
     else if (node is ProtoCore.AST.AssociativeAST.IntNode)
     {
         AST.AssociativeAST.IntNode intNode = node as ProtoCore.AST.AssociativeAST.IntNode;
         EmitIntNode(ref intNode);
     }
     else if (node is ProtoCore.AST.AssociativeAST.DoubleNode)
     {
         AST.AssociativeAST.DoubleNode doubleNode = node as ProtoCore.AST.AssociativeAST.DoubleNode;
         EmitDoubleNode(ref doubleNode);
     }
     else if (node is ProtoCore.AST.AssociativeAST.FunctionCallNode)
     {
         AST.AssociativeAST.FunctionCallNode funcCallNode = node as ProtoCore.AST.AssociativeAST.FunctionCallNode;
         EmitFunctionCallNode(ref funcCallNode);
     }
     else if (node is ProtoCore.AST.AssociativeAST.FunctionDotCallNode)
     {
         AST.AssociativeAST.FunctionDotCallNode funcDotCall = node as ProtoCore.AST.AssociativeAST.FunctionDotCallNode;
         EmitFunctionDotCallNode(ref funcDotCall);
     }
     else if (node is ProtoCore.AST.AssociativeAST.BinaryExpressionNode)
     {
         ProtoCore.AST.AssociativeAST.BinaryExpressionNode binaryExpr = node as ProtoCore.AST.AssociativeAST.BinaryExpressionNode;
         if (binaryExpr.Optr != DSASM.Operator.assign)
         {
             ;
         }
         //EmitCode("(");
         EmitBinaryNode(ref binaryExpr);
         if (binaryExpr.Optr == DSASM.Operator.assign)
         {
             //EmitCode(ProtoCore.DSASM.Constants.termline);
         }
         if (binaryExpr.Optr != DSASM.Operator.assign)
         {
             ;
         }
         //EmitCode(")");
     }
     else if (node is ProtoCore.AST.AssociativeAST.FunctionDefinitionNode)
     {
         AST.AssociativeAST.FunctionDefinitionNode funcDefNode = node as ProtoCore.AST.AssociativeAST.FunctionDefinitionNode;
         EmitFunctionDefNode(ref funcDefNode);
     }
     else if (node is ProtoCore.AST.AssociativeAST.ClassDeclNode)
     {
         AST.AssociativeAST.ClassDeclNode classDeclNode = node as ProtoCore.AST.AssociativeAST.ClassDeclNode;
         EmitClassDeclNode(ref classDeclNode);
     }
     else if (node is ProtoCore.AST.AssociativeAST.NullNode)
     {
         AST.AssociativeAST.NullNode nullNode = node as ProtoCore.AST.AssociativeAST.NullNode;
         EmitNullNode(ref nullNode);
     }
     else if (node is ProtoCore.AST.AssociativeAST.ArrayIndexerNode)
     {
         AST.AssociativeAST.ArrayIndexerNode arrIdxNode = node as ProtoCore.AST.AssociativeAST.ArrayIndexerNode;
         EmitArrayIndexerNode(ref arrIdxNode);
     }
     else if (node is ProtoCore.AST.AssociativeAST.ExprListNode)
     {
         AST.AssociativeAST.ExprListNode exprListNode = node as ProtoCore.AST.AssociativeAST.ExprListNode;
         EmitExprListNode(ref exprListNode);
     }
 }
Esempio n. 40
0
 /// <summary>
 /// Gets the has id of a function signature given the name and argument types
 /// </summary>
 /// <param name="functionDef"></param>
 /// <returns></returns>
 public static int GetFunctionHash(FunctionDefinitionNode functionDef)
 {
     Validity.Assert(null != functionDef);
     string functionDescription = functionDef.Name;
     foreach (VarDeclNode argNode in functionDef.Signature.Arguments)
     {
         functionDescription += argNode.ArgumentType.ToString();
     }
     return functionDescription.GetHashCode();
 }