on master help sign in
add basic tuple constructor to Tutorial D interpreter
35688d4AgentM
TutorialDInterpreter.hs+8−2
@@ -2,6 +2,8 @@
2module TutorialDInterpreter where
2module TutorialDInterpreter where
3import Relation
3import Relation
4import RelationType
4import RelationType
5+import RelationTuple
6+import RelationTupleSet
5import RelationalError
7import RelationalError
6import RelationExpr
8import RelationExpr
7import RelationTerm
9import RelationTerm
@@ -42,8 +44,12 @@ attributeList = sepBy identifier comma
42makeRelation :: Parser RelationalExpr
44makeRelation :: Parser RelationalExpr
43makeRelation = do
45makeRelation = do
44 reservedOp "relation"
46 reservedOp "relation"
45− attrs <- makeAttributes
47+ attrs <- (try makeAttributes <|> return M.empty)
46− return $ MakeStaticRelation attrs HS.empty
48+ if not (M.null attrs) then do
49+ return $ MakeStaticRelation attrs emptyTupleSet
50+ else do
51+ tuples <- braces (sepBy tupleP comma)
52+ return $ MakeStaticRelation (tupleAttributes (head tuples)) (HS.fromList tuples)
47
53
48--used in relation creation
54--used in relation creation
49makeAttributes :: Parser Attributes
55makeAttributes :: Parser Attributes
TutorialDInterpreterTest.hs+3−3
@@ -8,8 +8,6 @@ import qualified Data.HashSet as HS
8import qualified Data.Map as M
8import qualified Data.Map as M
9import System.Exit
9import System.Exit
10
10
11−import Debug.Trace
12−
13main = do
11main = do
14 counts <- runTestTT (TestList tests)
12 counts <- runTestTT (TestList tests)
15 if errors counts + failures counts > 0 then exitFailure else exitSuccess
13 if errors counts + failures counts > 0 then exitFailure else exitSuccess
@@ -22,6 +20,7 @@ main = do
22 ("x:=relation{a int}", mkRelation simpleAAttributes HS.empty),
20 ("x:=relation{a int}", mkRelation simpleAAttributes HS.empty),
23 ("x:=relation{c int} rename {c as d}", mkRelation simpleBAttributes HS.empty),
21 ("x:=relation{c int} rename {c as d}", mkRelation simpleBAttributes HS.empty),
24 ("y:=relation{b int, c int}; x:=y{c}", mkRelation simpleProjectionAttributes HS.empty),
22 ("y:=relation{b int, c int}; x:=y{c}", mkRelation simpleProjectionAttributes HS.empty),
23+ ("x:=relation{tuple{a char(\"spam\"), b int(5)}}", mkRelation simpleCAttributes (HS.fromList $ map RelationTuple [M.fromList [("a", StringAtom "spam"),("b",IntAtom 5)]])),
25 ("constraint failc true in false; x:=true", Left $ InclusionDependencyCheckError "failc"),
24 ("constraint failc true in false; x:=true", Left $ InclusionDependencyCheckError "failc"),
26 ("x:=true where true", Right relationTrue),
25 ("x:=true where true", Right relationTrue),
27 ("x:=true where false", Right relationFalse),
26 ("x:=true where false", Right relationFalse),
@@ -34,12 +33,13 @@ main = do
34 ]
33 ]
35 simpleAAttributes = M.fromList [("a", Attribute "a" IntAtomType)]
34 simpleAAttributes = M.fromList [("a", Attribute "a" IntAtomType)]
36 simpleBAttributes = M.fromList [("d", Attribute "d" IntAtomType)]
35 simpleBAttributes = M.fromList [("d", Attribute "d" IntAtomType)]
36+ simpleCAttributes = M.fromList [("a", Attribute "a" StringAtomType), ("b", Attribute "b" IntAtomType)]
37 simpleProjectionAttributes = M.fromList [("c", Attribute "c" IntAtomType)]
37 simpleProjectionAttributes = M.fromList [("c", Attribute "c" IntAtomType)]
38 dateExampleRelTests = [("x:=S where true", Right s),
38 dateExampleRelTests = [("x:=S where true", Right s),
39 ("x:=S where CITY = \"London\"", restrict (\(RelationTuple tupMap) -> tupMap M.! "CITY" == StringAtom "London") s),
39 ("x:=S where CITY = \"London\"", restrict (\(RelationTuple tupMap) -> tupMap M.! "CITY" == StringAtom "London") s),
40 ("x:=S where false", Right $ Relation (attributes s) HS.empty),
40 ("x:=S where false", Right $ Relation (attributes s) HS.empty),
41 ("a:=S; update a (STATUS:=50); x:=a{STATUS}", mkRelation (M.fromList [("STATUS", Attribute "STATUS" IntAtomType)]) (HS.fromList [RelationTuple $ M.fromList [("STATUS", IntAtom 50)]])),
41 ("a:=S; update a (STATUS:=50); x:=a{STATUS}", mkRelation (M.fromList [("STATUS", Attribute "STATUS" IntAtomType)]) (HS.fromList [RelationTuple $ M.fromList [("STATUS", IntAtom 50)]])),
42− ("x:=S; update x where SNAME=\"Blake\" (CITY:=\"Boston\")", relMap (\(RelationTuple tupMap) -> RelationTuple $ if (traceShowId tupMap) M.! "SNAME" == StringAtom "Blake" then M.insert "CITY" (StringAtom "Boston") tupMap else tupMap) s)
42+ ("x:=S; update x where SNAME=\"Blake\" (CITY:=\"Boston\")", relMap (\(RelationTuple tupMap) -> RelationTuple $ if tupMap M.! "SNAME" == StringAtom "Blake" then M.insert "CITY" (StringAtom "Boston") tupMap else tupMap) s)
43 ]
43 ]
44
44
45testSimple1 = TestCase $ assertTutdEqual basicDatabaseContext "true" (Right relationTrue)
45testSimple1 = TestCase $ assertTutdEqual basicDatabaseContext "true" (Right relationTrue)