Administrator approval is now required for registering new accounts. If you are registering a new account, and are external to the University, please ask the repository owner to contact ServiceLine to request your account be approved. Repository owners must include the newly registered email address, and specific repository in the request for approval.

Commit e04ef58f authored by pm3g19's avatar pm3g19
Browse files

Improved error checking

parent 84040589
......@@ -28,7 +28,7 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
IsEqual -> case args' of -- TODO not sufficent.
[e1, e2] -> Boolean (e1 == e2)
_ -> "isEqual argument error"
_ -> error "isEqual argument error"
XProduct -> case inputSets' of
[(Set l1), (Set l2)] -> Set $ [ x `concatRecord` y | x <- l1, y <- l2]
......@@ -38,16 +38,19 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
[Boolean e1, Boolean e2] -> Boolean (e1 `op` e2)
_ -> error "Boolean function takes in two arguments."
RecordIndex -> eval' $ let (Record recordData:Int index:_) = args' in
recordData !! (index - 1)
RecordIndex -> eval' $ case args' of
[Record recordData,Int index] -> recordData `listAtIndex` index
--TODO CBH
RecordSelect -> case args' of
(Record recordData: indexes ) -> Record filteredList
where
indexesRaw = map checkInt indexes
filteredList = map (eval'.(recordData!!)) indexesRaw
checkInt (Int i) = i - 1
--filteredList = map (eval'.(recordData !!)) indexesRaw
filteredList = map (eval'.listAtIndex recordData) indexesRaw
checkInt (Int i) = i
checkInt _ = error "Arguments to index record must be integers"
{-numberedElems = zip [1..] recordData :: [(Int, Expr)]
filtered = filter ((`elem` indexesRaw).fst) numberedElems :: [(Int, Expr)]
filteredList = map snd filtered :: [Expr]-}
......@@ -93,9 +96,13 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
let (newEnv, newControl) = evalControl1 env control in
eval newEnv newControl
(Record exprs) -> Record $ map eval' exprs
(Record exprs) -> Record $ map eval' exprs
_ -> expr
listAtIndex :: [a] -> Int -> a
listAtIndex recordData i | i > length recordData = error $ "Index " ++ show i ++ " too large."
| otherwise = recordData !! (i - 1)
evalControl1 :: Environment -> Expr -> (Environment, Expr)
evalControl1 env (Control last (currentExpr:exprs)) = (newEnv, Control newLast exprs)
......
......@@ -7,18 +7,24 @@ import CSV
import Lexer
import Parser
import Debug.Trace
import Control.Exception
printErr s = hPutStrLn stderr ("[ERROR] " ++ s)
parse :: String -> Program
parse = parseSource.(\x -> let o = alexScanTokens x in traceShowId o)
parse = parseSource.alexScanTokens
--outline
main = do
args <- getArgs
case args of
(srcname:_) -> interpret srcname
_ -> interpret "solutions/pr4.cql"
--_ -> interpret "extra-problems/pr3.cql"
--_ -> interpret "sampleprogram.txt"
--("default":_) -> catch (interpret "/home/patryk/dev/plc/extra-problems/ex1.cql") (errorFunc)
(srcname:_) -> catch (interpret srcname) (errorFunc)
--_ -> printErr "No source file specified."
_ -> catch (interpret "/home/patryk/dev/plc/extra-problems/ex1.cql") (errorFunc)
errorFunc :: ErrorCall -> IO ()
errorFunc = printErr.show
interpret :: FilePath -> IO () -- the main function, takes in file name, prints out result
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment