fixed printing:)
authorMart Lubbers <mart@martlubbers.net>
Mon, 23 May 2016 17:29:31 +0000 (19:29 +0200)
committerMart Lubbers <mart@martlubbers.net>
Mon, 23 May 2016 17:29:31 +0000 (19:29 +0200)
examples/readInt.spl [new file with mode: 0644]
gen.icl
spl.icl

diff --git a/examples/readInt.spl b/examples/readInt.spl
new file mode 100644 (file)
index 0000000..240bc01
--- /dev/null
@@ -0,0 +1,41 @@
+toInt(x) :: Char -> Int{
+       if(x == '0'){ return 0;
+       } else { if(x == '1'){ return 1;
+       } else { if(x == '2'){ return 2;
+       } else { if(x == '3'){ return 3;
+       } else { if(x == '4'){ return 4;
+       } else { if(x == '5'){ return 5;
+       } else { if(x == '6'){ return 6;
+       } else { if(x == '7'){ return 7;
+       } else { if(x == '8'){ return 8;
+       } else { if(x == '9'){ return 9;
+       } else { return -1; }}}}}}}}}}
+}
+
+isDigit(x) :: Char -> Bool {
+       return toInt(x) != -1;
+}
+
+strToInt(x) :: [Char] -> Int {
+       [Char] xs = x;
+       var i = 0;
+       var m = 1;
+       if(!isEmpty(x)){
+               if(xs.hd == '-'){
+                       xs = xs.tl;
+                       m = -1;
+               }
+
+               while(!isEmpty(xs)){
+                       i = i*10 + toInt(xs.hd);
+                       xs = xs.tl;
+               }
+       }
+       return i*m;
+}
+
+main(){
+       [Char] num = "-1234";
+       print(strToInt(num));
+       return 0;
+}
diff --git a/gen.icl b/gen.icl
index 5183804..6020d03 100644 (file)
--- a/gen.icl
+++ b/gen.icl
@@ -233,6 +233,8 @@ instance g FunDecl where
         foldM foldVarDecl 1 vds  >>|
         //and the statements
         mapM_ g stms >>|
+               //Ugly hack to always return
+               g (ReturnStmt Nothing) >>|
         updateAdressbook (const oldMap) >>| pure ()
 
 annote :: Int String -> Gen ()
diff --git a/spl.icl b/spl.icl
index 0f1c07a..3e970a4 100644 (file)
--- a/spl.icl
+++ b/spl.icl
@@ -40,15 +40,15 @@ preamble (AST fd) = AST (pre ++ fd)
                pre = [
                        FunDecl zero "1printstr" ["x"] Nothing [] [
                                IfStmt (FunExpr zero "isEmpty" [VarExpr zero (VarDef "x" [])] [])
-                                       []
+                                       [ReturnStmt Nothing]
                                        [FunStmt "1printchar" [VarExpr zero (VarDef "x" [FieldHd])] []
                                        ,FunStmt "1printstr" [VarExpr zero (VarDef "x" [FieldTl])] []]]
                        ,
                        FunDecl zero "1printbool" ["x"] Nothing [] [
                                IfStmt (VarExpr zero (VarDef "x" []))
                                        [FunStmt "1printstr" [makeStrExpr zero $ fromString "True"] []]
-                                       [FunStmt "1printstr" [makeStrExpr zero $ fromString "False"] []]
-                       ]] 
+                                       [FunStmt "1printstr" [makeStrExpr zero $ fromString "False"] []]]
+                       ] 
 
 Start :: *World -> *World
 Start w