1 {-# LANGUAGE ConstraintKinds #-} -- For Machine
2 {-# LANGUAGE DeriveLift #-} -- For TH.Lift (Failure tok)
3 {-# LANGUAGE DerivingStrategies #-} -- For Show (LetName a)
4 -- | Semantic of the parsing instructions used
5 -- to make the parsing control-flow explicit,
6 -- in the convenient tagless-final encoding.
7 module Symantic.Parser.Machine.Instructions where
9 import Data.Bool (Bool(..))
10 import Data.Either (Either)
11 import Data.Eq (Eq(..))
12 import Data.Function ((.))
13 import Data.Kind (Type)
15 import Text.Show (Show(..))
16 import qualified Language.Haskell.TH as TH
17 import qualified Symantic.Parser.Haskell as H
19 import Symantic.Parser.Grammar
20 import Symantic.Parser.Machine.Input
23 type TermInstr = H.Term TH.CodeQ
26 -- | All the 'Instr'uctions.
27 type Machine tok repr =
28 ( InstrBranchable repr
29 , InstrExceptionable repr
34 , InstrReadable tok repr
37 -- ** Type 'ReprInstr'
38 type ReprInstr = {-input-}Type -> {-valueStack-}[Type] -> {-a-}Type -> Type
41 -- | 'TH.Name' of a 'defLet' or 'defJoin'
42 -- indexed by the return type of the factorized 'Instr'uctions.
43 -- This helps type-inferencing.
44 newtype LetName a = LetName { unLetName :: TH.Name }
48 -- ** Class 'InstrValuable'
49 class InstrValuable (repr::ReprInstr) where
50 -- | @('pushValue' x k)@ pushes @(x)@ on the 'valueStack'
51 -- and continues with the next 'Instr'uction @(k)@.
54 repr inp (v ': vs) a ->
56 -- | @('popValue' k)@ pushes @(x)@ on the 'valueStack'.
60 -- | @('lift2Value' f k)@ pops two values from the 'valueStack',
61 -- and pushes the result of @(f)@ applied to them.
63 TermInstr (x -> y -> z) ->
64 repr inp (z ': vs) a ->
65 repr inp (y ': x ': vs) a
66 -- | @('swapValue' k)@ pops two values on the 'valueStack',
67 -- pushes the first popped-out, then the second,
68 -- and continues with the next 'Instr'uction @(k)@.
70 repr inp (x ': y ': vs) a ->
71 repr inp (y ': x ': vs) a
72 -- | @('mapValue' f k)@.
75 repr inp (y ': vs) a ->
77 mapValue f = pushValue f . lift2Value (H.flip H..@ (H.$))
78 -- | @('applyValue' k)@ pops @(x)@ and @(x2y)@ from the 'valueStack',
79 -- pushes @(x2y x)@ and continues with the next 'Instr'uction @(k)@.
81 repr inp (y ': vs) a ->
82 repr inp (x ': (x -> y) ': vs) a
83 applyValue = lift2Value (H.$)
85 -- ** Class 'InstrExceptionable'
86 class InstrExceptionable (repr::ReprInstr) where
87 -- | @('raise' exn)@ raises 'ExceptionLabel' @(exn)@.
88 raise :: ExceptionLabel -> repr inp vs a
89 -- | @('fail' fs)@ raises 'ExceptionFailure' @(exn)@.
90 fail :: Set SomeFailure -> repr inp vs a
91 -- | @('commit' exn k)@ removes the 'Catcher'
92 -- from the 'catchStackByLabel' for the given 'Exception' @(exn)@,
93 -- and continues with the next 'Instr'uction @(k)@.
94 commit :: Exception -> repr inp vs a -> repr inp vs a
95 -- | @('catch' exn l r)@ tries the @(l)@ 'Instr'uction
96 -- in a new failure scope such that if @(l)@ raises an exception within @(exn)@, it is caught,
97 -- then the input (and its 'Horizon') is pushed as it was before trying @(l)@ on the 'valueStack',
98 -- and the control flow goes on with the @(r)@ 'Instr'uction.
101 {-scope-}repr inp vs ret ->
102 {-catcher-}repr inp (Cursor inp ': vs) ret ->
105 -- ** Class 'InstrBranchable'
106 class InstrBranchable (repr::ReprInstr) where
107 -- | @('caseBranch' l r)@.
109 repr inp (x ': vs) r ->
110 repr inp (y ': vs) r ->
111 repr inp (Either x y ': vs) r
112 -- | @('choicesBranch' ps bs d)@.
114 [TermInstr (v -> Bool)] ->
118 -- | @('ifBranch' ok ko)@ pops a 'Bool' from the 'valueStack'
119 -- and continues either with the 'Instr'uction @(ok)@ if it is 'True'
120 -- or @(ko)@ otherwise.
124 repr inp (Bool ': vs) a
125 ifBranch ok ko = choicesBranch [H.id] [ok] ko
127 -- ** Class 'InstrCallable'
128 class InstrCallable (repr::ReprInstr) where
129 -- | @('defLet' n v k)@ binds the 'LetName' @(n)@ to the 'Instr'uction's @(v)@,
131 -- continues with the next 'Instr'uction @(k)@.
133 LetBindings TH.Name (repr inp '[]) ->
136 -- | @('call' n k)@ pass the control-flow to the 'DefLet' named @(n)@,
137 -- and when it 'Ret'urns, continues with the next 'Instr'uction @(k)@.
139 LetName v -> repr inp (v ': vs) a ->
141 -- | @('ret')@ returns the value stored in a singleton 'valueStack'.
144 -- | @('jump' n k)@ pass the control-flow to the 'DefLet' named @(n)@.
149 -- ** Class 'InstrJoinable'
150 class InstrJoinable (repr::ReprInstr) where
152 LetName v -> repr inp (v ': vs) a ->
159 -- ** Class 'InstrInputable'
160 class InstrInputable (repr::ReprInstr) where
161 -- | @('pushInput' k)@ pushes the input @(inp)@ on the 'valueStack'
162 -- and continues with the next 'Instr'uction @(k)@.
164 repr inp (Cursor inp ': vs) a ->
166 -- | @('loadInput' k)@ removes the input from the 'valueStack'
167 -- and continues with the next 'Instr'uction @(k)@ using that input.
170 repr inp (Cursor inp ': vs) a
172 -- ** Class 'InstrReadable'
173 class InstrReadable (tok::Type) (repr::ReprInstr) where
174 -- | @('read' fs p k)@ reads a 'Char' @(c)@ from the input,
175 -- if @(p c)@ is 'True' then continues with the next 'Instr'uction @(k)@,
178 tok ~ InputToken inp =>
180 TermInstr (tok -> Bool) ->
181 repr inp (tok ': vs) a ->