summaryrefslogtreecommitdiffstats
path: root/src/PythonIDE/src/esotericFORTRANIDE.py
blob: e132d9e0557a69eb2a8e5468d6b5eb981f567f0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as filedialogue
from tkinter import messagebox
import datetime
import subprocess
import fortranText
import resultsPane
import shutil
import sys
import os

class Application(tk.Tk):

    def __init__(self, program_jar, current_file = "unsaved program", *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.title("esotericFORTRAN IDE - %s" % current_file)
        self.program_jar = program_jar
        self.current_file = current_file

        # add widgets
        self.mainpain = ttk.PanedWindow(self, orient = tk.HORIZONTAL)
        self.mainpain.pack(fill = tk.BOTH, expand = True, side = tk.TOP)

        self.fortran_frame = fortranText.FortranText(self)
        self.mainpain.add(self.fortran_frame)

        self.results_pane = resultsPane.ResultsPane(self)
        self.mainpain.add(self.results_pane)

        # setup menubar
        self.menu = ApplicationMenu(self)
        self.config(menu = self.menu)

        # set up bindings etc
        self.bind('<Control-n>', lambda a: self.new_file())
        self.bind('<Control-o>', lambda a: self.open_file())
        self.bind('<Control-s>', lambda a: self.save_file())
        self.bind('<Control-S>', lambda a: self.save_file_as())
        self.bind('<F4>', lambda a: self.results_pane.clear_results())
        self.bind('<F5>', lambda a: self.execute())
        self.protocol("WM_DELETE_WINDOW", self.exit)

    def new_file(self):
        self.fortran_frame.clear()
        self.save_file_as()

    def open_file(self):
        dia = filedialogue.askopenfilename(
            initialdir = self.__get_initial_dir(),
            filetypes = (("FORTRAN Files", ".ft"), ("Text Files", ".txt"), ("All files", "*.*"))
        )
        if os.path.exists(dia):
            self.current_file = dia
            self.title("esotericFORTRAN IDE - %s" % str(dia))

            with open(dia, "r") as f:
                self.fortran_frame.replace_text_with("".join(f.readlines()))

    def save_file(self):
        if self.current_file == "unsaved program":
            self.save_file_as()
        else:
            with open(self.current_file, "w") as f:
                f.write(self.fortran_frame.get_text())

    def save_file_as(self):
        with filedialogue.asksaveasfile(
            defaultextension = ".ft",
            initialdir = self.__get_initial_dir(),
            filetypes = (("FORTRAN Files", ".ft"), ("Text Files", ".txt"), ("All files", "*.*"))
        ) as f:
            f.write(self.fortran_frame.get_text())
            self.current_file = f.name
            self.title("esotericFORTRAN IDE - %s" % str(f.name))

    def exit(self):
        print("exit")
        exit()

    def execute(self):
        if self.current_file == "unsaved program":
            messagebox.showwarning("Error",  "You need to make a file before it can be executed")
            return

        with open(self.current_file, "r") as f:
            unsaved_version = "".join(f.readlines())
        
        if unsaved_version != self.fortran_frame.get_text():
            if messagebox.askyesno("Save?", "You need to save before executing. Save now?"):
                self.save_file()
            else:
                return

        if os.path.exists("build"):
            shutil.rmtree("build")

        self.results_pane.clear_c_code()
        proc = subprocess.Popen(["java", "-jar", self.program_jar, self.current_file, "-c", "-e"], stdout=subprocess.PIPE)
        while True:
            line = proc.stdout.readline()
            if not line:
                break
            self.results_pane.append_results_line(line.rstrip().decode())

        if os.path.exists("build"):
            self.results_pane.append_results_line("Build Completed %s\n" % str(datetime.datetime.now()))
            for file_ in os.listdir("build"):
                if os.path.splitext(file_)[1] == ".c":
                    c_file_path = os.path.join(os.getcwd(), "build", file_)
                    with open(c_file_path, "r") as f:
                        self.results_pane.write_c_code("".join(f.readlines()))
                    
                    return

        else:
            self.results_pane.append_results_line("Build Failed %s\n" % str(datetime.datetime.now()))

    def __get_initial_dir(self):
        examples_path = os.path.join("..", "examples")
        if not os.path.exists(examples_path):
            return os.path.expanduser("~")
        return examples_path


class ApplicationMenu(tk.Menu):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.parent = parent

        self.file_menu = tk.Menu(self, tearoff = 0)
        self.add_cascade(label = "File", menu = self.file_menu)
        self.file_menu.add_command(
            label = "New FORTRAN file", 
            accelerator = "Ctrl+N",
            command = self.parent.new_file
        )
        self.file_menu.add_command(
            label = "Open FORTRAN file...", 
            accelerator = "Ctrl+O",
            command = self.parent.open_file
        )
        self.file_menu.add_separator()
        self.file_menu.add_command(
            label = "Save", 
            accelerator = "Ctrl+S",
            command = self.parent.save_file
        )
        self.file_menu.add_command(
            label = "Save As...", 
            accelerator = "Ctrl+Shift+S",
            command = self.parent.save_file_as
        )
        self.file_menu.add_separator()
        self.file_menu.add_command(
            label = "Exit", 
            accelerator = "Alt+F4",
            command = self.parent.exit
        )

        self.run_menu = tk.Menu(self, tearoff = 0)
        self.add_cascade(label = "Run", menu = self.run_menu)
        self.run_menu.add_command(
            label = "Clear results",
            command = self.parent.results_pane.clear_results(),
            accelerator = "F4"
        )
        self.run_menu.add_command(
            label = "Execute file", 
            accelerator = "F5",
            command = self.parent.execute
        )

if __name__ == "__main__": 
    try:
        app = Application(sys.argv[1])
        app.mainloop()  
    except IndexError:
        print("You need to specify the path to the .jar as the first argument")