-
Notifications
You must be signed in to change notification settings - Fork 1
/
PrecInfo.lhs
56 lines (45 loc) · 1.54 KB
/
PrecInfo.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
% -*- LaTeX -*-
% $Id: PrecInfo.lhs 2721 2008-06-13 16:17:39Z wlux $
%
% Copyright (c) 1999-2008, Wolfgang Lux
% See LICENSE for the full license.
%
\nwfilename{PrecInfo.lhs}
\section{Operator Precedences}
In order to parse infix expressions correctly, the compiler must know
the precedence and associativity of each operator. Operator
precedences are associated with entities and are checked after
renaming. If no fixity is assigned to an operator, it is given the
default precedence 9 and assumed to be a left-associative operator.
\begin{verbatim}
> module PrecInfo where
> import Curry
> import TopEnv
> data OpPrec = OpPrec Infix Integer deriving Eq
> instance Show OpPrec where
> showsPrec _ (OpPrec fix p) = showString (assoc fix) . shows p
> where assoc InfixL = "left "
> assoc InfixR = "right "
> assoc Infix = "non-assoc "
> defaultPrec :: OpPrec
> defaultPrec = OpPrec InfixL defaultP
> defaultP :: Integer
> defaultP = 9
\end{verbatim}
Operator precedences that are different from the default are recorded
in the precedence environment. A top-level environment is sufficient
because precedences are checked after renaming.
\begin{verbatim}
> type PEnv = TopEnv PrecInfo
> data PrecInfo = PrecInfo QualIdent OpPrec deriving (Eq,Show)
> instance Entity PrecInfo where
> origName (PrecInfo op _) = op
> merge p1 p2
> | p1 == p2 = Just p1
> | otherwise = Nothing
\end{verbatim}
The initial precedence environment \texttt{initPEnv} is empty.
\begin{verbatim}
> initPEnv :: PEnv
> initPEnv = emptyTopEnv
\end{verbatim}