on master help sign in
Move OpTable into IParser user state
6d27c5eEvan Czaplicki
compiler/Parse/Binop.hs+7−11
@@ -11,10 +11,8 @@ import SourceSyntax.Declaration (Assoc(..))
11import Text.Parsec
11import Text.Parsec
12import Parse.Helpers
12import Parse.Helpers
13
13
14−type OpTable = [(Int, Assoc, String)]
15−
16preludeTable :: OpTable
14preludeTable :: OpTable
17−preludeTable =
15+preludeTable = Map.fromList $ map (\(lvl,assoc,op) -> (op, (lvl,assoc)))
18 [ (9, R, ".")
16 [ (9, R, ".")
19 , (8, R, "^")
17 , (8, R, "^")
20 , (7, L, "*"), (7, L, "/"), (7, L, "mod"), (7, L, "div"), (7, L, "rem")
18 , (7, L, "*"), (7, L, "/"), (7, L, "mod"), (7, L, "div"), (7, L, "rem")
@@ -29,24 +27,22 @@ preludeTable =
29 ]
27 ]
30
28
31opLevel :: OpTable -> String -> Int
29opLevel :: OpTable -> String -> Int
32−opLevel table op = Map.findWithDefault 9 op dict
30+opLevel table op = fst $ Map.findWithDefault (9,R) op table
33− where dict = Map.fromList (map (\(lvl,_,op) -> (op,lvl)) table)
34
31
35opAssoc :: OpTable -> String -> Assoc
32opAssoc :: OpTable -> String -> Assoc
36−opAssoc table op = Map.findWithDefault R op dict
33+opAssoc table op = snd $ Map.findWithDefault (9,R) op table
37− where dict = Map.fromList (map (\(_,assoc,op) -> (op,assoc)) table)
38
34
39hasLevel :: OpTable -> Int -> (String, LExpr t v) -> Bool
35hasLevel :: OpTable -> Int -> (String, LExpr t v) -> Bool
40hasLevel table n (op,_) = opLevel table op == n
36hasLevel table n (op,_) = opLevel table op == n
41
37
42−binops :: OpTable
43− -> IParser (LExpr t v)
38+binops :: IParser (LExpr t v)
44 -> IParser (LExpr t v)
39 -> IParser (LExpr t v)
45 -> IParser String
40 -> IParser String
46 -> IParser (LExpr t v)
41 -> IParser (LExpr t v)
47−binops table term last anyOp =
42+binops term last anyOp =
48 do e <- term
43 do e <- term
44+ table <- getState
49− split (table ++ preludeTable) 0 e =<< nextOps
45+ split (Map.union preludeTable table) 0 e =<< nextOps
50 where
46 where
51 nextOps = choice [ commitIf (whitespace >> anyOp) $ do
47 nextOps = choice [ commitIf (whitespace >> anyOp) $ do
52 whitespace ; op <- anyOp ; whitespace
48 whitespace ; op <- anyOp ; whitespace
compiler/Parse/Expression.hs+1−1
@@ -144,7 +144,7 @@ appExpr = do
144-------- Normal Expressions --------
144-------- Normal Expressions --------
145
145
146binaryExpr :: IParser (LExpr t v)
146binaryExpr :: IParser (LExpr t v)
147−binaryExpr = binops [] appExpr lastExpr anyOp
147+binaryExpr = binops appExpr lastExpr anyOp
148 where lastExpr = addLocation (choice [ ifExpr, letExpr, caseExpr ])
148 where lastExpr = addLocation (choice [ ifExpr, letExpr, caseExpr ])
149 <|> lambdaExpr
149 <|> lambdaExpr
150
150
compiler/Parse/Helpers.hs+5−1
@@ -5,9 +5,11 @@ import Control.Monad
5import Control.Monad.State
5import Control.Monad.State
6import Data.Char (isUpper)
6import Data.Char (isUpper)
7import qualified Data.Set as Set
7import qualified Data.Set as Set
8+import qualified Data.Map as Map
8import SourceSyntax.Helpers as Help
9import SourceSyntax.Helpers as Help
9import SourceSyntax.Location as Location
10import SourceSyntax.Location as Location
10import SourceSyntax.Expression
11import SourceSyntax.Expression
12+import SourceSyntax.Declaration (Assoc)
11import Text.Parsec hiding (newline,spaces,State)
13import Text.Parsec hiding (newline,spaces,State)
12import Text.Parsec.Indent
14import Text.Parsec.Indent
13
15
@@ -35,7 +37,9 @@ jsReserveds = Set.fromList
35
37
36expecting = flip (<?>)
38expecting = flip (<?>)
37
39
40+type OpTable = Map.Map String (Int, Assoc)
41+
38−type IParser a = ParsecT String () (State SourcePos) a
42+type IParser a = ParsecT String OpTable (State SourcePos) a
39
43
40iParse :: IParser a -> SourceName -> String -> Either ParseError a
44iParse :: IParser a -> SourceName -> String -> Either ParseError a
41iParse aParser source_name input =
45iParse aParser source_name input =