Пример #1
0
        internal Expression MaybeImmediatelyResolve(PastelParser parser)
        {
            if (this.Root is CompileTimeFunctionReference)
            {
                CompileTimeFunctionReference constFunc = (CompileTimeFunctionReference)this.Root;
                InlineConstant argName = (InlineConstant)this.Args[0];
                switch (constFunc.NameToken.Value)
                {
                case "ext_boolean":
                    return(new InlineConstant(
                               PType.BOOL,
                               this.FirstToken,
                               parser.GetParseTimeBooleanConstant(argName.Value.ToString())));

                case "ext_integer":
                    return(new InlineConstant(
                               PType.INT,
                               this.FirstToken,
                               parser.GetParseTimeIntegerConstant(argName.Value.ToString())));

                default:
                    return(this);
                }
            }
            return(this);
        }
Пример #2
0
        internal Executable[] ImmediateResolveMaybe(PastelParser parser)
        {
            if (this.Expression is FunctionInvocation)
            {
                if (((FunctionInvocation)this.Expression).Root is CompileTimeFunctionReference)
                {
                    FunctionInvocation           functionInvocation  = (FunctionInvocation)this.Expression;
                    CompileTimeFunctionReference compileTimeFunction = (CompileTimeFunctionReference)functionInvocation.Root;
                    switch (compileTimeFunction.NameToken.Value)
                    {
                    case "import":
                        string path = ((InlineConstant)functionInvocation.Args[0]).Value.ToString();
                        return(parser.ParseImportedCode(path));

                    default:
                        throw new NotImplementedException();
                    }
                }
            }
            return(null);
        }
Пример #3
0
        // Attempts to pop a Namespaced.TypeName or a TypeName from the token stream.
        // The values are applied to reusableRootNameParserOut and the number of values
        // parsed are returned as an integer. Possible values are 0, 1, and 2.
        // This method will update the token stream through the valid tokens.
        private static int ParseRootNameImpl(TokenStream tokens)
        {
            if (!tokens.HasMore)
            {
                return(0);
            }
            int   zeroIndex  = tokens.SnapshotState();
            Token firstToken = tokens.Pop();

            if (!PastelParser.IsValidName(firstToken.Value))
            {
                tokens.RevertState(zeroIndex);
                return(0);
            }
            reusableRootNameParserOut[0] = firstToken;
            int oneIndex = tokens.SnapshotState();

            if (!tokens.PopIfPresent("."))
            {
                return(1);
            }

            if (!tokens.HasMore)
            {
                tokens.RevertState(oneIndex);
                return(1);
            }
            Token secondToken = tokens.Pop();

            if (!PastelParser.IsValidName(secondToken.Value))
            {
                tokens.RevertState(oneIndex);
                return(1);
            }

            reusableRootNameParserOut[1] = secondToken;

            return(2);
        }
Пример #4
0
        private static PType ParseImpl(TokenStream tokens)
        {
            Token token = tokens.Pop();

            switch (token.Value)
            {
            case "int":
            case "char":
            case "double":
            case "bool":
            case "void":
            case "string":
            case "object":
                return(new PType(token, token.Value));

            default:
                if (!PastelParser.IsValidName(token.Value))
                {
                    return(null);
                }
                break;
            }

            int  tokenIndex = tokens.SnapshotState();
            bool isError    = false;

            if (tokens.PopIfPresent("<"))
            {
                List <PType> generics = new List <PType>();
                while (!tokens.PopIfPresent(">"))
                {
                    if (generics.Count > 0)
                    {
                        if (!tokens.PopIfPresent(","))
                        {
                            isError = true;
                            break;
                        }
                    }

                    PType generic = ParseImpl(tokens);
                    if (generic == null)
                    {
                        return(null);
                    }

                    generics.Add(generic);
                }
                if (!isError)
                {
                    return(new PType(token, token.Value, generics));
                }

                // If there was an error while parsing generics, then this may still be a valid type.
                tokens.RevertState(tokenIndex);
                return(new PType(token, token.Value));
            }
            else
            {
                return(new PType(token, token.Value));
            }
        }