Skip to content

Commit a9310f6

Browse files
committed
edits in bobc, parser returns none, really?
1 parent 5ac8282 commit a9310f6

File tree

4 files changed

+58
-94
lines changed

4 files changed

+58
-94
lines changed

Diff for: bobc.py

+10-42
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,27 @@
44
#* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
55
# File Name : bobc.py
66
# Creation Date : 29-03-2012
7-
# Last Modified : Thu 10 May 2012 08:24:51 PM EEST
7+
# Last Modified : Fri 11 May 2012 11:32:47 AM EEST
88
#_._._._._._._._._._._._._._._._._._._._._.*/
99
from lexer import lexer
1010
from parser import parser
1111
from prules import perrors
1212
from tokrules import terrors
13-
import readline
14-
from sys import argv,stderr
15-
from tree import node
16-
from getopt import gnu_getopt
17-
from string import replace as sreplace
18-
19-
def find_column(inp,token):
20-
last_cr = inp.rfind('\n',0,token.lexpos)
21-
if last_cr < 0:
22-
last_cr = 0
23-
column = (token.lexpos - last_cr) + 1
24-
return column
2513

26-
def get_error_line_with_color(inp,token):
27-
last_cr = inp.rfind('\n',0,token.lexpos)
28-
next_cr = inp.find('\n',token.lexpos)
29-
if last_cr < 0:
30-
last_cr = 0
31-
line = inp[last_cr+1:next_cr]
32-
for i in range(len(line)):
33-
if ord(line[i]) == 9:
34-
continue
35-
else:
36-
break
37-
line = sreplace(line[i:],token.value,'\033[4;32m'+token.value+'\033[0m')
38-
return line
14+
from sys import argv
15+
from getopt import gnu_getopt
3916

40-
def print_errors(merrors,data,filename='stdin'):
41-
'''prints the errors returned by the parser'''
42-
if merrors:
43-
for p in merrors:
44-
text = get_error_line_with_color(data,p)
45-
pline = p.lineno
46-
pcolumn = find_column(data,p)
47-
stderr.write("{0}:{1}:{2} syntax error: '{3}'\n".format(filename,pline,pcolumn,text,p))
48-
exit(-1)
49-
return False
50-
else:
51-
return True
17+
from error_handler import find_column, get_error_line_with_color, print_errors
5218

53-
54-
5519
def main():
5620
if len(argv) > 1:
5721
#get command line arguements
58-
switches,args = gnu_getopt(argv[1:],':o:')
59-
switches = dict(switches)
22+
switches = {'-o':'a.xml'}
23+
setswitches,args = gnu_getopt(argv[1:],':o:')
24+
setswitches = dict(setswitches)
25+
26+
for i in setswitches.keys():
27+
switches[i] = setswitches[i]
6028

6129
filename = args[0]
6230

Diff for: parser.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,11 @@
44
#* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
55
# File Name : parser.py
66
# Creation Date : 21-03-2012
7-
# Last Modified : Thu 10 May 2012 07:32:48 PM EEST
7+
# Last Modified : Fri 11 May 2012 11:22:37 AM EEST
88
#_._._._._._._._._._._._._._._._._._._._._.*/
99

10-
# Build the lexer
1110
from tokrules import tokens
1211
from prules import *
1312
import ply.yacc as yacc
1413

15-
#def p_error(p):
16-
# print("Syntax error at '%s'" % p.value)
17-
1814
parser = yacc.yacc()
19-
20-
if __name__ == "__main__":
21-
main()
22-

Diff for: prules.py

+38-37
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
#!/usr/bin/env python
2-
# -*- coding: utf-8
1+
#!/usr/bin/env python # -*- coding: utf-8
32
#
43
#* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
54
# File Name : parserules.py
65
# Creation Date : 02-04-2012
7-
# Last Modified : Fri 11 May 2012 01:23:07 AM EEST
6+
# Last Modified : Fri 11 May 2012 11:57:49 AM EEST
87
#_._._._._._._._._._._._._._._._._._._._._.*/
98

109
from tokrules import *
@@ -27,25 +26,14 @@
2726
('left','+','-'),
2827
('left','*','/','%'),
2928
('nonassoc','UNAR','Size'),
30-
('nonassoc','[',']','(',')')
29+
('right','[',']','(',')')
3130
)
3231

3332
start = 'program'
3433

3534

3635
def gen_p_out(ptype,p,symbol=None):
37-
#r = 'nili'
38-
#try:
39-
# for i in p:
40-
# if type(i) == str:
41-
# try:
42-
# r.add(node(i,{'name':i}))
43-
# except AttributeError:
44-
# r = node(p[0],{'name':p[0]})
45-
# print i,r
46-
#except TypeError:
47-
# return None
48-
36+
print p
4937
children = []
5038
for i in p[1:]:
5139
children.append(i)
@@ -84,10 +72,9 @@ def : var_def ';'
8472
def p_type(p):
8573
'''
8674
type : simple_type
87-
| simple_type '[' ']'
75+
| simple_type '[' ']'
8876
'''
8977
p[0] = gen_p_out('type',p)
90-
#pass
9178

9279

9380

@@ -96,71 +83,83 @@ def p_ret_type(p):
9683
ret_type : Void
9784
| type
9885
'''
99-
pass
86+
p[0] = gen_p_out('ret_type',p)
10087

10188
def p_simple_type(p):
10289
'''
10390
simple_type : Bool
10491
| Int
10592
| Char
10693
'''
107-
pass
94+
p[0] = gen_p_out('ret_type',p)
10895

10996
def p_var_def(p):
11097
'''
11198
var_def : type Id
11299
| type Id ASSIGN expr
113100
'''
114-
pass
101+
p[0] = gen_p_out('Generic',p)
115102

116103
def p_func_def(p):
117104
'''
118105
func_def : ret_type Id '(' ')' block
119106
| ret_type Id '(' formal_params ')' block
120107
'''
121-
pass
108+
p[0] = gen_p_out('Generic',p)
122109

123110
def p_formal_params(p):
124111
'''
125112
formal_params : type Id rep_formal_params
126113
'''
127-
pass
114+
p[0] = gen_p_out('formal_params',p)
128115

129-
def p_rep_formal_params(p):
116+
117+
def p_rep_formal_params_empty(p):
130118
'''
131119
rep_formal_params : empty
132-
| ',' type Id rep_formal_params
133120
'''
134121
pass
135122

123+
def p_rep_formal_params(p):
124+
'''
125+
rep_formal_params : ',' type Id rep_formal_params
126+
'''
127+
p[0] = gen_p_out('formal_params',p)
128+
129+
136130
def p_actual_params(p):
137131
'''
138132
actual_params : expr rep_actual_params
139133
'''
140-
pass
134+
p[0] = gen_p_out('Generic',p)
141135

142-
def p_rep_actual_params(p):
136+
def p_rep_actual_params_empty(p):
143137
'''
144138
rep_actual_params : empty
145-
| ',' expr rep_actual_params
146139
'''
147140
pass
148141

142+
def p_rep_actual_params(p):
143+
'''
144+
rep_actual_params : ',' expr rep_actual_params
145+
'''
146+
p[0] = gen_p_out('params',p)
147+
149148

150149
def p_block(p):
151150
'''
152151
block : '{' '}'
153152
| '{' stmt_list '}'
154153
'''
155-
pass
154+
p[0] = gen_p_out('Generic',p)
156155

157156

158157
def p_stmtm_list(p):
159158
'''
160159
stmt_list : stmt ';'
161160
| stmt ';' stmt_list
162161
'''
163-
pass
162+
p[0] = gen_p_out('Generic',p)
164163

165164
def p_stmt(p):
166165
'''
@@ -169,12 +168,13 @@ def p_stmt(p):
169168
| If '(' expr ')' stmt Else stmt
170169
| For '(' var_def ';' expr ';' stmt ')' stmt
171170
| While '(' expr ')' stmt
172-
| expr
171+
| expr ';'
173172
| block
174-
| Delete Id
175-
| Ret expr
173+
| Delete expr ';'
174+
| Ret expr ';'
175+
| Ret ';'
176176
'''
177-
pass
177+
p[0] = gen_p_out('stmt',p)
178178

179179

180180
def p_expr_atom(p):
@@ -191,6 +191,7 @@ def p_expr_atom(p):
191191
| Id '(' actual_params ')'
192192
| Id ASSIGN expr
193193
'''
194+
p[0] = gen_p_out('expr_atom',p)
194195

195196
def p_expr(p):
196197
'''
@@ -211,7 +212,7 @@ def p_expr(p):
211212
| expr '^' expr
212213
213214
'''
214-
pass
215+
p[0] = gen_p_out('Generic',p)
215216

216217

217218
def p_un_op(p):
@@ -220,13 +221,13 @@ def p_un_op(p):
220221
| '-' expr %prec UNAR
221222
| '+' expr %prec UNAR
222223
'''
223-
pass
224+
p[0] = gen_p_out('Generic',p)
224225

225226
def p_lval(p):
226227
'''
227228
lval : Id
228229
| Id '[' expr ']'
229230
'''
230-
pass
231+
p[0] = gen_p_out('Generic',p)
231232

232233

Diff for: sample/helloworld.bob

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
print("hello world\n");
2-
print("hello world\n");
3-
print("hello world\n");
4-
print("hello world\n");
5-
print("hello world\n");
6-
print("hello world\n");
1+
void main()
2+
{
3+
print("hello world\n");
4+
print("hello world\n");
5+
print("hello world\n");
6+
print("hello world\n");
7+
print("hello world\n");
8+
print("hello world\n");
9+
}

0 commit comments

Comments
 (0)