Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. {-# LANGUAGE ExistentialQuantification #-}
  2. {-# LANGUAGE GADTs #-}
  3. module Spreadsheet where
  4.  
  5. import Data.Text
  6. import Data.Word
  7. import Data.Vector
  8.  
  9. newtype Row = Row Word32
  10. newtype Col = Col Word32
  11.  
  12. --------------------------------------------------------------------------------
  13. -- Spreadsheet Language --
  14. --------------------------------------------------------------------------------
  15.  
  16. -- catch some errors at compile-time with GADTs
  17. -- e.g., never mix up bool and numeric ops during eval
  18. -- no text support yet
  19. data Expr a where
  20. -- numeric operators
  21. Add :: Expr Double -> Expr Double -> Expr Double -- expr + expr
  22. Subtract :: Expr Double -> Expr Double -> Expr Double -- expr - expr
  23. Multiply :: Expr Double -> Expr Double -> Expr Double -- expr * expr
  24. Divide :: Expr Double -> Expr Double -> Expr Double -- expr / expr
  25. Power :: Expr Double -> Expr Double -> Expr Double -- expr ^ expr
  26. Sin :: Expr Double -> Expr Double -- sin(expr)
  27. Negate :: Expr Double -> Expr Double -- -expr
  28. Cos :: Expr Double -> Expr Double -- cos(expr)
  29. Tan :: Expr Double -> Expr Double -- tan(expr)
  30.  
  31. -- array operators
  32. Sum :: [Expr Double] -> Expr Double -- sum (expr, expr, expr, ...)
  33. Any :: [Expr Bool] -> Expr Bool -- any (expr, expr, expr, ...)
  34. All :: [Expr Bool] -> Expr Bool -- all (expr, expr, expr, ...)
  35. Product :: [Expr Double] -> Expr Double -- product (expr, expr, expr, ...)
  36.  
  37. -- boolean operators
  38. LessThan :: Expr Double-> Expr Double -> Expr Bool -- expr < expr
  39. GreaterThan :: Expr Double-> Expr Double -> Expr Bool -- expr > expr
  40. EqualTo :: Expr Double-> Expr Double -> Expr Bool -- expr == expr
  41. LessThanEqual :: Expr Double-> Expr Double -> Expr Bool -- expr <= expr
  42. GreaterThanEqual :: Expr Double-> Expr Double -> Expr Bool -- expr >= expr
  43. And :: Expr Bool-> Expr Bool -> Expr Bool -- expr & expr
  44. Or :: Expr Bool -> Expr Bool -> Expr Bool -- expr | expr
  45.  
  46. -- operational ops: affect env
  47. Assign :: Expr a -> Expr a -- = <expr>
  48. Parens :: Expr a -> Expr a -- (expr)
  49.  
  50. -- Terminal nodes
  51. Number :: Double -> Expr Double -- a number
  52. Reference :: Row -> Col -> Expr a -- $C1
  53. Boolean :: Bool -> Expr Bool -- true | false
  54.  
  55. --------------------------------------------------------------------------------
  56. -- Spreadsheet State --
  57. --------------------------------------------------------------------------------
  58. data Spreadsheet
  59. = Spreadsheet (Vector Cell) -- 2D structure flattened to 1D
  60.  
  61.  
  62. data Cell
  63. = Empty
  64. | forall a. Filled (Expr a) -- lazily evaluated cells
  65.  
  66.  
  67. --------------------------------------------------------------------------------
  68. -- Application Model --
  69. -- (Assumes GUI) --
  70. --------------------------------------------------------------------------------
  71. type TextStart = Word32
  72. type TextEnd = Word32
  73. type CursorPos = Word32
  74. type StartRow = Row
  75. type EndRow = Row
  76. type StartCol = Col
  77. type EndCol = Col
  78. type TopLeft = (Row,Col)
  79. type Width = Word32
  80. type Height = Word32
  81. type RegionSize = (Width,Height)
  82. data Region = Region TopLeft RegionSize
  83.  
  84.  
  85. data Action
  86.  
  87. -- valid actions while in a text input box for a cell; edit mode
  88. = Input Char
  89. | DeleteAtPoint Text CursorPos
  90. | BackspaceAtPoint Text CursorPos
  91. | DeleteWordForward Text CursorPos
  92. | DeleteWordBackwards Text CursorPos
  93. | SelectText TextStart TextEnd
  94. | SelectAll
  95. | CopyText TextStart TextEnd
  96. | CutText TextStart TextEnd
  97. | Paste Text CursorPos
  98. | FinalizeEdit
  99.  
  100. -- valid actions while on a cell; sheet mode
  101. -- leaving off paste functionality for now
  102. | StartEdit Row Col
  103. | ClearCell Row Col
  104. | CopyCell Row Col
  105. | SelectColumn Col
  106. | SelectRow Row
  107. | SelectRegion Region
  108. | ClearCells Region
  109. | CopyCells Region
  110.  
  111. -- movement over cells; sheet mode
  112. | MoveRight
  113. | MoveLeft
  114. | MoveUp
  115. | MoveDown
  116.  
  117. -- auxiliary operations; sheet mode
  118. | Quit
  119. | LoadFrom FilePath
  120. | SaveCurrent
  121. | SaveTo FilePath
  122.  
  123. --------------------------------------------------------------------------------
  124. -- Parser --
  125. --------------------------------------------------------------------------------
  126. data ParseError
  127.  
  128. parseExpr :: String -> Either ParseError (Expr a)
  129. parseExpr = undefined
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement