@@ -22,6 +22,8 @@ import TutorialD.Interpreter.DataTypes.Interval
2222
2323import Data.Time.Calendar (Day)
2424
25+--nuke this
26+{-
2527atomTypeP :: Parser AtomType
2628atomTypeP = (reserved "char" *> return stringAtomType) <|>
2729 (reserved "int" *> return intAtomType) <|>
@@ -31,7 +33,8 @@ atomTypeP = (reserved "char" *> return stringAtomType) <|>
3133 (reserved "bool" *> return boolAtomType) <|>
3234 (reserved "bytestring" *> return byteStringAtomType) <|>
3335 (RelationAtomType <$> (reserved "relation" *> makeAttributesP))
34−
36+-}
37+
3538--used in projection
3639attributeListP :: Parser AttributeNames
3740attributeListP = do
@@ -41,35 +44,49 @@ attributeListP = do
4144 return $ constructor (S.fromList (map T.pack attrs))
4245
4346makeRelationP :: Parser RelationalExpr
44−makeRelationP = ExistingRelation <$> relationP
47+makeRelationP = do
48+ reserved "relation"
49+ attrs <- (try makeAttributesP <|> return A.emptyAttributes)
50+ if not (A.null attrs) then do
51+ case mkRelation attrs emptyTupleSet of
52+ Left err -> fail (show err)
53+ Right rel -> pure (ExistingRelation rel)
54+ else do -- empty attributes
55+ tupleExprs <- try (braces (sepBy tupleExprP comma)) <|> pure []
56+ pure $ MakeRelationFromAtomExprs attrs
57+
58+
59+makeAttributeExprsP :: Parser [AttributeExpr]
60+makeAttributeExprsP = do
61+ attrList <- bra
4562
4663--used in relation creation
47−makeAttributesP :: Parser Attributes
48−makeAttributesP = do
49− attrList <- braces (sepBy attributeAndTypeP comma)
64+makeAttributeExprsP :: Parser [AttributeExprs]
65+makeAttributeExprsP = do
66+ attrList <- braces (sepBy attributeAndTypeNameP comma)
5067 return $ A.attributesFromList attrList
5168
52−attributeAndTypeP :: Parser Attribute
53−attributeAndTypeP = do
69+
70+attributeAndTypeNameP :: Parser AttributeExpr
71+attributeAndTypeNameP = do
5472 attrName <- identifier
5573 --convert type name into type
56− atomType <- atomTypeP
57− return $ Attribute (T.pack attrName) atomType
74+ atomType <- identifier
75+ return $ AttributeAndTypeNameExpr (T.pack attrName) atomType
5876
59−tupleP :: Parser RelationTuple
60−tupleP = do
77+tupleExprP :: Parser TupleExpr
78+tupleExprP = do
6179 reservedOp "tuple"
62− attrAssocs <- braces (sepBy tupleAtomP comma)
63− let attrs = A.attributesFromList $ map (\(attrName, atom) -> Attribute attrName (atomTypeForAtom atom)) attrAssocs
64− let atoms = V.fromList $ map snd attrAssocs
65− return $ mkRelationTuple attrs atoms
80+ attrAssocs <- braces (sepBy tupleAtomExprP comma)
81+ let attrs = A.attributesFromList $ map fst attrAssocs
82+ pure (TupleExpr attrs (map snd attrAssocs))
6683
67−tupleAtomP :: Parser (AttributeName, Atom)
68−tupleAtomP = do
84+tupleAtomExprP :: Parser (AttributeName, AtomExpr)
85+tupleAtomExprP = do
6986 attributeName <- identifier
70− atom <- atomP
71− return $ (T.pack attributeName, atom)
72−
87+ atomExpr <- atomExprP
88+ return $ (T.pack attributeName, atomExpr)
89+
7390projectP :: Parser (RelationalExpr -> RelationalExpr)
7491projectP = do
7592 attrs <- braces attributeListP
@@ -117,7 +134,7 @@ ungroupP = do
117134extendP :: Parser (RelationalExpr -> RelationalExpr)
118135extendP = do
119136 reservedOp ":"
120− tupleExpr <- braces tupleExpressionP
137+ tupleExpr <- braces extendTupleExpressionP
121138 return $ Extend tupleExpr
122139
123140{-
@@ -202,24 +219,25 @@ restrictionAtomExprP = do
202219 _ <- char '^' -- not ideal, but allows me to continue to use a context-free grammar
203220 AtomExprPredicate <$> atomExprP
204221
205−multiTupleExpressionP :: Parser [TupleExpr]
206−multiTupleExpressionP = sepBy tupleExpressionP comma
222+multiTupleExpressionP :: Parser [ExtendTupleExpr]
223+multiTupleExpressionP = sepBy extendTupleExpressionP comma
207224
208−tupleExpressionP :: Parser TupleExpr
209−tupleExpressionP = attributeTupleExpressionP
225+extendTupleExpressionP :: Parser ExtendTupleExpr
226+extendTupleExpressionP = attributeExtendTupleExpressionP
210227
211−attributeTupleExpressionP :: Parser TupleExpr
212−attributeTupleExpressionP = do
228+attributeExtendTupleExpressionP :: Parser ExtendTupleExpr
229+attributeExtendTupleExpressionP = do
213230 newAttr <- identifier
214231 reservedOp ":="
215232 atom <- atomExprP
216− return $ AttributeTupleExpr (T.pack newAttr) atom
233+ return $ AttributeExtendTupleExpr (T.pack newAttr) atom
217234
218235atomExprP :: Parser AtomExpr
219236atomExprP = try functionAtomExprP <|>
220237 attributeAtomExprP <|>
221238 nakedAtomExprP <|>
222− relationalAtomExprP
239+ relationalAtomExprP <|>
240+ constructedAtomP
223241
224242attributeAtomExprP :: Parser AtomExpr
225243attributeAtomExprP = do
@@ -237,7 +255,7 @@ constructedAtomP = do
237255 dConsName <- identifier
238256 dConsArgs <- sepBy atomExprP spaces
239257 reserved "]"
240− pure $ AtomConstructor dConsName dConsArgs
258+ pure $ ConstructedAtomExpr (T.pack dConsName) dConsArgs
241259
242260atomP :: Parser Atom
243261atomP = dateTimeAtomP <|>
@@ -250,9 +268,8 @@ atomP = dateTimeAtomP <|>
250268 doubleAtomP <|>
251269 intAtomP <|>
252270 boolAtomP <|>
253− relationAtomP <|>
254− constructedAtomP
255−
271+ relationAtomP
272+
256273functionAtomExprP :: Parser AtomExpr
257274functionAtomExprP = do
258275 funcName <- identifier
@@ -271,13 +288,15 @@ maybeTextAtomP = do
271288 maybeText <- try $ ((Just . T.pack <$> (reserved "Just" *> quotedString)) <|>
272289 (reserved "Nothing" *> return Nothing)) <* reserved "::maybe char"
273290 return $ Atom maybeText
274−
291+
292+--refactor to use polymorphic runtime constructors
275293maybeIntAtomP :: Parser Atom
276294maybeIntAtomP = do
277295 maybeInt <- try $ ((Just . fromIntegral <$> (reserved "Just" *> integer)) <|>
278296 (reserved "Nothing" *> return Nothing)) <* reserved "::maybe int"
279297 return $ Atom (maybeInt :: Maybe Int)
280−
298+
299+--refactor to use constructors
281300dateAtomP :: Parser Atom
282301dateAtomP = do
283302 dateString' <- try $ do
@@ -287,10 +306,10 @@ dateAtomP = do
287306 case parseTimeM False defaultTimeLocale "%Y-%m-%d" dateString' of
288307 Just todaytime -> return $ Atom (todaytime :: Day)
289308 Nothing -> fail "Failed to parse date"
290−
309+
291310doubleAtomP :: Parser Atom
292311doubleAtomP = Atom <$> (try float)
293−
312+
294313intAtomP :: Parser Atom
295314intAtomP = do
296315 i <- integer
@@ -301,9 +320,10 @@ boolAtomP = do
301320 val <- char 't' <|> char 'f'
302321 return $ Atom (val == 't')
303322
304−relationAtomP :: Parser Atom
305−relationAtomP = Atom <$> relationP
323+relationAtomExprP :: Parser AtomExpr
324+relationAtomExprP = RelationAtomExpr <$> relationExprP
306325
326+-- deprecated for removal- use constructors
307327byteStringAtomP :: Parser Atom
308328byteStringAtomP = do
309329 byteString' <- try $ do
@@ -314,6 +334,7 @@ byteStringAtomP = do
314334 Left err -> fail err
315335 Right bsVal -> return $ Atom bsVal
316336
337+{-
317338--relation constructor -- no choice but to propagate relation-construction errors as parse errors. Perhaps this could be improved in the future
318339relationP :: Parser Relation
319340relationP = do
@@ -329,4 +350,4 @@ relationP = do
329350 case mkRelationFromTuples tupleAttrs tuples of
330351 Left err -> fail (show err)
331352 Right rel -> return rel
332−
353+-}