packages feed

lhae-0.0.2: src/Model/Cell.hs

module Model.Cell (Cell(..),empty,showValue
                  ,setExpression,setRuntimeErrorValue
                  ,evaluate,addDependent,deleteDependent)
    where

import qualified Data.List as List
import Model.CellContent (CellExpr(EmptyExpr),CellValue(EmptyValue,Error)
                         ,RuntimeReason,Reference)
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}

showValue :: Cell -> String
showValue = show . 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}