Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
236 lines (202 sloc) 9.14 KB
{-
Copyright © 2012 Daniel Tahara, Kartik Venkatraman
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-}
module Recognition_Test where
import Recognition
import DTKV_Grammar
--- Code Sample / Test Recognition
test_recognition = all (\x -> x) tests
tests = [test_assignment_recog,
test_fnCall_recog,
test_nestedCalls_recog,
test_arithFnDecl_recog,
test_forLoop_recog,
test_whileLoop_recog,
test_forLoopInFn_recog,
test_stringUserFn_recog,
test_stringUserFnEmptyBody_recog,
test_noStms_recog]
testCode_assignment = "Assign Int x 15 \
\Return Int x"
test_assignment_recog = recognize dtkvGrammar testCode_assignment == True
test_assignment_items = getAllItems dtkvGrammar (words testCode_assignment)
testCode_fnCall = "Assign Int x 15 \
\Assign Int y 20 \
\Assign Int z Call Int + Int x Int y End \
\Return Int z"
test_fnCall_recog = recognize dtkvGrammar testCode_fnCall == True
test_fnCall_items = getAllItems dtkvGrammar (words testCode_fnCall)
testCode_simpleNested = "Assign Int x Call Int + 1 Call Int + 2 3 End End Return Int z"
test_simpleNested_recog = recognize dtkvGrammar testCode_simpleNested == True
test_simpleNested_items = getAllItems dtkvGrammar (words testCode_simpleNested)
testCode_nestedCalls = "Assign Int x 15 \
\Assign Int y 20 \
\Assign Int z Call Int + 1 Call Int + Int x Int y End End \
\Return Int z"
test_nestedCalls_recog = recognize dtkvGrammar testCode_nestedCalls == True
test_nestedCalls_items = getAllItems dtkvGrammar (words testCode_nestedCalls)
testCode_arithFnDecl = "Assign Int x 15 \
\Assign Int y 20 \
\Declare sumPlusOne Int Int x Int y \
\Assign Int answer Call Int + 1 Call Int + Int x Int y End End \
\Return Int answer \
\Assign Int z \
\Call Int sumPlusOne Int x Int y End \
\Return Int z"
test_arithFnDecl_recog = recognize dtkvGrammar testCode_arithFnDecl == True
test_arithFnDecl_items = getAllItems dtkvGrammar (words testCode_arithFnDecl)
testCode_forLoop = "Assign Int w 0 \
\For 1 End 10 End \
\Assign Int w Call Int + Int w Int loopVar End \
\End \
\Return Int w"
test_forLoop_recog = recognize dtkvGrammar testCode_forLoop == True
test_forLoop_items = getAllItems dtkvGrammar (words testCode_forLoop)
--
{-
testCode_whileLoop = "Assign Int v Call Int - 0 5 End \
\Assign Int max 101 \
\Assign Bool testCond \
\Call Bool < \
\Call Int abs Int v End \
\Int max End \
\While Bool testCond End \
\Assign Int v Call Int * Int v 2 End \
\End \
\Return Int v"
-}
testCode_whileLoop = "Assign Int v Call Int - 0 5 End \
\While Call Bool < Call Int abs Int v End 101 End End \
\Assign Int v Call Int * Int v 2 End \
\End \
\Return Int v"
{-
testCode_whileLoop = "Assign Int v 10 \
\Assign Int answer 0 \
\While Int v End \
\Assign Int answer Call Int + Int answer Int v End \
\Assign Int v Call Int - Int v 1 End \
\End \
\Return Int v"
-}
{-
testCode_whileLoop = "Assign Int answer 1 \
\While Int answer End \
\Assign Int answer Call Int - Int answer 0 End \
\End \
\Return Int answer"
-}
test_whileLoop_recog = recognize dtkvGrammar testCode_whileLoop == True
test_whileLoop_items = getAllItems dtkvGrammar (words testCode_whileLoop)
testCode_forLoopInFn = "Assign Int x 1000 \
\Declare ProdOfDif Int Int n Int x Int y \
\Assign Int u 0 \
\Assign Int diff Call Int - Int x Int y End \
\For 1 End Int n End \
\Assign Int u Call Int + Int u Int diff End \
\End \
\Return Int u \
\Assign Int answer Call Int ProdOfDif 3 2 5 End \
\Return Int answer"
test_forLoopInFn_recog = recognize dtkvGrammar testCode_forLoopInFn == True
test_forLoopInFn_items = getAllItems dtkvGrammar (words testCode_forLoopInFn)
testCode_stringUserFn = "Assign Str a hello \
\Assign Str b ice \
\Assign Str result 'shouldbeleftafterfn' \
\Declare ConcatHeads Str Str a Str b \
\Assign Str result Call Str ++ Call Str head Str a End Call Str head Str b End End \
\Return Str result \
\Assign Str result Call Str ++ Str result Call Str ConcatHeads Str a Str b End End \
\Return Str result"
test_stringUserFn_recog = recognize dtkvGrammar testCode_stringUserFn == True
test_stringUserFn_items = getAllItems dtkvGrammar (words testCode_stringUserFn)
testCode_stringUserFnEmptyBody = "Assign Str a hello \
\Assign Str b ice \
\Declare ConcatHeads Str Str a Str b \
\Return Call Str ++ Call Str head a Call Str head b End End End \
\Assign Str result Call Str ++ Str result Call Str ConcatHeads Str a Str b End End \
\Return Str result"
test_stringUserFnEmptyBody_recog = recognize dtkvGrammar testCode_stringUserFnEmptyBody == False
test_stringUserFnEmptyBody_items = getAllItems dtkvGrammar (words testCode_stringUserFnEmptyBody)
testCode_noStms = "Str answer"
test_noStms_recog = recognize dtkvGrammar testCode_noStms == False
test_noStms_items = getAllItems dtkvGrammar (words testCode_noStms)
-- Test Building Parse Tree
-- Test helper functions
--bounds = constituentsMeetingBounds dtkvGrammar recogItem_fnCall test_fnCall_items
--matches = matchingConstituents dtkvGrammar recogItem_fnCall test_fnCall_items
-- Valid Parses
validParsesList = map (map getParseTree)
[validParses_assignment,
validParses_fnCall,
validParses_nestedCalls,
validParses_arithFnDecl,
validParses_forLoop,
validParses_whileLoop,
validParses_forLoopInFn,
validParses_stringUserFn]
parseTreeList = [parseTree_assignment,
parseTree_fnCall,
parseTree_nestedCalls,
parseTree_arithFnDecl,
parseTree_forLoop,
parseTree_whileLoop,
parseTree_forLoopInFn,
parseTree_stringUserFn]
-- Resulting parseTrees
--parseTrees = map head parseTreesLists
validParses_assignment = validParses dtkvGrammar testCode_assignment
recogItem_assignment = head validParses_assignment
parseTree_assignment = getParseTree recogItem_assignment--getParseTree dtkvGrammar recogItem_assignment test_assignment_items
--filter (checkTree dtkvGrammar "PROG") $
--parseTrees_assignment = getParseTrees dtkvGrammar recogItem_assignment test_assignment_items
validParses_fnCall = validParses dtkvGrammar testCode_fnCall
recogItem_fnCall = head validParses_fnCall
parseTree_fnCall = getParseTree recogItem_fnCall
--getParseTree dtkvGrammar recogItem_fnCall test_fnCall_items
--parseTrees_fnCall = getParseTrees dtkvGrammar recogItem_fnCall test_fnCall_items
validParses_nestedCalls = validParses dtkvGrammar testCode_nestedCalls
recogItem_nestedCalls = head validParses_nestedCalls
parseTree_nestedCalls = getParseTree recogItem_nestedCalls
-- getParseTree dtkvGrammar recogItem_nestedCalls test_nestedCalls_items
--parseTrees_nestedCalls = getParseTrees dtkvGrammar recogItem_nestedCalls test_nestedCalls_items
validParses_arithFnDecl = validParses dtkvGrammar testCode_arithFnDecl
recogItem_arithFnDecl = head validParses_arithFnDecl
parseTree_arithFnDecl = getParseTree recogItem_arithFnDecl
--getParseTree dtkvGrammar recogItem_arithFnDecl test_arithFnDecl_items
--parseTrees_arithFnDecl = getParseTrees dtkvGrammar recogItem_arithFnDecl test_arithFnDecl_items
validParses_forLoop = validParses dtkvGrammar testCode_forLoop
recogItem_forLoop = head validParses_forLoop
parseTree_forLoop = getParseTree recogItem_forLoop
--getParseTree dtkvGrammar recogItem_forLoop test_forLoop_items
--parseTrees_forLoop = getParseTrees dtkvGrammar recogItem_forLoop test_forLoop_items
validParses_whileLoop = validParses dtkvGrammar testCode_whileLoop
recogItem_whileLoop = head validParses_whileLoop
parseTree_whileLoop = getParseTree recogItem_whileLoop
--getParseTree dtkvGrammar recogItem_whileLoop test_whileLoop_items
--parseTrees_whileLoop = getParseTrees dtkvGrammar recogItem_whileLoop test_whileLoop_items
validParses_forLoopInFn = validParses dtkvGrammar testCode_forLoopInFn
recogItem_forLoopInFn = head validParses_forLoopInFn
parseTree_forLoopInFn = getParseTree recogItem_forLoopInFn
--getParseTree dtkvGrammar recogItem_forLoopInFn test_forLoopInFn_items
--parseTrees_forLoopInFn = getParseTrees dtkvGrammar recogItem_forLoopInFn test_forLoopInFn_items
validParses_stringUserFn = validParses dtkvGrammar testCode_stringUserFn
recogItem_stringUserFn = head validParses_stringUserFn
parseTree_stringUserFn = getParseTree recogItem_stringUserFn
--getParseTree dtkvGrammar recogItem_stringUserFn test_stringUserFn_items
--parseTrees_stringUserFn = getParseTrees dtkvGrammar recogItem_stringUserFn test_stringUserFn_items