diff --git a/Interpreter.hs b/Interpreter.hs index 97a5a6bb650dfbc8dd0fd1cbf7d652d0a85e5236..1b8ee24cfb27e02be165741e1596d2a0c45c6d69 100644 --- a/Interpreter.hs +++ b/Interpreter.hs @@ -9,14 +9,14 @@ import Parser import Debug.Trace parse :: String -> Program -parse = parseSource.alexScanTokens +parse = parseSource.(\x -> let o = alexScanTokens x in traceShowId o) --outline main = do args <- getArgs case args of (srcname:_) -> interpret srcname - _ -> interpret "q1.txt" + _ -> interpret "solutions/pr1.cql" interpret :: FilePath -> IO () -- the main function, takes in file name, prints out result diff --git a/Parser.y b/Parser.y index 8875f8058af2b8aca899c58bc71f664724f83d61..426d5e870e774728af224dd86330c2cb9402ed8d 100644 --- a/Parser.y +++ b/Parser.y @@ -2,73 +2,115 @@ module Parser where import Lexer import Types +import CSV } %name parseSource %tokentype {Token} %error {parseError} +--\\map(\r -> r[1,2,3]) + %token - filter { TokenFilter } - in { TokenInSet } - out { TokenOutSet } - SetName { TokenSetName $$ } - Nat { TokenNat $$ } - VarName { TokenVarName $$ } - true { TokenTrue } - false { TokenFalse } - Str { TokenString $$ } - '[' { TokenLeftSqBracket } - ']' { TokenRightSqBracket } - "->" { TokenArrow } - "==" { TokenisEqual } - "/=" { TokenisNotEqual } - '(' { TokenLeftBracket } - ')' { TokenRightBracket } - ';' { TokenSemiCol } - '\\' { TokenLambda } - ',' { TokenComma } - '.' { TokenFullStop } - x { TokenXProduct } + filter { TokenFilter _ } + in { TokenInSet _ } + out { TokenOutSet _ } + SetName { TokenSetName _ $$ } + Nat { TokenNat _ $$ } + PosNat { TokenPosNat _ $$ } + VarName { TokenVarName _ $$ } + true { TokenTrue _ } + false { TokenFalse _ } + Str { TokenString _ $$ } + '[' { TokenLeftSqBracket _ } + ']' { TokenRightSqBracket _ } + "->" { TokenArrow _ } + "==" { TokenisEqual _ } + "/=" { TokenisNotEqual _ } + '(' { TokenLeftBracket _ } + ')' { TokenRightBracket _ } + ';' { TokenSemiCol _ } + ':' { TokenCol _ } + '\\' { TokenLambda _ } + ',' { TokenComma _ } + '.' { TokenFullStop _ } + '+' { TokenPlus _ } + x { TokenXProduct _ } + map { TokenMap _ } + xx { TokenXXProduct _ } +-- mapr { TokenMapr _ } +-- '!' { TokenNot _ } +-- zip { TokenZip _ } + contains { TokenContains _ } +-- isSubstring { TokenIsSubstring _ } + isEmpty { TokenIsEmpty _ } %right "->" %left "/=" "==" ';' +%right '+' %% -Prog : in SetNames out SetFuncCalls {($2,$4)} +Prog : in SetDecls out SetFuncCalls {($2,$4)} + + +SetDecl : SetName':'PosNat {$1} +SetDecls : SetDecl {[$1]} + | SetDecls','SetDecl {$3:$1} SetNames : SetName {[$1]} - | SetName ',' SetNames { $1:$3} + | SetName ',' SetNames { $1:$3 } VarNames : VarName {[$1]} | VarName ',' VarNames {$1:$3} -SetFuncCalls : SetFuncCall {[$1]} +SetFuncCalls : SetFuncCall';' {[$1]} | SetFuncCall';' SetFuncCalls {$1:$3} SetFuncCall : filter '['SetName']' '('Func')' {FuncCall (PredefFunc Filter) [Var $3] [$6]} - | filter '('Func')' {FuncCall (PredefFunc Filter) [] [$3]} + | filter '('Func')' {FuncCall (PredefFunc Filter) [] [$3]} + --| map '['SetName']' '('Func')' {FuncCall (PredefFunc Map) [Var $3] [$6]} + | map '('Func')' {FuncCall (PredefFunc Map) [] [$3]} + | SetName x SetName {FuncCall (PredefFunc XProduct) (map Var [$1, $3]) []} - | SetName x SetName {FuncCall (PredefFunc XProduct) (map Var [$1, $3]) []} - -Func : '\\' '(' VarNames ')' "->" Expr {FuncDef [] $3 $6} +Func : '\\' '(' VarNames ')' "->" Expr { FuncDef [] $3 $6 } Expr : Expr "==" Expr {FuncCall (PredefFunc IsEqual) [] [$1, $3]} | Expr'['Nat']' {FuncCall (PredefFunc RecordIndex) [] [$1, Types.Int $3]} - | Str {Types.String $1} + | Expr'['Nat','Nats']' {FuncCall (PredefFunc RecordSelect) [] ($1:(map Types.Int ($3:$5))) } + | Function'('Exprs')' {FuncCall $1 [] $3} + | Str {Types.String $ stripWhitespace $1} | VarName {Var $1} | Record {$1} | true {Boolean True} | false {Boolean False} + --TODO brackets + +Function : PredefFunc {PredefFunc $1} + | VarName { Var $1} + +PredefFunc : isEmpty {IsEmpty} +-- | isSubstring {IsSubstring} + | contains {Contains} +-- | zip {Zip} +-- | Map +-- | Mapr + Record : '['Exprs']' {Record $2} Exprs : Expr {[$1]} | Expr','Exprs {$1:$3} +Nats : Nat {[$1]} + | Nat ',' Nats {$1:$3} + { parseError :: [Token] -> a -parseError _ = error "Parse error" +parseError tokens = error $ "Parse error: " ++ (show.pos.head) tokens + +tokenPosn :: Token -> String +tokenPosn t = let (AlexPn _ line col) = pos t in (show line) ++ ':' : (show col) + }