From: Mart Lubbers Date: Thu, 26 May 2016 16:40:12 +0000 (+0200) Subject: demo X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=c3a8a66403f083b59e20fa75f69c78b67ffa474b;p=cc1516.git demo --- diff --git a/examples/higher.spl b/examples/higher.spl new file mode 100644 index 0000000..66f9024 --- /dev/null +++ b/examples/higher.spl @@ -0,0 +1,47 @@ +map(f, l){ + if(isEmpty(l)){ + return []; + } else { + return f(l.hd) : map(f, l.tl); + } +} + +foldr(f, acc, l){ + if(isEmpty(l)){ + return acc; + } else { + return foldr(f, f(acc, l.hd), l.tl); + } +} + +filter(f, l){ + if(isEmpty(l)){ + return []; + } else { + if(f(l.hd)){ + return filter(f, l.tl); + } else { + return l.hd : filter(f, l.tl); + } + } +} + +intList(x){ + [Int] l = []; + Int a = 1; + while(a <= x){ + l = a : l; + a = a + 1; + } + return l; +} + +main(){ + print( + "faculty of 5 is: ", + foldr(\x y->x*y, 1, intList(z)), + "sum of 1..5 is: ", + foldr(\x y->x+y, 0, intList(z)), + "filter evens from 0..12 is: ", + filter(\x->x % 2 == 0, intList(12))); +} diff --git a/examples/Markus/assignment_to_builtin.spl b/examples/old/Markus/assignment_to_builtin.spl similarity index 100% rename from examples/Markus/assignment_to_builtin.spl rename to examples/old/Markus/assignment_to_builtin.spl diff --git a/examples/Markus/higher_order_functions.spl b/examples/old/Markus/higher_order_functions.spl similarity index 100% rename from examples/Markus/higher_order_functions.spl rename to examples/old/Markus/higher_order_functions.spl diff --git a/examples/Markus/identity(2).spl b/examples/old/Markus/identity(2).spl similarity index 100% rename from examples/Markus/identity(2).spl rename to examples/old/Markus/identity(2).spl diff --git a/examples/Markus/infinite_type_shouldfail.spl b/examples/old/Markus/infinite_type_shouldfail.spl similarity index 100% rename from examples/Markus/infinite_type_shouldfail.spl rename to examples/old/Markus/infinite_type_shouldfail.spl diff --git a/examples/Markus/multiple_recursion.spl b/examples/old/Markus/multiple_recursion.spl similarity index 100% rename from examples/Markus/multiple_recursion.spl rename to examples/old/Markus/multiple_recursion.spl diff --git a/examples/Markus/multiple_recursion_values.spl b/examples/old/Markus/multiple_recursion_values.spl similarity index 100% rename from examples/Markus/multiple_recursion_values.spl rename to examples/old/Markus/multiple_recursion_values.spl diff --git a/examples/Markus/overloading.spl b/examples/old/Markus/overloading.spl similarity index 100% rename from examples/Markus/overloading.spl rename to examples/old/Markus/overloading.spl diff --git a/examples/Markus/polymorphic_value_again_shouldfail.spl b/examples/old/Markus/polymorphic_value_again_shouldfail.spl similarity index 100% rename from examples/Markus/polymorphic_value_again_shouldfail.spl rename to examples/old/Markus/polymorphic_value_again_shouldfail.spl diff --git a/examples/Markus/polymorphic_value_shouldfail.spl b/examples/old/Markus/polymorphic_value_shouldfail.spl similarity index 100% rename from examples/Markus/polymorphic_value_shouldfail.spl rename to examples/old/Markus/polymorphic_value_shouldfail.spl diff --git a/examples/Markus/recursion.spl b/examples/old/Markus/recursion.spl similarity index 100% rename from examples/Markus/recursion.spl rename to examples/old/Markus/recursion.spl diff --git a/examples/Markus/self_application_shouldfail(1).spl b/examples/old/Markus/self_application_shouldfail(1).spl similarity index 100% rename from examples/Markus/self_application_shouldfail(1).spl rename to examples/old/Markus/self_application_shouldfail(1).spl diff --git a/examples/Markus/stress_test.spl b/examples/old/Markus/stress_test.spl similarity index 100% rename from examples/Markus/stress_test.spl rename to examples/old/Markus/stress_test.spl diff --git a/examples/StmtEx.spl b/examples/old/StmtEx.spl similarity index 100% rename from examples/StmtEx.spl rename to examples/old/StmtEx.spl diff --git a/examples/codeGen.spl b/examples/old/codeGen.spl similarity index 100% rename from examples/codeGen.spl rename to examples/old/codeGen.spl diff --git a/examples/old/demo.spl b/examples/old/demo.spl new file mode 100644 index 0000000..49fd463 --- /dev/null +++ b/examples/old/demo.spl @@ -0,0 +1,43 @@ +//Classical higher order functions:) +map(f, l){ + if(isEmpty(l)){ + return []; + } else { + return f(l.hd) : map(f, l.tl); + } +} + +foldr(f, acc, l){ + if(isEmpty(l)){ + return acc; + } else { + return foldr(f, f(acc, l.hd), l.tl); + } +} + +//Functions for operators +plus(x, y){ return x + y; } +times(x, y){ return x * y; } + +intList(x){ + [Int] l = []; + Int a = 1; + while(a <= x){ + l = a : l; + a = a + 1; + } + return l; +} + +faculty(x){ + return foldr(times, 1, intList(x)); +} + +sum(x){ + return foldr(plus, 0, intList(x)); +} + +main(){ + print("faculty of 5 is: ", faculty(5)); + print("sum of 1..5 is: ", sum(50)); +} diff --git a/examples/example.spl b/examples/old/example.spl similarity index 100% rename from examples/example.spl rename to examples/old/example.spl diff --git a/examples/high.spl b/examples/old/high.spl similarity index 100% rename from examples/high.spl rename to examples/old/high.spl diff --git a/examples/readInt.spl b/examples/old/readInt.spl similarity index 100% rename from examples/readInt.spl rename to examples/old/readInt.spl diff --git a/examples/readWrite.spl b/examples/old/readWrite.spl similarity index 100% rename from examples/readWrite.spl rename to examples/old/readWrite.spl diff --git a/examples/tempTest.spl b/examples/old/tempTest.spl similarity index 100% rename from examples/tempTest.spl rename to examples/old/tempTest.spl diff --git a/examples/test.spl b/examples/old/test.spl similarity index 66% rename from examples/test.spl rename to examples/old/test.spl index f2b86b8..5f2ac69 100644 --- a/examples/test.spl +++ b/examples/old/test.spl @@ -22,9 +22,22 @@ times(x, y){ return x * y; } +intList(x){ + [Int] l = []; + Int a = 1; + while(a <= x){ + l = a : l; + a = a + 1; + } + return l; +} + +faculty(x){ + return foldr(times, 1, intList(x)); +} + main(){ [Int] l1 = 1 : 2 : 3 : 4 : 5 : []; - foldr(times, 1, l1); print("faculty of 5 is: "); - print(foldr(times, 1, l1)); + print(faculty(5)); } diff --git a/examples/varEx.spl b/examples/old/varEx.spl similarity index 100% rename from examples/varEx.spl rename to examples/old/varEx.spl diff --git a/examples/peano.spl b/examples/peano.spl new file mode 100644 index 0000000..73916b0 --- /dev/null +++ b/examples/peano.spl @@ -0,0 +1,20 @@ +hyper(n, a, b){ + if(n == 0){ return b + 1; + } else { if(b == 0 && n == 1){ return a; + } else { if(b == 0 && n == 2){ return 0; + } else { if(b == 0 && n >= 3){ return 1; + } else { return hyper(n-1, a, hyper(n, a, b - 1)); + }}}} +} + +main(){ + var plus = hyper(2); + var times = hyper(3); + var power = hyper(4); + var arrow = hyper(5); + + print("2 + 4 = ", plus(2, 4)); + print("2 * 4 = ", times(2, 4)); + print("2 ^ 4 = ", power(2, 4)); + print("3 | 2 = ", arrow(3, 2)); +} diff --git a/parse.icl b/parse.icl index 30a13a9..0a88f4e 100644 --- a/parse.icl +++ b/parse.icl @@ -48,13 +48,16 @@ parseFunDecl = liftM6 FunDecl (parseBBraces $ parseSepList CommaToken parseIdent) (optional (satTok DoubleColonToken *> parseFunType)) (satTok CBraceOpenToken *> many parseVarDecl) - (many parseStmt <* satTok CBraceCloseToken) + (flatten <$> (many parseStmt <* satTok CBraceCloseToken)) -parseStmt :: Parser Token Stmt -parseStmt = parseIfStmt <|> parseWhileStmt <|> +parseStmt :: Parser Token [Stmt] +parseStmt = (parseIfStmt <|> parseWhileStmt <|> parseSColon parseAssStmt <|> parseSColon parseReturnStmt <|> (parseSColon parseFunCall - >>= \(ident, args, fs)->pure $ FunStmt ident args fs) + >>= \(ident, args, fs)->pure $ FunStmt ident args fs)) + >>= \stmt->case stmt of + FunStmt "print" args fs = pure $ map (\a->FunStmt "print" [a] []) args + s = pure [s] where parseSColon :: (Parser Token a) -> Parser Token a parseSColon p = p <* satTok SColonToken @@ -79,10 +82,10 @@ parseStmt = parseIfStmt <|> parseWhileStmt <|> (parseBBraces parseExpr) <*> (parseBlock <|> parseOneLine)) parseBlock :: Parser Token [Stmt] - parseBlock = parseBCBraces (many parseStmt) + parseBlock = parseBCBraces (flatten <$> many parseStmt) parseOneLine :: Parser Token [Stmt] - parseOneLine = pure <$> parseStmt + parseOneLine = parseStmt parseFunType :: Parser Token Type parseFunType = parseFT >>= \t -> case t of