From 01b4fb55d0a7d90ba6a62d8473ab61aa5bbee539 Mon Sep 17 00:00:00 2001 From: Aaron Schmidlkofer Date: Sat, 31 Aug 2019 14:12:33 -0500 Subject: [PATCH 1/9] Added :export functionality --- app/Main.hs | 10 ++++++++-- src/Helper.hs | 12 +++++++++++- src/Parser.hs | 9 +++++++++ src/Syntax.hs | 1 + 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 3e87037..3078166 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -6,7 +6,7 @@ import Reducer import Helper import Control.Monad.State -import System.IO (hFlush, stdout) +import System.IO (hFlush, stdout, hPutStrLn, hClose, openFile, IOMode(WriteMode)) import Debug.Trace import System.Exit import System.Console.Haskeline @@ -93,7 +93,13 @@ execute line env = Import f -> do content <- liftIO $ readFile (importPath ++ f ++ ".plam") let exprs = lines content - execAll exprs env + execAll exprs env + Export f -> do + outFile <- liftIO $ openFile (importPath ++ f ++ ".plam") WriteMode + liftIO $ mapM_ (saveGlobal outFile) env + liftIO $ hClose outFile + outputStrLn("--- exported to " ++ f ++ " sucessful.") + return env Review r -> do case r of "all" -> do diff --git a/src/Helper.hs b/src/Helper.hs index 245c949..4f49925 100644 --- a/src/Helper.hs +++ b/src/Helper.hs @@ -5,7 +5,7 @@ import Reducer import Parser import Control.Monad.State -import System.IO (hFlush, stdout) +import System.IO (hFlush, stdout, Handle, hPutStrLn) import Debug.Trace import System.Console.Haskeline @@ -14,6 +14,16 @@ import System.Console.Haskeline showGlobal :: (String, Expression) -> InputT IO () showGlobal (n, e) = outputStrLn ("--- " ++ show n ++ " = " ++ show e) +removeLambda :: String -> String +removeLambda target = + let + repl 'λ' = '\\' + repl c = c + in map repl target + +saveGlobal :: Handle -> (String, Expression) -> IO () +saveGlobal h (n, e) = hPutStrLn h (n ++ " = " ++ (removeLambda (show e))) + convertToName :: Environment -> Expression -> String convertToName [] exp = findNumeral exp convertToName ((v,e):rest) ex diff --git a/src/Parser.hs b/src/Parser.hs index 4410f7c..1eb55a5 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -164,6 +164,13 @@ parseImport = do f <- filename return $ Import f +parseExport :: Parser Command +parseExport = do + reserved ":export" + spaces + f <- filename + return $ Export f + parseReview :: Parser Command parseReview = do reserved ":review" @@ -200,12 +207,14 @@ parseLine :: Parser Command parseLine = try parseDefine <|> parseShowDetailed <|> parseImport + <|> parseExport <|> parseReview <|> parseRun <|> parsePrint <|> parseComment <|> parseShow <|> parseEmptyLine + ------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------- diff --git a/src/Syntax.hs b/src/Syntax.hs index 382d8ae..7d9aeb1 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -43,6 +43,7 @@ data Command = Define String Expression | Show Expression | ShowDetailed Expression | Import String + | Export String | Review String | Comment String | Run String From e80a8dbd14828102bd00b8fcf36dfd18a95c2324 Mon Sep 17 00:00:00 2001 From: Aaron Schmidlkofer Date: Sat, 31 Aug 2019 14:17:19 -0500 Subject: [PATCH 2/9] Updated README.md to include :export description --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 0ef0206..45a9638 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,15 @@ A block of code in pLam is a line, and possible lines (commands) are the followi - example: `:import std` - restriction: `.plam` has to be inside `import/` directory within the pLam project directory + + +### Export + +- syntax `:export .plam` +- example: `:export test` +- restriction: `.plam cannot already exist + ### Comment From 7fba0cd769e906c67aeb98d1afadea8dcc8aa982 Mon Sep 17 00:00:00 2001 From: Aaron Schmidlkofer Date: Sat, 31 Aug 2019 14:42:00 -0500 Subject: [PATCH 3/9] Added file existence check to :export --- app/Main.hs | 14 ++++++++++---- pLam.cabal | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 3078166..87eeee1 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -11,6 +11,7 @@ import Debug.Trace import System.Exit import System.Console.Haskeline import System.Environment +import System.Directory (doesFileExist) version = "2.0.0" @@ -95,10 +96,15 @@ execute line env = let exprs = lines content execAll exprs env Export f -> do - outFile <- liftIO $ openFile (importPath ++ f ++ ".plam") WriteMode - liftIO $ mapM_ (saveGlobal outFile) env - liftIO $ hClose outFile - outputStrLn("--- exported to " ++ f ++ " sucessful.") + fileExists <- liftIO $ doesFileExist (importPath ++ f ++ ".plam") + if not fileExists + then do + outFile <- liftIO $ openFile (importPath ++ f ++ ".plam") WriteMode + liftIO $ mapM_ (saveGlobal outFile) env + liftIO $ hClose outFile + outputStrLn("--- exported to " ++ f ++ " sucessful.") + else do + outputStrLn("--- export failed : " ++ f ++ " already exists") return env Review r -> do case r of diff --git a/pLam.cabal b/pLam.cabal index a3226b0..a295f43 100644 --- a/pLam.cabal +++ b/pLam.cabal @@ -32,6 +32,7 @@ executable plam , mtl , containers , haskeline + , directory default-language: Haskell2010 source-repository head From 994c595e0a11e6149336ffcfb31e93d847b41287 Mon Sep 17 00:00:00 2001 From: Aaron Schmidlkofer Date: Sat, 31 Aug 2019 14:55:36 -0500 Subject: [PATCH 4/9] Added :review functionality to running programs --- app/Main.hs | 7 +++++++ src/Helper.hs | 3 +++ 2 files changed, 10 insertions(+) diff --git a/app/Main.hs b/app/Main.hs index 87eeee1..7a7078e 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -161,6 +161,13 @@ execJustProg (line:ls) env = Right exp -> do autoProgReduce env exp 0 execJustProg ls env' + Review r -> do + case r of + "all" -> do + putStrLn " ENVIRONMENT:" + mapM_ printGlobal env + otherwise -> putStrLn ("--- definition of " ++ show r ++ ": " ++ reviewVariable env r) + execJustProg ls env Print s -> do putStrLn s execJustProg ls env diff --git a/src/Helper.hs b/src/Helper.hs index 4f49925..2d51d36 100644 --- a/src/Helper.hs +++ b/src/Helper.hs @@ -14,6 +14,9 @@ import System.Console.Haskeline showGlobal :: (String, Expression) -> InputT IO () showGlobal (n, e) = outputStrLn ("--- " ++ show n ++ " = " ++ show e) +printGlobal :: (String, Expression) -> IO () +printGlobal (n, e) = putStrLn ("--- " ++ show n ++ " = " ++ show e) + removeLambda :: String -> String removeLambda target = let From 5b9144a4b468ecdd4570ae6b06ece9b991f47db7 Mon Sep 17 00:00:00 2001 From: Aaron Schmidlkofer Date: Sat, 31 Aug 2019 15:01:25 -0500 Subject: [PATCH 5/9] Fixed Typo in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 45a9638..d23e6d8 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,7 @@ A block of code in pLam is a line, and possible lines (commands) are the followi - syntax `:export .plam` - example: `:export test` -- restriction: `.plam cannot already exist +- restriction: `.plam` cannot already exist From 3846fb261544597d043731d275eef794852a8560 Mon Sep 17 00:00:00 2001 From: Aaron Schmidlkofer Date: Sat, 31 Aug 2019 15:27:13 -0500 Subject: [PATCH 6/9] Fixed export to no longer reverse definition order --- app/Main.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Main.hs b/app/Main.hs index 7a7078e..9b6d6c1 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -100,7 +100,7 @@ execute line env = if not fileExists then do outFile <- liftIO $ openFile (importPath ++ f ++ ".plam") WriteMode - liftIO $ mapM_ (saveGlobal outFile) env + liftIO $ mapM_ (saveGlobal outFile) (reverse env) liftIO $ hClose outFile outputStrLn("--- exported to " ++ f ++ " sucessful.") else do From d398b5f6cc6704e8ccbe5fc189a4362343975aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Lovni=C4=8Dki?= Date: Sun, 1 Sep 2019 13:09:08 +0200 Subject: [PATCH 7/9] app/Main: change successful ':export' wording and explicitly notify location --- app/Main.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Main.hs b/app/Main.hs index 9b6d6c1..a9982de 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -102,7 +102,7 @@ execute line env = outFile <- liftIO $ openFile (importPath ++ f ++ ".plam") WriteMode liftIO $ mapM_ (saveGlobal outFile) (reverse env) liftIO $ hClose outFile - outputStrLn("--- exported to " ++ f ++ " sucessful.") + outputStrLn("--- successfully exported to import/" ++ f ++ ".plam") else do outputStrLn("--- export failed : " ++ f ++ " already exists") return env From 9d3e536c0762653fb8591017ad9022272dcd2242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Lovni=C4=8Dki?= Date: Sun, 1 Sep 2019 13:17:21 +0200 Subject: [PATCH 8/9] README: [add] Export to ToC --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d23e6d8..671a39f 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ Inside `import/` directory, many useful λ-expressions are already implemented t - [Define](#def) - [Evaluate](#eval) - [Import](#imp) + - [Export](#exp) - [Comment](#comm) - [Run](#run) - [Print](#print) From 119f7899745c6544d85e4605813eddff62a0f2f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Lovni=C4=8Dki?= Date: Sun, 1 Sep 2019 13:30:42 +0200 Subject: [PATCH 9/9] [update] version to 2.1.0 --- app/Main.hs | 2 +- pLam.cabal | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index a9982de..b105b23 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -14,7 +14,7 @@ import System.Environment import System.Directory (doesFileExist) -version = "2.0.0" +version = "2.1.0" heading = "\x1b[1;36m\ \ _\n\ \ | |\n\ diff --git a/pLam.cabal b/pLam.cabal index a295f43..0be408f 100644 --- a/pLam.cabal +++ b/pLam.cabal @@ -1,5 +1,5 @@ name: pLam -version: 2.0.0 +version: 2.1.0 -- synopsis: -- description: homepage: https://github.com/sandrolovnicki/pLam#readme