aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2022-10-04 20:25:22 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-05 14:21:53 +0200
commit77968899b90cd9afe2ed2668b290b66cb553ced2 (patch)
tree4446ab6257f21603b45e04fadd4deeae231bffc9 /src
parentc1713b4ea6c542fa2fe0d66b51c5b7b27fedf57a (diff)
Implement naive export system
Diffstat (limited to 'src')
-rw-r--r--src/Interpreter.hs32
-rw-r--r--src/Utils.hs3
2 files changed, 29 insertions, 6 deletions
diff --git a/src/Interpreter.hs b/src/Interpreter.hs
index 33e36ea..37996f4 100644
--- a/src/Interpreter.hs
+++ b/src/Interpreter.hs
@@ -191,12 +191,34 @@ evaluateImport d env asts = do
case args of
[AST { astNode = ASTSymbol qualifier }, AST { astNode = ASTString path }] -> do
- (importedEnv, _) <- runScriptFile builtinEnv path
- let nameMangled = M.mapKeys (\k -> qualifier ++ ":" ++ k) importedEnv
- return (M.union env nameMangled, importAst { astNode = ASTUnit })
+ (evaledRawEnv, _) <- runScriptFile builtinEnv path
+ let exportsVecASTM = M.lookup "exports" evaledRawEnv
+ exportedEnv <- case exportsVecASTM of
+ Just (AST { astNode = ASTVector exportsVec }) -> do
+ exportSyms <- exportsVec $>
+ mapM (\case AST { astNode = ASTSymbol sym } -> return sym
+ ast -> throwL (astPos ast) $ "non-symbol value in exports vector: " ++ show ast)
+ let resultEnv = M.filterWithKey (\k _ -> L.elem k exportSyms) evaledRawEnv
+ return resultEnv
+ Just ast -> throwL (astPos ast) $ "exports symbol set to non-symbol value: " ++ show ast
+ Nothing -> throwL (astPos importAst) $ "no exports vector defined in file: " ++ path
+
+ let nameMangled = M.mapKeys (\k -> qualifier ++ ":" ++ k) exportedEnv
+ return $ (M.union env nameMangled, importAst { astNode = ASTUnit })
[AST { astNode = ASTString path }] -> do
- (importedEnv, _) <- runScriptFile builtinEnv path
- return (M.union env importedEnv, importAst { astNode = ASTUnit })
+ (evaledRawEnv, _) <- runScriptFile builtinEnv path
+ let exportsVecASTM = M.lookup "exports" evaledRawEnv
+ exportedEnv <- case exportsVecASTM of
+ Just (AST { astNode = ASTVector exportsVec }) -> do
+ exportSyms <- exportsVec $>
+ mapM (\case AST { astNode = ASTSymbol sym } -> return sym
+ ast -> throwL (astPos ast) $ "non-symbol value in exports vector: " ++ show ast)
+ let resultEnv = M.filterWithKey (\k _ -> L.elem k exportSyms) evaledRawEnv
+ return resultEnv
+ Just ast -> throwL (astPos ast) $ "exports symbol set to non-symbol value: " ++ show ast
+ Nothing -> throwL (astPos importAst) $ "no exports vector defined in file: " ++ path
+
+ return $ (M.union env exportedEnv, importAst { astNode = ASTUnit })
_ -> throwL (astPos importAst) $ "invalid arguments passed to import!: " ++ show args
evaluateUserFunction :: Depth -> Env -> [AST] -> LContext (Env, AST)
diff --git a/src/Utils.hs b/src/Utils.hs
index 2a2f607..1e9afc5 100644
--- a/src/Utils.hs
+++ b/src/Utils.hs
@@ -18,7 +18,8 @@ data Config = Config {
configVerboseMode :: Bool,
configShowHelp :: Bool,
configPrintEvaled :: Bool,
- configPrintCallStack :: Bool
+ configPrintCallStack :: Bool,
+ configUseREPL :: Bool
}
type LContext a = ReaderT Config (ExceptT LException IO) a