plunder

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

rex_tests.sire (9701B)


      1 ; Copyright 2023 The Plunder Authors
      2 ; Use of this source code is governed by a BSD-style license that can be
      3 ; found in the LICENSE file.
      4 
      5 #### rex_tests <- rex_show
      6 
      7 :| sire
      8 :| sire_24_rex
      9 :| rex_show
     10 
     11 
     12 ;;; Basic Lexing + Nesting ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     13 
     14 !! barAll wordy b#{ABCDEFGHIJKLMNOPQRSTUVWXYZ}
     15 !! barAll wordy b#{abcdefghijklmnopqrstuvwxyz}
     16 !! barAll wordy b#{0123456789}
     17 !! barAll wordy b#{_}
     18 !! barAll runic b#{!#$%&*+,-./:<=>?@\^`|~'}
     19 
     20 !! not (barAny wordy b#{!#$%&*+,-./:<=>?@\^`|~})
     21 
     22 x=(LN {fil} 1 b#{"foo" "foo})
     23 =?= (lexLine x) ~~ , x 0   5 | LTEXT
     24                 ~~ , x 5   6 | LWYTE
     25                 ~~ , x 6  10 | LTEXT
     26                 ~~ , x 10 10 | LTERM
     27 
     28 x=(LN {fil} 1 b#{foo bar})
     29 =?= (lexLine x) ~~ , x 0 3 | LWORD
     30                 ~~ , x 3 4 | LWYTE
     31                 ~~ , x 4 7 | LWORD
     32                 ~~ , x 7 7 | LTERM
     33 
     34 x=(LN {fil} 2 b#{a+b})
     35 =?= (lexLine x) ~~ , x 0 1 | LWORD
     36                 ~~ , x 1 2 | LRUNE
     37                 ~~ , x 2 3 | LWORD
     38                 ~~ , x 3 3 | LTERM
     39 
     40 x=(LN {fil} 3 b#{()})
     41 =?= (lexLine x) ~~ , x 0 2 | LNEST FALSE ~[x,1,2,LTERM]
     42                 ~~ , x 2 2 | LTERM
     43 
     44 x=(LN {fil} 4 b#{(a)})
     45 =?= (lexLine x) ~~ , x 0 3 | LNEST FALSE ~[x,1,2,LWORD x,2,3,LTERM]
     46                 ~~ , x 3 3 | LTERM
     47 
     48 x=(LN {fil} 5 b#{(+)})
     49 =?= (lexLine x) ~~ , x 0 3 | LNEST FALSE ~[x,1,2,LRUNE x,2,3,LTERM]
     50                 ~~ , x 3 3 | LTERM
     51 
     52 
     53 ;;; Merge Multi-Line Strings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     54 
     55 = x | LN {fil} 5 b#}  } hi
     56 = y | LN {fil} 6 b#}  } bye
     57 
     58 =?= (lexLine x) ~~ x,0,1,LWYTE
     59                 ~~ x,1,5,(LLINE ~[])
     60                 ~~ x,5,5,LTERM
     61 
     62 =?=  ++ ^ [x 0 1 _] | LWYTE
     63      ++ ^ [x 1 5 _] | LLINE ~[y,1,6,()]
     64      ++ ^ [y 6 6 _] | LTERM
     65   | listToRow
     66   | multiLine
     67   | listWeld (lexLine x) (lexLine y)
     68 
     69 = x | LN {fil} 5 b#} } hi
     70 = y | LN {fil} 6 b#} } bye
     71 
     72 =?=  ++ ^ [x 0 4 _] | LLINE ~[y,0,5,()]
     73      ++ ^ [y 5 5 _] | LTERM
     74   | listToRow
     75   | multiLine
     76   | listWeld (lexLine x) (lexLine y)
     77 
     78 
     79 ;;; Clump Juxtaposed Tokens ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     80 
     81 x=(LN %fil 2 b#{a+b c})
     82 
     83 =?= (lexLine x) ~~ , x 0 1 | LWORD
     84                 ~~ , x 1 2 | LRUNE
     85                 ~~ , x 2 3 | LWORD
     86                 ~~ , x 3 4 | LWYTE
     87                 ~~ , x 4 5 | LWORD
     88                 ~~ , x 5 5 | LTERM
     89 
     90 =?=  ++ SPAN x 0 3
     91          ++ [x 0 1 LWORD]
     92          ++ [x 1 2 LRUNE]
     93          ++ [x 2 3 LWORD]
     94      ++ SPAN x 4 5
     95          ++ [x 4 5 LWORD]
     96   | clump (lexLine x)
     97 
     98 x=(LN %fil 2 b#{a+ c})
     99 
    100 =?=  ++ SPAN x 0 1
    101          ++ [x 0 1 LWORD]
    102      ++ SPAN x 1 2
    103          ++ [x 1 2 LRUNE]
    104      ++ SPAN x 3 4
    105          ++ [x 3 4 LWORD]
    106   | clump (lexLine x)
    107 
    108 ;;; Layout Engine (Understand Structure Implied by Indentation) ;;;;;;;;;;;;;;;;
    109 
    110 lol=(SPAN (LN {file} 1 b#{ lol }) 1 4 LWORD)
    111 lus=(SPAN (LN {file} 1 b#{ + }) 1 2 LRUNE)
    112 
    113 =?= b#lol (spanBar lol)
    114 =?= b#{+} (spanBar lus)
    115 
    116 =?= (LAMB lol)       (spanFrag lol)
    117 =?= (WOLF b#{+} lus) (spanFrag lus)
    118 
    119 x=(LN {fil} 1 b#{ ++x})
    120 a=(SPAN x 1 3 [(SPAN x 1 2 LRUNE) (SPAN x 2 3 LWORD)])
    121 c=(SPAN x 1 3 [(SPAN x 1 3 LRUNE)])
    122 d=(SPAN x 2 3 [(SPAN x 2 3 LRUNE)])
    123 
    124 (clumpFrag a =?= (1, LAMB a))
    125 
    126 (clumpFrag d =?= (2, WOLF b#{+} d))
    127 
    128 (clumpFrag c =?= (2, WOLF b#{++} c))
    129 
    130 lol=(SPAN (LN {file} 1 b#{ lol }) 1 4 LWORD)
    131 lus=(SPAN (LN {file} 1 b#{ + }) 1 2 LRUNE)
    132 
    133 =?= b#lol (spanBar lol)
    134 =?= b#{+} (spanBar lus)
    135 
    136 =?= (LAMB lol)       (spanFrag lol)
    137 =?= (WOLF b#{+} lus) (spanFrag lus)
    138 
    139 x=(LN {fil} 1 b#{ ++x})
    140 a=(SPAN x 1 3 [(SPAN x 1 2 LRUNE) (SPAN x 2 3 LWORD)])
    141 c=(SPAN x 1 3 [(SPAN x 1 3 LRUNE)])
    142 d=(SPAN x 2 3 [(SPAN x 2 3 LRUNE)])
    143 
    144 (clumpFrag a =?= (1, LAMB a))
    145 
    146 (clumpFrag d =?= (2, WOLF b#{+} d))
    147 
    148 (clumpFrag c =?= (2, WOLF b#{++} c))
    149 
    150 = (renderSpan renderItem span@(SPAN (LN _ _ byt) off end val))
    151 ++ renderItem val
    152 ++ barSlice off (sub end off) byt
    153 
    154 = (renderRowOf render items)
    155 ^ [b#{[} _ b#{]}]
    156 | intersperse b#{ }
    157 | map render items
    158 
    159 =?= b#{[a b c]} | barFlat | renderRowOf natBar [%a %b %c]
    160 
    161 = (renderLexi renderSub lexi)
    162 # switch (lexiType lexi)
    163 * _      | {bad lexi} lexi
    164 * {rune} | b#RUNE
    165 * {word} | b#{}
    166 * {wyte} | b#WYTE
    167 * {semi} | b#SEMI
    168 * {fail} | b#FAIL
    169 * {term} | b#TERM
    170 * {text} | b#TEXT
    171 * {line} @ [_ m] lexi
    172         ++ b#{(LINE }
    173         ++ planRender m
    174         ++ b#{)}
    175 * {nest} @ [_ bracket xs] lexi
    176          ^ [b#{(} _ b#{ } renderSub-xs b#{)}]
    177          | if bracket b#BRAK (else b#PARA)
    178 
    179 = (renderElem renderClump elem)
    180 ^ renderSpan _ elem
    181 & lexi
    182 | renderLexi (renderRowOf renderClump) lexi
    183 
    184 = (renderClump (SPAN _ _ _ es))
    185 | renderRowOf (renderElem renderClump) es
    186 
    187 = (renderTree depth t)
    188 ^ (barRep depth { }, _)
    189 | treeCase t
    190 *   ? (onLeaf c)
    191    ++ renderClump c
    192    ++ barNewline
    193 *   ? (onNode rune sons heir)
    194    ++ rune
    195    ++ barNewline
    196    ++ @ nSons (len sons)
    197       : [ix s] < foreach (rowIndexed sons)
    198       ^ renderTree _ s
    199       | add depth (mul 2 (sub nSons ix))
    200    ++ # case heir
    201       - NONE   | 0
    202       - SOME h | renderTree depth h
    203 
    204 (showSpan renderItem span)=(barFlat | renderSpan renderItem span)
    205 
    206 = (testLayout input)
    207 @ toks
    208     | listCat
    209     : [ln bar] < listForEach (listIndexed | barLinesList input)
    210     | lexLine (**LN {file} ln bar)
    211 @ toks | multiLine toks
    212 @ clmp | clump toks
    213 | barFlat
    214 | intersperse barNewline
    215 | map (renderTree 0)
    216 | listToRow
    217 | layout (listMap clumpFrag | listFromRow clmp)
    218 
    219 x=(LN {file} 1 b#{ a b c })
    220 =?= (testLayout b#{ a b c })
    221   # b
    222   } [a]
    223   }
    224   } [b]
    225   }
    226   } [c]
    227   }
    228  ;++ TLEAF | SPAN x 1 2 [(SPAN x 1 2 LWORD)]
    229  ;++ TLEAF | SPAN x 3 4 [(SPAN x 3 4 LWORD)]
    230  ;++ TLEAF | SPAN x 5 6 [(SPAN x 5 6 LWORD)]
    231 
    232 =?= (testLayout b#{ a b# c })
    233   # b
    234   } [a]
    235   }
    236   } [b]
    237   }
    238   } #
    239   }   [c]
    240   }
    241 
    242 =?= (testLayout b#{# a # b})
    243   # b
    244   } #
    245   }     [a]
    246   }   #
    247   }     [b]
    248   }
    249 
    250 =?=   | testLayout
    251       # b
    252       } # {a}
    253       } # b
    254   # b
    255   } #
    256   }   [TEXT{a}]
    257   } #
    258   }   [b]
    259   }
    260 
    261 
    262 =?=   | testLayout
    263       # b
    264       } # a
    265       }  b
    266   # b
    267   } #
    268   }     [a]
    269   }   [b]
    270   }
    271 
    272 =?=   | testLayout
    273       # b
    274       } # a
    275       }  b
    276       } c
    277       } d
    278   # b
    279   } #
    280   }       [a]
    281   }     [b]
    282   }   [c]
    283   } [d]
    284   }
    285 
    286 =?=   | testLayout
    287       # b
    288       } # a # b
    289       }     # c
    290       }   # d
    291       } # e+(e)
    292   # b
    293   } #
    294   }       [a]
    295   }     #
    296   }       [b]
    297   }     #
    298   }       [c]
    299   }   #
    300   }     [d]
    301   } #
    302   }   [e RUNE+ (PARA [[e]])(e)]
    303   }
    304 
    305 =?=   | testLayout
    306       # b
    307       } b#} abc
    308   # b
    309   } [b RUNE# (LINE 0)} abc]
    310   }
    311 
    312 
    313 ;;; Splitting Blocks ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    314 
    315 =?=  ++ BS %REPL 1 WOODS
    316      ++ ~[]
    317   | treeStep (blockState {REPL}) NONE
    318 
    319 x=(LN %REPL 1 b#lol)
    320 =?=  ++ BS %REPL 2 WOODS
    321      ++ ~~ ++ 1
    322            ++ ~~ TLEAF (x, 0, 3, [[x 0 3 LWORD]])
    323   | treeStep (blockState {REPL})
    324   | SOME b#} lol
    325 
    326 x=(LN %REPL 1 b#{ #})
    327 =?=  ++ BS %REPL 2 ^ BLOCK ~[_] 1 2
    328                    | ~[[x 0 1 LWYTE] [x 1 2 LRUNE] [x 2 2 LTERM]]
    329      ++ ~[]
    330   | treeStep (blockState {REPL})
    331   | SOME b#{ #}
    332 
    333 x=(LN %REPL 1 b#{ #})
    334 =?=  ++ BS %REPL 2 ^ BLOCK ~[_] 1 2
    335                    | ~[[x 0 1 LWYTE] [x 1 2 LRUNE] [x 2 2 LTERM]]
    336      ++ ~[]
    337   | treeStep (blockState {REPL})
    338   | SOME b#{ #}
    339 
    340 
    341 ;;; Policy ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    342 
    343 =?= (SOME-[b#{#} ~[LAMB-5 LAMB-6]], ~[3 4])
    344   | lambs ~[LAMB-3 LAMB-4 WOLF-(b#{#})-4 LAMB-5 LAMB-6]
    345 
    346 =?=   | INFIX ~[3 4] b#{#} (SEQUE ~[5 6])
    347   | subLayout ~[LAMB-3 LAMB-4 WOLF-(b#{#})-4 LAMB-5 LAMB-6]
    348 
    349 =?=   | PREFX b#{#} ~[LAMB-5 LAMB-6]
    350   | subLayout ~[WOLF-(b#{#})-4 LAMB-5 LAMB-6]
    351 
    352 =?=   | SEQUE ~[5 6 5]
    353   | subLayout ~[LAMB-5 LAMB-6 LAMB-5]
    354 
    355 =?=   | PREFX {#} ~[]
    356   | subLayout ~[WOLF-{#}-999]
    357 
    358 =?= `(3(4(5))) | rexAddHeir `(3(4)) `5
    359 
    360 =?= {foo} | spanQuotedStr (SPAN (LN 6 7 b#}# {foo} #) 1 6 LTEXT)
    361 =?= {foo} | spanQuotedStr (SPAN (LN 6 7 b#}# }|foo| #) 1 7 LTEXT)
    362 
    363 =?= `foo | elemRex die | SPAN (LN 9 9 b#{ foo })   1 4 | LWORD
    364 =?= `(*) | elemRex die | SPAN (LN 9 9 b#{ * })     1 2 | LRUNE
    365 =?= `{x} | elemRex die | SPAN (LN 9 9 b#{ {x} })   1 4 | LTEXT
    366 =?= `{x} | elemRex die | SPAN (LN 9 9 b#}# }|x| #) 1 5 | LTEXT
    367 
    368 =?= ` } x
    369       } y
    370       } z
    371   | elemRex die
    372   | SPAN (LN 9 7 b#}# } x#) 1 5
    373   | LLINE
    374  ~~ SPAN (LN 9 9 b#}# } z#) 1 5 ()
    375  ~~ SPAN (LN 9 8 b#}# } y#) 1 5 ()
    376 
    377 =?= [`(a-b) `(+) `(c.d)]
    378   | map clumpRex | clump | lexLine | LN %fil 2 b#{a-b+ c.d}
    379 
    380 =?= [`[a b c]]
    381   | map clumpRex | clump | lexLine | LN %fil 2 b#{[a b , c]}
    382 
    383 =?= [`{a b , c}]
    384   | map clumpRex | clump | lexLine | LN %fil 2 b#{{a b , c}}
    385 
    386 =?= [`a]
    387   | map clumpRex | clump | lexLine | LN %fil 2 b#{(a)}
    388 
    389 =?= [`(a b c)]
    390   | map clumpRex | clump | lexLine | LN %fil 2 b#{(a b c)}
    391 
    392 =?= [`[a b c]]
    393   | map clumpRex | clump | lexLine | LN %fil 2 b#{(, a b c)}
    394 
    395 =?= [`[a [b (+ c)]]]
    396   | map clumpRex | clump | lexLine | LN %fil 2 b#{(, a , b + c)}
    397 
    398 =?= [`(a, b) `(a, b) `a,b]
    399   | map clumpRex | clump | lexLine | LN %fil 2 b#{(a , b) (a, b) (a,b)}
    400 
    401 =?= [`((a , b) + c)]
    402   | map clumpRex | clump | lexLine | LN %fil 2 b#{(a , b + c)}
    403 
    404 =?=   ` # (3+4)
    405           # } lol
    406   | testRex
    407   # b
    408   } # (3+4)
    409   }   # } lol
    410 
    411 =?=   ` # (3+4)
    412         # } lol
    413   | testRex
    414   # b
    415   } # (3+4)
    416   } # } lol
    417 
    418 x=(rexCog simpleCog-id | blockState {REPL})
    419 foo@(out, x)=(x | SOME b#{% hi})
    420 out=?=NIL
    421 (out, x)=(x | SOME b#{% hi})
    422 out=?=NIL
    423 (out, x)=(x | SOME b#{})
    424 out=?=NIL
    425 (out, x)=(x | SOME b#{})
    426 =?= out ~~ ^ 1,_ ` % hi
    427                    % hi
    428 
    429 x=(rexCog simpleCog-id | blockState {REPL})
    430 foo@(out, x)=(x | SOME b#}|} a|)
    431 out=?=(~[])
    432 (out, x)=(x | SOME b#}|} b|)
    433 out=?=(~[])
    434 (out, x)=(x NONE)
    435 =?= out ~~ ^ 1,_ ` } a
    436                    } b
    437 
    438 x=(rexCog simpleCog-id | blockState {REPL})
    439 foo@(out, x)=(x | SOME b#{%hi})
    440 (out =?= ~[1,(` %hi)])
    441 
    442 x=(rexCog simpleCog-id | blockState {REPL})
    443 foo@(out, x)=(x | SOME b#{()})
    444 (out =?= ~[1,(`())])
    445 
    446 x=(rexCog simpleCog-id | blockState {REPL})
    447 foo@(out, x)=(x | SOME b#{({(} x)})
    448 (out =?= ~[1,(` ({(} x))])