8 import Control.Applicative
11 import Text.Parsers.Simple.ParserCombinators
20 preprocess :: [[Char]] [Char] -> [Char]
21 preprocess ops c = rep '(' maxb (foldr prep (rep ')' maxb []) c)
25 rep :: Char Int [Char] -> [Char]
27 rep c i r = [c:rep c (i-1) r]
29 prep '(' cs = rep '(' maxb cs
30 prep ')' cs = rep ')' maxb cs
31 prep c cs = maybe [c:cs] (\(_, s)->rep ')' s [c:rep '(' s cs])
32 (find (isMember c o fst) (zip2 ops [maxb-1,maxb-2..0]))
34 :: Expr = BinOp Expr Char Expr | Lit Int | Var [Char]
36 parseExpr :: Parser Char Expr
37 parseExpr = flip pChainl1 (flip BinOp <$> pOneOf ['+^-/*'])
38 ( Lit o toInt o toString <$> some pDigit
39 <|> Var <$> some pAlpha
40 <|> pToken '(' *> parseExpr <* pToken ')'
43 //Start = parse parseExpr (preprocess ['a'])
44 //Start = preprocess operators ['a*1']
45 Start = runParser parseExpr (preprocess operators ['5+5*5'])