Exemple #1
0
        public bool Initialize()
        {
            if (Type.Children.Length > 1)
            {
                Parameters = new List <ParamVariable>();
                for (var i = 1; i < Type.Children.Length; i++)
                {
                    var e     = Type.Children[i] as FunctionParameter;
                    var Param = new ParamVariable(this, e.Name, e.TypeOfSelf);
                    Param.LocalIndex = LocalIdentifiers.Count;
                    LocalIdentifiers.Add(Param);
                    Parameters.Add(Param);
                }
            }

            var StructuredParent = Parent.RealContainer as StructuredScope;

            if (StructuredParent != null && (Function.Flags & IdentifierFlags.Static) == 0)
            {
                var StructuredType = StructuredParent.StructuredType;
                SelfVariable = new SelfVariable(this, StructuredType);
                LocalIdentifiers.Add(SelfVariable);

                var ClassType = StructuredType as ClassType;
                if (ClassType != null && ClassType.BaseStructures.Length == 1)
                {
                    BaseVariable = new BaseVariable(this, ClassType.BaseStructures[0].Base);
                    LocalIdentifiers.Add(BaseVariable);
                }
            }

            return(true);
        }
Exemple #2
0
 public void AddRange(AutoAllocatedList <T> List)
 {
     if (List.List != null)
     {
         Allocate();
         this.List.AddRange(List.List);
     }
 }
Exemple #3
0
        void WriteIdentifier(AutoAllocatedList <Identifier> List, Identifier Parent)
        {
            if (List.List == null)
            {
                WriteLEB128(0);
                return;
            }

            WriteIdentifier(List.List, Parent);
        }
Exemple #4
0
        public AutoAllocatedList <T2> Change <T2>() where T2 : class
        {
            var Ret = new AutoAllocatedList <T2>();

            for (var i = 0; i < Count; i++)
            {
                Ret.Add(this[i] as T2);
            }

            return(Ret);
        }
Exemple #5
0
        void GetExternPublicCode(StringBuilder Out, List <string> AlreadyExported, AutoAllocatedList <Identifier> List)
        {
            for (var i = 0; i < List.Count; i++)
            {
                var Id = List[i];
                if (State.Builder != null && State.Builder.Format == AssemblyFormat.Application)
                {
                    if (Id.AssemblyName == "_Internals_Reflection_Reflection_EntryAssembly")
                    {
                        continue;
                    }
                }

                if (Id.HasScopes && !(Id is IdentifierAlias))
                {
                    foreach (var Scope in Id.EnumScopes)
                    {
                        GetExternPublicCode(Out, AlreadyExported, Scope.IdentifierList);
                    }
                }

                if (!HasReferencableAssemblyName(Id))
                {
                    continue;
                }

                var DeclaredInOutputAssembly = Id.Container.IsScopeOfAssembly();
                if (!DeclaredInOutputAssembly || (Id.Flags & IdentifierFlags.Extern) != 0)
                {
                    if ((DeclaredInOutputAssembly && (Id.Flags & IdentifierFlags.Extern) != 0) ||
                        (!DeclaredInOutputAssembly && Id.Used))
                    {
                        var Name = Id.AssemblyName;
                        if (!AlreadyExported.Contains(Name))
                        {
                            Out.Append("\textrn " + Name + "\n");
                        }

                        if ((Id.Flags & IdentifierFlags.SpecialName) != 0)
                        {
                            AlreadyExported.Add(Name);
                        }
                    }
                }
                else
                {
                    if (Id.Access != IdentifierAccess.Internal && Id.Access != IdentifierAccess.Private)
                    {
                        Out.Append("\tpublic " + Id.AssemblyName + "\n");
                    }
                }
            }
        }
Exemple #6
0
        public override PluginResult End(ref ExpressionNode Node)
        {
            SetIdNodeUsed(Node);
            Node = Extract(Node);
            if (Node == null)
            {
                return(PluginResult.Failed);
            }

            if (!CheckUsedIdNodes(Node))
            {
                return(PluginResult.Failed);
            }

            var RetValue = true;

            for (var i = 0; i < DeclaredIds.Count; i++)
            {
                var e = DeclaredIds[i];
                if (MustGetValue && !AllPathGetsValue(e, Node))
                {
                    State.Messages.Add(MessageId.UnassignedVar, e.Name);
                    RetValue = false;
                    continue;
                }

                e.Container.DeclareIdentifier(e);
            }

            DeclaredIds = null;
            if (!RetValue)
            {
                return(PluginResult.Failed);
            }

            var OldVars = Node.Data.Get <NodeVariables>();

            if (OldVars == null)
            {
                Node.Data.Set(Vars);
            }
            else
            {
                OldVars.UnionInPlace(Vars);
            }

            return(PluginResult.Succeeded);
        }
Exemple #7
0
        public ExpressionNode DetachChild(int Index)
        {
            var Ret         = Children[Index];
            var LinkedNodes = new AutoAllocatedList <LinkedExprNode>();

            for (var i = 0; i < this.LinkedNodes.Count; i++)
            {
                var LNode = this.LinkedNodes[i];
                if ((LNode.Flags & LinkedNodeFlags.PostComputation) != 0)
                {
                    throw new NotImplementedException();
                }

                if ((LNode.Flags & LinkedNodeFlags.NotRemovable) != 0)
                {
                    LinkedNodes.Add(LNode);
                }
                else
                {
                    var LinkingCount = Children[Index].GetLinkingCount(LNode);
                    if (LinkingCount > 0)
                    {
                        LNode.LinkingCount = LinkingCount;
                        LinkedNodes.Add(LNode);
                    }
                }
            }

            for (var i = 0; i < Index; i++)
            {
                Children[i].GetLinkedNodes(ref LinkedNodes, true);
            }

            if (Children[Index].LinkedNodes.List != null)
            {
                LinkedNodes.AddRange(Children[Index].LinkedNodes.List);
            }

            for (var i = Index + 1; i < Children.Length; i++)
            {
                Children[i].GetLinkedNodes(ref LinkedNodes, true);
            }

            Ret.LinkedNodes = LinkedNodes;
            return(Ret);
        }
Exemple #8
0
        public void GetLinkedNodes(ref AutoAllocatedList <LinkedExprNode> Out, bool OnlyNotRemovable = false)
        {
            if (LinkedNodes.List != null)
            {
                Out.AddRange(LinkedNodes.List);
            }

            for (var i = 0; i < LinkedNodes.Count; i++)
            {
                var LNode = LinkedNodes[i];
                if (!OnlyNotRemovable || (LNode.Flags & LinkedNodeFlags.NotRemovable) != 0)
                {
                    Out.Add(LNode);
                }
            }

            if (Children != null)
            {
                for (var i = 0; i < Children.Length; i++)
                {
                    Children[i].GetLinkedNodes(ref Out, OnlyNotRemovable);
                }
            }
        }
Exemple #9
0
        public override PluginResult NewNode(ref ExpressionNode Node)
        {
            if (Node is StrExpressionNode)
            {
                var IdNode  = Node as StrExpressionNode;
                var Preproc = State.GlobalContainer.Preprocessor;
                var Macro   = Preproc.GetMacro(IdNode.Code.ToString());

                if (IfDef)
                {
                    Node = Parent.NewNode(Constants.GetBoolValue(Container, Macro != null, Node.Code));
                    return(Node == null ? PluginResult.Failed : PluginResult.Ready);
                }
                else if (Macro != null)
                {
                    if (Macro.Value == null)
                    {
                        State.Messages.Add(MessageId.MacroWithoutValue, Node.Code);
                        return(PluginResult.Failed);
                    }

                    Macro.Used = true;
                    if (Macro.Parameters == null || Macro.Parameters.Count == 0)
                    {
                        Node = Macro.Value.Copy(Parent, Mode: BeginEndMode.None, Code: IdNode.Code);
                    }
                    else
                    {
                        Node = Parent.NewNode(new MacroExpressionNode(Macro, Node.Code));
                    }
                    return(Node == null ? PluginResult.Failed : PluginResult.Ready);
                }
            }

            // ------------------------------------------------------------------------------------
            else if (Node is OpExpressionNode)
            {
                var OpNode = Node as OpExpressionNode;
                var Ch     = OpNode.Children;
                var Op     = OpNode.Operator;

                if (Op == Operator.Call && Ch[0] is MacroExpressionNode)
                {
                    var MFunc = Ch[0] as MacroExpressionNode;
                    var Macro = MFunc.Macro;
                    Macro.Used = true;

                    if (Ch.Length != Macro.Parameters.Count + 1)
                    {
                        State.Messages.Add(MessageId.ParamCount, Node.Code);
                        return(PluginResult.Failed);
                    }

                    var Nodes    = new AutoAllocatedList <ExpressionNode>();
                    var LnkNodes = new AutoAllocatedList <LinkedExprNode>();
                    for (var i = 1; i < Ch.Length; i++)
                    {
                        if (!(Ch[i] is OpExpressionNode))
                        {
                            Nodes.Add(Ch[i]);
                        }
                        else
                        {
                            var N = new LinkedExprNode(Ch[i]);
                            LnkNodes.Add(N);
                            Nodes.Add(new LinkingNode(N, Node.Code));
                        }
                    }

                    PluginFunc Func = (ref ExpressionNode x) =>
                    {
                        if (x is MacroArgNode)
                        {
                            var ArgIndex = (x as MacroArgNode).Index;
                            x = Nodes[ArgIndex].Copy(Parent, Mode: BeginEndMode.None);
                            return(x == null ? PluginResult.Failed : PluginResult.Ready);
                        }

                        return(PluginResult.Succeeded);
                    };

                    Node = Macro.Value.Copy(Parent, Func, BeginEndMode.None);
                    if (Node == null)
                    {
                        return(PluginResult.Failed);
                    }

                    Node.LinkedNodes.AddRange(LnkNodes);
                    Node = Parent.NewNode(Node);
                    return(Node == null ? PluginResult.Failed : PluginResult.Ready);
                }
            }

            // ------------------------------------------------------------------------------------
            if (!CheckMacroNodes(Node))
            {
                return(PluginResult.Failed);
            }
            else
            {
                return(PluginResult.Succeeded);
            }
        }
Exemple #10
0
        private PluginResult Evaluate(ref ExpressionNode Node)
        {
            var OpNode = Node as OpExpressionNode;
            var Op     = OpNode.Operator;
            var Ch     = OpNode.Children;

            var Dst = Ch[0] as ConstExpressionNode;
            var Src = Ch.Length > 1 ? Ch[1] as ConstExpressionNode : null;

            var AllLinkedNodes = new AutoAllocatedList <LinkedExprNode>();

            Node.GetLinkedNodes(ref AllLinkedNodes, true);

            if (Dst.Value is NullValue || (Src != null && Src.Value is NullValue))
            {
                if (Dst.Type.UnderlyingClassOrRealId is ClassType)
                {
                    if (Op == Operator.Equality || Op == Operator.Inequality)
                    {
                        var Value = Dst.Value is NullValue == Src.Value is NullValue;
                        var Ret   = Constants.GetBoolValue(Container, Value, Node.Code);
                        Ret.LinkedNodes.AddRange(AllLinkedNodes);

                        Node = Parent.NewNode(Ret);
                        return(Node == null ? PluginResult.Failed : PluginResult.Ready);
                    }
                }

                if (Dst.Type.RealId is StringType)
                {
                    if (Op == Operator.Add)
                    {
                        var DstVal = Dst.Value is NullValue ? "" : Dst.String;
                        var SrcVal = Src.Value is NullValue ? "" : Src.String;
                        var Value  = DstVal + SrcVal;

                        var Ret = Constants.GetStringValue(Container, Value, Node.Code);
                        Ret.LinkedNodes.AddRange(AllLinkedNodes);

                        Node = Parent.NewNode(Ret);
                        return(Node == null ? PluginResult.Failed : PluginResult.Ready);
                    }
                }

                throw new ApplicationException();
            }
            else
            {
                var DstType = Dst.Type;
                var SrcType = Src != null ? Src.Type : null;
                var SrcVal  = Src != null ? Src.Value : null;

                var Ret = Dst.Value.DoOperation(SrcVal, Op, Node.Type);
                if (Ret is IntegerValue && Node.CheckingMode == CheckingMode.Unchecked)
                {
                    var IRet = Ret as IntegerValue;
                    IRet.Value = DataStoring.WrapToType(IRet.Value, Node.Type);
                }

                Node = Ret.ToExpression(State, Node.Type, Node.Code);
                if (Node == null)
                {
                    return(PluginResult.Failed);
                }

                Node.LinkedNodes.AddRange(AllLinkedNodes);
                Node = Parent.NewNode(Node);
                return(Node == null ? PluginResult.Failed : PluginResult.Ready);
            }
        }
Exemple #11
0
        public ExpressionNode ExtractPropertyGetter(ExpressionNode Node, ref bool Extracted)
        {
            ExpressionNode[] CallCh;
            AutoAllocatedList <LinkedExprNode> LinkedNodes =
                new AutoAllocatedList <LinkedExprNode>();

            if ((Node.Flags & ExpressionFlags.EnableGetter) != 0)
            {
                var Ch = Node.Children;
                CallCh = new ExpressionNode[Ch.Length - 1];

                if (Expressions.GetOperator(Ch[0]) == Operator.Member)
                {
                    var Ch0Ch  = Ch[0].Children;
                    var Linked = new LinkedExprNode(Ch0Ch[0]);
                    LinkedNodes.Add(Linked);

                    Ch0Ch[0] = Parent.NewNode(new LinkingNode(Linked, Node.Code));
                    if (Ch0Ch[0] == null || (Ch[0] = Parent.NewNode(Ch[0])) == null)
                    {
                        return(null);
                    }

                    var CallCh0Ch = new ExpressionNode[]
                    {
                        Parent.NewNode(new LinkingNode(Linked, Node.Code)),
                        CallCh[0] = GetGetterForSetter(Ch0Ch[1]),
                    };

                    if (CallCh0Ch[0] == null || CallCh0Ch[1] == null)
                    {
                        return(null);
                    }
                    CallCh[0] = Parent.NewNode(new OpExpressionNode(Operator.Member, CallCh0Ch, Node.Code));
                    if (CallCh[0] == null)
                    {
                        return(null);
                    }
                }
                else if (Ch[0] is IdExpressionNode)
                {
                    CallCh[0] = GetGetterForSetter(Ch[0]);
                }

                for (var i = 1; i < Ch.Length - 1; i++)
                {
                    var LinkedNode = new LinkedExprNode(Ch[i]);
                    LinkedNodes.Add(LinkedNode);

                    Node.Children[i] = Parent.NewNode(new LinkingNode(LinkedNode, Node.Code));
                    CallCh[i]        = Parent.NewNode(new LinkingNode(LinkedNode, Node.Code));
                    if (Node.Children[i] == null || CallCh[i] == null)
                    {
                        return(null);
                    }
                }

                Node = Parent.NewNode(Node);
                if (Node == null)
                {
                    return(null);
                }

                Node.Flags &= ~ExpressionFlags.EnableGetter;
                LinkedNodes.Add(new LinkedExprNode(Node, LinkedNodeFlags.NotRemovable));
            }
            else if (Expressions.GetOperator(Node) == Operator.Index)
            {
                var NewNode = Node.Children[0];
                var Res     = ExtractPropertyFunction(ref NewNode, false);
                if (Res == SimpleRecResult.Unknown)
                {
                    return(Node);
                }
                if (Res == SimpleRecResult.Failed)
                {
                    return(null);
                }

                CallCh    = Node.Children.Slice(0);
                CallCh[0] = NewNode;
            }
            else
            {
                var NewNode = Node;
                var Res     = ExtractPropertyFunction(ref NewNode, false);
                if (Res == SimpleRecResult.Unknown)
                {
                    return(Node);
                }
                if (Res == SimpleRecResult.Failed)
                {
                    return(null);
                }

                CallCh = new ExpressionNode[] { NewNode };
            }

            var Ret = new OpExpressionNode(Operator.Call, CallCh, Node.Code);

            Ret.LinkedNodes.AddRange(LinkedNodes);
            Extracted = true;
            return(Parent.NewNode(Ret));
        }
Exemple #12
0
        PluginResult ProcessParams(ref ExpressionNode Node)
        {
            var Ch   = Node.Children;
            var Type = Ch[0].Type;

            if (Type == null)
            {
                var IdCh0 = Ch[0] as IdExpressionNode;
                if (IdCh0 != null)
                {
                    Type = IdCh0.Identifier.TypeOfSelf;
                }
                else
                {
                    return(PluginResult.Succeeded);
                }
            }

            var FuncType = Type.RealId as TypeOfFunction;

            if (FuncType == null)
            {
                return(PluginResult.Succeeded);
            }

            if (FuncType.Children.Length < Ch.Length)
            {
                if ((State.Language.Flags & LangaugeFlags.ConvertParametersToTuple) != 0)
                {
                    if (FuncType.Children.Length == 2 && Ch.TrueForAll(x => !(x is NamedParameterNode)))
                    {
                        var Param0 = FuncType.Children[1] as FunctionParameter;
                        if (Param0.TypeOfSelf.RealId is TupleType)
                        {
                            var NewCh1 = Parent.NewNode(new OpExpressionNode(Operator.Tuple, Ch.Slice(1), Node.Code));
                            if (NewCh1 == null || ResolveNode(ref NewCh1) == PluginResult.Failed)
                            {
                                return(PluginResult.Failed);
                            }

                            Node.Children = Ch = new ExpressionNode[] { Ch[0], NewCh1 };
                            return(PluginResult.Succeeded);
                        }
                    }
                }
            }

            var ExtraParams = new AutoAllocatedList <ExpressionNode>();
            var Ret         = new ExpressionNode[FuncType.Children.Length];

            Ret[0] = Ch[0];

            var RetValue       = true;
            var NamedParameter = false;

            for (var i = 1; i < Ch.Length; i++)
            {
                if (Ch[i] is NamedParameterNode)
                {
                    var Chi   = Ch[i] as NamedParameterNode;
                    var Param = FuncType.GetParameter(Chi.Name.ToString());

                    if (Param == null)
                    {
                        State.Messages.Add(MessageId.UnknownId, Chi.Name);
                        RetValue = false; continue;
                    }

                    var Index = FuncType.GetChildIndex(Param);
                    if (Ret[Index] != null)
                    {
                        State.Messages.Add(MessageId.ParamAlreadySpecified, Chi.Code);
                        RetValue = false; continue;
                    }

                    Ret[Index]     = Chi.Children[0];
                    NamedParameter = true;
                }
                else
                {
                    if (NamedParameter)
                    {
                        State.Messages.Add(MessageId.UnnamedParamAfterNamed, Ch[i].Code);
                        RetValue = false; continue;
                    }

                    if (i >= Ret.Length)
                    {
                        ExtraParams.Add(Ch[i]);
                    }
                    else
                    {
                        Ret[i] = Ch[i];
                    }
                }
            }

            var LastParam = FuncType.Children[FuncType.Children.Length - 1] as FunctionParameter;

            if (LastParam != null && (LastParam.ParamFlags & ParameterFlags.ParamArray) != 0)
            {
                var First = Ret[Ret.Length - 1];
                if (ExtraParams.Count > 0 || NeedToCreateParamArray(First, LastParam))
                {
                    ExtraParams.Insert(0, First);

                    var ArrCh = ExtraParams.ToArray();
                    var Arr   = Parent.NewNode(new OpExpressionNode(Operator.Array, ArrCh, Node.Code));
                    if (Arr == null)
                    {
                        return(PluginResult.Failed);
                    }

                    Ret[Ret.Length - 1] = Arr;
                }
            }
            else if (ExtraParams.Count > 0)
            {
                State.Messages.Add(MessageId.ParamCount, Node.Code);
                return(PluginResult.Failed);
            }

            for (var i = 1; i < Ret.Length; i++)
            {
                if (Ret[i] == null)
                {
                    var Param = FuncType.Children[i] as FunctionParameter;
                    if (Param.ConstInitValue != null)
                    {
                        Ret[i] = new ConstExpressionNode(Param.TypeOfSelf, Param.ConstInitValue, Node.Code);
                        if ((Ret[i] = Parent.NewNode(Ret[i])) == null)
                        {
                            RetValue = false;
                            continue;
                        }
                    }
                    else
                    {
                        State.Messages.Add(MessageId.ParamNotSpecified, Node.Code, Param.Name.ToString());
                        RetValue = false; continue;
                    }
                }
            }

            Node.Children = Ret;
            return(RetValue ? PluginResult.Succeeded : PluginResult.Failed);
        }
Exemple #13
0
 public Enumerator(AutoAllocatedList <T> List)
 {
     this.List = List;
 }