tricu

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

cps-worker.tri (4729B)


      1 !import prelude *
      2 !import consumers_structural as S
      3 !import consumers_lowerings_specialize as P
      4 !export *
      5 -- Bounded generic SCP lowering to CPS worker functions. This is not the
      6 -- generic interpreter: finite rules and output templates compile to functions.
      7 -- Workers use (input, success, failure); errors are built only at the edge.
      8 
      9 slotTail next bindings = next (snd bindings)
     10 slot_ self slots name = (lazyList (_ : err "unknown capture" t)
     11   (slot rest : lazyBool (_ : ok fst t)
     12     (_ : bindResult (self rest name) (next _ : ok (slotTail next) t))
     13     (equal? slot name)) slots)
     14 slot slots name = y slot_ slots name
     15 
     16 makeFork left right bindings recur invoke success failure =
     17   left bindings recur invoke (a : right bindings recur invoke (b : success (pair a b)) failure) failure
     18 makeStem child bindings recur invoke success failure =
     19   child bindings recur invoke (a : success (t a)) failure
     20 makeRecur nextInput nextState bindings recur invoke success failure =
     21   nextInput bindings recur invoke
     22     (input : nextState bindings recur invoke
     23       (state : recur input state success failure) failure) failure
     24 makeCall worker input receiver bindings recur invoke success failure =
     25   worker bindings recur invoke
     26     (target : input bindings recur invoke
     27       (argument : invoke target argument
     28         (returned : receiver (pair returned bindings) recur invoke success failure)
     29         failure) failure) failure
     30 makeEqual left right bindings recur invoke success failure =
     31   left bindings recur invoke
     32     (a : right bindings recur invoke
     33       (b : success (equal? a b)) failure) failure
     34 
     35 output_ self output slots = matchPair (tag payload :
     36   lazyBool (_ : ok (bindings recur invoke success failure : success payload) t)
     37     (_ : lazyBool (_ : bindResult (slot slots payload) (get _ :
     38       ok (bindings recur invoke success failure : success (get bindings)) t))
     39       (_ : lazyBool (_ : matchPair (left right : bindResult (self left slots)
     40         (a _ : bindResult (self right slots) (b _ : ok (makeFork a b) t))) payload)
     41         (_ : lazyBool (_ : matchPair (nextInput nextState : bindResult (self nextInput slots)
     42           (a _ : bindResult (self nextState slots) (b _ : ok (makeRecur a b) t))) payload)
     43           (_ : lazyBool (_ : matchPair (worker more : matchPair (argument continuation : matchPair
     44             (returnSlot receiver : bindResult (self worker slots) (a _ :
     45               bindResult (self argument slots) (b _ :
     46                 bindResult (self receiver (pair returnSlot slots)) (c _ :
     47                   ok (makeCall a b c) t)))) continuation) more) payload)
     48             (_ : lazyBool (_ : bindResult (self payload slots) (child _ : ok (makeStem child) t))
     49               (_ : lazyBool (_ : err "unsupported output form in CPS worker" t)
     50                 (_ : lazyBool (_ : matchPair (left right : bindResult (self left slots)
     51                   (a _ : bindResult (self right slots) (b _ : ok (makeEqual a b) t))) payload)
     52                   (_ : err "unsupported output form in CPS worker" t) (equal? tag 8)) (equal? tag 6)) (equal? tag 5))
     53             (equal? tag 4)) (equal? tag 3)) (equal? tag 2))
     54       (equal? tag 1)) (equal? tag 0)) output
     55 compileOutput output slots = y output_ output slots
     56 
     57 branch inputMatcher stateMatcher result fallback input state recur invoke success failure =
     58   lazyMaybe (_ : fallback input state recur invoke success failure)
     59     (inputBindings : lazyMaybe (_ : fallback input state recur invoke success failure)
     60       (bindings : result bindings recur invoke success failure)
     61       (stateMatcher state inputBindings)) (inputMatcher input t)
     62 rules_ self rules = (lazyList (_ : ok (input state recur invoke success failure : failure "no matching consumer rule") t)
     63   (rule rest : matchPair (inputPattern more : matchPair (statePattern template :
     64     bindResult (self rest) (fallback _ : bindResult
     65       (compileOutput template (P.patternSlots statePattern (P.patternSlots inputPattern t)))
     66       (result _ : ok (branch (P.indexedPattern inputPattern)
     67         (P.indexedPattern statePattern) result fallback) t))) more) rule) rules)
     68 compileRules rules = y rules_ rules
     69 compile consumer = bindResult (compileRules (snd consumer)) (function _ : ok (S.makeConsumer (fst consumer) function) t)
     70 
     71 run_ self input state program success failure = (snd program) input state (nextInput nextState ok fail : self nextInput nextState program ok fail) (worker argument ok fail : worker argument ok fail) success failure
     72 run program input success failure = y run_ input (fst program) program success failure
     73 makeWorker program = (input success failure : run program input success failure)
     74 runChecked program input = run program input (value : ok value t) (message : err message t)