summaryrefslogtreecommitdiff
path: root/ipl/procs/expander.icn
blob: e346029f0302ba8c0d2c2f08975c9dc638cb45dd (plain)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
############################################################################
#
#	File:     expander.icn
#
#	Subject:  Procedures to convert character pattern expressions
#
#	Author:   Ralph E. Griswold
#
#	Date:     May 2, 2001
#
############################################################################
#
#  This file is in the public domain.
#
############################################################################
#
#  pfl2str(pattern) expands pattern-form expressions, which have the form
#
#	[<expr><op><expr>]
#
#  to the corresponding string.
#
#  The value of <op> determines the operation to be performed.
#
#  pfl2gxp(pattern) expands pattern-form expressions into generators
#  that, when compiled and evaluated, produce the corresponding
#  string.
#
#  pfl2pwl(pattern) converts pattern-form expressions to Painter's
#  weaving language.
#
###########################################################################n
#
#  Links:  strings, weaving
#
############################################################################

link strings
link weaving

procedure pfl2str(pattern)		#: pattern-form to plain string
   local result, expr1, expr2, op
   static operator, optbl

   initial {
      operator := '*-!|+,/~:?%<>#`'

      optbl := table()

      optbl["*"] := repl
      optbl["<"] := Upto
      optbl[">"] := Downto
      optbl["-"] := UpDown
      optbl["|"] := Palindrome
#     optbl["!"] := Palindroid
      optbl["+"] := Block
      optbl["~"] := Interleave
      optbl["->"] := Extend
      optbl[":"] := Template
      optbl["?"] := Permute
      optbl["%"] := Pbox
      optbl["<>"] := UpDown
      optbl["><"] := DownUp
      optbl["#"] := rotate
      optbl["`"] := reverse
      optbl[","] := proc("||", 2)
      }

   result := ""

   pattern ? {
      while result ||:= tab(upto('[')) do {
         move(1)
#        expr1 := pfl2str(tab(bal(operator, '[', ']'))) | return error("1", pattern)
         expr1 := pfl2str(tab(bal(operator, '[', ']'))) | {
            result ||:= pfl2str(tab(bal(']', '[', ']')))
            move(1)
            next
            }
         op := tab(many(operator)) | return error("2", pattern)
         expr2 := pfl2str(tab(bal(']', '[', ']'))) | return error("3", pattern)
         result ||:= \optbl[op](expr1, expr2) | return error("4", pattern)
         move(1)
         }
      if not pos(0) then result ||:= tab(0)
      }

   return result

end

procedure pfl2pwl(pattern)		#: pattern form to Painter expression
   local result, i, j, slist, s, expr1, expr2, op, head
   static operator, optbl

   initial {
      operator := '*-!|+,;/~:?%<>#`'

      optbl := table()

      optbl["*"] := "*"
      optbl["<"] := "<"
      optbl[">"] := ">"
      optbl["-"] := "-"
      optbl["|"] := "|"
      optbl["!"] := "!"		# not supported in PWL
      optbl["+"] := "[]"
      optbl["->"] := "->"
      optbl["~"] := "~"
      optbl[":"] := ":"
      optbl["?"] := " perm "
      optbl["%"] := " pbox "
      optbl["<>"] := "<>"
      optbl["><"] := "><"
      optbl["#"] := "#"
      optbl["`"] := "`"
      optbl[","] := ","
      }

   result := ""

   pattern ? {
      while head :=  tab(upto('[')) do {
         if *head > 0 then result ||:= "," || head
         move(1)
         expr1 := pfl2pwl(tab(bal(operator, '[', ']'))) | return error()
         op := tab(many(operator)) | return error()
         expr2 := pfl2pwl(tab(bal(']', '[', ']'))) | return error()
         result ||:= "," ||  "(" || expr1 || \optbl[op] || expr2 || ")" |
            return error()
         move(1)
         }
      if not pos(0) then result ||:= "," || tab(0)
      }

   return result[2:0]

end

procedure error(expr1, expr2)

   write(&errout, "*** error ", expr1, " ", expr2)

   fail

end

procedure pfl2gxp(pattern, arg)		#: pattern form to generating expression
   local result, i, j, slist, s, expr1, expr2, op
   static operator, optbl, argtbl

   initial {

      operator := ',.*-!|+;/~:?%<>#`'

      optbl := table()

      optbl["*"] := "Repl{"
      optbl["<"] := "Upto{"
      optbl[">"] := "Downto{"
      optbl["-"] := "UpDownto{"
      optbl["|"] := "TileMirror{"
      optbl["!"] := "Palin{"
      optbl["+"] := "Valrpt{"
      optbl["~"] := "Inter{"
      optbl["->"] := "ExtendSeq{"
      optbl["~"] := "Parallel{"
      optbl[":"] := "Template{"
      optbl["?"] := "Permut{"
      optbl["%"] := "Pbox{"
      optbl["<>"] := "UpDown{"
      optbl["><"] := "DownUp{"
      optbl["#"] := "Rotate{"
      optbl["`"] := "Reverse{"
      optbl["*"] := repl
      }

   /arg := str

      # Handling of literal arguments

      argtbl := table(str)
      argtbl["*"] := 1
      argtbl["#"] := 1
      argtbl["->"] := 1

   if /pattern | (*pattern = 0) then return image("")

   result := ""

   pattern ? {
      while result ||:= arg(tab(upto('['))) do {
         move(1)
         expr1 := pfl2gxp(tab(bal(operator, '[', ']')), arg) | {
            result ||:= tab(bal(']', '[', ']')) || " | "	# no operator
            move(1)
            next
            }
         if ="." then result ||:= tab(bal(']', '[', ']')) || " | "
         else {
            op := tab(many(operator))  | return error()
            expr2 := pfl2gxp(tab(bal(']', '[', ']')), argtbl[op]) | return error()
            result ||:= \optbl[op] || expr1 || "," || expr2 || ") | " |
              return error()
            }
         move(1)
         }
      if not pos(0) then result ||:= arg(tab(0))
      }

   return trim(result, '| ')

end

procedure lit(s)

   return "!" || image(s)

end

procedure str(s)

   return lit(s) || " | "

end

procedure galt(s)

   return "Galt{" || collate(s, repl(",", *s - 1)) || "}"

end

procedure pwl2pfl(wexpr)		#: Painter expression to pattern form

   return pwlcvt(prepare(wexpr))

end

procedure prepare(wexpr)		# preprocess pwl
   local inter, result
   static names, names1

   initial {
      names := [
         "",				# expression placeholder
         " block ", "[]",
         " repeat ", "*",
         " rep ", "*",
         " extend ", "==",
         " ext ", "==",
         " concat ", ",",
         " interleave ", "~",
         " int ", "~",
         " upto ", ">",
         " downto ", "<",
         " template ", ":",
         " temp ", ":",
         " palindrome ", "|",
         " pal ", "|",
         " pal", "|",
         " permute ", "?",
         " perm ", "?",
         " pbox ", "%",
         " updown ", "<>",
         " downup ", "><",
         " rotate ", "#",
         " rot ", "#",
         " reverse ", "`",
         " rev ", "`",
         " rev", "`",
         ]

      names1 := [
         "",				# expression placeholder
         "pal", "|",
         "rev", "`"
         ]

      }

   result := ""

   wexpr ? {
      while result ||:= tab(upto('[')) do {
         move(1)
         inter := tab(bal(']'))
         if *inter > 0 then result ||:= spray(inter)
         else result ||:= "[]"
         move(1)
         }
      result ||:= tab(0)
      }

   if upto(result, ' ') then {
      if upto(result, &letters) then {
         names[1] := result
         result := (replacem ! names)
         }
      }

   if upto(result, &letters) then {
      names1[1] := result
      result := (replacem ! names1)
      }

   return deletec(map(result, "[]", "=="), ' ')

end

procedure pwlcvt(wexpr)
   local result, inter

   wexpr ?:= {
      2(="(", tab(bal(')')), pos(-1))
      }
      
   result := ""

   wexpr ? {
      while result ||:= form1(pwlcvt(tab(bal('|`', '([', ']('))), move(1))
      result ||:= tab(0)
      }

   wexpr := result
   result := ""

   wexpr ? {
      while result ||:= form2(pwlcvt(tab(bal('->:#*=~', '([', ')]'))),
         =("#" | "*" | "->" | "~" | ":" | "=="), pwlcvt(tab(0)))
      result ||:= tab(0)
      }

   wexpr := result
   result := ""
 
   wexpr ? {
      while result ||:= form2(pwlcvt(tab(bal('<>', '([', ')]'))),
         =("><" | "<>"), pwlcvt(tab(0)))
      result ||:= tab(0)
      }

   wexpr := result
   result := ""
 
   wexpr ? {
      while result ||:= form2(pwlcvt(tab(bal('<->,', '([', ')]'))),
         =(">" | "<" | "-" | ","), pwlcvt(tab(0)))
      result ||:= tab(0)
      }
      
   return result

end

procedure form1(wexpr, op)

   return "[" || wexpr || op || "]"

end

procedure form2(wexpr1, op, wexpr2)

   return "[" || wexpr1 || op || wexpr2 || "]"

end

procedure spray(inter)
   local count, s1, s2, s3, colors

   s1 := s2 := s3 := ""

   inter ?:= {		# only palindome and reflection allowed, it seems
      1(tab(upto('|`') | 0), s3 := tab(0))
      }

   inter ? {
      while s1 ||:= colors := tab(upto(' ')) do {
         tab(many(' '))
         count := tab(upto(' ') | 0)
         if *count = 1 then s2 ||:= repl(count, *colors)
         else s2 ||:= repl("{" || count || "}", *colors)
         move(1) | break
         }
      }
   
   return "((" || s1 || s3 || ")" || "[]" || s2 || ")"

end