Autore Topic: Area di testo per scrittura codice sorgente C  (Letto 400 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.254
  • Ne mors quidem nos iunget
    • Mostra profilo
Area di testo per scrittura codice sorgente C
« il: 28 Gennaio 2017, 03:32:48 »
Questa semplicissima applicazione in ambiente grafico fornisce all'utente un'area di testo per la scrittura e la contestuale compilazione di codice sorgente in linguaggio C.
Il codice compilato viene anche immediatamente eseguito, rendendo così quest'applicazione un vero - seppur semplice - banco di lavoro.

Di seguito il codice Gambas:
Codice: [Seleziona]
Private TextArea1 As TextArea
Private TextBox1 As TextBox
Private TextBox2 As TextBox
Private Iniziale As String = "#define _GNU_SOURCE\n#include <stdio.h>\n#include <stdlib.h>\n" &
                             "#include <unistd.h>\n#include <string.h>" &
                             "\n\n\nint main() {" &
                             "\n\n\t\n\n\n" &
                             "      return (0);\n\n}"


Public Sub Form_Open()

  Dim Frame1 As Frame
  Dim Frame2 As Frame
  Dim Button1 As Button
  Dim Label1 As Label
  Dim mn As Menu
  Dim submn1 As Menu
  Dim submn2 As Menu
  Dim submn3 As Menu
  Dim submn4 As Menu
  Dim submn5 As Menu

  With Me
    .W = Desktop.W * 0.5
    .H = Desktop.Height
    .X = Desktop.W - Me.W
    .Y = 10
  End With
 
  With TextArea1 = New TextArea(Me)
    .X = 10
    .Y = 10
    .W = Me.W - 20
    .H = Me.H * 0.67
    .Text = Iniziale
    .Pos = 116
  End With
 
  With Frame1 = New Frame(Me)
    .X = 10
    .Y = TextArea1.Y + TextArea1.H + 10
    .W = 680
    .H = Me.H * 0.06
    .Text = "Eventuali librerie ed opzioni per la compilazione (es. -lm ):"
  End With
 
  With TextBox1 = New TextBox(Frame1)
    .X = 10
    .Y = 15
    .W = Frame1.W - 20
    .H = Frame1.H * 0.54
  End With
 
  With Frame2 = New Frame(Me)
    .X = 10
    .Y = Frame1.Y + Frame1.H + (Me.H * 0.01)
    .W = 680
    .H = Me.H * 0.06
    .Text = "Eventuali argomenti da passare al programma al suo avvio:"
  End With
 
  With TextBox2 = New TextBox(Frame2)
    .X = 10
    .Y = 15
    .W = Frame2.W - 20
    .H = Frame2.H * 0.54
  End With
 
  With Button1 = New Button(Me) As "Button1"
    .W = Frame1.W / 2
    .H = Me.H * 0.05
    .X = (Me.W / 2) - (Button1.W / 2)
    .Y = Frame2.Y + Frame2.H + (Me.H * 0.02)
    .Text = "Compila il codice e lancia il programma"
  End With
 
  With Label1 = New Label(Me)
    .Font.Size = 7
    .Text = "Il codice sorgente verrà salvato nel file: \"/tmp/sorgente.c\""
    .W = .Font.TextWidth(.Text)
    .X = (Me.W / 2) - (Label1.W / 2)
    .Y = Button1.Y + Button1.H + 5
    .H = 15
  End With
 
  With mn = New Menu(Me, False) As "Menu"
    .Caption = "Opzioni"
  End With
  With submn1 = New Menu(mn, False) As "Submn"
    .Caption = "Pulisci la console/Terminale"
    .Name = "Console"
  End With
  With submn2 = New Menu(mn, False) As "Submn"
    .Caption = "Pulisci l'area di testo"
    .Name = "Pulisci"
  End With
  With submn3 = New Menu(mn, False) As "Submn"
    .Caption = "Codice Iniziale"
    .Name = "Iniziale"
  End With
  With submn4 = New Menu(mn, False) As "Submn"
    .Caption = "Carica un file sorgente .c"
    .Name = "Carica"
  End With
  With submn5 = New Menu(mn, False) As "Submn"
    .Caption = "Elimina file sorgente e compilato creati"
    .Name = "Elimina"
  End With

End


Public Sub Button1_Click()
 
   File.Save("/tmp/sorgente.c", TextArea1.Text)

   Shell "gcc -o /tmp/sorgente /tmp/sorgente.c " & TextBox1.Text Wait
 
   Shell "/tmp/sorgente " & TextBox2.Text

End


Public Sub submn_Click()
 
    Select Case Last.Name
      Case "Console"
        Write "\e[2J"
      Case "Pulisci"
        TextArea1.Clear
      Case "Iniziale"
        TextArea1.Text = Iniziale
        If Exist("/tmp/sorgente.c") And Exist("/tmp/sorgente") Then
          Kill "/tmp/sorgente.c"
          Kill "/tmp/sorgente"
        Endif
      Case "Carica"
        With Dialog
          .Filter = ["*.c", "Codice sorgente C"]
          .Title = "Carica un file sorgente .c"
          .Path = "/tmp"
          If .OpenFile() Then Return
          TextArea1.Text = File.Load(.Path)
        End With
      Case "Elimina"
        If Exist("/tmp/sorgente.c") And Exist("/tmp/sorgente") Then
          Kill "/tmp/sorgente.c"
          Kill "/tmp/sorgente"
        Endif
    End Select
 
End


Public Sub Form_Close()
 
  Shell "killall sorgente"
 
End
« Ultima modifica: 07 Agosto 2018, 13:27:17 da Gianluigi »
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »