module Funcons.Exceptions where

import Funcons.Types
import Funcons.Printer

import Data.Text

-- handling exception from the interpreter
type IException = (Funcons, Funcons, IE) --global, local, exception
data IE = SortErr String
        | Err String --TODO when used?
        | PartialOp String
        | Internal String 
        | NoRule 
        | NoMoreBranches
        | SideCondFail String
        | InsufficientInput Name
        | InsufficientInputConsumed Name
        | PatternMismatch String
        | StepOnValue [Values]

showIException :: IException -> String
showIException (f0,f,ie) = "Internal Exception: " ++ show ie ++ " on \n" ++ showFuncons f0

instance Show IE where
    show (SortErr err) = "dynamic sort check (" ++ err ++ ")"
    show NoRule      = "no rule to execute"
    show (Err err)   = "exception (" ++ err ++ ")"
    show (Internal err)    = "exception (" ++ err ++ ")"
    show (SideCondFail str)         = str
    show (PatternMismatch str)  = str
    show (InsufficientInput nm) = "insufficient supply for " ++ unpack nm
    show (InsufficientInputConsumed nm) = "insufficient input consumed for entity " ++ unpack nm
    show (PartialOp str) = "partial operation"
    show (NoMoreBranches) = "no more branches to try"
    show (StepOnValue v) = "attempting to step a value: " ++ showValuesSeq v

-- which exceptions stop a rule from executing so that the next one can be attempted?
failsRule :: IException -> Bool
failsRule (_,_,SideCondFail _)      = True
failsRule (_,_,PatternMismatch _)   = True
failsRule (_,_,SortErr  _)          = True
failsRule (_,_,PartialOp  _)        = True
failsRule (_,_,StepOnValue _)       = True
failsRule (_,_,NoMoreBranches)      = True
failsRule (_,_,NoRule)              = True 
failsRule _                         = False