tricu

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

consumer_source_frontend.tri (1707B)


      1 -- The tricu-owned lexer/parser lowers authored syntax to the existing IR.
      2 !import "consumers.base-source" B
      3 !import "consumers.structural" SCP
      4 !import "consumers.fizzbuzz" F
      5 !import "parser" P
      6 !import "io" !Local
      7 !import "prelude" !Local
      8 
      9 sourceFailureCode input expected = matchResult (diagnostic _ : equal? (P.parserDiagnosticCode diagnostic) expected) (_ _ : false) (B.compileBaseSource input)
     10 
     11 badSourceChecks = foldl and? true [(sourceFailureCode "consumer fizzbuzz [x | x] @ _ => []" 21)
     12    (sourceFailureCode "consumer fizzbuzz [x | rest] @ _ => [z | recur rest @ (2,4)]" 22)
     13    (sourceFailureCode "consumer fizzbuzz [] @ _ + []" 2)
     14    (sourceFailureCode "consumer fizzbuzz [] @ _ => [\"unterminated]" 5)
     15    (sourceFailureCode "consumer fizzbuzz $" 4)]
     16 
     17 checkCompiled compiled =
     18   (program : foldl and? true
     19     [(equal? (fst compiled) "fizzbuzz")
     20      (equal? (SCP.makeConsumer (pair 2 4) (snd program)) F.fizzbuzz)
     21      (equal? (resultOr (t t) (SCP.runConsumer
     22        (SCP.makeConsumer (pair 2 4) (snd program)) F.input)) F.expected)
     23      (equal? (resultOr (t t) (SCP.runConsumer
     24        (SCP.replaceLiteral (SCP.makeConsumer (pair 2 4) (snd program))
     25          "Fizz" "Fizz!") F.input)) F.excitedExpected)
     26      badSourceChecks]) (snd compiled)
     27 
     28 main = io (onResult_ (readFile "demos/consumers/fizzbuzz/fizzbuzz.scp")
     29   (problem : putStrLn (append "read-error: " problem))
     30   (source : lazyResult
     31     (diagnostic _ : putStrLn (append "source-error="
     32       (showNumber (P.parserDiagnosticCode diagnostic))))
     33     (compiled _ : lazyBool
     34       (_ : putStrLn "consumer-source-ok")
     35       (_ : putStrLn "consumer-source-wrong-output")
     36       (checkCompiled compiled))
     37     (B.compileBaseSource source)))
     38 main