fixed printing:)
[cc1516.git] / gen.icl
diff --git a/gen.icl b/gen.icl
index 082c34d..6020d03 100644 (file)
--- a/gen.icl
+++ b/gen.icl
@@ -42,7 +42,7 @@ labelStream = ["lbl_" +++ toString i\\i<-[1..]]
 defaultAddressBook :: Addressbook 
 defaultAddressBook = extend "1printint" (LAB "1printint" 1)
        $ extend "1printchar" (LAB "1printchar" 1)
-       $ extend "1readchar" (LAB "1readchar" 0)
+       $ extend "read" (LAB "read" 0)
        $ extend "1readint" (LAB "1readint" 0)
        $ extend "isEmpty" (LAB "isempty" 1)
        'Map'.newMap
@@ -77,7 +77,7 @@ programContext =    [Lab "1printint"
                     ,Instr "str" [Raw "RR"] ""
                     ,Instr "unlink" [] ""
                     ,Instr "ret" [] ""
-                    ,Lab "1readchar"
+                    ,Lab "read"
                     ,Instr "link" [Lit 0] ""
                                        ,Instr "trap" [Lit 11] "read char"
                     ,Instr "str" [Raw "RR"] ""
@@ -92,6 +92,7 @@ programContext =    [Lab "1printint"
                     ,Instr "str" [Raw "RR"] ""
                     ,Instr "unlink" [] ""
                     ,Instr "ret" [] ""
+                    ,Lab "read"
                     ]
 
 //helper functions for the gen monad
@@ -160,11 +161,14 @@ instance g Expr where
     g (VarExpr _ (VarDef k fs)) = getAdressbook >>= \ab->case 'Map'.get k ab of
                Just (ADDR t) = tell [Instr "ldl" [Lit t] ""] >>| mapM_ g fs >>| pure ()
                _ = liftT (Left $ Error "Higher order functions not implemented")
-    g (FunExpr _ k es fs) = 
-               mapM_ g es
-               >>| jump "bsr" k
-        >>| mapM_ (const $ tell [Instr "ajs" [Lit -1] ""]) es //clean up args
-               >>| tell [Instr "ldr" [Raw "RR"] ""]
+    g (FunExpr _ k es fs) = getAdressbook >>= \ab->case 'Map'.get k ab of
+               Just (LAB l arity) = if (arity <> (length es))
+                       (liftT $ Left $ Error "Higher order functions not implemented")
+                       (       mapM_ g es
+                               >>| jump "bsr" k
+                       >>| mapM_ (const $ tell [Instr "ajs" [Lit -1] ""]) es //clean up args
+                               >>| tell [Instr "ldr" [Raw "RR"] ""])
+               _ = liftT (Left $ Error "Funcall to variable?")
 
 jump :: String String -> Gen ()
 jump instr k = getAdressbook >>= \ab->case 'Map'.get k ab of
@@ -229,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 ()