-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrb2l_header.rb
More file actions
204 lines (200 loc) · 5.62 KB
/
rb2l_header.rb
File metadata and controls
204 lines (200 loc) · 5.62 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
def check_validity(a)
i=0
cnt=0
while i < a.length
if a[i] == '('
cnt=cnt+1
end
if a[i] == ')'
cnt=cnt-1
end
i=i+1
end
if cnt != 0
return false
end
return true
end
###############################################################################
def extract(i, lispfun)
j=i
count=1
#print lispfun[j]
while count != 0 do
#print lispfun[j], " ", count, "\n"
if lispfun[j] == '('
count=count+1
end
if lispfun[j] == ')'
count=count-1
end
j=j+1
end
return j
end
###############################################################################
def write_words(parameters, aFile)
i=0
while i < parameters.length() do
aFile.syswrite(parameters[i])
aFile.syswrite(" ")
i=i+1
end
end
###############################################################################
def superloop(i, lispfun)
a=[]
print "\n\n \t building blocks of the body of the function which will be"
print "\n\t used to construct the ruby function \n\n"
while i < lispfun.length() do
if lispfun[i] == '('
j=extract(i+1,lispfun)
parameters = lispfun[i,j-i+1].split(/\W([><+-^\*\,\.\s]*)/)
parameters = parameters.reject { |c| c.empty? }
parameters = parameters.reject { |c| c==" "}
print "\n","parameters_1",parameters
a.push(parameters)
end
i=i+1
end
print "\n"
#print a
print "\n"
return a
end
###############################################################################
def hyperloop(i, lispfun)
a=[]
while i < lispfun.length() do
if lispfun[i] == '('
j=extract(i+1,lispfun)
parameters = lispfun[i,j-i]
print "\n","parameters_2",parameters
a.push(parameters)
end
i=i+1
end
#print "\n"
#print a
#print "\n"
return a
end
###############################################################################
def squash(a)
if a.class == [].class && a.length() > 2
#print "\n Too big to squash \n"
return "#{a[0]} " + squash(a[1..-1])
end
if a[0] == "cdr"
return "#{a[1]}[1]"
######################### Following is the reason why
######## (cdr x) will be x[1] and not x[1..-1]
################################ ########
#$ irb
#2.1.2 :001 > x=[]
# => []
#2.1.2 :002 > x.class
# => Array
#2.1.2 :003 > x.push(1)
# => [1]
#2.1.2 :004 > x
# => [1]
#2.1.2 :005 > x[0]
# => 1
#2.1.2 :006 > x[1]
# => nil
#2.1.2 :007 > x[1..-1]
# => []
#2.1.2 :008 > if x[1..-1] == nil
#2.1.2 :009?> print "lol"
#2.1.2 :010?> end
# => nil
#2.1.2 :011 > if x[1] == nil
#2.1.2 :012?> print "lol"
#2.1.2 :013?> end
#lol => nil
#2.1.2 :014 >
end
if a[0] == "car"
return "#{a[1]}[0]"
end
if a == "null"
return "nil"
end
return a
end
###############################################################################
def get_if_block(build_blocks)
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] != "> "
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_len=1
condition_rh = build_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_len=condition_lh.length()
condition_rh = build_blocks[3]
condition_rh_len=condition_rh.length()
opr_if=4+condition_rh_len-2
opr_else=5+condition_rh_len-2
end
#print "`````````\n",condition_lh,"\n````````````"
#print "`````````\n",condition_rh,"\n````````````"
return_if_bl = build_blocks[opr_if]
return_else_bl = build_blocks[opr_else]
print condition_lh_len + condition_rh_len + return_if_bl.length(), "!!!!!!!", block_len,"!!!!!!!!!"
if condition_lh_len + condition_rh_len + return_if_bl.length() == block_len-1
#lack of else block
print "`````````\n","yaaayyyyyyy","\n````````````"
print "`````````\n","yaayyyy","\n````````````"
if return_if_bl[0] == "if"
return_if_bl=get_if_block(build_blocks[opr_if..-1])
end
condition_lh = squash(condition_lh)
condition_rh = squash(condition_rh)
return_if_bl = squash(return_if_bl)
return "if #{condition_lh} #{condition_it} #{condition_rh} \n \t #{return_if_bl} \n end \n"
else
#else block is present
print "`````````\n","naaayyyyyyy","\n````````````"
print "`````````\n","naayyyy","\n````````````"
if return_if_bl[0] == "if"
return_if_bl = get_if_block(build_blocks[opr_if..-1])
end
if return_else_bl[0] == "if"
print "`````````\n",return_else_bl,"\n````````````"
return_else_bl = get_if_block(build_blocks[opr_else..-1])
end
condition_lh = squash(condition_lh)
condition_rh = squash(condition_rh)
return_if_bl = squash(return_if_bl)
return_else_bl = squash(return_else_bl)
return "if #{condition_lh} #{condition_it} #{condition_rh} \n \t #{return_if_bl} \n else \n \t #{return_else_bl} \n end \n"
end
end
###############################################################################
def convert(build_blocks)
write_block=[]
i=0
#while i<build_blocks.length() do
if build_blocks[i][0] == "if"
write_block.push(get_if_block(build_blocks))
i=i+1
end
# i=i+1
#end
return write_block
end