-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell
executable file
·245 lines (194 loc) · 5.58 KB
/
shell
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python
import sys
import re
if sys.version_info[0] >= 3:
raw_input = input
indentStack = []
beginBlock = False
blockBuffer = ''
blockVar = ''
prompt = '>>> '
globalAR = {}
def execute():
global blockBuffer
f = open('temp.pir', 'w')
f.write("%s"%".sub main\n" + blockBuffer + "\n.end")
f.close()
import subprocess
print(subprocess.getoutput('parrot temp.pir'))
blockBuffer = ''
reserved = {
'for': 'FOR',
'range': 'RANGE',
'in': 'IN',
'while': 'WHILE',
'print': 'PRINT'
}
tokens = ['ID', 'NUMBER', 'STRING'] + list(reserved.values())
literals = ['(', ')', ',', ':', '>', '<', '=', '!']
t_ignore = " \t"
def t_ID(t):
r'[a-zA-Z_][a-zA-Z0-9_]*'
t.type = reserved.get(t.value, 'ID')
return t
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t
def t_STRING(t):
r'\'.*\'|\".*\"|\'\'\'.*\'\'\''
return t
def t_error(t):
print("Illegal character '%s'" % t)
t.lexer.skip(1)
import ply.lex as lex
lex.lex()
def p_statement(p):
'''
statement : print_stmt
| assign_stmt
| while_stmt
| for_stmt
'''
def p_expression_print_stmt(p):
'''
print_stmt : PRINT "(" ID ")"
| PRINT "(" STRING ")"
| PRINT "(" NUMBER ")"
'''
global blockBuffer, globalAR
blockBuffer += 'print ' + str(globalAR.get(p[3], p[3])) +'\n'
if prompt == '>>> ':
execute()
def p_expression_assign_stmt(p):
'''
assign_stmt : ID "=" NUMBER
| ID "=" ID
| ID "=" STRING
'''
global blockBuffer
blockBuffer += p[1] + "=" + str(p[3]) + "\n"
if prompt == '>>> ':
global globalAR
globalAR[p[1]] = globalAR.get(p[3], p[3])
#FIXME: Should we call execute() here
blockBuffer = ''
def p_expression_while_stmt(p):
'''
while_stmt : WHILE condition ":"
'''
global beginBlock, prompt
beginBlock = True
prompt = '... '
global blockBuffer
blockBuffer += "loop:\n"
def p_expression_for_stmt(p):
'''
for_stmt : FOR ID IN RANGE "(" NUMBER "," NUMBER ")" ":"
| FOR ID IN RANGE "(" NUMBER ")" ":"
'''
global beginBlock, prompt
beginBlock = True
prompt = '... '
global blockBuffer
global blockVar
if(len(p) <= 9):
blockBuffer += ".local int " + p[2] + "\n" + p[2] + " = 0 " + "\n"
maximum = p[6]
else :
blockBuffer += ".local int " + p[2] + "\n" + p[2] + " = " + str(p[6]) + "\n"
maximum = p[8]
blockVar = p[2]
blockBuffer += "loop:\n"
blockBuffer += "if " + p[2] + " >= " + str(maximum) + " goto out\n"
def p_expression_condition(p):
'''
condition : ID opr ID
| ID opr NUMBER
| NUMBER opr NUMBER
'''
global blockBuffer
blockBuffer += "if "
if p[2] == '>':
p[2] = '<'
else:
p[2] = '>'
blockBuffer += str(p[1]) + str(p[2]) + str(p[3]) + '\n'
def p_expression_opr(p) :
'''
opr : "<"
| ">"
| "<" "="
| ">" "="
| "=" "="
| "!" "="
'''
def p_error(p):
if p:
print("Syntax error at '%s'" % p.value)
else:
print("Syntax error at EOF")
def check_indent(indent):
global beginBlock, indentStack, prompt
global blockVar, blockBuffer
if beginBlock:
beginBlock = False
if not indent:
print("Expecting a new block")
return False
#if tabs were used for parent block...the same should be used for the child
#TODO: looks stupid, improve
elif indentStack == []:
indentStack.append(indent)
elif indent[0] != indentStack[-1][0]:
print("Mixing of spaces and tabs for indentation not allowed")
return False
#the characters are the same, check for length
elif len(indent) <= len(indentStack[-1]):
print("Expecting a new block")
return False
else:
indentStack.append(indent)
else:
if not indent and not indentStack:
return True
elif not indent:
#break out of nested loops on getting a newline
indentStack = []
prompt = '>>> '
blockBuffer += 'print "\\n"\ninc ' + blockVar + "\ngoto loop\n" + "out:\n"+"end"
execute()
return True
if indent and not indentStack:
print("Wrong indentation")
return False
elif len(indent) > len(indentStack[-1]):
print("Not expecting a new block")
return False
elif len(indent) < len(indentStack[-1]):
if len(indentStack) == 1:
prompt = '>>> '
if len(indentStack) > 1 and indent != indentStack[-2]:
print("Improper end of block")
return False
else:
indentStack.pop()
return True
import ply.yacc as yacc
yacc.yacc()
print('''Test Prompt''')
while True:
line = raw_input(prompt)
#temp = re.sub(r'( +|\t+)(\w.*)', lambda m: m.group(1), line).split('\n')
m = re.match(r'^( +|\t+)', line)
indent = ''
if m:
indent = line[ :m.span()[-1]]
line = line[m.span()[-1]:]
if check_indent(indent):
if line == 'die':
break
if line:
yacc.parse(line)
else:
prompt = '>>> '