This repository has been archived by the owner on Nov 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
rrhine
executable file
·101 lines (87 loc) · 2.53 KB
/
rrhine
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
#!/usr/bin/env python2
from os import walk
from subprocess import Popen, PIPE
import sys
class colors:
HEADER = '\033[95m'
OK = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
END = '\033[0m'
def run():
stdlib = open("stdlib.rh").read()
pasting = False
i = ""
last_llvm = ""
last_io = ""
current_input = ""
total_input = ""
total_output = " "
total_failed_input = ""
while i not in ["quit", "exit", "q"]:
if pasting:
sys.stdout.write(colors.HEADER)
else:
sys.stdout.write(colors.END)
try:
i = raw_input('> ')
i = i.strip()
if pasting:
ci += i + "\n"
continue
else:
ci = i
except KeyboardInterrupt:
if pasting:
pasting = False
sys.stdout.write(colors.HEADER)
print "\nPASTED:"
print ci
else:
continue
except EOFError:
sys.stdout.write(colors.WARNING)
print "\n\nThanks for using Rhine! :)"
exit(1)
if ci == "":
continue
elif ci in ["paste", "p"]:
sys.stdout.write(colors.HEADER)
pasting = True
ci = ""
print "\n\nNow in paste mode..."
continue
elif ci in ["llvm", "ll"]:
print last_llvm
continue
elif ci in ["i", "input"]:
print total_input
continue
elif ci in ["o", "output"]:
print total_output
continue
elif ci in ["last"]:
print last_io
continue
p = Popen(["./rhine", "-"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output = ""
stdout, stderr = p.communicate(stdlib+"\n"+total_input+"\n"+ci)
if stderr.find("error") >= 0:
sys.stdout.write(colors.FAIL)
print stderr.strip()
continue
else:
total_input += "\n"+ci
for line in stdout.split("\n"):
if line.startswith("Evaluated to ") or line.strip() == "":
continue
output += line.strip()+"\n"
from_idx = len(total_output)-1
if from_idx < 0:
from_idx = 0
print output[:-1][from_idx:]
total_output = output
last_io = ci+"\n\n"+output[:-1][len(total_output):]
last_llvm = stderr
if __name__ == "__main__":
run()