Esempio n. 1
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();
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        private void FixFunctionOperations(FunctionElement func)
        {
            foreach (var op in m_currentOperations)
            {
                switch (op.Code)
                {
                case GenCodes.UpdateFunc:
                {
                    op.Code  = GenCodes.Func;
                    op.Bytes = ByteConverter.New()
                               .CastInt32(func.GetFunctionRequiredSpaceForVariables()
                                          + func.RequiredSpaceInLocals)
                               .Bytes;
                }
                break;

                case GenCodes.ConvertAddressSetTempVar:
                {
                    var converter = ByteConverter.New(op.Bytes.ToArray());
                    var offset    = converter.GetInt32();
                    var size      = converter.GetInt32();

                    int spaceOffset = func.GetFunctionRequiredSpaceForVariables();

                    op.Code  = GenCodes.SetLVarVal;
                    op.Bytes = ByteConverter.New().CastInt32(spaceOffset + offset).CastInt32(size).Bytes;
                }
                break;

                case GenCodes.ConvertAddressGetTempVar:
                {
                    var converter = ByteConverter.New(op.Bytes.ToArray());
                    var offset    = converter.GetInt32();
                    var size      = converter.GetInt32();

                    int spaceOffset = func.GetFunctionRequiredSpaceForVariables();

                    op.Code  = GenCodes.GetLVarVal;
                    op.Bytes = ByteConverter.New().CastInt32(spaceOffset + offset).CastInt32(size).Bytes;
                }
                break;
                }
            }
        }
Esempio n. 4
0
        public override void GenerateInstructions(Generator generator)
        {
            FunctionElement func = RootParent <FunctionElement>();

            foreach (var child in m_children)
            {
                child.GenerateInstructions(generator);
            }

            if (m_localVariables.Count > 0)
            {
                foreach (var variable in m_localVariables)
                {
                    variable.GenLowLevelDelete(generator);
                }

                foreach (var variable in m_localVariables)
                {
                    func.PopLocalVarStackSpace(variable.VarType, variable.VarName);
                }

                generator.RemoveLastNLocalVars(m_localVariables.Count);
            }
        }