Skip to content
Snippets Groups Projects
Commit 96af91ae authored by pm3g19's avatar pm3g19
Browse files

Basic version of let works

parent 88cba6ba
Branches
Tags
No related merge requests found
......@@ -16,7 +16,8 @@ main = do
args <- getArgs
case args of
(srcname:_) -> interpret srcname
_ -> interpret "solutions/pr2.cql"
--_ -> interpret "solutions/pr3.cql"
_ -> interpret "sampleprogram.txt"
interpret :: FilePath -> IO () -- the main function, takes in file name, prints out result
......
......@@ -21,6 +21,10 @@ isEmpty {\p s -> TokenIsEmpty p }
filter {\p s -> TokenFilter p }
true {\p s -> TokenTrue p }
false {\p s -> TokenFalse p }
let {\p s -> TokenLet p }
if {\p s -> TokenIf p }
else {\p s -> TokenElse p }
then {\p s -> TokenThen p }
\.in {\p s -> TokenInSet p }
\.out {\p s -> TokenOutSet p }
\[ {\p s -> TokenLeftSqBracket p }
......@@ -29,6 +33,7 @@ false {\p s -> TokenFalse p }
"==" {\p s -> TokenisEqual p }
"/=" {\p s -> TokenisNotEqual p }
"+" {\p s -> TokenPlus p }
\= {\p s -> TokenEqual p }
\( {\p s -> TokenLeftBracket p }
\) {\p s -> TokenRightBracket p }
\: {\p s -> TokenCol p }
......@@ -76,7 +81,12 @@ data Token =
TokenXProduct AlexPosn |
TokenXXProduct AlexPosn |
TokenOutSet AlexPosn |
TokenMap AlexPosn
TokenMap AlexPosn |
TokenLet AlexPosn |
TokenIf AlexPosn |
TokenElse AlexPosn |
TokenThen AlexPosn |
TokenEqual AlexPosn
deriving (Eq, Show)
......@@ -111,5 +121,9 @@ pos token = case token of
(TokenXXProduct p ) -> p
(TokenOutSet p ) -> p
(TokenMap p) -> p
(TokenLet p) -> p
(TokenElse p) -> p
(TokenIf p) -> p
(TokenThen p) -> p
(TokenEqual p) -> p
}
\ No newline at end of file
......@@ -9,7 +9,6 @@ import CSV
%tokentype {Token}
%error {parseError}
--\\map(\r -> r[1,2,3])
%token
filter { TokenFilter _ }
......@@ -44,16 +43,21 @@ import CSV
contains { TokenContains _ }
-- isSubstring { TokenIsSubstring _ }
isEmpty { TokenIsEmpty _ }
let { TokenLet _}
if { TokenIf _}
else { TokenElse _}
then { TokenThen _}
'=' { TokenEqual _}
%right "->"
%left "/=" "==" ';'
%right '+'
%nonassoc '('
%%
Prog : in SetDecls out SetFuncCalls {($2,$4)}
SetDecl : SetName':'PosNat {$1}
SetDecl : SetName':'Nat {$1}
SetDecls : SetDecl {[$1]}
| SetDecls','SetDecl {$3:$1}
......@@ -67,32 +71,35 @@ VarNames : VarName {[$1]}
SetFuncCalls : SetFuncCall';' {[$1]}
| SetFuncCall';' SetFuncCalls {$1:$3}
SetFuncCall : filter '['SetName']' '('Func')' {FuncCall (PredefFunc Filter) [Var $3] [$6]}
| filter '('Func')' {FuncCall (PredefFunc Filter) [] [$3]}
--| map '['SetName']' '('Func')' {FuncCall (PredefFunc Map) [Var $3] [$6]}
| map '('Func')' {FuncCall (PredefFunc Map) [] [$3]}
SetFuncCall : filter '['SetName']' '('Expr')' {FuncCall (PredefFunc Filter) [Var $3] [$6]}
| filter '('Expr')' {FuncCall (PredefFunc Filter) [] [$3]}
--| map '['SetName']' '('Expr')' {FuncCall (PredefFunc Map) [Var $3] [$6]}
| map '('Expr')' {FuncCall (PredefFunc Map) [] [$3]}
| SetName x SetName {FuncCall (PredefFunc XProduct) (map Var [$1, $3]) []}
| let VarName '=' Expr {Let $2 $4}
Func : '\\' '(' VarNames ')' "->" Expr { FuncDef [] $3 $6 }
Expr : Expr "==" Expr {FuncCall (PredefFunc IsEqual) [] [$1, $3]}
| Expr'['Nat']' {FuncCall (PredefFunc RecordIndex) [] [$1, Types.Int $3]}
| Expr'['Nat','Nats']' {FuncCall (PredefFunc RecordSelect) [] ($1:(map Types.Int ($3:$5))) }
| Function'('Exprs')' {FuncCall $1 [] $3}
| '\\' '(' VarNames ')' "->" Expr { FuncDef [] $3 $6 }
| if Expr then Expr else Expr {If $2 $4 $6}
| Str {Types.String $ stripWhitespace $1}
| VarName {Var $1}
| Record {$1}
| true {Boolean True}
| false {Boolean False}
| '(' Expr ')' {$2}
--TODO brackets
Function : PredefFunc {PredefFunc $1}
| VarName { Var $1}
PredefFunc : isEmpty {IsEmpty}
-- | isSubstring {IsSubstring}
| contains {Contains}
| isEmpty {IsEmpty}
-- | zip {Zip}
-- | Map
-- | Mapr
......@@ -107,6 +114,7 @@ Nats : Nat {[$1]}
| Nat ',' Nats {$1:$3}
{
parseError :: [Token] -> a
parseError tokens = error $ "Parse error: " ++ (show.pos.head) tokens
......
......@@ -12,6 +12,8 @@ data PredefFunc = XProduct | XXProduct | IsEqual | IsNotEqual | Plus --operators
| RecordIndex -- [] operator
| RecordSelect
| IsEmpty | NotEmpty | Contains -- string functions
| And
| Or
deriving (Show, Eq)
......@@ -24,7 +26,6 @@ data PredefFunc = XProduct | XXProduct | IsEqual | IsNotEqual | Plus --operators
data Expr = Control Expr [Expr] -- result of last computation, sequence of instructions
| FuncCall {func::Expr, inputSets::[Expr], args::[Expr]} -- args: function to call, input sets, extra argument. Applicable for all kinds of functions: the set functions e.g. filter, map or simple functions or even the operators e.g. ==, /= ! We don't define separate expr for each operator
| FuncDef {inputSetNames::[SymbolName], argsNames::[SymbolName], body::Expr} -- function definition
| If Expr Expr Expr
| Set [Expr]
......
.in
SampleSet
SampleSet:4
.out
filter (\(r) -> r[0] == "hello")
\ No newline at end of file
let f = \(r) -> r[1] == "hello";
filter[SampleSet](f);
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment