mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
dynamic: add read-file
This commit is contained in:
parent
0edbddd32a
commit
a7cb1fb46f
@ -1430,6 +1430,25 @@
|
||||
|
||||
</p>
|
||||
</div>
|
||||
<div class="binder">
|
||||
<a class="anchor" href="#read-file">
|
||||
<h3 id="read-file">
|
||||
read-file
|
||||
</h3>
|
||||
</a>
|
||||
<div class="description">
|
||||
command
|
||||
</div>
|
||||
<p class="sig">
|
||||
Dynamic
|
||||
</p>
|
||||
<span>
|
||||
|
||||
</span>
|
||||
<p class="doc">
|
||||
|
||||
</p>
|
||||
</div>
|
||||
<div class="binder">
|
||||
<a class="anchor" href="#reload">
|
||||
<h3 id="reload">
|
||||
|
@ -8,9 +8,10 @@ import Data.Maybe (fromMaybe)
|
||||
import Data.List (elemIndex)
|
||||
import System.Exit (exitSuccess, exitFailure, exitWith, ExitCode(..))
|
||||
import System.FilePath (takeDirectory)
|
||||
import qualified Data.Map as Map
|
||||
import System.IO
|
||||
import System.Process (callCommand, spawnCommand, waitForProcess)
|
||||
import Control.Exception
|
||||
import qualified Data.Map as Map
|
||||
|
||||
import Parsing
|
||||
import Emit
|
||||
@ -786,6 +787,19 @@ commandNot [x] =
|
||||
_ ->
|
||||
return (Left (EvalError ("Can't perform logical operation (not) on " ++ pretty x) (info x)))
|
||||
|
||||
commandReadFile :: CommandCallback
|
||||
commandReadFile [filename] =
|
||||
case filename of
|
||||
XObj (Str fname) _ _ -> do
|
||||
exceptional <- liftIO $ ((try $ readFile fname) :: (IO (Either IOException String)))
|
||||
case exceptional of
|
||||
Right contents ->
|
||||
return (Right (XObj (Str contents) (Just dummyInfo) (Just StringTy)))
|
||||
Left _ ->
|
||||
return (Left (EvalError ("The argument to `read-file` `" ++ fname ++ "` does not exist") (info filename)))
|
||||
_ ->
|
||||
return (Left (EvalError ("The argument to `read-file` must be a string, I got `" ++ pretty filename ++ "`") (info filename)))
|
||||
|
||||
commandSaveDocsInternal :: CommandCallback
|
||||
commandSaveDocsInternal [modulePath] = do
|
||||
ctx <- get
|
||||
|
@ -153,6 +153,7 @@ toC toCMode root = emitterSrc (execState (visit startingIndent root) (EmitterSta
|
||||
visitString indent (XObj (Pattern str) (Just i) _) = visitStr' indent str i
|
||||
visitString _ _ = error "Not a string."
|
||||
escaper '\"' acc = "\\\"" ++ acc
|
||||
escaper '\n' acc = "\\n" ++ acc
|
||||
escaper x acc = x : acc
|
||||
escapeString = foldr escaper ""
|
||||
|
||||
|
@ -236,6 +236,7 @@ dynamicModule = Env { envBindings = bindings
|
||||
, addCommand "system-include" 1 commandAddSystemInclude
|
||||
, addCommand "local-include" 1 commandAddLocalInclude
|
||||
, addCommand "save-docs-internal" 1 commandSaveDocsInternal
|
||||
, addCommand "read-file" 1 commandReadFile
|
||||
]
|
||||
++ [("String", Binder emptyMeta (XObj (Mod dynamicStringModule) Nothing Nothing))
|
||||
,("Symbol", Binder emptyMeta (XObj (Mod dynamicSymModule) Nothing Nothing))
|
||||
|
1
test/fixture_file.txt
Normal file
1
test/fixture_file.txt
Normal file
@ -0,0 +1 @@
|
||||
test file contents
|
@ -196,4 +196,8 @@
|
||||
1
|
||||
(test-join)
|
||||
"Symbol.join works as expected")
|
||||
(assert-equal test
|
||||
"test file contents\n"
|
||||
(Dynamic.read-file "test/fixture_file.txt")
|
||||
"Dynamic.read-file works as expected")
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user