-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
181 lines (145 loc) · 6.56 KB
/
main.py
File metadata and controls
181 lines (145 loc) · 6.56 KB
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
181
# -*- coding: utf8 -*-
u"""
Mathics: a general-purpose computer algebra system
Copyright (C) 2011 Jan Pöschko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
import argparse
# Try importing readline to enable arrow keys support etc.
try:
import readline
except ImportError:
print("NOTE: Arrow keys are not supported.")
from mathics.core.definitions import Definitions
from mathics.core.expression import Symbol, Expression
from mathics.core.evaluation import Evaluation
from mathics import settings
from mathics import print_version, print_license, get_version_string
def to_output(text):
return '\n . '.join(text.splitlines())
def out_callback(out):
print to_output(unicode(out))
# Adapted from code at http://mydezigns.wordpress.com/2009/09/22/balanced-brackets-in-python/
def brackets_balanced(input_string):
brackets = [ ('(',')'), ('[',']'), ('{','}')]
kStart, kEnd, stack = 0, 1, []
for char in input_string:
for bracketPair in brackets:
if char == bracketPair[kStart]:
stack.append(char)
elif char == bracketPair[kEnd] and (len(stack) == 0 or stack.pop() != bracketPair[kStart]):
# Brackets are not balanced, but return True so that a parse error can be raised
return True
if len(stack) == 0:
return True
return False
def loadnrun(total_input, definitions, script=False):
stripped_input = total_input.strip()
if stripped_input.endswith(';'):
stripped_input = stripped_input[:-1]
if stripped_input.startswith("<<"):
stripped_input = stripped_input[2:].strip()
if '\n' not in stripped_input and \
not stripped_input.startswith('"'):
with open(stripped_input) as infile:
execute(infile, definitions, script)
#except:
# print "Error:", sys.exc_info()[0]
# print "Problem execute:", stripped_input
return True
def execute(file, definitions, script):
'''load a .m script: <<file.m;'''
total_input = ""
for line in file:
if script and line.startswith('#!'):
continue
if total_input == "":
print '>> ', line,
else:
print ' ', line,
if line.rstrip().endswith('\\'):
endi = line.rfind('\\')
total_input += line[:endi]
continue
total_input += line
if not line: pass
elif any(line.rstrip().endswith(op) for op in trailing_ops) or not brackets_balanced(total_input):
continue
if not loadnrun(total_input, definitions, script):
evaluation = Evaluation(total_input, definitions, timeout=30, out_callback=out_callback)
for result in evaluation.results:
if result.result is not None:
print ' = %s' % to_output(unicode(result.result))
total_input = ""
# TODO all binary operators?
trailing_ops = ['+', '-', '/', '*', '^', '=',
'>', '<', '/;', '/:', '/.', '&&', '||']
def main():
argparser = argparse.ArgumentParser(
prog='mathics',
usage='%(prog)s [options] [FILE]',
add_help=False,
description = "Mathics is a general-purpose computer algebra system.",
epilog = """Please feel encouraged to contribute to Mathics! Create
your own fork, make the desired changes, commit, and make a pull
request.""")
argparser.add_argument('FILE', nargs='?', type=argparse.FileType('r'), help='execute commands from FILE')
argparser.add_argument('--help', '-h', help='show this help message and exit', action='help')
argparser.add_argument('--persist', help='go to interactive shell after evaluating FILE', action='store_true')
argparser.add_argument('--quiet', '-q', help='don\'t print message at startup', action='store_true')
argparser.add_argument('-script', help='run a mathics file in script mode', action='store_true')
argparser.add_argument('--execute', '-e', nargs='?', help='execute a command')
argparser.add_argument('--version', '-v', action='version', version=get_version_string(False))
args = argparser.parse_args()
quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-D'
definitions = Definitions(add_builtin=True)
if args.execute:
print ">> %s" % args.execute
evaluation = Evaluation(args.execute, definitions, timeout=30, out_callback=out_callback)
for result in evaluation.results:
if result.result is not None:
print ' = %s' % to_output(unicode(result.result))
return
if not (args.quiet or args.script):
print_version(is_server=False)
print_license()
print u"Quit by pressing %s" % quit_command
print ''
if args.FILE is not None:
execute(args.FILE, definitions, args.script)
if not args.persist:
return
while True:
try:
total_input = ""
line_input = raw_input('>> ')
while line_input != "":
total_input += ' ' + line_input
if any([line_input.rstrip().endswith(op) for op in trailing_ops]):
pass
elif brackets_balanced(total_input):
break
line_input = raw_input(' ')
if not loadnrun(total_input, definitions):
evaluation = Evaluation(total_input, definitions, timeout=30, out_callback=out_callback)
for result in evaluation.results:
if result.result is not None:
print ' = %s' % to_output(unicode(result.result))
except (KeyboardInterrupt):
print '\nKeyboardInterrupt'
except (SystemExit, EOFError):
print "\n\nGood bye!\n"
break
if __name__ == '__main__':
main()