module Model.Cell (Cell(..),empty,showValueInGrid ,setExpression,setRuntimeErrorValue ,evaluate,addDependent,deleteDependent) where import qualified Data.List as List import Model.CellContent (CellExpr(EmptyExpr),RuntimeReason,Reference ,CellValue(EmptyValue,Error)) import qualified Model.CellContent as CC import qualified Model.CellExpression.Evaluator as E import CellCoordinate (CellCoord) data Cell = Cell { expression :: CellExpr , value :: CellValue , dependent :: [CellCoord] , dependencies :: [Reference] , input :: String } empty :: Cell empty = Cell EmptyExpr EmptyValue [] [] "" setExpression :: String -> CellExpr -> Cell -> Cell setExpression input expr cell = cell { input = input , expression = expr , dependencies = CC.dependencies expr} setRuntimeErrorValue :: RuntimeReason -> Cell -> Cell setRuntimeErrorValue reason cell = cell {value = Error reason} showValueInGrid :: Cell -> String showValueInGrid = CC.showValueInGrid . value evaluate :: E.Lookup -> Cell -> IO Cell evaluate lookup cell = do value' <- E.evaluate lookup $ expression cell return $ cell {value = value'} addDependent :: CellCoord -> Cell -> Cell addDependent coords cell = cell {dependent = coords : (dependent cell)} deleteDependent :: CellCoord -> Cell -> Cell deleteDependent coords cell = cell {dependent = List.delete coords $ dependent cell}