Skip to content

Commit

Permalink
Transformed modules return init explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
bjpop committed Jul 10, 2011
1 parent 1aa2925 commit 1cedd4d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 99 deletions.
50 changes: 1 addition & 49 deletions src/Berp/Base/Prims.hs
Expand Up @@ -559,11 +559,6 @@ mapIterator f obj = do
f =<< next iterObj
pass

{-
importModuleRef :: FilePath -> Eval ObjectRef
importModuleRef path = newIORef =<< importModule path
-}

importModule :: FilePath -> Eval Object -> Eval Object
importModule path comp = do
maybeImported <- lookupModuleCache path
Expand All @@ -574,51 +569,8 @@ importModule path comp = do
updateModuleCache path obj
return obj

{-
liftIO $ putStrLn $ "loading " ++ path
obj <- compileModuleAndLoadInit path
liftIO $ putStrLn $ "loaded " ++ path
updateModuleCache path obj
return obj
-}

{-
compileModuleAndLoadInit :: FilePath -> Eval Object
compileModuleAndLoadInit path = do
{-
maybePath <- findModulePath name
case maybePath of
Nothing -> raise ("could not find module")
Just path -> do
compiled <- isCompiled path
if compiled
then liftIO $ load path "init"
else do
-}
liftIO $ putStrLn "compiling to object file"
moduleUnique <- nextModuleUnique
objFile <- liftIO $ compilePythonToObjectFile path -- may raise exception
liftIO $ putStrLn "compiled to object file"
liftIO $ putStrLn $ "loading " ++ " " ++ objFile
loadStatus <- liftIO $ load_ objFile [] "init" -- should catch haskell exceptions here
liftIO $ putStrLn "loaded"
case loadStatus of
LoadSuccess _module init -> init
LoadFailure errs -> error ("load failed: " ++ show errs)
-}

{-
mkModule :: Eval Object
mkModule = do
dict <- mkAttributes =<< getGlobalScopeHashTable
identity <- newIdentity
return $
Module { object_identity = identity
, object_dict = dict }
-}

pushGlobalScope :: Maybe HashTable -> Eval ()
-- we don't have a readl hashtable to push (ie for prims) so we duplicate the top
-- we don't have a real hashtable to push (ie for prims) so we duplicate the top
-- of the stack, to make sure we have something to pop later
pushGlobalScope Nothing = do
scope <- gets state_global_scope
Expand Down
68 changes: 18 additions & 50 deletions src/Berp/Compile/Compile.hs
Expand Up @@ -89,17 +89,20 @@ instance Compilable ModuleSpan where
importedModules)
where
initDecl :: Hask.Exp -> Hask.Decl
initDecl = patBind bogusSrcLoc $ pvar $ name "init"
initDecl = patBind bogusSrcLoc $ pvar $ name initName
pragmas = []
warnings = Nothing
exports = Nothing -- should change this to init
exports = Just [EVar $ UnQual $ name initName]
srcImports names = mkImportStmts $ map mkSrcImport names
stdImports = mkImportStmts [(Prim.preludeModuleName,False,Just []),
(Prim.berpModuleName,False,Nothing)]
imports names = stdImports ++ srcImports (map mkBerpModuleName names)

initName :: String
initName = "init"

mkSrcImport :: String -> (ModuleName, Bool, Maybe [String])
mkSrcImport name = (ModuleName name, True, Just ["init"])
mkSrcImport name = (ModuleName name, True, Just [initName])

mkImportStmts :: [(ModuleName, Bool, Maybe [String])] -> [ImportDecl]
mkImportStmts = map toImportStmt
Expand All @@ -120,35 +123,6 @@ mkImportSpecs :: Maybe [String] -> Maybe (Bool, [ImportSpec])
mkImportSpecs Nothing = Nothing
mkImportSpecs (Just items) = Just (False, map (IVar . name) items)

{-
imports :: [ImportDecl]
imports = [importBerp, importPrelude]
importBerp :: ImportDecl
importBerp =
ImportDecl
{ importLoc = bogusSrcLoc
, importModule = Prim.berpModuleName
, importQualified = False
, importSrc = False
, importAs = Nothing
, importSpecs = Nothing
, importPkg = Nothing
}
importPrelude :: ImportDecl
importPrelude =
ImportDecl
{ importLoc = bogusSrcLoc
, importModule = Prim.preludeModuleName
, importQualified = True
, importSrc = False
, importAs = Nothing
, importSpecs = Nothing
, importPkg = Nothing
}
-}

instance Compilable StatementSpan where
type (CompileResult StatementSpan) = [Stmt]

Expand Down Expand Up @@ -216,7 +190,6 @@ instance Compilable StatementSpan where
-}
compile (StmtExpr { stmt_expr = expr }) = do
(stmts, compiledExpr) <- compileExprComp expr
-- let newStmt = qualStmt $ app Prim.stmt $ parens compiledExpr
let newStmt = qualStmt $ compiledExpr
return (stmts ++ [newStmt])
compile (While { while_cond = cond, while_body = body, while_else = elseSuite }) = do
Expand All @@ -230,7 +203,6 @@ instance Compilable StatementSpan where
-- XXX fixme, only supports one target
compile (For { for_targets = [var], for_generator = generator, for_body = body, for_else = elseSuite }) = do
(generatorStmts, compiledGenerator) <- compileExprObject generator
-- compiledBody <- compileSuiteDo body
compiledStmtss <- compile body
newVar <- freshHaskellVar
writeStmt <- qualStmt <$> compileWrite var (Hask.var newVar)
Expand All @@ -257,7 +229,6 @@ instance Compilable StatementSpan where
attributes <- qualStmt <$> app Prim.pure <$> listE <$> mapM compileClassLocal locals
let klassExp = appFun Prim.klass
[ strE $ identString ident
-- , identToMangledVar ident
, listE compiledArgs
, parens $ doBlock $ compiledBody ++ [attributes]]

Expand All @@ -278,7 +249,6 @@ instance Compilable StatementSpan where
let handlerLam = lamE bogusSrcLoc [pvar asName] handlerExp
compiledElse <- compile elseSuite
compiledFinally <- compile finally
-- returnStmt $ appFun Prim.try [parens bodyExp, handlerLam]
returnStmt $ mkTry (parens bodyExp) handlerLam (concat compiledElse) (concat compiledFinally)
compile (Raise { raise_expr = RaiseV3 raised }) =
case raised of
Expand Down Expand Up @@ -316,20 +286,18 @@ compileWrite ident exp = do
instance Compilable ImportItemSpan where
type CompileResult ImportItemSpan = [Hask.Stmt]
compile (ImportItem {import_item_name = dottedName, import_as_name = maybeAsName }) =
case maybeAsName of
Just _asName -> error "import as name not supported"
Nothing ->
case dottedName of
[ident] -> do
let identStr = ident_string ident
berpIdentStr = mkBerpModuleName identStr
let importExp = appFun Prim.importModule
[strE identStr, qvar (ModuleName berpIdentStr) (name "init")]
(binderStmts, binderExp) <- stmtBinder importExp
writeStmt <- qualStmt <$> compileWrite ident binderExp
addImport identStr
return (binderStmts ++ [writeStmt])
_other -> error ("import of " ++ show dottedName ++ " not supported")
case dottedName of
[ident] -> do
let identStr = ident_string ident
berpIdentStr = mkBerpModuleName identStr
importExp = appFun Prim.importModule
[strE identStr, qvar (ModuleName berpIdentStr) (name initName)]
(binderStmts, binderExp) <- stmtBinder importExp
let objectName = maybe ident id maybeAsName
writeStmt <- qualStmt <$> compileWrite objectName binderExp
addImport identStr
return (binderStmts ++ [writeStmt])
_other -> error ("import of " ++ show dottedName ++ " not supported")

mkBerpModuleName :: String -> String
mkBerpModuleName = ("Berp_" ++)
Expand Down

0 comments on commit 1cedd4d

Please sign in to comment.