on master help sign in
#21 add help and version commands to tutd
commit78add9d9c9cd1b3a0e51ab844f65810316386668 authorAgentM <agentm@themactionfaction.com> dateTue 12 Jul 2016, 23:39 (10 years ago) parentf1dfb09
4 files changed+80−3
TutorialD/Interpreter.hs+8 TutorialD/Interpreter/InformationOperator.hs+57 TutorialD/tutd.hs+13−2 project-m36.cabal+2−1
TutorialD/Interpreter.hs+8
@@ -4,6 +4,7 @@ import TutorialD.Interpreter.Base
44import TutorialD.Interpreter.RODatabaseContextOperator
55import TutorialD.Interpreter.DatabaseExpr
66import TutorialD.Interpreter.TransactionGraphOperator
7+import TutorialD.Interpreter.InformationOperator
78
89import TutorialD.Interpreter.Import.CSV
910import TutorialD.Interpreter.Import.TutorialD
@@ -34,6 +35,7 @@ graph ops are read-write operations which change the transaction graph
3435-}
3536data ParsedOperation = RODatabaseContextOp RODatabaseContextOperator |
3637 DatabaseExprOp DatabaseExpr |
38+ InfoOp InformationOperator |
3739 GraphOp TransactionGraphOperator |
3840 ROGraphOp ROTransactionGraphOperator |
3941 ImportRelVarOp RelVarDataImportOperator |
@@ -42,6 +44,7 @@ data ParsedOperation = RODatabaseContextOp RODatabaseContextOperator |
4244
4345interpreterParserP :: Parser ParsedOperation
4446interpreterParserP = liftM RODatabaseContextOp (roDatabaseContextOperatorP <* eof) <|>
47+ liftM InfoOp (infoOpP <* eof) <|>
4548 liftM GraphOp (transactionGraphOpP <* eof) <|>
4649 liftM ROGraphOp (roTransactionGraphOpP <* eof) <|>
4750 liftM DatabaseExprOp (databaseExprOpP <* eof) <|>
@@ -102,6 +105,11 @@ evalTutorialD sessionId conn expr = case expr of
102105 Left err -> barf err
103106 Right dbexprs -> evalTutorialD sessionId conn (DatabaseExprOp dbexprs)
104107
108+ (InfoOp execOp) -> do
109+ case evalInformationOperator execOp of
110+ Left err -> barf err
111+ Right info -> pure (DisplayResult info)
112+
105113 (RelVarExportOp execOp@(RelVarDataExportOperator relExpr _ _)) -> do
106114 --eval relexpr to relation and pass to export function
107115 eRel <- C.executeRelationalExpr sessionId conn relExpr
TutorialD/Interpreter/InformationOperator.hsnew file+57
@@ -0,0 +1,57 @@
1+{-# LANGUAGE CPP, OverloadedStrings #-}
2+module TutorialD.Interpreter.InformationOperator where
3+import Data.Text
4+import Text.Parsec
5+import Text.Parsec.String
6+import TutorialD.Interpreter.Base
7+
8+-- this module provides information about the current interpreter
9+
10+data InformationOperator = HelpOperator |
11+ GetVersionOperator
12+
13+infoOpP :: Parser InformationOperator
14+infoOpP = helpOpP <|> getVersionP
15+
16+helpOpP :: Parser InformationOperator
17+helpOpP = reserved ":help" >> pure HelpOperator
18+
19+getVersionP :: Parser InformationOperator
20+getVersionP = reserved ":version" >> pure GetVersionOperator
21+
22+evalInformationOperator :: InformationOperator -> Either Text Text
23+evalInformationOperator GetVersionOperator = Right ("tutd " `append` PROJECTM36_VERSION)
24+-- display generic help
25+evalInformationOperator HelpOperator = Right $ intercalate "\n" help
26+ where
27+ help = ["tutd Help",
28+ "Quick Examples:",
29+ ":showexpr true",
30+ ":showexpr relation{name Text, address Text}{tuple{name \"Steve\", address \"Main St.\"}}",
31+ "address := relation{tuple{name \"Steve\", address \"Main St.\"}}",
32+ ":showexpr true join false = false",
33+ "Relational Operators:",
34+ ":showexpr relation{a Int, b Text}{} -- relation creation",
35+ ":showexpr relation{tuple{c t}} -- relation creation",
36+ ":showexpr relation{tuple{a 4, b 4}}{a} -- projection",
37+ ":showexpr relation{tuple{a 5}} rename {a as num} -- rename",
38+ ":showexpr relation{tuple{d 10}} where d=10 or d=5 -- restriction",
39+ ":showexpr relation{tuple{d 10}} : {e:=add(@d,5)} -- extension",
40+ "Database Context Operators:",
41+ "animal := relation{tuple{name \"octopus\", legs_count 8}} -- assignment",
42+ "insert animal relation{tuple{name \"cat\", legs_count 4}} -- insertion",
43+ "car :: {model Text, make Text, year Int} -- definition",
44+ "undefine car -- undefine",
45+ "delete animal where legs_count=4 -- deletion",
46+ "update animal where name=\"octopus\" (name:=\"Mr. Octopus\") -- updating",
47+ "employee:=relation{id Int, name Text, age Int}{}; key emp_unique_id {id} employee --uniqueness constraint",
48+ "constraint age_gt_zero (employee{age} where ^lt(@age,0)){} equals false -- constraint",
49+ "notify teenager_added employee where ^lt(@age,20) and ^gte(@age,13) employee{age} where ^lt(@age,20) and ^gte(@age,13) -- change notification",
50+ "Graph Operators: ",
51+ ":jumphead <head_name> - change the current database context to point to a current head",
52+ ":jump <transaction_id> - change the current database context to that of a past transaction",
53+ ":commit - push the current context into the current head and make it immutable",
54+ ":rollback - discard any changes made in the current context",
55+ ":showgraph - display the transaction graph",
56+ "View more documentation at: https://github.com/agentm/project-m36/blob/master/docs/tutd_tutorial.markdown"
57+ ]
TutorialD/tutd.hs+13−2
@@ -1,4 +1,4 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE OverloadedStrings, CPP #-}
22import TutorialD.Interpreter
33import ProjectM36.Base
44import ProjectM36.Client
@@ -59,6 +59,16 @@ headNameForConfig (RemoteInterpreterConfig _ _ headn) = headn
5959errDie :: String -> IO ()
6060errDie err = hPutStrLn stderr err >> exitFailure
6161
62+#ifndef PROJECTM36_VERSION
63+#error PROJECTM36_VERSION is not defined
64+#endif
65+printWelcome :: IO ()
66+printWelcome = do
67+ putStrLn $ "Project:M36 TutorialD Interpreter " ++ PROJECTM36_VERSION
68+ putStrLn "Type \":help\" for more information."
69+ putStrLn "A full tutorial is available at:"
70+ putStrLn "https://github.com/agentm/project-m36/blob/master/docs/tutd_tutorial.markdown"
71+
6272main :: IO ()
6373main = do
6474 interpreterConfig <- execParser opts
@@ -72,7 +82,8 @@ main = do
7282 eSessionId <- createSessionAtHead connHeadName conn
7383 case eSessionId of
7484 Left err -> errDie ("Failed to create database session at \"" ++ show connHeadName ++ "\": " ++ show err)
75− Right sessionId -> do
85+ Right sessionId -> do
86+ printWelcome
7687 _ <- reprLoop interpreterConfig sessionId conn
7788 pure ()
7889
project-m36.cabal+2−1
@@ -209,7 +209,8 @@ Executable tutd
209209 TutorialD.Interpreter.RelationalExpr,
210210 TutorialD.Interpreter.Types
211211 main-is: TutorialD/tutd.hs
212− GHC-Options: -Wall
212+ CPP-Options: -DPROJECTM36_VERSION="v0.1"
213+ GHC-Options: -Wall
213214 Default-Language: Haskell2010
214215 if os(windows)
215216 Build-Depends: Win32 >= 2.3 && < 2.4