on master help sign in
add basic tuple constructor to Tutorial D interpreter
commit35688d4c23360c7adb8fc25f182846a4c03d1264 authorAgentM <agentm@themactionfaction.com> dateTue 27 Jan 2015, 21:54 (12 years ago) parentf95e64c
2 files changed+11−5
TutorialDInterpreter.hs+8−2 TutorialDInterpreterTest.hs+3−3
TutorialDInterpreter.hs+8−2
@@ -2,6 +2,8 @@
22module TutorialDInterpreter where
33import Relation
44import RelationType
5+import RelationTuple
6+import RelationTupleSet
57import RelationalError
68import RelationExpr
79import RelationTerm
@@ -42,8 +44,12 @@ attributeList = sepBy identifier comma
4244makeRelation :: Parser RelationalExpr
4345makeRelation = do
4446 reservedOp "relation"
45− attrs <- makeAttributes
46− return $ MakeStaticRelation attrs HS.empty
47+ attrs <- (try makeAttributes <|> return M.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)
4753
4854--used in relation creation
4955makeAttributes :: Parser Attributes
TutorialDInterpreterTest.hs+3−3
@@ -8,8 +8,6 @@ import qualified Data.HashSet as HS
88import qualified Data.Map as M
99import System.Exit
1010
11−import Debug.Trace
12−
1311main = do
1412 counts <- runTestTT (TestList tests)
1513 if errors counts + failures counts > 0 then exitFailure else exitSuccess
@@ -22,6 +20,7 @@ main = do
2220 ("x:=relation{a int}", mkRelation simpleAAttributes HS.empty),
2321 ("x:=relation{c int} rename {c as d}", mkRelation simpleBAttributes HS.empty),
2422 ("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)]])),
2524 ("constraint failc true in false; x:=true", Left $ InclusionDependencyCheckError "failc"),
2625 ("x:=true where true", Right relationTrue),
2726 ("x:=true where false", Right relationFalse),
@@ -34,12 +33,13 @@ main = do
3433 ]
3534 simpleAAttributes = M.fromList [("a", Attribute "a" IntAtomType)]
3635 simpleBAttributes = M.fromList [("d", Attribute "d" IntAtomType)]
36+ simpleCAttributes = M.fromList [("a", Attribute "a" StringAtomType), ("b", Attribute "b" IntAtomType)]
3737 simpleProjectionAttributes = M.fromList [("c", Attribute "c" IntAtomType)]
3838 dateExampleRelTests = [("x:=S where true", Right s),
3939 ("x:=S where CITY = \"London\"", restrict (\(RelationTuple tupMap) -> tupMap M.! "CITY" == StringAtom "London") s),
4040 ("x:=S where false", Right $ Relation (attributes s) HS.empty),
4141 ("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)
4343 ]
4444
4545testSimple1 = TestCase $ assertTutdEqual basicDatabaseContext "true" (Right relationTrue)