Skip to content

Commit

Permalink
Merge pull request #9 from sandrolovnicki/dev
Browse files Browse the repository at this point in the history
dev + export-command
  • Loading branch information
Sandro Lovnički authored Sep 1, 2019
2 parents c5505a8 + 119f789 commit 98c4845
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -190,6 +191,15 @@ A block of code in pLam is a line, and possible lines (commands) are the followi
- example: `:import std`
- restriction: `<string>.plam` has to be inside `import/` directory within the pLam project directory

<a name="exp"/>

### Export

- syntax `:export <string<`
- semantics: put all the expressions in the list of environment variables into the file `import/<string>.plam`
- example: `:export test`
- restriction: `<string>.plam` cannot already exist

<a name="comm"/>

### Comment
Expand Down
25 changes: 22 additions & 3 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ 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
import System.Environment
import System.Directory (doesFileExist)


version = "2.0.0"
version = "2.1.0"
heading = "\x1b[1;36m\
\ _\n\
\ | |\n\
Expand Down Expand Up @@ -93,7 +94,18 @@ 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
fileExists <- liftIO $ doesFileExist (importPath ++ f ++ ".plam")
if not fileExists
then do
outFile <- liftIO $ openFile (importPath ++ f ++ ".plam") WriteMode
liftIO $ mapM_ (saveGlobal outFile) (reverse env)
liftIO $ hClose outFile
outputStrLn("--- successfully exported to import/" ++ f ++ ".plam")
else do
outputStrLn("--- export failed : " ++ f ++ " already exists")
return env
Review r -> do
case r of
"all" -> do
Expand Down Expand Up @@ -149,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
Expand Down
3 changes: 2 additions & 1 deletion pLam.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: pLam
version: 2.0.0
version: 2.1.0
-- synopsis:
-- description:
homepage: https://github.com/sandrolovnicki/pLam#readme
Expand Down Expand Up @@ -32,6 +32,7 @@ executable plam
, mtl
, containers
, haskeline
, directory
default-language: Haskell2010

source-repository head
Expand Down
15 changes: 14 additions & 1 deletion src/Helper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -14,6 +14,19 @@ 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
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
Expand Down
9 changes: 9 additions & 0 deletions src/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -200,12 +207,14 @@ parseLine :: Parser Command
parseLine = try parseDefine
<|> parseShowDetailed
<|> parseImport
<|> parseExport
<|> parseReview
<|> parseRun
<|> parsePrint
<|> parseComment
<|> parseShow
<|> parseEmptyLine

-------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ data Command = Define String Expression
| Show Expression
| ShowDetailed Expression
| Import String
| Export String
| Review String
| Comment String
| Run String
Expand Down

0 comments on commit 98c4845

Please sign in to comment.