OpPrec.hs 3.33 KB
Newer Older
1 2
{- |
    Module      :  $Header$
Björn Peemöller 's avatar
Björn Peemöller committed
3 4
    Description :  Environment of operator precedences
    Copyright   :  (c) 2002 - 2004, Wolfgang Lux
Björn Peemöller 's avatar
Björn Peemöller committed
5
                       2011 - 2013, Björn Peemöller
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    License     :  OtherLicense

    Maintainer  :  bjp@informatik.uni-kiel.de
    Stability   :  experimental
    Portability :  portable

    In order to parse infix expressions correctly, the compiler must know
    the precedence and fixity of each operator. Operator precedences are
    associated with entities and will be checked after renaming was
    applied. Nevertheless, we need to save precedences for ambiguous names
    in order to handle them correctly while computing the exported
    interface of a module.

    If no fixity is assigned to an operator, it will be given the default
    precedence 9 and assumed to be a left-associative operator.

Björn Peemöller 's avatar
Björn Peemöller committed
22
    /Note:/ this modified version uses Haskell type 'Integer'
23 24 25 26
    for representing the precedence. This change had to be done due to the
    introduction of unlimited integer constants in the parser / lexer.
-}
module Env.OpPrec
27
  ( OpPrec (..), defaultP, defaultAssoc, defaultPrecedence, mkPrec
Björn Peemöller 's avatar
Björn Peemöller committed
28
  , OpPrecEnv,  PrecInfo (..), bindP, lookupP, qualLookupP, initOpPrecEnv
Björn Peemöller 's avatar
Björn Peemöller committed
29
  ) where
30 31

import Curry.Base.Ident
Björn Peemöller 's avatar
Björn Peemöller committed
32
import Curry.Syntax     (Infix (..))
33 34 35

import Base.TopEnv

36 37
import Data.Maybe (fromMaybe)

Björn Peemöller 's avatar
Björn Peemöller committed
38
-- |Operator precedence.
39 40 41
data OpPrec = OpPrec Infix Precedence deriving Eq

type Precedence = Integer
42

43 44
-- TODO: Change to real show instance and provide Pretty instance
-- if used anywhere.
45 46 47 48 49 50
instance Show OpPrec where
  showsPrec _ (OpPrec fix p) = showString (assoc fix) . shows p
    where assoc InfixL = "left "
          assoc InfixR = "right "
          assoc Infix  = "non-assoc "

51
-- |Default operator declaration (associativity and precedence).
52
defaultP :: OpPrec
53 54 55 56 57 58 59 60 61 62 63 64
defaultP = OpPrec defaultAssoc defaultPrecedence

-- |Default operator associativity.
defaultAssoc :: Infix
defaultAssoc = InfixL

-- |Default operator precedence.
defaultPrecedence :: Precedence
defaultPrecedence = 9

mkPrec :: Maybe Precedence -> Precedence
mkPrec mprec = fromMaybe defaultPrecedence mprec
65

Björn Peemöller 's avatar
Björn Peemöller committed
66
-- |Precedence information for an identifier.
67 68 69 70 71
data PrecInfo = PrecInfo QualIdent OpPrec deriving (Eq, Show)

instance Entity PrecInfo where
  origName (PrecInfo op _) = op

Björn Peemöller 's avatar
Björn Peemöller committed
72
-- |Environment mapping identifiers to their operator precedence.
Björn Peemöller 's avatar
Björn Peemöller committed
73
type OpPrecEnv = TopEnv PrecInfo
74

Björn Peemöller 's avatar
Björn Peemöller committed
75 76 77 78 79 80 81 82 83
-- |Initial 'OpPrecEnv'.
initOpPrecEnv :: OpPrecEnv
initOpPrecEnv = predefTopEnv qConsId consPrec emptyTopEnv

-- |Precedence of list constructor.
consPrec :: PrecInfo
consPrec = PrecInfo qConsId (OpPrec InfixR 5)

-- |Bind an operator precedence.
Björn Peemöller 's avatar
Björn Peemöller committed
84
bindP :: ModuleIdent -> Ident -> OpPrec -> OpPrecEnv -> OpPrecEnv
85
bindP m op p
Björn Peemöller 's avatar
Björn Peemöller committed
86 87
  | hasGlobalScope op = bindTopEnv fun op info . qualBindTopEnv fun qop info
  | otherwise         = bindTopEnv fun op info
88 89 90 91 92 93 94 95
  where qop  = qualifyWith m op
        info = PrecInfo qop p
        fun  = "Env.OpPrec.bindP"

-- The lookup functions for the environment which maintains the operator
-- precedences are simpler than for the type and value environments
-- because they do not need to handle tuple constructors.

Björn Peemöller 's avatar
Björn Peemöller committed
96
-- |Lookup the operator precedence for an 'Ident'.
Björn Peemöller 's avatar
Björn Peemöller committed
97
lookupP :: Ident -> OpPrecEnv -> [PrecInfo]
98 99
lookupP = lookupTopEnv

Björn Peemöller 's avatar
Björn Peemöller committed
100
-- |Lookup the operator precedence for an 'QualIdent'.
Björn Peemöller 's avatar
Björn Peemöller committed
101
qualLookupP :: QualIdent -> OpPrecEnv -> [PrecInfo]
102
qualLookupP = qualLookupTopEnv