From bcc469bc7f1d73e66828637b5b518b7cab8e2781 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Fri, 9 Sep 2022 16:54:06 +0100 Subject: SDCC generation WIP --- src/Micro/Compiler.hs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'src/Micro/Compiler.hs') diff --git a/src/Micro/Compiler.hs b/src/Micro/Compiler.hs index f7ae71f..b765c16 100644 --- a/src/Micro/Compiler.hs +++ b/src/Micro/Compiler.hs @@ -148,15 +148,15 @@ compile x = do (ev, errs) <- return $ (ev, (verifyFuncType ident params ret pos) ++ errs) -- updated with the function (ev, errs) <- - return $ case addSymUniq ev (ident, ftype, priv, pos) of + return $ case addSymUniq ev (newSym ident ftype priv False pos) of Left err -> (ev, err : errs) Right ev -> (ev, errs) -- lambdas can only access local variables (closures aren't supported) fev <- return $ if anon then emptyEnv else ev -- with parameters - (nev, errs) <- return $ foldlEither addSymUniq (addEnv fev, errs) params + (nev, errs) <- return $ foldlEither addSymUniq (addEnv fev, errs) $ map (toSym True) params -- helper for return - nev <- return $ addSym nev ("$fn$", ftype, True, pos) + nev <- return $ addSym nev $ newSym "$fn$" ftype True True pos put (nev, errs) _ <- compileAll body (_, errs) <- get @@ -176,7 +176,7 @@ compile x = do _ -> addError $ Error NonCallable "non callable value in function call" pos (A.Var ident typ val priv pos) -> do (ev, errs) <- get - (ev, errs) <- return $ foldlEither addSymUniq (ev, errs) [(ident, typ, priv, pos)] + (ev, errs) <- return $ foldlEither addSymUniq (ev, errs) [newSym ident typ priv True pos] errs <- return $ if not (definedType typ) @@ -190,7 +190,7 @@ compile x = do (A.Return value pos) -> do (ev, _) <- get case getSyml ev "$fn$" of - Just (_, A.FuncType _ rtyp, _, _) -> do + Just Sym {symType = A.FuncType _ rtyp} -> do r <- typecheckReturn value rtyp case r of Just err -> addError $ Error TypeError err pos @@ -199,17 +199,13 @@ compile x = do (A.Variable ident pos) -> do (ev, _) <- get case getSym ev ident of - Just (_, t, _, _) -> return $ typeResult $ Just t + Just Sym {symType = t} -> return $ typeResult $ Just t Nothing -> addError $ Error Undefined ("undefined \"" ++ ident ++ "\"") pos compileAll :: [A.Expr] -> State CompState CompResult -compileAll ast = - case ast of - (x : xs) -> do - _ <- compile x - compileAll xs - [] -> do - ((Env sym _), errs) <- get - case errs of - [] -> pure $ successResult $ generate sym ast - _ -> return $ errorResult errs +compileAll ast = do + _ <- traverse compile ast + ((Env sym _), errs) <- get + case errs of + [] -> pure $ successResult $ generate sym ast + _ -> return $ errorResult errs -- cgit v1.2.3