Beispiel #1
0
        static bool Check(CompilerState State, CodeString Code)
        {
            if (Code[0] == '(' && Code.GetBracketPos(State) == -1)
            {
                return(false);
            }

            if (Code[0] == ')')
            {
                State.Messages.Add(MessageId.ZNumErr, Code.Substring(0, 1));
                return(false);
            }

            var P = Code.Length - 1;

            if (Code[P] == '(')
            {
                State.Messages.Add(MessageId.ZNumErr, Code.Substring(P));
                return(false);
            }

            if (Code[P] == ')' && Code.GetBracketPos(State, true) == -1)
            {
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        public Identifier TypeExtractor(IdContainer Container, ref CodeString Code)
        {
            if (TypeCodes == null)
            {
                return(null);
            }

            var OldCode = Code;
            var Global  = Container.GlobalContainer;
            var State   = Container.State;

            var EndStr = Code.EndStr(LetterCase.OnlyLower);

            Code = Code.Substring(0, Code.Length - EndStr.Length);

            if (EndStr.Length > 0)
            {
                for (var i = 0; i < TypeCodes.Length; i++)
                {
                    if (TypeCodes[i].String == EndStr)
                    {
                        return(TypeCodes[i].Identifier);
                    }
                }
            }

            Code = OldCode;
            return(null);
        }
Beispiel #3
0
        public SimpleRecResult GetGenericParams(CompilerState State, ref CodeString Code,
                                                out CodeString[] Out, bool EnableMessages = true)
        {
            Out = null;
            if (Code.Length > 0 && Code[Code.Length - 1] == '>')
            {
                var BracketPos = Code.GetBracketPos(State, true);
                if (BracketPos == -1)
                {
                    return(SimpleRecResult.Failed);
                }

                var ParamStart = BracketPos + 1;
                var StrParams  = Code.TrimmedSubstring(State, ParamStart, Code.Length - ParamStart - 1);
                if (!StrParams.IsValid)
                {
                    return(SimpleRecResult.Failed);
                }

                Out = RecognizerHelper.GetParamList(State, StrParams);
                if (Out == null)
                {
                    return(SimpleRecResult.Failed);
                }

                Code = Code.Substring(0, BracketPos).Trim();
            }

            return(SimpleRecResult.Unknown);
        }
Beispiel #4
0
        public bool GetParameters(CompilerState State, CodeString Code, out CodeString Function, out CodeString Parameters)
        {
            var Pos = FindParamPosition(State, Code.String);

            if (Pos != -1)
            {
                Function = Code.Substring(0, Pos).Trim();
                var Handlers = State.Language.GlobalHandlers;
                Parameters = Code.Substring(Pos).TrimOneBracket(Handlers);
            }
            else
            {
                Function   = Code;
                Parameters = Code.Substring(Code.Length);
            }

            return(true);
        }
Beispiel #5
0
        public SimpleRecResult Recognize(IdContainer Container, ref CodeString Code, List <Modifier> Out)
        {
            var Result = Code.StartsWith(Strings, null, new IdCharCheck(true));

            if (Result.Index != -1)
            {
                var State   = Container.State;
                var ModCode = Code.Substring(0, Result.String.Length);

                for (var i = 0; i < Out.Count; i++)
                {
                    if (Out[i] is CallingConventionModifier)
                    {
                        State.Messages.Add(MessageId.NotExpected, ModCode);
                        return(SimpleRecResult.Failed);
                    }
                }

                CallingConvention Conv;
                if (Result.Index == 0)
                {
                    Conv = CallingConvention.StdCall;
                }
                else if (Result.Index == 1)
                {
                    Conv = CallingConvention.CDecl;
                }
                else if (Result.Index == 2)
                {
                    Conv = CallingConvention.ZinniaCall;
                }
                else
                {
                    throw new NotImplementedException();
                }

                Out.Add(new CallingConventionModifier(ModCode, Conv));
                Code = Code.Substring(Result.String.Length).Trim();
                return(SimpleRecResult.Succeeded);
            }

            return(SimpleRecResult.Unknown);
        }
Beispiel #6
0
        public ExprRecResult Recognize(CodeString Code, PluginRoot Plugin, ref ExpressionNode Out)
        {
            var Result = Code.StartsWith(Operators, Skip, new IdCharCheck(true));

            if (Result.Position != -1)
            {
                var State = Plugin.State;
                var Right = Code.Substring(Result.String.Length).Trim();
                Right = Right.TrimOneBracket(SkippingHandlers);

                if (Right.Length == 0)
                {
                    State.Messages.Add(MessageId.DeficientExpr, Code);
                    return(ExprRecResult.Failed);
                }

                var TypeMngrPlugin = Plugin.GetPlugin <TypeMngrPlugin>();
                if (TypeMngrPlugin == null)
                {
                    return(ExprRecResult.Unknown);
                }

                var OldCheckingMode = TypeMngrPlugin.CheckingMode;
                if (Result.Index == 0)
                {
                    TypeMngrPlugin.CheckingMode = CheckingMode.Checked;
                }
                else if (Result.Index == 1)
                {
                    TypeMngrPlugin.CheckingMode = CheckingMode.Unchecked;
                }
                else
                {
                    throw new ApplicationException();
                }

                Out = Expressions.Recognize(Right, Plugin);
                TypeMngrPlugin.CheckingMode = OldCheckingMode;

                if (Out == null)
                {
                    return(ExprRecResult.Failed);
                }
                else
                {
                    return(ExprRecResult.Ready);
                }
            }

            return(ExprRecResult.Unknown);
        }
Beispiel #7
0
        public SimpleRecResult Recognize(IdContainer Container, ref CodeString Code, List <Modifier> Out)
        {
            if (Code.StartsWith(String, new IdCharCheck(true)))
            {
                var State   = Container.State;
                var ModCode = Code.Substring(0, String.Length);

                for (var i = 0; i < Out.Count; i++)
                {
                    if (Out[i] is NoBaseModifier)
                    {
                        State.Messages.Add(MessageId.NotExpected, ModCode);
                        return(SimpleRecResult.Failed);
                    }
                }

                Out.Add(new NoBaseModifier(ModCode));
                Code = Code.Substring(String.Length).Trim();
                return(SimpleRecResult.Succeeded);
            }

            return(SimpleRecResult.Unknown);
        }
Beispiel #8
0
        public SimpleRecResult Recognize(IdContainer Container, ref CodeString Code, List <Modifier> Out)
        {
            var Result = Code.StartsWith(Strings, null, new IdCharCheck(true));

            if (Result.Index != -1)
            {
                var State   = Container.State;
                var ModCode = Code.Substring(0, Result.String.Length);

                ParameterFlags Flags;
                if (Result.Index == 0)
                {
                    Flags = ParameterFlags.ParamArray;
                }
                else
                {
                    throw new NotImplementedException();
                }

                for (var i = 0; i < Out.Count; i++)
                {
                    var FlagMod = Out[i] as ParamFlagModifier;
                    if (FlagMod != null && (FlagMod.Flags & Flags) != 0)
                    {
                        State.Messages.Add(MessageId.NotExpected, ModCode);
                        return(SimpleRecResult.Failed);
                    }
                }

                Out.Add(new ParamFlagModifier(ModCode, Flags));
                Code = Code.Substring(Result.String.Length).Trim();
                return(SimpleRecResult.Succeeded);
            }

            return(SimpleRecResult.Unknown);
        }
Beispiel #9
0
        public ExprRecResult Recognize(CodeString Code, PluginRoot Plugin, ref ExpressionNode Out)
        {
            var Result = Code.StartsWith(Operators, Skip, new IdCharCheck(true));

            if (Result.Position != -1)
            {
                var State = Plugin.State;
                var Right = Code.Substring(Result.String.Length).Trim();
                Right = Right.TrimOneBracket(SkippingHandlers);

                if (Right.Length == 0)
                {
                    State.Messages.Add(MessageId.DeficientExpr, Code);
                    return(ExprRecResult.Failed);
                }

                var Scope = new CodeScopeNode(Plugin.Container, Right);
                if (!Scope.ProcessCode())
                {
                    return(ExprRecResult.Failed);
                }

                var List   = Scope.GetContainerId("retvar", x => x is LocalVariable);
                var RetVar = Identifiers.SelectIdentifier(State, List, Code) as LocalVariable;
                if (RetVar == null)
                {
                    return(ExprRecResult.Failed);
                }

                Out = new ScopeExpressionNode(Scope, Code)
                {
                    ReturnVar = RetVar,
                };

                return(ExprRecResult.Succeeded);
            }

            return(ExprRecResult.Unknown);
        }
Beispiel #10
0
        public bool Process(CompilerState State, CodeString Code)
        {
            var File             = Code.File;
            var StringRecognizer = State.Language.Root.GetObject <StringRecognizer>();
            var SkippingHandlers = new IResultSkippingHandler[] { StringRecognizer };

            var Line = Code.Line;

            while (Code.HasLine(Line))
            {
                var LineStr = Code.GetLine(Line);
                var Result  = LineStr.Find(Strings, Handlers: SkippingHandlers);
                if (Result.Index == 0)
                {
                    var Index  = LineStr.Index + Result.Position;
                    var Length = LineStr.FirstLineLength - Result.Position;
                    File.RemoveCode(Index, Length, Line);
                }
                else if (Result.Index == 1)
                {
                    var Index  = LineStr.Index + Result.Position;
                    var Right  = Code.Substring(LineStr.Index + Result.NextChar);
                    var EndRes = Right.Find(EndStrings);

                    if (EndRes.Position == -1)
                    {
                        State.Messages.Add(MessageId.DeficientExpr, Right.Substring(EndRes));
                        return(false);
                    }

                    var Length = Right.Index + EndRes.NextChar - Index;
                    File.RemoveCode(Index, Length, Line);
                }

                Line++;
            }

            return(true);
        }
Beispiel #11
0
        public CodeString BetweenOperatos(CodeString Code)
        {
            if (UseMarker)
            {
                var StrLength = Code.Length;
                if (StrLength < 2 || Code[0] != Marker || Code[StrLength - 1] != Marker)
                {
                    return(new CodeString());
                }
            }

            var StartRes = Code.StartsWith(Operators, Skip);
            var EndRes   = Code.EndsWith(Operators, Skip);

            if (StartRes.Index == -1 || EndRes.Index == -1)
            {
                return(new CodeString());
            }
            if (StartRes.Index != EndRes.Index)
            {
                return(new CodeString());
            }

            var Length = StartRes.String.Length;

            if (EndRes.Position < Length)
            {
                return(new CodeString());
            }

            Code = Code.Substring(Length, Code.Length - Length * 2);
            if (Code.Find(Operators, Skip).Index != -1)
            {
                return(new CodeString());
            }
            return(Code);
        }
Beispiel #12
0
        public SimpleRecResult Recognize(IdContainer Container, CodeString Code, GetIdOptions Options, ref Identifier Ret)
        {
            var Position = Code.Length - 1;

            if (Position >= 0 && Code[Position] == ']')
            {
                var State = Container.State;
                var ZPos  = Code.GetBracketPos(State, true, Options.EnableMessages);
                if (ZPos == -1)
                {
                    return(SimpleRecResult.Failed);
                }

                var Left = Code.TrimmedSubstring(State, 0, ZPos, Options.EnableMessages);
                if (!Left.IsValid)
                {
                    return(SimpleRecResult.Failed);
                }

                var TOptions = Options;
                TOptions.Func = x => x.RealId is Type;

                var TypeOfVals = Identifiers.Recognize(Container, Left, TOptions);
                if (TypeOfVals == null)
                {
                    return(SimpleRecResult.Failed);
                }

                var RTypeOfVals = TypeOfVals.RealId as Type;
                if (RTypeOfVals == null || (RTypeOfVals.TypeFlags & TypeFlags.CanBeArrayType) == 0)
                {
                    if (Options.EnableMessages)
                    {
                        State.Messages.Add(MessageId.UnknownId, Code);
                    }

                    return(SimpleRecResult.Failed);
                }

                var StrParams = Code.Substring(ZPos + 1, Position - ZPos - 1).Trim();
                if (StrParams.IsEqual("?"))
                {
                    Ret = new NonrefArrayType(Container, TypeOfVals, null);
                    if (Options.Func == null || Options.Func(Ret))
                    {
                        return(SimpleRecResult.Succeeded);
                    }
                }
                else if (StrParams.IsEqual("*"))
                {
                    if ((RTypeOfVals.TypeFlags & TypeFlags.CanBePointer) == 0)
                    {
                        if (Options.EnableMessages)
                        {
                            State.Messages.Add(MessageId.UnknownId, Code);
                        }

                        return(SimpleRecResult.Failed);
                    }

                    Ret = new PointerAndLength(Container, TypeOfVals);
                    if (Options.Func == null || Options.Func(Ret))
                    {
                        return(SimpleRecResult.Succeeded);
                    }
                }

                var SplParams = RecognizerHelper.SplitToParameters(State, StrParams,
                                                                   ',', Options.EnableMessages, true);

                if (SplParams == null)
                {
                    return(SimpleRecResult.Failed);
                }

                if (SplParams.Length == 0 || SplParams[0].Length == 0)
                {
                    for (var i = 0; i < SplParams.Length; i++)
                    {
                        if (SplParams[i].Length > 0)
                        {
                            State.Messages.Add(MessageId.NotExpected, SplParams[i]);
                            return(SimpleRecResult.Failed);
                        }
                    }

                    var IndexCount = SplParams.Length == 0 ? 1 : SplParams.Length;
                    Ret = new RefArrayType(Container, TypeOfVals, IndexCount);
                    if (Options.Func == null || Options.Func(Ret))
                    {
                        return(SimpleRecResult.Succeeded);
                    }
                }
                else
                {
                    var Plugin = Container.GetPlugin();
                    Plugin.GetPlugin <EvaluatorPlugin>().MustBeConst = true;

                    var Lengths = new int[SplParams.Length];
                    for (var i = 0; i < SplParams.Length; i++)
                    {
                        var Node    = Expressions.CreateExpression(SplParams[i], Plugin);
                        var ConstCh = Node as ConstExpressionNode;
                        if (ConstCh == null)
                        {
                            return(SimpleRecResult.Failed);
                        }

                        if (!(ConstCh.Type is NonFloatType))
                        {
                            if (Options.EnableMessages)
                            {
                                State.Messages.Add(MessageId.MustBeInteger, StrParams);
                            }

                            return(SimpleRecResult.Failed);
                        }

                        if (!VerifyArrayLength(State, ConstCh, StrParams, Options.EnableMessages))
                        {
                            return(SimpleRecResult.Failed);
                        }

                        Lengths[i] = (int)ConstCh.Integer;
                    }

                    Ret = new NonrefArrayType(Container, TypeOfVals, Lengths);
                    if (Options.Func == null || Options.Func(Ret))
                    {
                        return(SimpleRecResult.Succeeded);
                    }
                }
            }

            return(SimpleRecResult.Unknown);
        }
Beispiel #13
0
        public SimpleRecResult Recognize(IdContainer Container, CodeString Code, GetIdOptions Options, ref Identifier Ret)
        {
            if (Code.StartsWith(Operators, Skip, IdCharCheck: new IdCharCheck(true)).Index == 0)
            {
                var State = Container.State;
                if (Code[Code.Length - 1] != ')')
                {
                    if (Options.EnableMessages)
                    {
                        State.Messages.Add(MessageId.NotExpected, Code);
                    }

                    return(SimpleRecResult.Failed);
                }

                var BracketPos = Code.GetBracketPos(State, true, Options.EnableMessages);
                if (BracketPos == -1)
                {
                    return(SimpleRecResult.Failed);
                }

                var Left = Code.TrimmedSubstring(State, 3, BracketPos - 3, Options.EnableMessages);
                if (!Left.IsValid)
                {
                    return(SimpleRecResult.Failed);
                }

                CallingConvention CallConv;
                if (!Modifiers.GetCallConv(Container, ref Left, out CallConv))
                {
                    return(SimpleRecResult.Failed);
                }

                if (CallConv == CallingConvention.Unknown)
                {
                    CallConv = Container.DefaultCallConv;
                }

                var TOptions = Options;
                TOptions.Func = x => x.RealId is Type;

                var RetType   = Identifiers.Recognize(Container, Left, TOptions);
                var StrParams = Code.Substring(BracketPos + 1, Code.Length - BracketPos - 2).Trim();

                var Flags = VarDeclarationListFlags.EnableUnnamed | VarDeclarationListFlags.EnableInitValue;
                if (Options.EnableMessages)
                {
                    Flags |= VarDeclarationListFlags.EnableMessages;
                }
                var DeclList = VarDeclarationList.Create(Container, StrParams, null, Flags);
                if (DeclList == null || RetType == null)
                {
                    return(SimpleRecResult.Failed);
                }

                if (!(RetType.RealId is Type))
                {
                    State.Messages.Add(MessageId.UnknownType, Left);
                    return(SimpleRecResult.Failed);
                }

                var Params = DeclList.ToFuncParams(Container.GetPlugin());
                if (Params == null || Params.Contains(null))
                {
                    return(SimpleRecResult.Failed);
                }

                Ret = new TypeOfFunction(Container, CallConv, RetType, Params);
                if (Options.Func == null || Options.Func(Ret))
                {
                    return(SimpleRecResult.Succeeded);
                }
            }

            return(SimpleRecResult.Unknown);
        }
Beispiel #14
0
        public SimpleRecResult Recognize(IdContainer Container, ref CodeString Code, List <Modifier> Out)
        {
            var Result = Code.StartsWith(Strings, null, new IdCharCheck(true));

            if (Result.Index != -1)
            {
                var State   = Container.State;
                var ModCode = Code.Substring(0, Result.String.Length);

                IdentifierFlags Flags;
                if (Result.Index == 0)
                {
                    Flags = IdentifierFlags.Virtual;
                }
                else if (Result.Index == 1)
                {
                    Flags = IdentifierFlags.Override;
                }
                else if (Result.Index == 2)
                {
                    Flags = IdentifierFlags.Abstract;
                }
                else if (Result.Index == 3)
                {
                    Flags = IdentifierFlags.Sealed;
                }
                else if (Result.Index == 4)
                {
                    Flags = IdentifierFlags.Static;
                }
                else if (Result.Index == 5)
                {
                    Flags = IdentifierFlags.Extern;
                }
                else if (Result.Index == 6)
                {
                    Flags = IdentifierFlags.ReadOnly;
                }
                else if (Result.Index == 7)
                {
                    Flags = IdentifierFlags.HideBaseId;
                }
                else
                {
                    throw new NotImplementedException();
                }

                for (var i = 0; i < Out.Count; i++)
                {
                    var FlagMod = Out[i] as FlagModifier;
                    if (FlagMod != null && (FlagMod.Flags & Flags) != 0)
                    {
                        State.Messages.Add(MessageId.NotExpected, ModCode);
                        return(SimpleRecResult.Failed);
                    }
                }

                Out.Add(new FlagModifier(ModCode, Flags));
                Code = Code.Substring(Result.String.Length).Trim();
                return(SimpleRecResult.Succeeded);
            }

            return(SimpleRecResult.Unknown);
        }
Beispiel #15
0
        public SimpleRecResult Recognize(IdContainer Container, ref CodeString Code, List <Modifier> Out)
        {
            var Result = Code.StartsWith(Strings, null, new IdCharCheck(true));

            if (Result.Index != -1)
            {
                var State   = Container.State;
                var ModCode = Code.Substring(0, Result.String.Length);

                IdentifierAccess Access;
                if (Result.Index == 0)
                {
                    Access = IdentifierAccess.Public;
                }
                else if (Result.Index == 1)
                {
                    Access = IdentifierAccess.Protected;
                }
                else if (Result.Index == 2)
                {
                    Access = IdentifierAccess.Private;
                }
                else if (Result.Index == 3)
                {
                    Access = IdentifierAccess.Internal;
                }
                else
                {
                    throw new NotImplementedException();
                }

                for (var i = 0; i < Out.Count; i++)
                {
                    var AccessMod = Out[i] as AccessModifier;
                    if (AccessMod != null)
                    {
                        var Allowed = false;
                        if (Access == IdentifierAccess.Internal)
                        {
                            Allowed = AccessMod.Access == IdentifierAccess.Protected;
                        }
                        else if (Access == IdentifierAccess.Protected)
                        {
                            Allowed = AccessMod.Access == IdentifierAccess.Internal;
                        }

                        if (!Allowed)
                        {
                            State.Messages.Add(MessageId.NotExpected, ModCode);
                            return(SimpleRecResult.Failed);
                        }
                    }
                }

                Out.Add(new AccessModifier(ModCode, Access));
                Code = Code.Substring(Result.String.Length).Trim();
                return(SimpleRecResult.Succeeded);
            }

            return(SimpleRecResult.Unknown);
        }