( Thue interpreter 07.09.2025 ) 0 value B 0 value L 10000 constant maxlen : >B ( i -- a ) B + ; ( : line here dup to B maxlen accept to L ; ) : B. B L type cr ; ( rule layout - 1 cell - next rule - 2 cells - pointer to a string and its length for the left-hand side - 2 cells - pointer to a string and its length for the right-hand side ) 0 value first 0 value last : >side ( r i -- a ) 2* 1+ cells + ; : length ( r i -- 0 ) >side cell+ @ ; : side@ ( r i -- s l ) >side dup @ swap cell+ @ ; 0 value S 0 value SL : rewind ( s l -- n ) to SL to S L SL - dup 0< if drop 0 then 1+ ; : match ( i -- f ) >B SL S over compare 0= ; : index ( s l -- 0 | i 1 ) rewind 0 ?do i match if i unloop 1 exit then loop 0 ; 0 value fin : open r/o open-file throw to fin ; : close fin close-file throw ; : whole ( status -- ) throw 0= if abort" Line too long!" then ; : line here dup to B maxlen fin read-line whole dup to L chars allot align ; : rule ( r -- ) dup 0 side@ type ." ::=" 1 side@ type cr ; : list first begin dup while dup rule @ repeat drop ; : first! first 0= if here to first then ; : last! last if here last ! then here to last ; : record ( i -- ) first! last! 0 , B , dup , 3 + dup >B , L swap - , ; : ::= ( -- i ) s" ::=" index 0= if abort" No rule!" then ; : rule ( -- f ) line ::= L 3 = if not else record 0 then ; : parse open begin rule until line close ; : restrict ( r -- n ) L over 0 length - swap 1 length + dup maxlen > if abort" Out of space!" then ; : dir ( r -- f ) dup 0 length swap 1 length < ; : frame ( r i -- n ) swap 0 length + L swap - ; : places ( r i -- s d ) >B >r dup 0 length r@ + swap 1 length r> + ; : by ( r i -- s d l ) 2dup frame >r places r> ; : adjust ( r i -- ) over dir if by cmove> else by cmove then ; : put ( r i -- ) >B >r 1 side@ r> swap cmove ; : replace ( r i -- ) over restrict >r 2dup adjust put r> to L ; : match ( -- f ) 0 side@ index if replace 1 else 0 then ; : pass ( -- f ) first begin @ dup while dup match if 1 exit then repeat ; : eval begin B. pass 0= until ; : run ( s l -- ) parse eval ; s" test.thue" run