tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

fizzbuzz.tri (1467B)


      1 !import consumers_structural *
      2 !import prelude *
      3 !export *
      4 -- Independently authored FizzBuzz IR for reference/lexical comparisons.
      5 -- The two compact state numbers represent phases of periods three and five.
      6 
      7 phase3 = [(pair (patLiteral 0) (outLiteral 2))
      8   (pair (patLiteral 1) (outLiteral 0))
      9   (pair (patLiteral 2) (outLiteral 1))]
     10 phase5 = [(pair (patLiteral 0) (outLiteral 4))
     11   (pair (patLiteral 1) (outLiteral 0))
     12   (pair (patLiteral 2) (outLiteral 1))
     13   (pair (patLiteral 3) (outLiteral 2))
     14   (pair (patLiteral 4) (outLiteral 3))]
     15 label = [(pair (patFork (patLiteral 0) (patLiteral 0)) (outLiteral "FizzBuzz"))
     16   (pair (patFork (patLiteral 0) patAny) (outLiteral "Fizz"))
     17   (pair (patFork patAny (patLiteral 0)) (outLiteral "Buzz"))
     18   (pair patAny (outCapture 0))]
     19 listInput = patFork (patCapture 0) (patCapture 1)
     20 phaseState = patFork (patCapture 2) (patCapture 3)
     21 nextRest = outRecur (outCapture 1) (outFork
     22   (outMatch (outCapture 2) phase3)
     23   (outMatch (outCapture 3) phase5))
     24 
     25 fizzbuzz = makeConsumer (pair 2 4) [
     26   (makeRule patLeaf patAny (outLiteral t))
     27   (makeRule listInput phaseState (outFork
     28     (outMatch (outFork (outCapture 2) (outCapture 3)) label)
     29     nextRest))]
     30 
     31 excited = replaceLiteral fizzbuzz "Fizz" "Fizz!"
     32 input = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
     33 expected = [1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz"]
     34 excitedExpected = [1 2 "Fizz!" 4 "Buzz" "Fizz!" 7 8 "Fizz!" "Buzz" 11 "Fizz!" 13 14 "FizzBuzz"]