forked from built/wheeler
/
bigwheel.py
executable file
·107 lines (74 loc) · 2.2 KB
/
bigwheel.py
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
#!/usr/bin/env python
from category import Category
import fileinput
import sys
from interpreter.tools import parse, evaluate, expand_arrows, has_dangling_quote
from common import *
import os
def load_file(filename):
try:
for line in open( os.path.dirname(os.path.abspath(__file__)) + "/%s.w" % filename, "r"):
line = line.strip()
# Comment stripper. Fragile!
line = line[:line.find('#')] if '#' in line else line
# Ignore blank lines
if line:
evaluate( parse( expand_arrows(line), ROOT), ROOT )
except:
print "Can't load that file for some damned reason or another."
def load_prelude():
for line in open( os.path.dirname(os.path.abspath(__file__)) + "/prelude.w", "r"):
line = line.strip()
# Comment stripper. Fragile!
line = line[:line.find('#')] if '#' in line else line
# Ignore blank lines
if line:
evaluate( parse(line, ROOT), ROOT )
exiting = False
print "%s\nType 'exit', 'quit', or 'q' to quit." % VERSION
exit_commands = ('exit', 'quit', 'q')
clear_command = ('clear',)
help_command = ('help',)
help_text = """Commands:
'exit', 'quit', or 'q' to end session
'clear' to clear screen"""
load_prelude()
def next_line_of_input():
line = raw_input(">> ") + "\n"
if not has_dangling_quote(line): return line
# Otherwise keep accumulating the line until we get balanced quotes.
accumulating = True
while(accumulating):
nextline = raw_input() + "\n"
line += nextline
accumulating = not has_dangling_quote(nextline) # stop
return line
while not exiting:
line = next_line_of_input()
command = line.lower().strip() # The line may be a REPL command.
if command in exit_commands:
exiting = True
break
if command in clear_command:
import os
os.system('clear' if sys.platform != 'win32' else 'cls')
continue
if command in ('help',):
print help_text
continue
if command in ('reset',):
ROOT = Category("*")
ROOT.add(ROOT)
load_prelude()
print "* was reset"
continue
if command.split() and command.split()[0] == "load":
args = command.split()
if len(args) != 2:
print "This didn't make sense for loading: ", args
else:
load_file(args[1])
continue
# Ignore blank lines
if line:
evaluate( parse( expand_arrows(line), ROOT), ROOT )