Exemplo n.º 1
0
        public override void Parse()
        {
            var token = NextToken();

            if ((PascalTokenType)token.m_Type == PascalTokenType.Const)
            {
                var const_parser = new ConstDeclarationParser(this);
                const_parser.Parse();
            }

            //TODO: error?
            token = CurrentToken();
            if ((PascalTokenType)token.m_Type == PascalTokenType.Variable)
            {
                var var_parser = new VariableDeclarationParser(this);
                var_parser.Parse();
            }

            //
            CodeGenerator.Emit(Operation.Jump, 0, 0);//default 0
            int current = CodeGenerator.m_Code.Count - 1;


            //
            for (; ;)
            {
                token = CurrentToken();

                if ((PascalTokenType)token.m_Type == PascalTokenType.Procedure)
                {
                    //
                    var proceduer_parser = new ProcedureDeclarationParser(this);
                    proceduer_parser.Parse();
                }
                else
                {
                    break;
                }
            }

            //TODO:
            SymbolTablePrinter.RecordSymbolTable(m_SymbolTableStack.GetLocalSymbolTable());

            //
            CodeGenerator.Refine(current, CodeGenerator.m_Code.Count);

            //allocate variables
            var local = m_SymbolTableStack.GetLocalSymbolTable();

            CodeGenerator.Emit(Operation.Allocate, 0, (local as SymbolTable).m_VariableAmount);

            //
            token = CurrentToken();
            var statement_parser = new StatementParser(this);

            statement_parser.Parse();

            //
            CodeGenerator.Emit(Operation.Return);
        }
Exemplo n.º 2
0
        public override void Parse()
        {
            // <condition> JPC <then-statement1> JMP label_JPC <else-statement> label_JMP

            try
            {
                ConsumeCurrentToken();//skip if

                var condition_parser = new ConditionParser(this);
                condition_parser.Parse();

                var token = CurrentToken();
                if (token.m_Text != "then")
                {
                    //TODO: error
                    ErrorReporter.RecordError(22, m_Scanner.m_CurrentToken.m_LineNumber);
                    throw new Exception();
                }
                ConsumeCurrentToken();//skip then

                //
                CodeGenerator.Emit(Operation.ConditionalJump, 0, 0); //default 0
                int JPC = CodeGenerator.m_Code.Count - 1;            //label1 is the address of JPC 0 ...

                //
                var statement_parser = new StatementParser(this);
                statement_parser.Parse();

                CodeGenerator.Emit(Operation.Jump, 0, 0);       //default 0
                int JMP       = CodeGenerator.m_Code.Count - 1; //label1 is the address of JMP 0 ...
                int label_JPC = CodeGenerator.m_Code.Count;
                CodeGenerator.Refine(JPC, label_JPC);

                token = CurrentToken();
                if (token.m_Text == "else")
                {
                    ConsumeCurrentToken();//skip then
                    statement_parser = new StatementParser(this);
                    statement_parser.Parse();

                    int label_JMP = CodeGenerator.m_Code.Count;
                    CodeGenerator.Refine(JMP, label_JMP);
                }
                else
                {
                    CodeGenerator.Refine(JMP, label_JPC);
                }
            }
            catch (Exception)
            {
            }
        }
Exemplo n.º 3
0
        public override void Parse()
        {
            ConsumeCurrentToken();//skip while

            int label_true = CodeGenerator.m_Code.Count;

            var condition_parser = new ConditionParser(this);

            condition_parser.Parse();

            if (condition_parser.IsValid == false)
            {
                return;
            }
            var token = CurrentToken();

            if (token.m_Text != "do")
            {
                //TODO: error
            }
            ConsumeCurrentToken();//skip do

            //
            int label_condition_jump = CodeGenerator.m_Code.Count;

            CodeGenerator.Emit(Operation.ConditionalJump, 0, 0);//default 0

            //
            var statement_parser = new StatementParser(this);

            statement_parser.Parse();

            //

            //
            CodeGenerator.Emit(Operation.Jump, 0, label_true);//default 0
            int label_false = CodeGenerator.m_Code.Count;


            CodeGenerator.Refine(label_condition_jump, label_false);
        }
Exemplo n.º 4
0
        private void OnParse()
        {
            var statement_parser = new StatementParser(this);

            statement_parser.Parse();
        }