Пример #1
0
        private void ExecuteTest(Stream stream, string fileName)
        {
            string astData;
            Stream astDataStream = null;

            string irData;
            Stream irDataStream = null;

            try
            {
                MiniParser miniParser = new MiniParser(stream);
                AstProgram astProgram = miniParser.ParseProgram();
                stream.Close();
                astProgram.GenerateAstData(true);

                // Output AST data to UI
                OnAstOut(new AstOutEventArgs(astProgram.AstData));
                Ast.ResetAstData();

                // Present AST data to AST parser as a stream
                AstParser astPsr = new AstParser(astDataStream = CreateMemoryStream(astProgram.AstData));
                AstProgram p2 = AstParser.Program();
                astDataStream.Close();

                // -------------------------------------------------------

                SymbolVisitor sv = new SymbolVisitor();
                sv.visit(p2); //sv.symTable.show();

                TypeVisitor tv = new TypeVisitor(sv.symTable);
                tv.visit(p2);

                IrgenVisitor iv = new IrgenVisitor(sv.symTable, tv);
                IrProg ir0 = iv.visit(p2);
                Canon cv = new Canon();
                Ir.Clear();

                IrProg irProg = cv.visit(ir0);
                irProg.GenerateIrData();

                // Output AST data to UI
                OnIrOut(new IrOutEventArgs(irData = Ir.IrData));
                Ir.Clear();

                // -------------------------------------------------------

                IrProg irProgram = null;
                try
                {
                    //stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                    irProgram = (new IrParser(irDataStream = CreateMemoryStream(irData))).Program();
                }
                catch (InterpException ex)
                {
                    OnAppException(new AppExceptionEventArgs(ex));
                }
                catch (Exception ex)
                {
                    OnAppException(new AppExceptionEventArgs(ex));
                }
                finally
                {
                    if (stream != null)
                        stream.Close();
                }

                if (irProgram != null)
                {
                    try
                    {
                        InterpVisitor interpreter = new InterpVisitor();
                        interpreter.AddOnSystemOut(Interpreter_OnSystemOut);
                        interpreter.visit(irProgram);
                    }
                    catch (Exception ex)
                    {
                        OnAppException(new AppExceptionEventArgs(ex));
                    }
                }
            }
            catch (TypeException ex)
            {
                OnAppException(new AppExceptionEventArgs(ex));
            }
            catch (SymbolException ex)
            {
                OnAppException(new AppExceptionEventArgs(ex));
            }
            catch (Exception ex)
            {
                OnAppException(new AppExceptionEventArgs(ex));
            }
        }
Пример #2
0
        public void ExecuteAstTest(Stream stream, string fileName)
        {
            try
            {
                // create AST parser
                AstParser psr = new AstParser(stream);

                // Parse AST file and return pointer to program at root of tree
                AstProgram p = AstParser.Program();
                stream.Close();

                //  Create a symbol table visitor
                SymbolVisitor sv = new SymbolVisitor();

                // traverse AST to create symbol table
                sv.visit(p);
                //sv.symTable.show();

                // Traverse AST to verify type
                TypeVisitor tv = new TypeVisitor(sv.symTable);
                tv.visit(p);

                // Generate intermediate language
                IrgenVisitor iv = new IrgenVisitor(sv.symTable, tv);
                IrProg ir0 = iv.visit(p);
                Canon cv = new Canon();
                Ir.Clear();
                IrProg ir = cv.visit(ir0);
                ir.GenerateIrData();
                Ir.Clear();
            }
            catch (TypeException ex)
            {
                OnAppException(new AppExceptionEventArgs(ex));
            }
            catch (SymbolException ex)
            {
                OnAppException(new AppExceptionEventArgs(ex));
            }
            catch (Exception ex)
            {
                OnAppException(new AppExceptionEventArgs(ex));
            }
        }