Пример #1
0
        /// <summary>
        /// Add new block
        /// </summary>
        /// <param name="lexemes">List of lexemes</param>
        /// <param name="pos">Position of current parsed lexeme</param>
        /// <param name="elem">Statement builded on lexemes. Always null</param>
        /// <returns>Position of a next lexeme</returns>
        private int ParseBlock(List <Lexeme> lexemes, int pos, out TreeElement elem)
        {
            StatementListElement block = new StatementListElement();

            m_currentBlock.Peek().AddChild(block);
            m_currentBlock.Push(block);

            ++pos; //skip '{'

            elem = null;

            return(pos);
        }
Пример #2
0
        /// <summary>
        /// Parse function body
        /// </summary>
        /// <param name="funcInfo">Function to parse</param>
        private void ParseFunction(Parser1To2Pass.FunctionInfo funcInfo)
        {
            var funcElem = new FunctionElement();

            m_currentFunction = funcElem;

            funcElem.Info = funcInfo.Info;

            StatementListElement statements = new StatementListElement();

            m_currentBlock.Push(statements);

            foreach (var arg in funcInfo.Info.Arguments)
            {
                var declElem = new VarDeclarationElement
                {
                    VarType = arg.TypeInfo.Name,
                    VarName = arg.ArgName,
                    IgnoreInitialization = true
                };
                declElem.SetParent(statements);
                funcElem.AddArgumentVariable(declElem);
            }

            if (funcInfo.FuncLexemes.Count > 0)
            {
                for (int i = 0, end = funcInfo.FuncLexemes.Count;;)
                {
                    TreeElement element;
                    i = ParseStatement(funcInfo.FuncLexemes, i, out element);
                    if (element != null) //if null => ignore (block element)
                    {
                        var statementElem = new StatementElement();
                        statementElem.AddChild(element);

                        m_currentBlock.Peek().AddChild(statementElem);
                    }

                    if (i >= end)
                    {
                        break;
                    }
                }
            }

            funcElem.AddChild(statements);

            m_parserOutput.Functions.Add(funcElem);

            m_currentBlock.Pop();
        }
Пример #3
0
        private static FunctionElement CreateMain()
        {
            var symbols = LanguageSymbols.Instance;

            LanguageFunction functionInfo = new LanguageFunction();

            functionInfo.Name        = "!_Main";
            functionInfo.Arguments   = new List <LanguageFunction.FunctionArg>();
            functionInfo.ReturnTypes = new List <LanguageType>();

            symbols.AddUserFunction(functionInfo);

            FunctionElement funcElement = new FunctionElement();

            funcElement.Info = functionInfo;

            StatementListElement statements = new StatementListElement();

            funcElement.AddChild(statements);

            FunctionCallElement callElement = new FunctionCallElement();

            LanguageFunction mainFuncInfo = null;

            try
            {
                mainFuncInfo = symbols.GetFunction("Main", new List <string>());
            }
            catch (CompilationException)
            {
                Compilation.WriteError("Main() wasn't found. Did you forget it?", -1);
            }
            callElement.FunctionInfo  = mainFuncInfo;
            callElement.CallArguments = new List <ValueElement>();

            statements.AddChild(callElement);

            SingleGenOpElement exitElement = new SingleGenOpElement();

            exitElement.Operation = new GenOp {
                Code = GenCodes.Exit, ArgCount = 0
            };

            statements.AddChild(exitElement);

            return(funcElement);
        }