/libs/src/Berp/Base/Builtins/Exceptions.hs

https://github.com/yihuang/berp · Haskell · 170 lines · 76 code · 28 blank · 66 comment · 0 complexity · 554ce39e04f5ca20f9dce4e35456b2dc MD5 · raw file

  1. -----------------------------------------------------------------------------
  2. -- |
  3. -- Module : Berp.Base.Builtins.Exceptions
  4. -- Copyright : (c) 2010 Bernie Pope
  5. -- License : BSD-style
  6. -- Maintainer : florbitous@gmail.com
  7. -- Stability : experimental
  8. -- Portability : ghc
  9. --
  10. -- Standard Python exception classes.
  11. --
  12. -----------------------------------------------------------------------------
  13. {-
  14. From http://docs.python.org/dev/3.0/library/exceptions.html
  15. BaseException
  16. +-- SystemExit
  17. +-- KeyboardInterrupt
  18. +-- GeneratorExit
  19. +-- Exception
  20. +-- StopIteration
  21. +-- ArithmeticError
  22. | +-- FloatingPointError
  23. | +-- OverflowError
  24. | +-- ZeroDivisionError
  25. +-- AssertionError
  26. +-- AttributeError
  27. +-- BufferError
  28. +-- EnvironmentError
  29. | +-- IOError
  30. | +-- OSError
  31. | +-- WindowsError (Windows)
  32. | +-- VMSError (VMS)
  33. +-- EOFError
  34. +-- ImportError
  35. +-- LookupError
  36. | +-- IndexError
  37. | +-- KeyError
  38. +-- MemoryError
  39. +-- NameError
  40. | +-- UnboundLocalError
  41. +-- ReferenceError
  42. +-- RuntimeError
  43. | +-- NotImplementedError
  44. +-- SyntaxError
  45. | +-- IndentationError
  46. | +-- TabError
  47. +-- SystemError
  48. +-- TypeError
  49. +-- ValueError
  50. | +-- UnicodeError
  51. | +-- UnicodeDecodeError
  52. | +-- UnicodeEncodeError
  53. | +-- UnicodeTranslateError
  54. +-- Warning
  55. +-- DeprecationWarning
  56. +-- PendingDeprecationWarning
  57. +-- RuntimeWarning
  58. +-- SyntaxWarning
  59. +-- UserWarning
  60. +-- FutureWarning
  61. +-- ImportWarning
  62. +-- UnicodeWarning
  63. +-- BytesWarning
  64. -}
  65. module Berp.Base.Builtins.Exceptions
  66. ( baseException, _s_BaseException
  67. , exception, _s_Exception
  68. , stopIteration, _s_StopIteration
  69. , typeError, _s_TypeError
  70. , nameError, _s_NameError
  71. , lookupError, _s_LookupError
  72. , keyError, _s_KeyError
  73. , valueError, _s_ValueError
  74. , arithmeticError, _s_ArithmeticError
  75. , zeroDivisionError, _s_ZeroDivisionError
  76. , runtimeError, _s_RuntimeError
  77. , notImplementedError, _s_NotImplementedError
  78. )
  79. where
  80. import Control.Monad.Trans (liftIO)
  81. import Berp.Base.Monad (constantEval)
  82. import Berp.Base.Builtins.Utils (primConstant)
  83. import Berp.Base.SemanticTypes (Object (..), Eval, ObjectRef)
  84. import {-# SOURCE #-} Berp.Base.StdTypes.Type (newType)
  85. import {-# SOURCE #-} Berp.Base.StdTypes.Object (object)
  86. import {-# SOURCE #-} Berp.Base.StdTypes.Tuple (tuple)
  87. import {-# SOURCE #-} Berp.Base.StdTypes.Dictionary (dictionary)
  88. import {-# SOURCE #-} Berp.Base.StdTypes.String (string)
  89. mkExceptionType :: String -> [Object] -> Eval Object
  90. mkExceptionType name bases = do
  91. dict <- dictionary []
  92. liftIO $ newType [string name, tuple bases, dict]
  93. baseException :: Object
  94. baseException = constantEval $ mkExceptionType "BaseException" [object]
  95. _s_BaseException :: ObjectRef
  96. _s_BaseException = primConstant baseException
  97. exception :: Object
  98. exception = constantEval $ mkExceptionType "Exception" [baseException]
  99. _s_Exception :: ObjectRef
  100. _s_Exception = primConstant exception
  101. stopIteration :: Object
  102. stopIteration = constantEval $ mkExceptionType "StopIteration" [exception]
  103. _s_StopIteration :: ObjectRef
  104. _s_StopIteration = primConstant stopIteration
  105. arithmeticError :: Object
  106. arithmeticError = constantEval $ mkExceptionType "ArithmeticError" [exception]
  107. _s_ArithmeticError :: ObjectRef
  108. _s_ArithmeticError = primConstant arithmeticError
  109. zeroDivisionError :: Object
  110. zeroDivisionError = constantEval $ mkExceptionType "ZeroDivisionError" [arithmeticError]
  111. _s_ZeroDivisionError :: ObjectRef
  112. _s_ZeroDivisionError = primConstant zeroDivisionError
  113. typeError :: Object
  114. typeError = constantEval $ mkExceptionType "TypeError" [exception]
  115. _s_TypeError :: ObjectRef
  116. _s_TypeError = primConstant typeError
  117. nameError :: Object
  118. nameError = constantEval $ mkExceptionType "NameError" [exception]
  119. _s_NameError :: ObjectRef
  120. _s_NameError = primConstant nameError
  121. lookupError :: Object
  122. lookupError = constantEval $ mkExceptionType "LookupError" [exception]
  123. _s_LookupError :: ObjectRef
  124. _s_LookupError = primConstant lookupError
  125. keyError :: Object
  126. keyError = constantEval $ mkExceptionType "KeyError" [lookupError]
  127. _s_KeyError :: ObjectRef
  128. _s_KeyError = primConstant keyError
  129. valueError :: Object
  130. valueError = constantEval $ mkExceptionType "ValueError" [exception]
  131. _s_ValueError :: ObjectRef
  132. _s_ValueError = primConstant valueError
  133. runtimeError :: Object
  134. runtimeError = constantEval $ mkExceptionType "RuntimeError" [exception]
  135. _s_RuntimeError :: ObjectRef
  136. _s_RuntimeError = primConstant runtimeError
  137. notImplementedError :: Object
  138. notImplementedError = constantEval $ mkExceptionType "NotImplementedError" [runtimeError]
  139. _s_NotImplementedError :: ObjectRef
  140. _s_NotImplementedError = primConstant notImplementedError