-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterse.py
More file actions
176 lines (174 loc) · 3.79 KB
/
Copy pathterse.py
File metadata and controls
176 lines (174 loc) · 3.79 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
################################
################################
####PLEASE NOTE THAT THIS#######
###########IS NOT THE ##########
########MOST UPDATED ###########
#########VERSION OF THE#########
#######TERSE INTERPRETER########
################################
################################
text = "".join(open(raw_input("Filename? ")))
lines = text.split("\n")
executing = True
class Function:
def __init__(self):
self.text = list()
self.args = list()
def call(self, args):
"""call the function"""
if args:
for arg in args:
locs[self.args[args.index(arg)] = arg
for line in self.text:
execute(line)
def array_remove_element(array, element):
new = list()
for e in array:
if not e == element:
new.append(e)
return new
inFunction = False
fName = str()
fargs = list()
array_remove_element(lines, '')
globs = dict()
locs = dict()
funcs = dict()
subs = dict()
stack = list()
prev = None
def val(word):
if word[0] == "@":
return globs[word] or ""
elif word[0] == "$":
return locs[word] or ""
elif word == "read":
return f.read()
elif word == "in":
return raw_input("")
elif word == "<>":
return "\n"
elif word == "pop":
return stack.pop()
else:
try:
return eval(word)
except Exception:
return word
def decl(args):
#Declare a global variable
globs[args[0].replace("&", "@")] = True
def push(args):
stack.append(prev)
def pop(args):
return stack.pop()
def out(args):
narg = list()
for arg in args:
print str(arg).replace("_", " ").replace("\"", ""),
def add(args):
global prev
prev = sum(map(int, args))
def sub(args):
global prev
prev = args[0] - args[1]
def mult(args):
global prev
prod = 1
for a in args:
prod *= int(a)
prev = prod
def div(args):
global prev
prev = args[0] / args[1]
def mod(args):
global prev
prev = args[0] % args[1]
def string(phrase):
phrasel = list(phrase)
nphrasel = list()
inQuote = False
for l in phrasel:
if l == "\"":
inQuote = not inQuote
if l == " " and inQuote:
nphrasel.append("_")
else:
nphrasel.append(l)
return "".join(nphrasel)
def eq(args):
return args[0] == args[1]
def lt(args):
return args[0] < args[1]
def gt(args):
return args[0] > args[1]
def execute(linet):
global fName
global funcs
line = array_remove_element(linet.split("\t"), "")
command = line[0]
if command in subs:
#Calling a subroutine (takes no arguments)
subs[command].call(None)
return
if command == "push":
push(prev)
return
args = string(line[1]).split(" ")
for arg in args:
args[args.index(arg)] = val(arg)
if command[0] == "@":
#We are defining a global variable
globs[command] = args[0]
return
if command in funcs:
#Calling a function
funcs[command].call(args)
return
if command in ("eq", "lt", "gt"):
global executing
#Conditional statement!
executing = eval(command)(args)
if command == "func":
#define a function
global fName
global inFunction
inFunction = True
global fargs
fName = args.pop(0)
if len(args) > 0:
fargs = [arg.replace("&", "$") for arg in args]
funcs[fName] = Function()
funcs[fName].args = fargs
else:
subs[fName] = Function()
subs[fName].args = fargs
return
eval(command)(args)
for line in lines:
if line == "end":
executing = True
continue
if line and line[0] != ";":
if executing:
#Either no if block, or condition is true
if not inFunction:
try:
execute(line)
except Exception as e:
#report errors with line numbers
print "Error @ line ", lines.index(line) + 1
print e
raise SystemExit()
else:
#adding lines to a function
if line != "end":
#still inside function body
try:
funcs[fName].text.append(line)
except KeyError:
#No function of that name, try it in Subroutines!
subs[fName].text.append(line)
else:
#Line is "end", exit function!
inFunction = False