#include "StdAfx.h" #include "MainForm.h" #include "inp.h" #include "out.h" #include "lex.h" #include "code1.h" #include "expr1.h" #include "stat1.h" #include "example.h" using namespace System::IO; using namespace System::Windows; Prekl::MainForm ^ getMainForm () { return Prekl::MainForm::win; } namespace Prekl { ref class CompilerException: System::Exception { public: CompilerException (String ^ txt) : Exception (txt) { } }; std::string SysToStd (String ^ txt) { int len = txt->Length; char * p = new char [len+1]; for (int i = 0; i < len; i ++) p[i] = char (txt[i]); p[len] = 0; std::string s (p); delete [] p; return s; } String ^ StdToSys (std::string s) { String ^ txt = gcnew String (s.c_str ()); return txt; } String ^ IntToStr (int n) { return "" + n; } /* Source Text */ std::string MainForm::GetInpLine (int line_num) { String ^ txt = ""; if (line_num <= inputArea->Lines->Length) txt = inputArea->Lines [line_num-1]; else txt = "\0"; return SysToStd (txt); } std::string MainForm::GetInpText () { String ^ txt = inputArea->Text; return SysToStd (txt); } /* Output Text */ void MainForm::ClearOutLines () { outputArea->Clear (); } void MainForm::ShowOutLine (std::string txt) { txt = txt; outputArea->AppendText (StdToSys (txt) + "\r\n"); } /* Status Bar */ void MainForm::SetStatus (String ^txt) { statusLabel->Text = txt; } /* Show Error */ void MainForm::ShowError (std::string txt) { CloseInp (); /* uzavri vstup a vystup */ CloseOut (); String ^ s = StdToSys (txt); SetStatus ("Line: " + IntToStr (InpLineNum) + " " + "Col: " + IntToStr (InpColNum) + " " + "Error: " + s); inputArea->SelectionStart = InpPos; /* pozice kurzoru ve vstupnim okenku */ inputArea->SelectionLength = 1; MessageBox::Show ( s, "Error", MessageBoxButtons::OK, MessageBoxIcon::Error ); throw gcnew CompilerException (s); /* skok zpet do funkce Compile */ } /* Compile */ void MainForm::Compile () { try { OpenInp (); /* otevri vstupni a vystupni text */ OpenOut (); InitLex (); /* inicializace modulu Lex, precteni prvniho symbolu */ /* init_labels (); while (sy!=EOS) { statement (); } */ #if 0 while (sy!=EOS) { PutCurrentSymbol (); /* odesli text do vystupniho okna */ PutChr (' '); // PutEol (); NextSymbol (); /* precti dalsi symbol */ } #endif example (); CloseInp (); /* uzavri vstupni a vystupni text */ CloseOut (); SetStatus ("O.K."); /* stavova radka */ outputArea->SelectionStart = 0; /* zobraz zacatek textu */ outputArea->SelectionLength = 1; outputArea->ScrollToCaret (); } catch (CompilerException ^ e) { /* nic - informace o chybe jiz zobrazila funkce ShowError */ } } /* Read File */ void MainForm::Read (String ^ file_name) { try { StreamReader ^ f = gcnew StreamReader (file_name); inputArea->Text = f->ReadToEnd (); f->Close (); } catch (FileNotFoundException ^ e) { SetStatus (e->Message); } inputArea->DeselectAll (); } /* Open File */ void MainForm::openMenuItem_Click (System::Object^ sender, System::EventArgs^ e) { if (openFileDialog->ShowDialog () == System::Windows::Forms::DialogResult::OK) { StreamReader ^ f = gcnew StreamReader (openFileDialog->FileName); inputArea->Text = f->ReadToEnd (); f->Close (); inputArea->DeselectAll (); } } /* Save File */ void MainForm::saveMenuItem_Click (System::Object^ sender, System::EventArgs^ e) { if (saveFileDialog->ShowDialog () == Forms::DialogResult::OK) { StreamWriter ^ f = gcnew StreamWriter (saveFileDialog->FileName); f->Write (inputArea->Text); f->Close (); } } /* Change Font */ void MainForm::fontMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { fontDialog->Font = inputArea->Font; if (fontDialog->ShowDialog () == Forms::DialogResult::OK) { inputArea->Font = fontDialog->Font; outputArea->Font = fontDialog->Font; } } } // end of namespace