From: Mart Lubbers Date: Sun, 28 Feb 2016 12:46:25 +0000 (+0100) Subject: ASSOCIATIVITY, DO WE EVER PARSE IT??????!!!!111!one!111eleven X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=d1e64de8a0cc6c01fba1a3dc17686f9539bf5744;p=cc1516.git ASSOCIATIVITY, DO WE EVER PARSE IT??????!!!!111!one!111eleven --- diff --git a/src/grammar.txt b/src/grammar.txt index 697cca2..ab2e4db 100644 --- a/src/grammar.txt +++ b/src/grammar.txt @@ -1,35 +1,27 @@ - ::= - - - ::= '+' - - ::= ['%' ] - - ::= [('+' | '-') ] - - ::= ['*' | '/' | '%'] ] - + ::= [':' ] + ::= ['||' ] + ::= ['&&' ] + ::= [('==' | '<' | '>' | '<=' | '>=' | '!=') ] + ::= (('+' | '-') )* + ::= ('*' | '/' | '%'] )* ::= ['hd'|'tl'|'fst'|'snd] - | - | - | - | 'False' - | 'True' - | '(' ')' - | '(' [] ')' - | '[]' - | '(' ',' ')' - - ::= '+' | '-' | '*' | '/' | '%' | '==' | '<' | '>' | '<=' | '>=' - | '!=' | '&&' | '||' | ':' - - ::= [',' ActArgs] - - ::= ( | 'var') '=' ';' - - ::= 'Int' - | 'Bool' - | 'Char' - | '(' Type ',' Type ')' - | '[' Type ']' - | + | + | + | + | 'False' + | 'True' + | '(' ')' + | '(' [] ')' + | '[]' + | '(' ',' ')' + + ::= [',' ActArgs] + + ::= ( | 'var') '=' ';' + + ::= 'Int' + | 'Bool' + | 'Char' + | '(' Type ',' Type ')' + | '[' Type ']' + | diff --git a/src/parse.icl b/src/parse.icl index 75eed41..800d3ed 100644 --- a/src/parse.icl +++ b/src/parse.icl @@ -23,7 +23,7 @@ parser (Right r) = case runParser parseProgram r of 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 @@ -50,28 +50,31 @@ parseType = 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 = @@ -81,7 +84,7 @@ parseExpr = >>= \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) <|>