Vegetarianboy30

logical branflakes competition results (1994)

Jan 17th, 2021
1,233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. The contest was to make a branflakes program which could compute and output the
  2. value of any valid expression formed of bitwise logical operators (acting on
  3. bytes) and numerical constants. The expressions were input in reverse Polish
  4. notation, using the C characters for bitwise operators ~&^|. The constants
  5. were to be input in decimal, and separated by commas where necessary; at every
  6. linefeed, the expression immediately preceding it is to be output, but
  7. linefeeds do not otherwise interrupt expressions (they don't clear the stack).
  8.  
  9. Only one person finished an entry by the deadline: LV, with the
  10. following very well-constructed program. Thus he was declared the winner.
  11.  
  12.  
  13.  
  14. In short: get an input byte; write it in binary; determine what
  15. input byte it was by jumping at meaningful bit positions; Then
  16. according to the input byte do different actions
  17.  NOT:  compute 255 minus number
  18.  LINEFEED: print the number in decimal
  19.  COMMA: just push a number on the stack
  20.  DIGIT: multiply previous number by ten and add digit to it
  21.  OR; AND; XOR: store the number of binary operation to do later
  22. If there is an operation number at the end: then perform the
  23. binary operation: expand both numbers in binary with digits interweaved;
  24. then make a pass over them two bits at a time; finally reconstruct the
  25. result
  26.  
  27. >,[
  28.  
  29.  structure is:  nux
  30.                   ^
  31.  where n = number on top of stack;
  32.        u = 0 if not inside a number; 1 if in a number
  33.        x = next input byte
  34.  
  35.  nux000000000
  36.    ^
  37.  
  38.  write the input byte in binary
  39.  [>>[>]+<[-<]<-]
  40.  
  41.  nu00lxxxxxxh
  42.    ^
  43.  >>
  44.  
  45.  01010000 (nl) | 01100100 (&) | 00110100 (co) | xxxx1100
  46.  ^               ^              ^               ^
  47.  01111010 (^)  | 00111110 (|) | 01111110 (~)
  48.  ^               ^              ^            
  49.  
  50.  >>>>
  51.  01010000 (nl) | 01100100 (&) | 00110100 (co) |
  52.      ^               ^              ^              
  53.  xxxx1100  | 01111010 (^)  | 00111110 (|) | 01111110 (~)
  54.      ^           ^               ^              ^            
  55.  [
  56.    >>
  57.  xxxx1100  | 01111010 (^)  | 00111110 (|) | 01111110 (~)
  58.        ^           ^               ^              ^            
  59.    [
  60.  01111010 (^)  | 00111110 (|) | 01111110 (~)
  61.        ^               ^              ^            
  62.      -<
  63.  01111000 (^)  | 00111100 (|) | 01111100 (~)
  64.       ^               ^              ^            
  65.      [<<<<
  66.  00111100 (|) | 01111100 (~)
  67.   ^              ^            
  68.        [  this is the NOT case
  69.        
  70.          nu00 01111100 (~)
  71.                ^  
  72.              
  73.          compute 255
  74.          <+<+[>]+[>[-<++>]<<]
  75.          
  76.          nu0z 00000000   (z=255)
  77.            ^  
  78.          <[-]<[->>>-<<<]>>>[-<<<+>>>]
  79.          mu00 00000000
  80.             ^
  81.        ]>
  82.  nu00 00111100 (|) | mu00 000000
  83.         ^                 ^  
  84.        [  OR case
  85.          strip the ones
  86.          [>]<[-<]
  87.  
  88.          abu00 00000000
  89.                 ^                
  90.          +<
  91.        ]
  92.   abu00 010      | mu00 000
  93.         ^               ^
  94.      ]<
  95.  
  96.   abu00 01111000 (^)  | abu00 010 | mu00 000
  97.             ^               ^          ^
  98.      [ XOR case; strip the ones
  99.        [-<]>++<<
  100.      ]
  101.      
  102.   abu00 020  | abu00 010 | mu00 000
  103.       ^            ^          ^
  104.   ]<
  105.  
  106.   nu00 xxxx1100  | abu00 020  | abu00 010 | mu00 000
  107.             ^         ^            ^          ^
  108.   [ digit
  109.  
  110.     -<++[<[-<++>]>[-<+>]<-]
  111.              
  112.     nu00 d0    (d = digit)
  113.           ^
  114.     <<<<
  115.     nu00 d0
  116.      ^
  117.     [ if u==1 then we accumulate to the next number
  118.       -<
  119.       [->++<]>[->>+++++<<]
  120.      
  121.       000N d
  122.        ^
  123.       >>>[-<+>]<<<<
  124.       000m 0
  125.       ^
  126.     ]
  127.     n000 d  | 000 m
  128.      ^        ^
  129.    
  130.     >+>>[-<<<+>>>]
  131.     m100
  132.        ^
  133.     <
  134.   ]
  135.  
  136.   mu00 000  | abu00 020  | abu00 010 | mu00 000
  137.     ^            ^            ^          ^
  138.  
  139.   >
  140.  
  141.   mu00 000  | abu00 0o0  
  142.      ^            ^      
  143.  
  144.  ]>
  145.  
  146.  01010000 (nl) | 01100100 (&) | 00110100 (co) | mu00 000  | abu00 0o0
  147.       ^               ^              ^               ^            ^
  148.  
  149.  [
  150.    abu00 01100100 (&) | nu00 00110100 (co)
  151.               ^                   ^        
  152.    -<<
  153.    abu00 01100000 (&) | nu00 00110000 (co)
  154.             ^                   ^        
  155.    
  156.    [ comma
  157.      remove ones
  158.      
  159.      -<-<
  160.      n100 00000000
  161.            ^
  162.      
  163.      clear u field
  164.      <<<<->>>
  165.            
  166.      nu00 000  
  167.           ^      
  168.    ]<
  169.    abu00 01100000 (&) | mu00 000
  170.            ^               ^        
  171.    [ AND
  172.      -<++<<
  173.    ]
  174.    abu00 030 | mu00 000
  175.        ^          ^    
  176.    >
  177.  ]
  178.  
  179.  01010000 (nl) | mu00 000  | abu00 0o0
  180.       ^               ^            ^
  181.  <<
  182.  01010000 (nl) | mu00 000  | abu00 0o0
  183.     ^              ^            ^
  184.  [ LINEFEED: print the number
  185.  
  186.    nu00 01010000
  187.            ^
  188.    [-<<]<<[-]<
  189.    
  190.    n
  191.    ^
  192.    copy n
  193.    
  194.    [->+>+<<]
  195.    0nn
  196.    ^
  197.    >[-<+>]>
  198.    n0n0
  199.      ^
  200.    [ if n is not zero
  201.      [ for the quotient
  202.        [ number  n a q 0 0
  203.          ->>+<[->]>[<+++++++++>>>]<<<<
  204.        ] 0 r q 0 0
  205.        >
  206.        [<+++++ +++++>[-<->]>-<]
  207.        +>
  208.      ]
  209.      <[<<]
  210.    ]
  211.    <[>]
  212.    >>[-]+[>>]<<
  213.    [+++++++[-<++++++>]<.[-]<]
  214.    +++++ +++++.[-]
  215.    
  216.    n0
  217.     ^
  218.    >
  219.    nu00
  220.      ^
  221.  ]
  222.  
  223.  mu00 000  | abu00 0o0
  224.    ^            ^
  225.  >>>
  226.  mu00 000  | abu00 0o0
  227.        ^            ^
  228.  [ binary operation!
  229.  
  230.    abu00 0o0
  231.           ^    
  232.  
  233.    [-<<<+>>>]<<<<[-]+>
  234.    
  235.    ab1o0
  236.       ^
  237.      
  238.      
  239.    [-<<[>>>>>>[>>]+<<[-<<]<<<<-]>]
  240.    
  241.    
  242.  
  243.      bxx000  | a0xx000x
  244.      ^         ^    
  245.      
  246.    
  247.    
  248.    000o00ababab
  249.     ^
  250.    >>[->+<]++++++++
  251.    0008o0ababab
  252.       ^
  253.    [-
  254.      io0ab
  255.      ^
  256.      >>+>>[-<+>]<<<
  257.      io1s0
  258.       ^
  259.      
  260.      i01s0 (or) | i11s0 (xor) | i21s0 (and)
  261.       ^            ^             ^      
  262.      [-  
  263.        i01s0 (xor) | i11s0 (and)
  264.         ^             ^  
  265.        [->->
  266.        
  267.          i00s0 (and)
  268.             ^
  269.          [-[->+<]]
  270.          i000r
  271.             ^
  272.          ++<<
  273.          i002r
  274.           ^
  275.          
  276.        ]>
  277.        i01s0 (xor) | i00or
  278.          ^             ^
  279.        [
  280.          i01s0 (xor)
  281.            ^
  282.          ->[->+<[->-<]]+<
  283.          i00or
  284.            ^
  285.        ]<
  286.        i00or
  287.         ^
  288.      ]>
  289.      
  290.      i01s0 (or) | i00or
  291.        ^            ^
  292.      [->
  293.        i00s0
  294.           ^  
  295.        [>+<[-]]<
  296.        i00or
  297.          ^
  298.      ]
  299.      <<[->>+<<]>+>>>[-<<<<+>>>>]<<
  300.      r1io0
  301.        ^
  302.    ]
  303.    0000r1r1r1r1r1r1r1r10o
  304.                        ^
  305.    >[-]<<[->[-<<++>>]<<<]
  306.    
  307.    000n
  308.      ^
  309.    >[-<<<+>>>]
  310.    n000
  311.       ^
  312.    >>      
  313.    mu00 000
  314.          ^
  315.  ]
  316.  <<<,
  317. ]
Add Comment
Please, Sign In to add comment