Commit 9d11d14a authored by Simon Marlow's avatar Simon Marlow
Browse files

fixes from Igloo

parent b69c9dd0
...@@ -11,9 +11,9 @@ Haskell expressions. Fixity resolution also applies to Haskell ...@@ -11,9 +11,9 @@ Haskell expressions. Fixity resolution also applies to Haskell
patterns, but patterns are a subset of expressions so in what follows patterns, but patterns are a subset of expressions so in what follows
we consider only expressions for simplicity. we consider only expressions for simplicity.
The function @resolve@ takes a list consisting of alternating The function @resolve@ takes a list in which the elements are
expressions and operators; i.e. an instance of the "infixexp" expressions or operators, i.e. an instance of the "infixexp"
non-terminal in the context-free grammar, and returns either @Just e@ non-terminal in the context-free grammar. It returns either @Just e@
where @e@ is the resolved expression, or @Nothing@ if the input does where @e@ is the resolved expression, or @Nothing@ if the input does
not represent a valid expression. In a compiler, of course, it would not represent a valid expression. In a compiler, of course, it would
be better to return more information about the operators involved for be better to return more information about the operators involved for
...@@ -22,13 +22,22 @@ will suffice to illustrate the algorithm here. ...@@ -22,13 +22,22 @@ will suffice to illustrate the algorithm here.
\bprog \bprog
@ @
import Control.Monad
type Prec = Int type Prec = Int
type Var = String type Var = String
data Op = Op String Prec Fixity deriving Eq data Op = Op String Prec Fixity
data Fixity = Leftfix | Rightfix | Nonfix deriving Eq deriving (Eq,Show)
data Exp = Var Var | OpApp Exp Op Exp | Neg Exp deriving Eq
data Tok = TExp Exp | TOp Op | TNeg deriving Eq data Fixity = Leftfix | Rightfix | Nonfix
deriving (Eq,Show)
data Exp = Var Var | OpApp Exp Op Exp | Neg Exp
deriving (Eq,Show)
data Tok = TExp Exp | TOp Op | TNeg
deriving (Eq,Show)
resolve :: [Tok] -> Maybe Exp resolve :: [Tok] -> Maybe Exp
resolve tokens = fmap fst $ parseNeg (Op "" (-1) Nonfix) tokens resolve tokens = fmap fst $ parseNeg (Op "" (-1) Nonfix) tokens
...@@ -121,7 +130,7 @@ illegal. ...@@ -121,7 +130,7 @@ illegal.
The function @parseNeg@ handles prefix negation. If we encounter a The function @parseNeg@ handles prefix negation. If we encounter a
negation operator, and it is legal in this position (the operator to negation operator, and it is legal in this position (the operator to
the left has precedence lower than 6), then we proceed in a similar the left has precedence lower than 6), then we proceed in a similar
way to case (3) above: compute the argument to '-' by recursively way to case (3) above: compute the argument to @-@ by recursively
calling @parseNeg@, and then continue by calling @parse@. calling @parseNeg@, and then continue by calling @parse@.
Note that this algorithm is insensitive to the range and resolution of Note that this algorithm is insensitive to the range and resolution of
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment