summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2022-01-31 22:49:36 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2022-01-31 22:49:36 +0200
commitb235c2887ed60a8fd48263f6c53e90ee7e2073fd (patch)
tree2a47ec5bc38ef95b65b130d277e6ba41dc9f2213
parent7390af67e18e6d17934541dd3c2d6d46cfad84d0 (diff)
Some general refactoring
-rwxr-xr-xrun.sh5
-rw-r--r--src/interpreter.hs14
-rw-r--r--src/main.hs14
-rw-r--r--src/parser.hs20
-rw-r--r--src/utils.hs2
5 files changed, 25 insertions, 30 deletions
diff --git a/run.sh b/run.sh
index 588afe3..460a851 100755
--- a/run.sh
+++ b/run.sh
@@ -1,8 +1,5 @@
#!/bin/bash
set -uo pipefail
-ghc -o interpreter src/*.hs
+ghc -v0 -o interpreter src/*.hs
./interpreter $@
-ret=$?
-rm src/*.{hi,o}
-exit $ret
diff --git a/src/interpreter.hs b/src/interpreter.hs
index 9ba4b3c..fb3b34a 100644
--- a/src/interpreter.hs
+++ b/src/interpreter.hs
@@ -5,13 +5,11 @@ import Data.Char
import Data.Map (Map, (!))
import qualified Data.Map as M
import LTypes
-import System.IO (BufferMode (NoBuffering), hSetBuffering, stdin)
import Utils
-debugWaitForChar Config {configDebugMode = mode} =
+debugWaitForChar Config {configDebugMode = mode} = do
if mode
then do
- hSetBuffering stdin NoBuffering
c <- getChar
pure $ case c of
'q' -> ExecExit
@@ -23,16 +21,16 @@ debugPrint Config {configDebugMode = mode} message =
then liftIO $ putStrLn $ "[debug] " ++ message
else pure ()
-interpretSource :: Config -> LState -> ExceptT LException IO ()
-interpretSource config LState {lSource = []} = pure ()
+interpretSource :: Config -> LState -> ExceptT LException IO LState
+interpretSource config state@LState {lSource = []} = pure state
interpretSource config state@LState {lSource = (word : rest)} = do
+ liftIO $ debugPrint config $ "processing " ++ show word ++ ", current state ="
+ liftIO $ debugState config state
newState <- interpretWord state {lSource = rest} word
- liftIO $ debugPrint config $ "processed " ++ show word ++ ", newState ="
- liftIO $ debugState config newState
step <- liftIO $ debugWaitForChar config
case step of
ExecContinue -> interpretSource config newState
- ExecExit -> pure ()
+ ExecExit -> pure newState
interpretWord :: LState -> LWord -> ExceptT LException IO LState
-- non-nestable structures
diff --git a/src/main.hs b/src/main.hs
index 39caad9..e23dcf3 100644
--- a/src/main.hs
+++ b/src/main.hs
@@ -10,6 +10,7 @@ import LTypes
import Parser
import System.Environment (getArgs)
import System.Exit (exitFailure, exitSuccess)
+import System.IO (BufferMode (NoBuffering), hSetBuffering, stdin)
import Utils
getConfig config [] = config
@@ -24,7 +25,7 @@ getFileName :: Config -> ExceptT LException IO String
getFileName Config {configFileNameM = fileNameM} = do
case fileNameM of
Just str -> pure str
- Nothing -> throwError $ LException "no file name specified"
+ Nothing -> throwError $ LException "no filename specified"
bootstrap :: [String] -> ExceptT LException IO ()
bootstrap args = do
@@ -33,6 +34,7 @@ bootstrap args = do
{ configFileNameM = Nothing,
configDebugMode = False
}
+
let config = getConfig initialConfig args
fileName <- getFileName config
@@ -51,15 +53,15 @@ bootstrap args = do
lSource = parsedSource,
lStrLitRefMap = strLitRefMap
}
- interpretSource config initialState
+ void $ interpretSource config initialState
main :: IO ()
main = do
+ hSetBuffering stdin NoBuffering
args <- getArgs
result <- runExceptT $ bootstrap args
case result of
- Left ex -> case ex of
- LException errStr -> do
- putStrLn $ "[error] " ++ errStr
- exitFailure
+ Left (LException errStr) -> do
+ putStrLn $ "[error] " ++ errStr
+ exitFailure
_ -> exitSuccess
diff --git a/src/parser.hs b/src/parser.hs
index e1bea13..9a24fda 100644
--- a/src/parser.hs
+++ b/src/parser.hs
@@ -22,13 +22,11 @@ removeComments source =
let lines_ = lines source $> map (T.pack .> T.splitOn (T.pack "--") .> head .> T.unpack)
in unlines lines_
-processStringLiterals = processStringLiterals' Nothing M.empty
-
-processStringLiterals' :: Maybe String -> Map String [LWord] -> String -> ExceptT LException IO (String, Map String [LWord])
-processStringLiterals' currentM refMap [] = case currentM of
+processStringLiterals :: Maybe String -> Map String [LWord] -> String -> ExceptT LException IO (String, Map String [LWord])
+processStringLiterals currentM refMap [] = case currentM of
Just _ -> throwError $ LException "nonterminated string literal"
Nothing -> pure ([], refMap)
-processStringLiterals' currentM refMap (c : source) = case c of
+processStringLiterals currentM refMap (c : source) = case c of
'"' -> case currentM of
Just str -> do
-- string literal ends
@@ -36,18 +34,18 @@ processStringLiterals' currentM refMap (c : source) = case c of
let strP = str $> map LChar .> LPhrase
interpolated <- processSLInterpolations strP
let newRefMap = M.insert hash interpolated refMap
- (resSource, resRefMap) <- processStringLiterals' Nothing newRefMap source
+ (resSource, resRefMap) <- processStringLiterals Nothing newRefMap source
pure ("##" ++ hash ++ resSource, resRefMap)
Nothing ->
-- string literal starts
- processStringLiterals' (Just "") refMap source
+ processStringLiterals (Just "") refMap source
other -> case currentM of
Just str ->
-- add char to current string literal
- processStringLiterals' (Just $ str ++ [c]) refMap source
+ processStringLiterals (Just $ str ++ [c]) refMap source
Nothing ->
-- proceed normally
- processStringLiterals' Nothing refMap source $> fmap (B.first (c :))
+ processStringLiterals Nothing refMap source $> fmap (B.first (c :))
processSLInterpolations :: LWord -> ExceptT LException IO [LWord]
processSLInterpolations strP@(LPhrase lChars)
@@ -73,5 +71,5 @@ processSLInterpolations p = throwError $ LException $ "non-phrase in processSLIn
parseSource source = do
let woComments = source $> removeComments
- (woStringLiterals, stringLiteralRefMap) <- processStringLiterals woComments
- pure (woStringLiterals $> words .> map parseWord, stringLiteralRefMap)
+ (woStringLiterals, stringLiteralRefMap) <- processStringLiterals Nothing M.empty woComments
+ pure (woStringLiterals $> words .> map parseWord, stringLiteralRefMap) \ No newline at end of file
diff --git a/src/utils.hs b/src/utils.hs
index 4ffdf3b..252804f 100644
--- a/src/utils.hs
+++ b/src/utils.hs
@@ -49,5 +49,5 @@ safeBreak cond ex = safeBreak' cond ex []
safeBreak' _ ex _ [] = throwError ex
safeBreak' cond ex acc lst@(x : xs)
- | cond x = pure (acc, lst)
+ | cond x = pure (reverse acc, lst)
| otherwise = safeBreak' cond ex (x : acc) xs