Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -15,13 +15,13 @@ isOp _ = False
|
|||
simSYA xs = final ++ [lastStep]
|
||||
where final = scanl f ([],[],"") xs
|
||||
lastStep = (\(x,y,_) -> (reverse y ++ x, [], "")) $ last final
|
||||
f (out,st,_) t | isOp t =
|
||||
f (out,st,_) t | isOp t =
|
||||
(reverse (takeWhile testOp st) ++ out
|
||||
, (t:) $ (dropWhile testOp st), t)
|
||||
| t == "(" = (out, "(":st, t)
|
||||
| t == ")" = (reverse (takeWhile (/="(") st) ++ out,
|
||||
| t == "(" = (out, "(":st, t)
|
||||
| t == ")" = (reverse (takeWhile (/="(") st) ++ out,
|
||||
tail $ dropWhile (/="(") st, t)
|
||||
| True = (t:out, st, t)
|
||||
| otherwise = (t:out, st, t)
|
||||
where testOp x = isOp x && (leftAssoc t && prec t == prec x
|
||||
|| prec t < prec x)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
{-# LANGUAGE LambdaCase #-}
|
||||
import Control.Applicative
|
||||
import Control.Lens
|
||||
import Control.Monad
|
||||
import Control.Monad.Error
|
||||
import Control.Monad.State
|
||||
import System.Console.Readline
|
||||
|
|
@ -60,13 +61,11 @@ pushVal n = _1 %= (OutVal n:)
|
|||
pushParen :: RPNComp ()
|
||||
pushParen = _2 %= (Paren:)
|
||||
|
||||
--Run StateT. `process` is effectively foldM_ with the base case to
|
||||
--format the output string
|
||||
--Run StateT
|
||||
toRPN :: [InToken] -> Either String [OutToken]
|
||||
toRPN xs = evalStateT (process (return ()) xs) ([],[])
|
||||
where process st [] = st >> get >>= \(a,b) -> (reverse a++) <$>
|
||||
(mapM toOut b)
|
||||
process st (x:xs) = process (st >> processToken x) xs
|
||||
toRPN xs = evalStateT process ([],[])
|
||||
where process = mapM_ processToken xs
|
||||
>> get >>= \(a,b) -> (reverse a++) <$> (mapM toOut b)
|
||||
toOut :: StackElem -> RPNComp OutToken
|
||||
toOut (StOp o) = return $ OutOp o
|
||||
toOut Paren = throwError "Unmatched left parenthesis"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue