[Tool/Web/Other] (Prova de Conceito) Compilador Dinвmico - C#
#1

Compilador Run Time


Vн a dъvida de um carinha aн no fуrum sobre como compilar um cуdigo em tempo real. Pensando nisto fiz um cуdigo em C# que faz isto facilmente - estou aprendendo C#

Ideia
http://forum.sa-mp.com/showpost.php?...71&postcount=8

Cуdigo Fonte:

- Todo explicado para quem quer desenvolver um editor.

pawn Код:
using System;
using System.IO;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.ComponentModel;




namespace WindowsFormsApplication5 {
    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();

        }

        private void textBox1_Click(object sender, EventArgs e) {

            // caminho do arquivo temporario para compilar
            string arquivoTemporario = "c:/arquivoCompilado.txt";

            // escrever a edicao no arquivo
            File.WriteAllText(arquivoTemporario, textBox1.Text);

            Process p = new System.Diagnostics.Process();

            // diretorio atual
            p.StartInfo.WorkingDirectory =  Directory.GetCurrentDirectory();

            // iniciar minimizado
            //p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            // aplicativo a abrir
            p.StartInfo.FileName = "pawncc.exe";

            // comando abrir
            p.StartInfo.Verb = "open";

            // colocar argumentos -c
            p.StartInfo.Arguments = arquivoTemporario;

            // redirecionar a saida do aplicativo
            p.StartInfo.RedirectStandardOutput = true;
           
            p.StartInfo.UseShellExecute = false;
           
            // iniciar
            p.Start();

            // esperar ele finalizar
            p.WaitForExit();

            // pegar o retorno dado por pawncc
            StreamReader bufferOut = p.StandardOutput;

            // verificar se encontrou erro  
            textBox2.Text = bufferOut.ReadToEnd();

            if (textBox2.Text.IndexOf("Error") != -1 || textBox2.Text.IndexOf("rrror") != -1)
            {
                textBox2.Text = "Foi encontrado um erro na ъltima linha editada";
            }
            else
            {
                textBox2.Text = "Foi compilado com sucesso";
            }

        }

        private void teclaPressionada(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar ==  (Char)Keys.Return )
            {
                string arquivoTemporario = "c:/arquivoCompilado.txt";

                // escrever a edicao no arquivo
                File.WriteAllText(arquivoTemporario, textBox1.Text);

                Process p = new System.Diagnostics.Process();

                // diretorio atual
                p.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory();

                // iniciar minimizado
                //p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                // aplicativo a abrir
                p.StartInfo.FileName = "pawncc.exe";

                // comando abrir
                p.StartInfo.Verb = "open";

                // colocar argumentos -c
                p.StartInfo.Arguments = arquivoTemporario;

                // redirecionar a saida do aplicativo
                p.StartInfo.RedirectStandardOutput = true;

                p.StartInfo.UseShellExecute = false;

                // iniciar
                p.Start();

                // esperar ele finalizar
                p.WaitForExit();

                // pegar o retorno dado por pawncc
                StreamReader bufferOut = p.StandardOutput;

                // verificar se encontrou erro  
                textBox2.Text = bufferOut.ReadToEnd();

                if (textBox2.Text.IndexOf("Error") != -1 || textBox2.Text.IndexOf("rrror") != -1)
                {
                    textBox2.Text = "Foi encontrado um erro na ъltima linha editada";
                }
                else
                {
                    textBox2.Text = "Foi compilado com sucesso";
                }
            }
        }
    }
}
Demonstraзгo:
http://www.solidfiles.com/d/23e140b3ea/

_________

Lуgica usada:

A cada ENTER ou CLIQUE no texto ele compila com aplicativo e mostra se deu erro na ъltima linha editada. Lб vocк tem a opзгo de compilar em modo oculto ou nгo. Deixei bem bбsico, fiz em poucos minutos. Й a segunda vez que mexo com C#, entгo se tiver alguma coisa falhada com relaзгo ao cуdigo basta me avisar

- Agora quero ver o pessoal fazer um IDE com isto, hein
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)