-
Notifications
You must be signed in to change notification settings - Fork 0
/
condhead_wc.rb
136 lines (134 loc) · 3.91 KB
/
condhead_wc.rb
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
###############################################################################
###############################################################################
# This file has function which will translate a "(cond ())" block of lisp
# into the respective ruby code with if - else ladder
###############################################################################
# Extract2 function essentiialy helps get the words between two brackets '(' ')'
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
###############################################################################
# This function helps get all the various conditions in the "(cond ())" block
# try printing #{gg} and feel the difference
def get_all_cond(bb, ab)
i=1
a=[]
while i<ab[0].length
if ab[0][i] == '('
j=extract2(i+1, ab[0])
gg = ab[0][i,j[0]-i]
i=j[0]
print "\n```` #{gg} `````\n"
a.push(gg)
end
i=i+1
end
return a
end
###############################################################################
def ind_cond(a)
j = extract2(2,a)
a1 = a[2, j[0]-3]
a2 = a[j[0]..-2]
return a1, a2
end
###############################################################################
# beautiful takes small brackets in lisp which deals with cons cells like car, cdr, nconc,
# and converts them in respective ruby code.
# car of a list a, would be the same as taking first element a[0]
# cdr of a list a, would be the same as taking all but first element a[1..-1]
def beautify(pars, wars)
#print "\n\t #{pars} \t #{wars}"
if pars[0] == "null"
return "nil == #{pars[1]}"
end
if pars[0] == "atom"
return "#{pars[1]}.length == 1"
end
if pars[0] == "consp"
return "#{pars[1]}.length > 1"
end
if pars[0] == "cons"
return "#{pars[1]}"
end
if pars[0] == "car"
# car of a list a, would be the same as taking first element a[0]
return "#{pars[1]}[0]"
end
if pars[0] == "cdr"
# cdr of a list a, would be the same as taking all but first element a[1..-1]
return "#{pars[1]}[1..-1]"
end
if pars[1] == "car"
return "[#{pars[0]}([#{pars[2]}[0]])]"
end
if pars[1] == "cdr"
return "#{pars[0]}([#{pars[2]}[1..-1]])"
end
if pars[0] == "nconc"
j=extract2(9, wars)
lhs = wars[9,j[0]-10]
rhs = wars[j[0]..-2]
print "\n````` #{lhs} 111111 #{rhs}``````\n"
plhs = lhs.split(/\W([><+-^\*\,\.\s]*)/)
plhs = plhs.reject { |c| c.empty? }
plhs = plhs.reject { |c| c==" "}
prhs = rhs.split(/\W([><+-^\*\,\.\s]*)/)
prhs = prhs.reject { |c| c.empty? }
prhs = prhs.reject { |c| c==" "}
lhs = beautify(plhs, lhs)
rhs = beautify(prhs, rhs)
return "#{lhs} + #{rhs}"
end
return pars[0]
end
###############################################################################
def simplify2(blk)
pars = blk[0].split(/\W([><+-^\*\,\.\s]*)/)
pars = pars.reject { |c| c.empty? }
pars = pars.reject { |c| c==" "}
print "\n``````` #{pars} ``````\n"
condition_it = "=="
pars = beautify(pars, blk[0])
qars = blk[1].split(/\W([><+-^\*\,\.\s]*)/)
qars = qars.reject { |c| c.empty? }
qars = qars.reject { |c| c==" "}
print "\n``````` #{qars} ``````\n"
qars = beautify(qars, blk[1])
return "\t if #{pars} \n \t \t return #{qars} \n \t end \n"
end
###############################################################################
# this is like mothercall which takes the string of lisp defun and makes calls
# to various finctions which will parse the defun
def get_cond_block(bb, ab)
gg = get_all_cond(bb, ab)
#print gg, "\n"
i=0
kk=[]
while i < gg.length
blk = ind_cond (gg[i])
#print "\n`````` #{blk} `````\n"
plk = simplify2 (blk)
i=i+1
kk.push(plk)
end
print kk
return kk, 3
end