Run Prolog Program
-
Comments:
- here.
In the continuing effort to do ‘productive’ tasks, but not actually the project I am supposed to be working on, I present a TextMate command to run the current prolog file, with nice HTML output, and input via a dialog box.
1 #! /usr/bin/env ruby
2
3 require ENV["TM_SUPPORT_PATH"] + "/lib/tm/executor"
4
5 command = [ENV["TM_PROLOG"] || "swipl", "-s", ENV["TM_FILEPATH"]]
6 two_line = false
7
8 welcome = /^(Welcome to SWI-Prolog)|(Copyright )|(SWI-Prolog comes with)|(and you are welcome)|(Please visit)|(For help, use)/
9
10 TextMate::Executor.run(command) do |str, type|
11 if type == :err
12 if two_line
13 two_line = false
14 # this line is part of the previous message
15 "#{str}</div>"
16 # Is this a warning line?
17 elsif str =~ /(Warning):\s(.*):(\d+):/
18 warn, file, line = $1, $2, $3
19 filename = file.split('/')[-1]
20 two_line = true
21 file_link = "<a class=\"near\" href=\"txmt://open?line=#{line}&url=file://#{file}\">#{filename}</a>"
22 "<div class=\"#{warn}\">#{warn}: #{file_link}, line #{line}:"
23 elsif str =~ /(ERROR):\s(.*):(\d+):(\d+):\s(.*)/
24 file, line, char, message = $2, $3, $4, $5
25 filename = file.split('/')[-1]
26 file_link = "<a class=\"near\" href=\"txmt://open?line=#{line}&column=#{char}&url=file://#{file}\">#{filename}</a>"
27 "<div class=\"err\">ERROR: #{file_link}, line #{line}, col #{char}: #{message}</div>"
28 elsif str =~ /ERROR:\s(.*)/
29 message = $1
30 "<div class=\"test fail\">ERROR: #{message}</div>"
31 elsif str =~ /%\s(.*)\scompiled\s(.*)\ssec,\s(.*)\sbytes/
32 file, time, length = $1, $2, $3
33 filename = file.split('/')[-1]
34 file_link = "<a class=\"near\" href=\"txmt://open?url=file://#{file}\">#{filename}</a>"
35 "<div class=\"test ok\"> #{file_link} (#{length} bytes) compiled in #{time} sec.</div>"
36 elsif str =~ welcome
37 "<span class=\"copyright\" style=\"font-size:xx-small;\">#{str}</span> "
38 else
39 "<div class=\"output\">#{str}</div>"
40 end
41 else
42 "<div class=\"output\">#{str}</div>"
43 end
44 end