demo
authorMart Lubbers <mart@martlubbers.net>
Thu, 26 May 2016 16:40:12 +0000 (18:40 +0200)
committerMart Lubbers <mart@martlubbers.net>
Thu, 26 May 2016 16:40:12 +0000 (18:40 +0200)
25 files changed:
examples/higher.spl [new file with mode: 0644]
examples/old/Markus/assignment_to_builtin.spl [moved from examples/Markus/assignment_to_builtin.spl with 100% similarity]
examples/old/Markus/higher_order_functions.spl [moved from examples/Markus/higher_order_functions.spl with 100% similarity]
examples/old/Markus/identity(2).spl [moved from examples/Markus/identity(2).spl with 100% similarity]
examples/old/Markus/infinite_type_shouldfail.spl [moved from examples/Markus/infinite_type_shouldfail.spl with 100% similarity]
examples/old/Markus/multiple_recursion.spl [moved from examples/Markus/multiple_recursion.spl with 100% similarity]
examples/old/Markus/multiple_recursion_values.spl [moved from examples/Markus/multiple_recursion_values.spl with 100% similarity]
examples/old/Markus/overloading.spl [moved from examples/Markus/overloading.spl with 100% similarity]
examples/old/Markus/polymorphic_value_again_shouldfail.spl [moved from examples/Markus/polymorphic_value_again_shouldfail.spl with 100% similarity]
examples/old/Markus/polymorphic_value_shouldfail.spl [moved from examples/Markus/polymorphic_value_shouldfail.spl with 100% similarity]
examples/old/Markus/recursion.spl [moved from examples/Markus/recursion.spl with 100% similarity]
examples/old/Markus/self_application_shouldfail(1).spl [moved from examples/Markus/self_application_shouldfail(1).spl with 100% similarity]
examples/old/Markus/stress_test.spl [moved from examples/Markus/stress_test.spl with 100% similarity]
examples/old/StmtEx.spl [moved from examples/StmtEx.spl with 100% similarity]
examples/old/codeGen.spl [moved from examples/codeGen.spl with 100% similarity]
examples/old/demo.spl [new file with mode: 0644]
examples/old/example.spl [moved from examples/example.spl with 100% similarity]
examples/old/high.spl [moved from examples/high.spl with 100% similarity]
examples/old/readInt.spl [moved from examples/readInt.spl with 100% similarity]
examples/old/readWrite.spl [moved from examples/readWrite.spl with 100% similarity]
examples/old/tempTest.spl [moved from examples/tempTest.spl with 100% similarity]
examples/old/test.spl [moved from examples/test.spl with 66% similarity]
examples/old/varEx.spl [moved from examples/varEx.spl with 100% similarity]
examples/peano.spl [new file with mode: 0644]
parse.icl

diff --git a/examples/higher.spl b/examples/higher.spl
new file mode 100644 (file)
index 0000000..66f9024
--- /dev/null
@@ -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)));
+}
similarity index 100%
rename from examples/StmtEx.spl
rename to examples/old/StmtEx.spl
diff --git a/examples/old/demo.spl b/examples/old/demo.spl
new file mode 100644 (file)
index 0000000..49fd463
--- /dev/null
@@ -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));
+}
similarity index 100%
rename from examples/high.spl
rename to examples/old/high.spl
similarity index 66%
rename from examples/test.spl
rename to examples/old/test.spl
index f2b86b8..5f2ac69 100644 (file)
@@ -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));
 }
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 (file)
index 0000000..73916b0
--- /dev/null
@@ -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));
+}
index 30a13a9..0a88f4e 100644 (file)
--- 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