-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseform.tcl
293 lines (244 loc) · 6.54 KB
/
parseform.tcl
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/tclsh
package provide parseform 1.0
# Package to transform Grail prolog-style formula strings into a (recursive) tcl list.
# so
#
# parseform "dr(0,dl(1,a,b),c)"
#
# returns
#
# {dr 0 {dl 1 a b} c}
#
# (normally without the outer braces).
# parseform
#
# transform a string containing a formula into a list
proc parseform {string} {
global res
set string [string trim $string]
set i [parseform1 $string 0]
if {$i != [string length $string]} {
.msg configure -text "Trailing material deleted! .[string range $string $i end].$res."
}
return $res
}
proc parseform1 {string begin} {
global arg1 arg2 index res
set i $begin
if {[string equal [string range $string $i [expr $i+2]] "dl("]} {
set i [parserest $string [expr $i+3]]
set res [list dl $index $arg1 $arg2]
} elseif {[string equal [string range $string $i [expr $i+2]] "dr("]} {
set i [parserest $string [expr $i+3]]
set res [list dr $index $arg1 $arg2]
} elseif {[string equal [string range $string $i [expr $i+5]] "lambda("]} {
set i [parserest $string [expr $i+3]]
set res [list lambda $index $arg1 $arg2]
} elseif {[string equal [string range $string $i [expr $i+4]] "appl("]} {
set i [parserest $string [expr $i+3]]
set res [list appl $index $arg1 $arg2]
} elseif {[string equal [string range $string $i [expr $i+1]] "p("]} {
set i [parserest $string [expr $i+2]]
set res [list p $index $arg1 $arg2]
} elseif {[string equal [string range $string $i [expr $i+3]] "dia("]} {
set i [parserest_u $string [expr $i+4]]
set res [list dia $index $arg1]
} elseif {[string equal [string range $string $i [expr $i+3]] "box("]} {
set i [parserest_u $string [expr $i+4]]
set res [list box $index $arg1]
} else {
# atomic formula
set j [string first "," $string $i]
set k [string first ")" $string $i]
if {$j < $k && $j != -1} {
set res_end [expr $j-1]
set i $j
} elseif {$k != -1} {
set res_end [expr $k-1]
set i $k
} else {
set res_end [expr [string length $string]-1]
set i [string length $string]
}
if {[string is wordchar -strict -failindex fail [string range $string $begin $res_end]]} {
set res [string range $string $begin $res_end]
} else {
.msg configure -text "Illegal character in atom: [string range $string $begin [expr $fail-1]]*HERE*[string range $string $fail end]"
set res [string range $string $begin [expr $fail-1]]
}
}
return $i
}
proc parserest {string i} {
global res arg1 arg2 index
set i [parseindex $string $i]
set thisindex $index
set i [parsecomma $string $i]
set i [parseform1 $string $i]
set thisarg1 $res
set i [parsecomma $string $i]
set i [parseform1 $string $i]
set thisarg2 $res
set i [parseparnc $string $i]
set index $thisindex
set arg1 $thisarg1
set arg2 $thisarg2
return $i
}
proc parserest_u {string i} {
global res arg1 index
set i [parseindex $string $i]
set thisindex $index
set i [parsecomma $string $i]
set i [parseform1 $string $i]
set thisarg1 $res
set i [parseparnc $string $i]
set index $thisindex
set arg1 $thisarg1
return $i
}
proc parsecomma {string i} {
if {[string equal [string range $string $i $i] ","]} {
return [expr $i+1]
} else {
.msg configure -text "Missing comma: [string range $string 0 $i]*HERE*[string range $string [expr $i+1] end]"
}
}
proc parseparnc {string i} {
if {[string equal [string range $string $i $i] ")"]} {
return [expr $i+1]
} else {
.msg configure -text "Missing close parenthesis: [string range $string 0 $i]*HERE*[string range $string [expr $i+1] end]"
}
}
proc parseindex {string i} {
global index
if {[set j [string first "," $string $i]] != -1} {
set index [string range $string $i [expr $j-1]]
return $j
} else {
.msg configure -text "Missing comma: $string"
}
}
proc printform {string} {
set l [parseform $string]
if {[llength $l] == 1} {
return $string
} else {
set c [lindex $l 0]
set m [lindex $l 1]
if {[string equal $m "0"]} {
set m ""
}
set s1 [printform1 [lindex $l 2]]
set s2 [printform1 [lindex $l 3]]
if {[string equal $c "dl"]} {
return "$s1 \\$m $s2"
} elseif {[string equal $c "dr"]} {
return "$s1 /$m $s2"
} elseif {[string equal $c "p"]} {
return "$s1 *$m $s2"
}
}
}
proc printform1 {l} {
if {[llength $l] == 1} {
return $l
} elseif {[llength $l] == 3} {
set c [lindex $l 0]
set m [lindex $l 1]
if {[string equal $m "0"]} {
set m ""
}
set s1 [printform1 [lindex $l 2]]
if {[string equal $c "dia"]} {
return "<>$m $s1"
} elseif {[string equal $c "box"]} {
return "\[\]$m $s1"
}
} elseif {[llength $l] == 4} {
set c [lindex $l 0]
set m [lindex $l 1]
if {[string equal $m "0"]} {
set m ""
}
set s1 [printform1 [lindex $l 2]]
set s2 [printform1 [lindex $l 3]]
if {[string equal $c "dl"]} {
return "($s1 \\$m $s2)"
} elseif {[string equal $c "dr"]} {
return "($s1 /$m $s2)"
} elseif {[string equal $c "p"]} {
return "($s1 *$m $s2)"
}
}
}
proc printform_alt {string} {
set l [parseform $string]
if {[llength $l] == 1} {
return $string
} else {
set c [lindex $l 0]
set m [lindex $l 1]
if {[string equal $m "0"]} {
set m ""
}
set s1 [printform_alt1 [lindex $l 2]]
set s2 [printform_alt1 [lindex $l 3]]
if {[string equal $c "dl"]} {
return "$s1 \\\\$m $s2"
} elseif {[string equal $c "dr"]} {
return "$s1 /$m $s2"
} elseif {[string equal $c "p"]} {
return "$s1 *$m $s2"
}
}
}
proc printform_alt1 {l} {
if {[llength $l] == 1} {
return $l
} elseif {[llength $l] == 3} {
set c [lindex $l 0]
set m [lindex $l 1]
if {[string equal $m "0"]} {
set m ""
}
set s1 [printform1 [lindex $l 2]]
if {[string equal $c "dia"]} {
return "<>$m $s1"
} elseif {[string equal $c "box"]} {
return "\[\]$m $s1"
}
} elseif {[llength $l] == 4} {
set c [lindex $l 0]
set m [lindex $l 1]
if {[string equal $m "0"]} {
set m ""
}
set s1 [printform1 [lindex $l 2]]
set s2 [printform1 [lindex $l 3]]
if {[string equal $c "dl"]} {
return "($s1 \\\\$m $s2)"
} elseif {[string equal $c "dr"]} {
return "($s1 /$m $s2)"
} elseif {[string equal $c "p"]} {
return "($s1 *$m $s2)"
}
}
}
proc writeform {l} {
if {[llength $l] == 1} {
return $l
} else {
set c [lindex $l 0]
set m [lindex $l 1]
if {[llength $l] == 3} {
set s1 [writeform [lindex $l 2]]
return "$c\($m,$s1\)"
} else {
set s1 [writeform [lindex $l 2]]
set s2 [writeform [lindex $l 3]]
return "$c\($m,$s1,$s2\)"
}
}
}