From f297ddf3ea579fcafe3111510b385da1ba150a89 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Mon, 14 Mar 2016 23:33:44 +0100 Subject: [PATCH] fixed nasty pretty print bug, added selftest to readme --- AST.icl | 2 +- README.md | 6 ++++++ parse.icl | 14 +++++++------- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/AST.icl b/AST.icl index fb4c405..70c1282 100644 --- a/AST.icl +++ b/AST.icl @@ -82,7 +82,7 @@ instance print Expr where print (VarExpr _ vd) = print vd print (Op2Expr _ e1 o e2) = ["(":print e1] ++ [" ",case o of BiPlus = "+"; BiMinus = "-"; BiTimes = "*"; BiDivide = "/" - BiMod = "%"; BiEquals = "="; BiLesser = "<"; BiGreater = ">" + BiMod = "%"; BiEquals = "=="; BiLesser = "<"; BiGreater = ">" BiLesserEq = "<="; BiGreaterEq = ">="; BiUnEqual = "!="; BiAnd = "&&"; BiOr = "||"; BiCons = ":" ," ":print e2] ++ [")"] diff --git a/README.md b/README.md index 2747d59..18cae80 100644 --- a/README.md +++ b/README.md @@ -19,5 +19,11 @@ Install a real operating system and try again. If `help2man` is installed you can generate a manpage by running `make man`. For full instruction see `man ./spl.1` or run `./spl --help`. +## Fun +You can verify the parser and pretty printer by running: +``` +diff -s <(./spl examples/example.spl | ./spl) <(./spl examples/example.spl) +``` + ## `LICENCE` See `LICENCE` file. diff --git a/parse.icl b/parse.icl index 8c4a25e..34ec15b 100644 --- a/parse.icl +++ b/parse.icl @@ -29,15 +29,15 @@ parseFunDecl :: Parser Token FunDecl parseFunDecl = liftM6 FunDecl (peekPos) (parseIdent) - (satTok BraceOpenToken *> parseSepList CommaToken parseIdent <* satTok BraceCloseToken) - (optional parseFunType <* satTok CBraceOpenToken) - (many parseVarDecl) + (parseBBraces $ parseSepList CommaToken parseIdent) + (optional parseFunType) + (satTok CBraceOpenToken *> many parseVarDecl) (many parseStmt <* satTok CBraceCloseToken) parseStmt :: Parser Token Stmt parseStmt = parseIfStmt <|> parseWhileStmt <|> parseSColon parseAssStmt <|> parseSColon parseReturnStmt <|> - (liftM FunStmt (parseSColon parseFunCall)) + (FunStmt <$> parseSColon parseFunCall) where parseSColon :: (Parser Token a) -> Parser Token a parseSColon p = p <* satTok SColonToken @@ -58,8 +58,8 @@ parseStmt = parseIfStmt <|> parseWhileStmt <|> (optional (satTok ElseToken *> (parseBlock<|> parseOneLine)))) parseWhileStmt :: Parser Token Stmt - parseWhileStmt = satTok WhileToken *> - (WhileStmt <$> (parseBBraces parseExpr) <*> parseBlock) + parseWhileStmt = satTok WhileToken *> (WhileStmt <$> + (parseBBraces parseExpr) <*> (parseBlock <|> parseOneLine)) parseBlock :: Parser Token [Stmt] parseBlock = parseBCBraces (many parseStmt) @@ -197,4 +197,4 @@ parseIdent :: Parser Token String parseIdent = trans2 (IdentToken "") (\(IdentToken e).toString e) //liftM only goes to liftM5 -liftM6 f m1 m2 m3 m4 m5 m6 = f <$> m1 <*> m2 <*> m3 <*> m4 <*> m5 <*> m6 \ No newline at end of file +liftM6 f m1 m2 m3 m4 m5 m6 = f <$> m1 <*> m2 <*> m3 <*> m4 <*> m5 <*> m6 -- 2.20.1