From 555e682444519f46b33ad9b3455df891c53d47c6 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Mon, 23 May 2016 19:29:31 +0200 Subject: [PATCH] fixed printing:) --- examples/readInt.spl | 41 +++++++++++++++++++++++++++++++++++++++++ gen.icl | 2 ++ spl.icl | 6 +++--- 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 examples/readInt.spl diff --git a/examples/readInt.spl b/examples/readInt.spl new file mode 100644 index 0000000..240bc01 --- /dev/null +++ b/examples/readInt.spl @@ -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 --- 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 --- 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 -- 2.20.1