-<Expr> ::= <ExprWithoutOp> <ExprMaybeOp>
-
-
-<ExprMaybeOp> ::= '+' <Expr>
-
-<ModExpr> ::= <PlusMinExpr> ['%' <ModExpr>]
-
-<PlusMinExpr> ::= <TimesDivExpr> [('+' | '-') <PlusMinExpr>]
-
-<TimesDivExpr> ::= <BasicExpr> ['*' | '/' | '%'] <TimesDivExpr>]
-
+<Expr> ::= <BinOrExpr> [':' <Expr>]
+<BinOrExpr> ::= <BinAndExpr> ['||' <BinOrExpr>]
+<BinAndExpr> ::= <CompareExpr> ['&&' <BinAndExpr>]
+<CompareExpr> ::= <PlusMinExpr> [('==' | '<' | '>' | '<=' | '>=' | '!=') <CompareExpr>]
+<PlusMinExpr> ::= <TimesDivExpr> (('+' | '-') <TimesDivExpr>)*
+<TimesDivExpr> ::= <BasicExpr> ('*' | '/' | '%'] <BasicExpr>)*
<BasicExpr> ::= <id> ['hd'|'tl'|'fst'|'snd]
- | <Op1> <Expr>
- | <int>
- | <char>
- | 'False'
- | 'True'
- | '(' <Expr> ')'
- | <id> '(' [<ActArgs>] ')'
- | '[]' <Expr>
- | '(' <Expr> ',' <Expr> ')'
-
-<Op2> ::= '+' | '-' | '*' | '/' | '%' | '==' | '<' | '>' | '<=' | '>='
- | '!=' | '&&' | '||' | ':'
-
-<ActArgs> ::= <Expr> [',' ActArgs]
-
-<VarDecl> ::= (<Type> | 'var') <id> '=' <Expr> ';'
-
-<Type> ::= 'Int'
- | 'Bool'
- | 'Char'
- | '(' Type ',' Type ')'
- | '[' Type ']'
- | <id>
+ | <Op1> <Expr>
+ | <int>
+ | <char>
+ | 'False'
+ | 'True'
+ | '(' <Expr> ')'
+ | <id> '(' [<ActArgs>] ')'
+ | '[]' <Expr>
+ | '(' <Expr> ',' <Expr> ')'
+
+<ActArgs> ::= <Expr> [',' ActArgs]
+
+<VarDecl> ::= (<Type> | 'var') <id> '=' <Expr> ';'
+
+<Type> ::= 'Int'
+ | 'Bool'
+ | 'Char'
+ | '(' Type ',' Type ')'
+ | '[' Type ']'
+ | <id>
parseProgram :: Parser Token AST
parseProgram = some parseVarDecl
- >>= \vd->some parseFunDecl >>= \fd->pure $ AST vd fd
+ >>= \vd->pure $ AST vd []
parseFunDecl :: Parser Token FunDecl
parseFunDecl = empty
parseExpr :: Parser Token Expr
parseExpr =
- parseOps (trans1 ColonToken BiCons) $
- parseOps (trans1 PipesToken BiOr) $
- parseOps (trans1 AmpersandsToken BiAnd) $
- parseOps (trans1 EqualsToken BiEquals <|>
+ parseOpRight (trans1 ColonToken BiCons) $
+ parseOpRight (trans1 PipesToken BiOr) $
+ parseOpRight (trans1 AmpersandsToken BiAnd) $
+ parseOpRight (trans1 EqualsToken BiEquals <|>
trans1 LesserToken BiLesser <|>
trans1 BiggerToken BiGreater <|>
trans1 LesserEqToken BiLesserEq <|>
trans1 GreaterEqToken BiGreaterEq <|>
trans1 NotEqualToken BiUnEqual) $
- parseOps (trans1 PlusToken BiPlus <|>
+ parseOpLeft (trans1 PlusToken BiPlus <|>
trans1 DashToken BiMinus) $
- parseOps (trans1 StarToken BiTimes <|>
+ parseOpLeft (trans1 StarToken BiTimes <|>
trans1 SlashToken BiDivide <|>
trans1 PercentToken BiMod) $
parseBasicExpr
where
- parseOps :: (Parser Token Op2) (Parser Token Expr) -> Parser Token Expr
- parseOps ops prev = prev >>= \e1->optional (
- ops >>= \op->parseOps ops prev >>= \e->pure (op, e)
- ) >>= \moe-> pure case moe of
- Nothing = e1
- (Just (op, e2)) = Op2Expr e1 op e2
+ parseOpRight :: (Parser Token Op2) (Parser Token Expr) -> Parser Token Expr
+ parseOpRight ops prev = prev >>= \e1->optional (
+ ops >>= \op->parseOpRight ops prev >>= \e->pure (op, e)
+ ) >>= \moe->pure $ maybe e1 (\(op,e2)->Op2Expr e1 op e2) moe
+
+ parseOpLeft :: (Parser Token Op2) (Parser Token Expr) -> Parser Token Expr
+ parseOpLeft ops prev = prev >>= \e1->many (
+ ops >>= \op->prev >>= \e->pure (op, e)
+ ) >>= \moe->foldM (\e->(\(op,e2)->pure $ Op2Expr e op e2)) e1 moe
parseBasicExpr :: Parser Token Expr
parseBasicExpr =
>>= \e2->pure $ TupleExpr e1 e2) <|>
trans1 EmptyListToken EmptyListExpr <|>
trans2 TrueToken (const $ BoolExpr True) <|>
- trans2 FalseToken (const $ BoolExpr True) <|>
+ trans2 FalseToken (const $ BoolExpr False) <|>
trans2 (NumberToken zero) (\(NumberToken i)->IntExpr i) <|>
trans2 (CharToken zero) (\(CharToken c)->CharExpr c) <|>
(parseOp1 >>= \o->parseExpr >>= \e.pure $ Op1Expr o e) <|>