From: pimjager Date: Wed, 1 Jun 2016 11:12:36 +0000 (+0200) Subject: Added listliterals X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=7289067f6d9ade01476d527212583ce0152e197f;p=cc1516.git Added listliterals --- diff --git a/examples/old/listLiteral.spl b/examples/old/listLiteral.spl new file mode 100644 index 0000000..d2937f3 --- /dev/null +++ b/examples/old/listLiteral.spl @@ -0,0 +1,9 @@ +main() { + [Int] x = [1,2,3]; + var y = [[1], [1,2,3], [4,5,900]]; + var z = [(\x->x+1), (\x->3), (\x->x*3)]; + var f = \x->x+1; + var fs = [f, f, f, f]; + var gs = [\x->x+1]; + return; +} \ No newline at end of file diff --git a/grammar/grammar.txt b/grammar/grammar.txt index 08ddedf..bebb2cd 100644 --- a/grammar/grammar.txt +++ b/grammar/grammar.txt @@ -25,6 +25,7 @@ | '(' ')' | | '[]' + | '[' ( ',')* () ']' | '(' ',' ')' | '"' '"' ::= '~' | '!' diff --git a/parse.icl b/parse.icl index 1739eb9..2dd8f1a 100644 --- a/parse.icl +++ b/parse.icl @@ -150,6 +150,7 @@ parseValueExpr = //Operators in order of binding strength (trans2 (StringToken []) (\(StringToken cs)->makeStrExpr pos cs)) <|> (TupleExpr pos <$> (parseTuple parseExpr)) <|> parseBBraces parseExpr <|> + parseListLiteral <|> trans1 EmptyListToken (EmptyListExpr pos) <|> trans1 TrueToken (BoolExpr pos True) <|> trans1 FalseToken (BoolExpr pos False) <|> @@ -160,6 +161,17 @@ parseValueExpr = //Operators in order of binding strength pure $ FunExpr pos ident args fs) <|> (VarExpr pos <$> parseVarDef) + parseListLiteral :: Parser Token Expr + parseListLiteral = peekPos >>= \p-> + satTok SquareOpenToken >>| + many (parseExpr <* satTok CommaToken) >>= \es-> + parseExpr >>= \e-> + foldM (\res e-> pure $ Op2Expr p e BiCons res) + (EmptyListExpr p) + [e : reverse es] >>= \res-> + satTok SquareCloseToken >>| + pure res + parseLambda :: Parser Token Expr parseLambda = LambdaExpr <$> peekPos <*> (satTok BackslashToken *> many parseIdent) diff --git a/sem.icl b/sem.icl index b000eac..9eac35b 100644 --- a/sem.icl +++ b/sem.icl @@ -132,6 +132,13 @@ instance unfoldL_ Expr where pure ([fd], fe) unfoldL_ (FunExpr p f es fs) = flattenT <$> mapM unfoldL_ es >>= \(fds, es_)-> pure (fds, FunExpr p f es_ fs) + unfoldL_ (Op2Expr p e1 op e2) = unfoldL_ e1 >>= \(fds1, e1_)-> + unfoldL_ e2 >>= \(fds2, e2_)-> + pure (fds1++fds2, Op2Expr p e1_ op e2_) + unfoldL_ (Op1Expr p op e1) = unfoldL_ e1 >>= \(fds, e1_)->pure (fds, Op1Expr p op e1_) + unfoldL_ (TupleExpr p (e1, e2)) = unfoldL_ e1 >>= \(fds1, e1_)-> + unfoldL_ e2 >>= \(fds2, e2_)-> + pure (fds1++fds2, TupleExpr p (e1_, e2_)) unfoldL_ e = pure ([], e) //------------