-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
106 lines (86 loc) · 3.56 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
import sys
from parser import *
from interpreter import *
if __name__ == "__main__":
interpreter = Interpreter()
parser = Parser()
# interpret a list of expressions
def interpretExpressions(listOfExpressions):
for expression in listOfExpressions:
result = interpretExpression(expression)
if result != None:
print result
# function that processes code using parser and interpreter modules
def interpretExpression(code):
# parse the input
tokensSublists = parser.parse(code)
# pass the sublists of tokens (structure mirrors the levels of parens in
# scheme code) to the interpreter
result = interpreter.interpret(tokensSublists)
return parser.convertToParens(result)
# try to open the specified file, and return a string with the contents of the file
# newlines at the end of each line are removed
def getFileContents(filename):
# open file and save the contents into string s
s = str()
try:
with open(filename, 'r') as f:
for line in f:
s = s + line.rstrip() + " "
return s
except IOError as e:
print "Unable to open file: " + filename
sys.exit(1)
except Exception as ex:
print e
sys.exit(1)
if len(sys.argv) > 2:
print "Please specify the scheme file to run"
print "Example usage: python main.py [code.scm]"
sys.exit(1)
# if file was specified to interpret
elif len(sys.argv) == 2:
filename = sys.argv[1]
s = getFileContents(filename)
try:
# build up list of expressions to evaluate from the code in the input file
listOfExpressions = list()
expression = str()
for char in s:
expression += char
if char == "(" or char == ")":
# check if the parens are balanced. if so, current string is a complete expression
if parser.hasBalancedParensWaitForCompletion(expression):
# add current expression to list of expressions to evaluate
listOfExpressions.append(expression)
# reset current expression string
expression = ""
# add this invalid expression to the list to be evaluated so that the error is displayed
# strip trailing and leading whitespace
expression = expression.strip()
if expression != "":
listOfExpressions.append(expression)
interpretExpressions(listOfExpressions)
except ParseError as ex:
print ex
# interactive mode
elif len(sys.argv) == 1:
# prompt user for input until user presses ctrl+c or ctrl+d
while(True):
try:
# display prompt
sys.stdout.write('>> ')
code = ""
# accept user input until the input has balanced parens
while (code == "" or not parser.hasBalancedParensWaitForCompletion(code)):
s = raw_input()
code += s.rstrip() + " "
result = interpretExpression(code)
if result != None:
print result
except EOFError:
sys.exit(0)
except KeyboardInterrupt:
sys.exit(0)
except ParseError as ex:
print ex