Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/berpcompiler.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ maintainer: florbitous@gmail.com
homepage: http://wiki.github.com/bjpop/berp/
build-type: Simple
stability: experimental
tested-with: GHC==7.0.3
tested-with: GHC==7.4.1, GHC==7.6.3, GHC==7.8.3, GHC==7.10.1

Executable berp
ghc-options: -Wall -fno-warn-name-shadowing -fno-warn-orphans
Expand All @@ -34,7 +34,7 @@ Executable berp
language-python,
haskell-src-exts,
filepath,
process == 1.0.*,
process >= 1.0 && < 1.3,
parseargs,
directory,
berplibs == 0.0.3
8 changes: 3 additions & 5 deletions interpreter/berpinterpreter.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ maintainer: florbitous@gmail.com
homepage: http://wiki.github.com/bjpop/berp/
build-type: Simple
stability: experimental
tested-with: GHC==7.0.3
tested-with: GHC==7.6.3, GHC==7.8.3, GHC==7.10.1

Executable berpi
ghc-options: -Wall -fno-warn-name-shadowing -fno-warn-orphans
Expand All @@ -32,13 +32,11 @@ Executable berpi
language-python,
haskell-src-exts,
filepath,
process == 1.0.*,
parseargs,
directory,
berplibs == 0.0.3,
hint == 0.3.3.*,
MonadCatchIO-mtl == 0.3.0.*,
haskeline == 0.6.4.*
hint >= 0.3.3 && < 0.5,
haskeline >= 0.6.4 && < 0.8
other-modules:
Berp.Interpreter.Input
Berp.Interpreter.Monad
Expand Down
9 changes: 4 additions & 5 deletions interpreter/src/Berp/Interpreter/Input.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ isComment (CommentToken {}) = True
isComment _other = False

lastTokenIsColon :: [Token] -> Bool
lastTokenIsColon [] = False
lastTokenIsColon tokens =
isColon $ last tokens
lastTokenIsColon = isColon . reverse
where
isColon :: Token -> Bool
isColon (ColonToken {}) = True
isColon :: [Token] -> Bool
isColon (ColonToken {}:_) = True
isColon (NewlineToken {}:xs) = isColon xs
isColon _other = False

nonEmptyParenStack :: ParseState -> Bool
Expand Down
2 changes: 1 addition & 1 deletion interpreter/src/Berp/Interpreter/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Berp.Interpreter.Monad (Repl, runRepl, withInputState, getGlobalScope) wh

import Control.Monad.Trans as Trans (lift, liftIO)
import Control.Monad.State.Strict as State (StateT (..), evalStateT, gets)
import Control.Monad.CatchIO as CatchIO (MonadCatchIO (..))
-- import Control.Monad.CatchIO as CatchIO (MonadCatchIO (..))
import Language.Haskell.Interpreter (InterpreterT, runInterpreter)
import System.Console.Haskeline as Haskeline (defaultSettings)
import System.Console.Haskeline.IO (initializeInput, InputState)
Expand Down
17 changes: 15 additions & 2 deletions interpreter/src/Berp/Interpreter/Repl.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, StandaloneDeriving, DeriveDataTypeable, CPP #-}
-----------------------------------------------------------------------------
-- |
-- Module : Berp.Interpreter.Repl
Expand All @@ -14,7 +14,13 @@

module Berp.Interpreter.Repl (repl) where

#if MIN_VERSION_base(4,7,0)
import Data.Typeable (Typeable)
import Control.Monad.State.Strict (StateT)
import Control.Monad.Cont (ContT)
#else
import Data.Typeable (Typeable (..), mkTyConApp, mkTyCon)
#endif
import Control.Monad.Trans (lift, liftIO)
import Control.Monad (when)
import System.IO (hSetBuffering, stdout, BufferMode (..))
Expand All @@ -33,7 +39,7 @@ import Berp.Compile.PrimName as Prim (init)
import Berp.Compile.PySyntaxUtils (InterpreterStmt (..))
import Berp.Interpreter.Monad (Repl, runRepl, getGlobalScope)
import Berp.Interpreter.Input (getInputLines)
import Berp.Base.SemanticTypes (Eval, Object (None), HashTable)
import Berp.Base.SemanticTypes (Eval, Object (None), HashTable, EvalState)
import Berp.Base (runWithGlobals)
import Berp.Base.Prims (printObject)

Expand Down Expand Up @@ -95,8 +101,15 @@ printObjectNotNone obj@None = return ()
printObjectNotNone object = printObject object >> liftIO (putStr "\n")

-- these Typeable instances are needed by the Hint interpret function.
#if MIN_VERSION_base(4,7,0)
deriving instance Typeable ContT
deriving instance Typeable StateT
deriving instance Typeable EvalState
deriving instance Typeable Object
#else
instance Typeable Object where
typeOf _ = mkTyConApp (mkTyCon "Object") []

instance Typeable (Eval Object) where
typeOf _ = mkTyConApp (mkTyCon "Eval") [typeOf (undefined :: Object)]
#endif
13 changes: 7 additions & 6 deletions libs/berplibs.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ maintainer: florbitous@gmail.com
homepage: http://wiki.github.com/bjpop/berp/
build-type: Simple
stability: experimental
tested-with: GHC==7.0.3
tested-with: GHC==7.4.1, GHC==7.6.3, GHC==7.8.3, GHC==7.10.1
extra-source-files: src/include/BerpDebug.h

Library
Expand All @@ -31,13 +31,13 @@ Library
base == 4.*,
mtl == 2.*,
containers,
array < 0.4,
array >= 0.3 && < 0.6,
exceptions >= 0.6.1 && < 0.9,
extensible-exceptions == 0.1.*,
filepath,
MonadCatchIO-mtl == 0.3.0.*,
haskell-src-exts == 1.11.*,
language-python == 0.3.*,
directory == 1.1.0.*
haskell-src-exts >= 1.11 && < 1.18,
language-python >= 0.3 && < 0.6,
directory >= 1.1.0 && < 1.3
exposed-modules:
Berp.Base
Berp.Base.HashTable
Expand Down Expand Up @@ -65,6 +65,7 @@ Library
Berp.Base.HashSet
Berp.Base.Ident
Berp.Base.Identity
Berp.Base.LegacyHash
Berp.Base.LiftedIO
Berp.Base.Mangle
Berp.Base.Monad
Expand Down
4 changes: 2 additions & 2 deletions libs/src/Berp/Base/Hash.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE MagicHash, TypeSynonymInstances #-}
{-# LANGUAGE MagicHash, TypeSynonymInstances, FlexibleInstances #-}

-----------------------------------------------------------------------------
-- |
Expand All @@ -16,7 +16,7 @@
module Berp.Base.Hash (Hash (..), Hashed, hashedStr) where

import Berp.Base.Mangle (mangle)
import Data.HashTable as HT (hashString)
import Berp.Base.LegacyHash as HT (hashString)

type Hashed a = (Int, a)

Expand Down
118 changes: 118 additions & 0 deletions libs/src/Berp/Base/LegacyHash.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{-# LANGUAGE Trustworthy #-}
{-# OPTIONS_GHC -funbox-strict-fields #-}

-----------------------------------------------------------------------------
-- |
-- Module : Berp.Base.LegacyHash
-- Copyright : (c) The University of Glasgow 2003
-- License : See the GHC license below.
--
-- Maintainer : (of this branch) florbitous@gmail.com
-- Stability : provisional
-- Portability : portable
--
-- Ported hashString from base-4.6.0.0, since it is not included in
-- the more recent base libraries.
--
-----------------------------------------------------------------------------

{-
The Glasgow Haskell Compiler License

Copyright 2004, The University Court of the University of Glasgow.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

- Neither name of the University nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY COURT OF THE UNIVERSITY OF
GLASGOW AND THE CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
UNIVERSITY COURT OF THE UNIVERSITY OF GLASGOW OR THE CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
-}

module Berp.Base.LegacyHash (hashString) where

import Data.Bits (shiftR)
import Data.Char (ord)
import Data.List (foldl')
import Data.Int (Int32, Int64)

-- | A sample hash function for Strings. We keep multiplying by the
-- golden ratio and adding. The implementation is:
--
-- > hashString = foldl' f golden
-- > where f m c = fromIntegral (ord c) * magic + hashInt32 m
-- > magic = 0xdeadbeef
--
-- Where hashInt32 works just as hashInt shown above.
--
-- Knuth argues that repeated multiplication by the golden ratio
-- will minimize gaps in the hash space, and thus it's a good choice
-- for combining together multiple keys to form one.
--
-- Here we know that individual characters c are often small, and this
-- produces frequent collisions if we use ord c alone. A
-- particular problem are the shorter low ASCII and ISO-8859-1
-- character strings. We pre-multiply by a magic twiddle factor to
-- obtain a good distribution. In fact, given the following test:
--
-- > testp :: Int32 -> Int
-- > testp k = (n - ) . length . group . sort . map hs . take n $ ls
-- > where ls = [] : [c : l | l <- ls, c <- ['\0'..'\xff']]
-- > hs = foldl' f golden
-- > f m c = fromIntegral (ord c) * k + hashInt32 m
-- > n = 100000
--
-- We discover that testp magic = 0.

hashString :: String -> Int32
hashString = foldl' f golden
where f m c = fromIntegral (ord c) * magic + hashInt32 m
magic = 0xdeadbeef

golden :: Int32
golden = 1013904242 -- = round ((sqrt 5 - 1) * 2^32) :: Int32
-- was -1640531527 = round ((sqrt 5 - 1) * 2^31) :: Int32
-- but that has bad mulHi properties (even adding 2^32 to get its inverse)
-- Whereas the above works well and contains no hash duplications for
-- [-32767..65536]

-- | A sample (and useful) hash function for Int and Int32,
-- implemented by extracting the uppermost 32 bits of the 64-bit
-- result of multiplying by a 33-bit constant. The constant is from
-- Knuth, derived from the golden ratio:
--
-- > golden = round ((sqrt 5 - 1) * 2^32)
--
-- We get good key uniqueness on small inputs
-- (a problem with previous versions):
-- (length $ group $ sort $ map hashInt [-32767..65536]) == 65536 + 32768
--
hashInt32 :: Int32 -> Int32
hashInt32 x = mulHi x golden + x

-- hi 32 bits of a x-bit * 32 bit -> 64-bit multiply
mulHi :: Int32 -> Int32 -> Int32
mulHi a b = fromIntegral (r `shiftR` 32)
where r :: Int64
r = fromIntegral a * fromIntegral b
2 changes: 1 addition & 1 deletion libs/src/Berp/Base/Operators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ mulComplexFloatComplex = specialiseOpComplexFloatComplex (Prelude.*)
_other -> raise typeError
(*) x y = binop specialMulName x y

checkDivByZero :: Num a => a -> Eval Object -> Eval Object
checkDivByZero :: (Num a, Eq a) => a -> Eval Object -> Eval Object
checkDivByZero denom comp
| denom Prelude.== 0 = raise zeroDivisionError
| otherwise = comp
Expand Down
5 changes: 4 additions & 1 deletion libs/src/Berp/Base/SemanticTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
module Berp.Base.SemanticTypes
( Procedure, ControlStack (..), EvalState (..), Object (..), Eval, ObjectRef
, HashTable, HashSet, ListArray, Arity, ModuleCache
, initState ) where
, initState
-- re-exports needed for interpreter (due to the type of Eval)
, StateT, ContT, IO
) where

import Data.Typeable (Typeable)
import Control.Monad.State.Strict (StateT)
Expand Down
Loading