@@ -1,4 +1,6 @@
1+{-# LANGUAGE GADTs #-}
12module TutorialDInterpreter where
3+import Relation
24import RelationType
35import RelationalError
46import RelationExpr
@@ -11,6 +13,7 @@ import qualified Text.Parsec.Token as Token
1113import qualified Data.Set as S
1214import qualified Data.HashSet as HS
1315import qualified Data.Map as M
16+import qualified Data.List as L
1417import Control.Applicative (liftA)
1518import Control.Monad.State
1619import System.Console.Readline
@@ -65,11 +68,19 @@ tutDTypeToAtomType tutDType = case tutDType of
6568 "int" -> Just IntAtomType
6669 _ -> Nothing
6770
71+atomTypeToTutDType :: AtomType -> Maybe String
72+atomTypeToTutDType atomType = case atomType of
73+ StringAtomType -> Just "char"
74+ IntAtomType -> Just "int"
75+ --RelationAtomType rel ->
76+ _ -> Nothing
77+
6878relVarP :: Parser RelationalExpr
6979relVarP = liftA RelationVariable identifier
7080
7181relTerm = parens relExpr
7282 <|> makeRelation
83+ <|> (try defineP)
7384 <|> relVarP
7485
7586projectOp = do
@@ -111,14 +122,29 @@ ungroupP = do
111122 reservedOp "ungroup"
112123 rvaAttrName <- identifier
113124 return $ Ungroup rvaAttrName
114−
115−
125+
126+insertP :: Parser (RelationalExpr -> RelationalExpr)
127+insertP = do
128+ reservedOp "insert"
129+ relvar <- identifier
130+ return $ Insert relvar
131+
132+defineP :: Parser RelationalExpr
133+defineP = do
134+ relVarName <- identifier
135+ reservedOp "::"
136+ attributes <- makeAttributes
137+ return $ Define relVarName attributes
138+
116139relOperators = [
117140 [Postfix projectOp],
118141 [Postfix renameP],
119142 [Postfix groupP],
143+ [Postfix ungroupP],
120144 [Infix (reservedOp "join" >> return Join) AssocLeft],
121145 [Infix (reservedOp "union" >> return Union) AssocLeft],
146+ [Prefix insertP],
147+
122148 [Prefix (try assignP)]
123149 ]
124150
@@ -134,6 +160,29 @@ parseString :: String -> Either RelationalError RelationalExpr
134160parseString str = case parse multipleRelExpr "" str of
135161 Left err -> Left $ ParseError (show err)
136162 Right r -> Right r
163+
164+data TutorialDOperator where
165+ ShowRelationVariableType :: String -> TutorialDOperator
166+
167+interpreterOps :: Parser TutorialDOperator
168+interpreterOps = do
169+ reservedOp ":t"
170+ relVarName <- identifier
171+ return $ ShowRelationVariableType relVarName
172+
173+showRelationAttributes :: Relation -> String
174+showRelationAttributes rel = "{" ++ concat (L.intersperse ", " $ map showAttribute attrs) ++ "}"
175+ where
176+ showAttribute (Attribute name atomType) = name ++ " " ++ case atomTypeToTutDType atomType of
177+ Just t -> show t
178+ Nothing -> "unknown"
179+ attrs = values (attributes rel)
180+ values m = map snd (M.toAscList m)
181+
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"
137186
138187example1 = "relA {a,b, c}"
139188example2 = "relA join relB"
@@ -149,7 +198,13 @@ interpret :: RelVarContext -> String -> (Either RelationalError Relation, RelVar
149198interpret context tutdstring = case parseString tutdstring of
150199 Left err -> (Left err, context)
151200 Right parsed -> runState (eval parsed) context
152−
201+
202+-- for interpreter-specific operations
203+interpretOps :: RelVarContext -> String -> Maybe String
204+interpretOps context instring = case parse interpreterOps "" instring of
205+ Left err -> Nothing
206+ Right ops -> Just $ evalTutorialDOp context ops
207+
153208reprLoop :: RelVarContext -> IO ()
154209reprLoop context = do
155210 maybeLine <- readline "TutorialD: "
@@ -157,6 +212,13 @@ reprLoop context = do
157212 Nothing -> return ()
158213 Just line -> do
159214 addHistory line
215+
216+ case interpretOps context line of
217+ Just out -> do
218+ putStrLn out
219+ reprLoop context
220+ Nothing -> return ()
221+
160222 let (value, contextup) = interpret context line
161223 case value of
162224 (Right rel) -> do