From ff0b269598ad277346c81db9d9bc86b4c244e1e2 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Sun, 28 Feb 2016 15:02:09 +0100 Subject: [PATCH] added test files for variable declaration parsing --- src/exVarDecl.spl | 25 ++++++++++++++++++ src/main.icl | 7 ++++- src/parse.dcl | 4 ++- src/parse.icl | 67 +++++++++++++++++++++++++++++------------------ 4 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 src/exVarDecl.spl diff --git a/src/exVarDecl.spl b/src/exVarDecl.spl new file mode 100644 index 0000000..4879075 --- /dev/null +++ b/src/exVarDecl.spl @@ -0,0 +1,25 @@ +var v1ar1 = 1; +Int int1 = 1; +Bool bool1 = 1; +Char char1 = 1; +[Char] listchar1 = 1; +(Bool, Int) tupleboolint1 = 1; +var v1 = True; +var v2 = False; +var v3 = True && False; +var v4 = True || !False; +var v4 = 1 - 2 - 3 -- 4; +var v5 = 1 * 2 - 3 * 4; +var v6 = 1 * (2 - 3) * 4; +var v7 = 1 * 2 % 3 * 4; +var v8 = a * 4; +var v9 = a.tl * 4; +var w0 = a.fst * 4; +var w1 = a.snd * 4; +var w2 = a.hd : anotherVariable; +var w3 = [] + 5; +var w4 = func(); +var w5 = func(1); +var w5 = func(1, [], 1+2); +var w6 = ([] + 5, func(1)); +([a], [(b, (c, [[[(d, e)]]]))]) t = f(1+2-3*4%5/6+(6+(6+1)), g(h(a.fst), [])); diff --git a/src/main.icl b/src/main.icl index edd07f1..f2b5c93 100644 --- a/src/main.icl +++ b/src/main.icl @@ -41,7 +41,12 @@ Start w (Left e) = (Left e, Left "Parsing disabled", w) = case contents of (Left e) = (Left e, Left "", w) - (Right cs) = let lexOut = lexer cs in (lexOut, parser lexOut, w) + (Right cs) + # lexout = lexer cs + # parsout = parser lexout + # stdin = stdin <<< (either (const "") toString parsout) + # (_, w) = fclose stdin w + = (lexout, parser lexout, w) parseArgs :: *World -> (Opts, *World) parseArgs w diff --git a/src/parse.dcl b/src/parse.dcl index e7d2a25..a5bc93a 100644 --- a/src/parse.dcl +++ b/src/parse.dcl @@ -36,7 +36,9 @@ import lex //TODO -:: FunDecl = Stub +:: FunDecl = FunDecl String [String] FunType [VarDecl] [Stmt] +:: FunType = Stub +:: Stmt = Stubbel instance toString AST diff --git a/src/parse.icl b/src/parse.icl index 800d3ed..aca2489 100644 --- a/src/parse.icl +++ b/src/parse.icl @@ -49,7 +49,7 @@ parseType = empty parseExpr :: Parser Token Expr -parseExpr = +parseExpr = //Operators in order of binding strength parseOpRight (trans1 ColonToken BiCons) $ parseOpRight (trans1 PipesToken BiOr) $ parseOpRight (trans1 AmpersandsToken BiAnd) $ @@ -64,7 +64,7 @@ parseExpr = parseOpLeft (trans1 StarToken BiTimes <|> trans1 SlashToken BiDivide <|> trans1 PercentToken BiMod) $ - parseBasicExpr + parseBasicExpr where parseOpRight :: (Parser Token Op2) (Parser Token Expr) -> Parser Token Expr parseOpRight ops prev = prev >>= \e1->optional ( @@ -78,27 +78,20 @@ parseExpr = parseBasicExpr :: Parser Token Expr parseBasicExpr = - (satTok BraceOpenToken *> parseExpr <* satTok BraceCloseToken) <|> (satTok BraceOpenToken *> parseExpr <* satTok CommaToken >>= \e1->parseExpr <* satTok BraceCloseToken >>= \e2->pure $ TupleExpr e1 e2) <|> + (parseIdent <* satTok BraceOpenToken + >>= \i->parseSepList CommaToken parseExpr <* satTok BraceCloseToken + >>= \es->pure $ FunExpr i es) <|> + (satTok BraceOpenToken *> parseExpr <* satTok BraceCloseToken) <|> trans1 EmptyListToken EmptyListExpr <|> trans2 TrueToken (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) <|> - (parseIdent <* satTok BraceOpenToken - >>= \i->parseActArgs <* satTok BraceCloseToken - >>= \es->pure $ FunExpr i es) <|> - (parseIdent >>= \i. parseFieldSelector >>= \f.pure $ VarExpr i f) - -parseActArgs :: Parser Token [Expr] -parseActArgs = - (some (parseExpr <* satTok CommaToken) >>= \es->parseExpr - >>= \e.pure [e:es]) <|> - (parseExpr >>= \e->pure [e]) <|> - empty + (parseIdent >>= \i->parseFieldSelector >>= \f.pure $ VarExpr i f) parseOp1 :: Parser Token Op1 parseOp1 = trans1 DashToken UnMinus <|> @@ -120,22 +113,41 @@ trans1 t r = trans2 t $ const r satTok :: TokenValue -> Parser Token Token satTok t = satisfy ((===) t) +parseSepList :: TokenValue (Parser Token a) -> Parser Token [a] +parseSepList sep p = + (some (p <* satTok sep) >>= \es->p >>= \e.pure $ reverse [e:es]) <|> + (p >>= \e->pure [e]) <|> pure [] + parseIdent :: Parser Token String parseIdent = trans2 (IdentToken []) (\(IdentToken e).toString e) instance toString AST where - toString (AST v f) = concat (print v ++ ["\n":print f]) + toString (AST v f) = concat ( + ["\n":strJoin "\n" (map print v)] ++ + ["\n":strJoin "\n" (map print f)]) class print a :: a -> [String] -instance print [a] | print a where - print [] = ["\n"] - print [v:vs] = print v ++ ["\n":print vs] +instance print FunDecl where + print (FunDecl i as t vs ss) = + ["\n", i, " (":strJoin "," (map (\i->[i]) as)] ++ + [") :: ":print t] ++ + ["{\n\t":strJoin "\n\t" (map print vs)] ++ + ["\n":printStatements ss 1] ++ ["\n}"] + +printStatements :: [Stmt] Int -> [String] +printStatements _ _ = [] + +//TODO +instance print FunType where + print _ = [] + +//TODO +instance print Stmt where + print _ = [] instance print VarDecl where print (VarDecl i t e) = print t ++ [" ":i:"=":print e] ++ [";"] -instance print FunDecl where - print _ = ["Function printing not yet implemented"] instance print Type where print (TupleType t1 t2) = ["(":print t1] ++ [", ":print t2] ++ [")"] @@ -146,20 +158,24 @@ instance print Type where print CharType = ["Char"] print VarType = ["var"] +strJoin :: String [[String]] -> [String] +strJoin _ [] = [] +strJoin j [s:rest] = s ++ flatten [[j:ss]\\ss<-rest] + instance print Expr where print (VarExpr i Nothing) = [i] print (VarExpr i (Just mf)) = [i, case mf of FieldHd = ".hd"; FieldTl = ".tl" FieldSnd = ".snd"; FieldFst = ".fst"] - print (Op2Expr e1 o e2) = print e1 ++ [case o of + print (Op2Expr e1 o e2) = ["(":print e1] ++ [case o of BiPlus = "+"; BiMinus = "-"; BiTimes = "*"; BiDivide = "/" BiMod = "%"; BiEquals = "="; BiLesser = "<"; BiGreater = ">" BiLesserEq = "<="; BiGreaterEq = ">="; BiUnEqual = "!="; BiAnd = "&&"; BiOr = "||"; BiCons = ":" - :print e2] - print (Op1Expr o e) = [case o of + :print e2] ++ [")"] + print (Op1Expr o e) = ["(",case o of UnNegation = "!"; UnMinus = "-" - :print e] + :print e] ++ [")"] print (IntExpr i) = [toString i] print (CharExpr c) = ["\'", case c of '\b' = "\\b"; '\f' = "\\f"; '\n' = "\\n" @@ -167,7 +183,6 @@ instance print Expr where c = if (c == toChar 7) "\\a" (toString c) ,"\'"] print (BoolExpr b) = [toString b] - print (FunExpr i es) = pe ++ flatten [[",":x]\\x<-tl pes] - where [pe:pes] = map print es + print (FunExpr i es) = [i,"(":strJoin "," (map print es)] ++ [")"] print EmptyListExpr = ["[]"] print (TupleExpr e1 e2) = ["(":print e1] ++ [",":print e2] ++ [")"] -- 2.20.1