2module TutorialDInterpreter where
9import Text.Parsec.String
10import Text.Parsec.Expr
11import Text.Parsec.Language
12import qualified Text.Parsec.Token as Token
13import qualified Data.Set as S
14import qualified Data.HashSet as HS
15import qualified Data.Map as M
16import qualified Data.List as L
17import Control.Applicative (liftA)
18import Control.Monad.State
19import System.Console.Readline
21lexer :: Token.TokenParser ()
22lexer = Token.makeTokenParser tutD
23 where tutD = emptyDef {
24 Token.reservedOpNames = ["join", "where", "union", "group", "ungroup"],
25 Token.reservedNames = [],
26 Token.identStart = letter,
27 Token.identLetter = alphaNum}
29parens = Token.parens lexer
30reservedOp = Token.reservedOp lexer
31reserved = Token.reserved lexer
32braces = Token.braces lexer
33identifier = Token.identifier lexer
34comma = Token.comma lexer
35semi = Token.semi lexer
38attributeList :: Parser [AttributeName]
39attributeList = sepBy identifier comma
41makeRelation :: Parser RelationalExpr
44 attrs <- makeAttributes
45 return $ MakeStaticRelation attrs HS.empty
47--used in relation creation
48makeAttributes :: Parser Attributes
50 attrList <- braces (sepBy attributeAndType comma)
51 return $ M.fromList $ map toAttributeAssocList attrList
53 toAttributeAssocList attr@(Attribute attrName _) = (attrName, attr)
55attributeAndType :: Parser Attribute
57 attrName <- identifier
58 attrTypeName <- identifier
59 --convert type name into type
60 case tutDTypeToAtomType attrTypeName of
61 Just t -> return $ Attribute attrName t
62 Nothing -> fail (attrTypeName ++ " is not a valid type name.")
64--convert Tutorial D type to AtomType
65tutDTypeToAtomType :: String -> Maybe AtomType
66tutDTypeToAtomType tutDType = case tutDType of
67 "char" -> Just StringAtomType
68 "int" -> Just IntAtomType
71atomTypeToTutDType :: AtomType -> Maybe String
72atomTypeToTutDType atomType = case atomType of
73 StringAtomType -> Just "char"
74 IntAtomType -> Just "int"
75 --RelationAtomType rel ->
78relVarP :: Parser RelationalExpr
79relVarP = liftA RelationVariable identifier
81relTerm = parens relExpr
87 attrs <- braces attributeList
88 return $ Project (S.fromList attrs)
91 relVarName <- identifier
93 return $ Assign relVarName
99 return $ (oldAttr, newAttr)
101renameP :: Parser (RelationalExpr -> RelationalExpr)
104 (oldAttr, newAttr) <- braces renameClause
105 return $ Rename oldAttr newAttr
108 attrs <- braces attributeList
110 newAttrName <- identifier
111 return $ (attrs, newAttrName)
113groupP :: Parser (RelationalExpr -> RelationalExpr)
116 (groupAttrList, groupAttrName) <- parens groupClause
117 return $ Group (S.fromList groupAttrList) groupAttrName
119--in "Time and Relational Theory" (2014), Date's Tutorial D grammar for ungroup takes one attribute, while in previous books, it take multiple arguments. Let us assume that nested ungroups are the same as multiple attributes.
120ungroupP :: Parser (RelationalExpr -> RelationalExpr)
123 rvaAttrName <- identifier
124 return $ Ungroup rvaAttrName
126insertP :: Parser (RelationalExpr -> RelationalExpr)
130 return $ Insert relvar
132defineP :: Parser RelationalExpr
134 relVarName <- identifier
136 attributes <- makeAttributes
137 return $ Define relVarName attributes
144 [Infix (reservedOp "join" >> return Join) AssocLeft],
145 [Infix (reservedOp "union" >> return Union) AssocLeft],
148 [Prefix (try assignP)]
151relExpr :: Parser RelationalExpr
152relExpr = buildExpressionParser relOperators relTerm
154multipleRelExpr :: Parser RelationalExpr
156 exprs <- sepBy1 relExpr semi
157 return $ MultipleExpr exprs
159parseString :: String -> Either RelationalError RelationalExpr
160parseString str = case parse multipleRelExpr "" str of
161 Left err -> Left $ ParseError (show err)
164data TutorialDOperator where
165 ShowRelationVariableType :: String -> TutorialDOperator
167interpreterOps :: Parser TutorialDOperator
170 relVarName <- identifier
171 return $ ShowRelationVariableType relVarName
173showRelationAttributes :: Relation -> String
174showRelationAttributes rel = "{" ++ concat (L.intersperse ", " $ map showAttribute attrs) ++ "}"
176 showAttribute (Attribute name atomType) = name ++ " " ++ case atomTypeToTutDType atomType of
179 attrs = values (attributes rel)
180 values m = map snd (M.toAscList m)
182evalTutorialDOp :: RelVarContext -> TutorialDOperator -> String
183evalTutorialDOp context (ShowRelationVariableType relVarName) = case M.lookup relVarName context of
184 Just rel -> showRelationAttributes rel
185 Nothing -> relVarName ++ " not defined"
187example1 = "relA {a,b, c}"
188example2 = "relA join relB"
189example3 = "relA join relB {x,y,z}"
190example4 = "(relA) {x,y,z}"
191example5 = "relA union relB"
192example6 = "rv1 := true"
193example7 = "rv1 := relA union relB"
194example8 = "relA := true; relB := false"
195example9 = "relA := relation { SNO CHAR }"
197interpret :: RelVarContext -> String -> (Either RelationalError Relation, RelVarContext)
198interpret context tutdstring = case parseString tutdstring of
199 Left err -> (Left err, context)
200 Right parsed -> runState (eval parsed) context
202-- for interpreter-specific operations
203interpretOps :: RelVarContext -> String -> Maybe String
204interpretOps context instring = case parse interpreterOps "" instring of
206 Right ops -> Just $ evalTutorialDOp context ops
208reprLoop :: RelVarContext -> IO ()
210 maybeLine <- readline "TutorialD: "
216 case interpretOps context line of
222 let (value, contextup) = interpret context line
225 putStrLn $ showRelation rel
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list