-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimhead.rb
More file actions
248 lines (242 loc) · 6.95 KB
/
simhead.rb
File metadata and controls
248 lines (242 loc) · 6.95 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
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
244
245
246
247
248
#using all_blocks
require './condhead.rb'
###############################################################################
def extract2(i, lispfun)
j=i
count=1
cmax=1
#print lispfun[j]
while count != 0 do
#print lispfun[j], " ", count, "\n"
if lispfun[j] == '('
count=count+1
end
if count>cmax
cmax=count
end
if lispfun[j] == ')'
count=count-1
end
j=j+1
end
return j, cmax
end
###############################################################################
def squash(a)
if a.class == [].class && a.length() > 2
#print "\n Too big to squash directly \n"
return "#{a[0]} " + "#{squash(a[1..-1])}"
end
if a[0] == "cdr"
return "#{a[1]}[1..-1]"
end
if a[0] == "car"
return "#{a[1]}[0]"
end
if a == "null"
return "nil"
end
return a
end
###############################################################################
def squash2(a)
#print "\n``````````` #{a} `````````\n"
if a[0] == "car"
return "#{a[1]}[0]"
end
if a[0] == "cdr"
return "#{a[1]}[1..-1]"
end
if a[1] == "cdr"
return "#{a[0]}(#{a[2]}[1..-1])"
end
if a[1] == "car"
return "#{a[0]}(#{a[2]}[0])"
end
if a[0] == "list"
a=a[1..-1]
i=0
gg=""
#while i < a.length
# g = squash2(a[i])
# gg="#{gg}.concat ( [#{g}] )"
# i=i+1
#end
#gg=gg[3..-1]
k1=squash2(a[0])
k2=squash2(a[1])
gg = "[#{k1}].concat([#{k2}])"
return gg
end
if a == "null"
return "nil"
end
#return "#{a[0]} #{a[1]}"
return a
end
###############################################################################
def l_of(a)
i=0
b=0
while i < a.length
b=b+a[i].length
i=i+1
#print "\n \t b #{b}"
end
return b
end
###############################################################################
def simplify(bb, ab)
i=1
opr=[]
count=0
ptr=1
iend=ab.length()
while i < iend
#print "\n #{i} \t #{ab[i]}"
if ab[i] == '('
j = extract2(i+1, ab)
gg = ab[i,j[0]-i]
#print "\n gg \t #{gg}"
pas = gg.split(/\W([><+-^\*\,\.\s]*)/)
pas = pas.reject { |c| c.empty? }
pas = pas.reject { |c| c==" "}
#pas = squash(pas)
opr.push(pas)
i=j[0]
count=count+pas.length
ptr=ptr + j[1]
elsif ab[i] == ' '
i=i+1
next
elsif ab[i] == ')'
i=i+1
next
else
#pas = squash(bb[0][count])
pas=bb[0][count]
opr.push(pas)
i=i+bb[0][count].length
count=count+1
end
#print "\n printing opr #{i} \t"
#print opr
end
opr=squash2(opr)
return opr
end
###############################################################################
def get_if_blk(build_blocks, all_blocks)
cc=0
block_len=build_blocks[0].length()
condition_len=build_blocks[1].length()
#print "`````````\n",condition_len,"\n````````````"
condition_check=build_blocks[2].length()
if build_blocks[1][0] != "* " && build_blocks[1][0] != "> " && build_blocks[1][0] != "+ " && build_blocks[1][0] != "- " && build_blocks[1][0] != "* "
condition_it="=="
lag=0
else
condition_it = build_blocks[1][0]
lag=1
end
if condition_len - condition_check == 1+lag
condition_lh = build_blocks[1][0+lag]
condition_lh_ab = all_blocks[1][0+lag]
condition_lh_len=1
condition_rh = build_blocks[2]
condition_rh_ab = all_blocks[2]
condition_rh_len=condition_rh.length()
opr_if=3+condition_rh_len-2
opr_else=4+condition_rh_len-2
else
condition_lh = build_blocks[2]
condition_lh_ab = all_blocks[2]
condition_lh_len=condition_lh.length()
condition_rh = build_blocks[3]
condition_rh_ab = all_blocks[3]
condition_rh_len=condition_rh.length()
opr_if=4+condition_rh_len-2
opr_else=5+condition_rh_len-2
end
return_if_bl = build_blocks[opr_if..-1]
return_else_bl = build_blocks[opr_else..-1]
#print "\n\t~~",condition_lh_len , condition_rh_len , return_if_bl.length(), "~~", block_len, "\n"
if condition_lh_len + condition_rh_len + return_if_bl[0].length() == block_len-1
#lack of else block
if return_if_bl[0][0] == "if"
return_if_bl=get_if_blk(build_blocks[opr_if..-1], all_blocks[opr_if..-1])
end
#condition_lh = squash2(condition_lh)
#condition_rh = squash2(condition_rh)
condition_lh = beautify(condition_lh, condition_lh_ab)
condition_rh = beautify(condition_rh, condition_rh_ab)
#return_if_bl = simplify(return_if_bl, all_blocks[opr_if])
return_if_bl = beautify(return_if_bl[0], all_blocks[opr_if])
#print "if #{condition_lh} #{condition_it} #{condition_rh} \n \t #{return_if_bl} \n end \n"
cc = cc + opr_if + 1
return "if #{condition_lh} #{condition_it} #{condition_rh} \n \t return #{return_if_bl} \n end \n", cc
else
#else block is present
flag_if_crunch=false
if return_if_bl[0][0] == "if"
return_if_bl = get_if_blk(build_blocks[opr_if..-1], all_blocks[opr_if..-1])
return_if_bl=return_if_bl[0]
flag_if_crunch=true
end
flag_else_crunch=false
if return_else_bl[0][0] == "if"
return_else_bl = get_if_blk(build_blocks[opr_else..-1], all_blocks[opr_else..-1])
cc = cc + return_else_bl[1]
return_else_bl=return_else_bl[0]
flag_else_crunch=true
end
#condition_lh = squash2(condition_lh)
#condition_rh = squash2(condition_rh)
condition_lh = beautify(condition_lh, condition_lh_ab)
condition_rh = beautify(condition_rh, condition_rh_ab)
if flag_if_crunch == false
#return_if_bl = simplify(return_if_bl, all_blocks[opr_if])
return_if_bl = beautify(return_if_bl[0], all_blocks[opr_if])
else
return_if_bl = squash2(return_if_bl)
#return_if_bl = beautify(return_if_bl, all_blocks[opr_if])
end
if flag_else_crunch == false
#return_else_bl = simplify(return_else_bl, all_blocks[opr_else])
return_else_bl = beautify(return_else_bl[0], all_blocks[opr_else])
else
return_else_bl = squash2(return_else_bl)
#return_else_bl = beautify(return_else_bl)
end
cc = cc + opr_else + 1
if condition_rh == "x[1..-1]" and condition_lh == "nil"
condition_lh = "[]"
end
return "if #{condition_lh} #{condition_it} #{condition_rh} \n \t return #{return_if_bl} \n else \n \t #{return_else_bl} \n end \n", cc
end
end
###############################################################################
def consume(build_blocks, all_blocks)
#print "\n in consume \n"
krieg=[]
i=0
kk=[]
#flag=false
while i<build_blocks.length
kk[1]=1
if build_blocks[i][0] == "if"
kk = get_if_blk(build_blocks, all_blocks)
#print "\n #{kk} \n #{count}"
krieg.push(kk[0])
end
if build_blocks[i][0] == "cond"
#print "\n in the cond block"
kk = get_cond_block(build_blocks, all_blocks)
krieg.concat kk[0]
#krieg.push("end\n")
end
i=i+kk[1]
end
krieg.push("end\n")
return krieg
end