@@ -1,4 +1,6 @@
1module TutorialDInterpreter where
2module TutorialDInterpreter where
2import RelationType
4import RelationType
3import RelationalError
5import RelationalError
4import RelationExpr
6import RelationExpr
@@ -11,6 +13,7 @@ import qualified Text.Parsec.Token as Token
11import qualified Data.Set as S
13import qualified Data.Set as S
12import qualified Data.HashSet as HS
14import qualified Data.HashSet as HS
13import qualified Data.Map as M
15import qualified Data.Map as M
16+import qualified Data.List as L
14import Control.Applicative (liftA)
17import Control.Applicative (liftA)
15import Control.Monad.State
18import Control.Monad.State
16import System.Console.Readline
19import System.Console.Readline
@@ -65,11 +68,19 @@ tutDTypeToAtomType tutDType = case tutDType of
65 "int" -> Just IntAtomType
68 "int" -> Just IntAtomType
66 _ -> Nothing
69 _ -> Nothing
71+atomTypeToTutDType :: AtomType -> Maybe String
72+atomTypeToTutDType atomType = case atomType of
73+ StringAtomType -> Just "char"
74+ IntAtomType -> Just "int"
75+ --RelationAtomType rel ->
68relVarP :: Parser RelationalExpr
78relVarP :: Parser RelationalExpr
69relVarP = liftA RelationVariable identifier
79relVarP = liftA RelationVariable identifier
71relTerm = parens relExpr
81relTerm = parens relExpr
72 <|> makeRelation
82 <|> makeRelation
73 <|> relVarP
84 <|> relVarP
75projectOp = do
86projectOp = do
@@ -111,14 +122,29 @@ ungroupP = do
111 reservedOp "ungroup"
122 reservedOp "ungroup"
112 rvaAttrName <- identifier
123 rvaAttrName <- identifier
113 return $ Ungroup rvaAttrName
124 return $ Ungroup rvaAttrName
115−
126+insertP :: Parser (RelationalExpr -> RelationalExpr)
129+ relvar <- identifier
130+ return $ Insert relvar
132+defineP :: Parser RelationalExpr
134+ relVarName <- identifier
136+ attributes <- makeAttributes
137+ return $ Define relVarName attributes
116relOperators = [
139relOperators = [
117 [Postfix projectOp],
140 [Postfix projectOp],
118 [Postfix renameP],
141 [Postfix renameP],
119 [Postfix groupP],
142 [Postfix groupP],
120 [Infix (reservedOp "join" >> return Join) AssocLeft],
144 [Infix (reservedOp "join" >> return Join) AssocLeft],
121 [Infix (reservedOp "union" >> return Union) AssocLeft],
145 [Infix (reservedOp "union" >> return Union) AssocLeft],
122 [Prefix (try assignP)]
148 [Prefix (try assignP)]
@@ -134,6 +160,29 @@ parseString :: String -> Either RelationalError RelationalExpr
134parseString str = case parse multipleRelExpr "" str of
160parseString str = case parse multipleRelExpr "" str of
135 Left err -> Left $ ParseError (show err)
161 Left err -> Left $ ParseError (show err)
136 Right r -> Right r
162 Right r -> Right r
164+data TutorialDOperator where
165+ ShowRelationVariableType :: String -> TutorialDOperator
167+interpreterOps :: Parser TutorialDOperator
170+ relVarName <- identifier
171+ return $ ShowRelationVariableType relVarName
173+showRelationAttributes :: Relation -> String
174+showRelationAttributes rel = "{" ++ concat (L.intersperse ", " $ map showAttribute attrs) ++ "}"
176+ showAttribute (Attribute name atomType) = name ++ " " ++ case atomTypeToTutDType atomType of
178+ Nothing -> "unknown"
179+ attrs = values (attributes rel)
180+ values m = map snd (M.toAscList m)
182+evalTutorialDOp :: RelVarContext -> TutorialDOperator -> String
183+evalTutorialDOp context (ShowRelationVariableType relVarName) = case M.lookup relVarName context of
184+ Just rel -> showRelationAttributes rel
185+ Nothing -> relVarName ++ " not defined"
138example1 = "relA {a,b, c}"
187example1 = "relA {a,b, c}"
139example2 = "relA join relB"
188example2 = "relA join relB"
@@ -149,7 +198,13 @@ interpret :: RelVarContext -> String -> (Either RelationalError Relation, RelVar
149interpret context tutdstring = case parseString tutdstring of
198interpret context tutdstring = case parseString tutdstring of
150 Left err -> (Left err, context)
199 Left err -> (Left err, context)
151 Right parsed -> runState (eval parsed) context
200 Right parsed -> runState (eval parsed) context
202+-- for interpreter-specific operations
203+interpretOps :: RelVarContext -> String -> Maybe String
204+interpretOps context instring = case parse interpreterOps "" instring of
206+ Right ops -> Just $ evalTutorialDOp context ops
153reprLoop :: RelVarContext -> IO ()
208reprLoop :: RelVarContext -> IO ()
154reprLoop context = do
209reprLoop context = do
155 maybeLine <- readline "TutorialD: "
210 maybeLine <- readline "TutorialD: "
@@ -157,6 +212,13 @@ reprLoop context = do
157 Nothing -> return ()
212 Nothing -> return ()
158 Just line -> do
213 Just line -> do
159 addHistory line
214 addHistory line
216+ case interpretOps context line of
220+ Nothing -> return ()
160 let (value, contextup) = interpret context line
222 let (value, contextup) = interpret context line
161 case value of
223 case value of
162 (Right rel) -> do
224 (Right rel) -> do