diff options
author | Juan J. Martinez <jjm@usebox.net> | 2022-08-13 08:49:39 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2022-08-13 08:49:39 +0100 |
commit | 1f2485fc44cad6155953de43567f9e7cf51c8ed9 (patch) | |
tree | eecc3d7471a1589ee0bde8a23047f136edba5e1b | |
parent | 326386322d26b633672de37f045a53f18f2336a5 (diff) | |
download | micro-lang-hs-1f2485fc44cad6155953de43567f9e7cf51c8ed9.tar.gz micro-lang-hs-1f2485fc44cad6155953de43567f9e7cf51c8ed9.zip |
Extracted function
-rw-r--r-- | src/Compiler.hs | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/Compiler.hs b/src/Compiler.hs index ea97bb8..978e32b 100644 --- a/src/Compiler.hs +++ b/src/Compiler.hs @@ -15,6 +15,19 @@ type CompResult = Either [Error] () startState :: CompState startState = (emptyEnv, []) +-- | @foldlEither fn init xs@ folds left @xs@ applying a function @fn@ that +-- returns either, returning accumulated right and the collected lefts as a +-- list. +foldlEither :: (accr -> b -> Either accl accr) -> (accr, [accl]) -> [b] -> (accr, [accl]) +foldlEither fn init xs = + foldl + ( \(r, l) x -> case fn r x of + Left e -> (r, e : l) + Right x -> (x, l) + ) + init + xs + compile :: [A.Expr] -> State CompState CompResult compile (x : xs) = do case x of @@ -27,14 +40,7 @@ compile (x : xs) = do (nev, nerrs) <- return $ case addSymUniq ev (ident, A.toFuncType params ret, pos) of Left e -> (ev, e : errs) - Right fev -> - foldl - ( \(ev, errs) (id, typ, pos) -> case addSymUniq ev (id, typ, pos) of - Right ev -> (ev, errs) - Left nerr -> (ev, nerr : errs) - ) - (addEnv fev, errs) - params + Right fev -> foldlEither addSymUniq (addEnv fev, errs) params put (nev, nerrs) r <- compile body (_, errs) <- get |