curry gotcha
[cc1516.git] / gen.icl
diff --git a/gen.icl b/gen.icl
index f72f7f3..87e4f8e 100644 (file)
--- a/gen.icl
+++ b/gen.icl
@@ -43,9 +43,8 @@ defaultAddressBook :: [FunDecl] -> Addressbook
 defaultAddressBook fd = extend "1printint" (LAB "1printint" 1 0)
        $ extend "1printchar" (LAB "1printchar" 1 1)
        $ extend "read" (LAB "read" 0 2)
-       $ extend "1readint" (LAB "1readint" 0 3)
-       $ extend "isEmpty" (LAB "isempty" 1 4)
-       $ addFuncs fd 5
+       $ extend "isEmpty" (LAB "isEmpty" 1 3)
+       $ addFuncs fd 4
        where
                addFuncs [] _ = 'Map'.newMap
                addFuncs [(FunDecl _ k args _ _ _):xs] n =
@@ -64,8 +63,7 @@ gen (AST fds) = case evalRWST prog () (defaultAddressBook fds, labelStream) of
 
 programContext :: [FunDecl] -> SSMProgram
 programContext x = [Lab "1func"
-                               :fS ["1printint" ,"1printchar"
-                                       ,"read" ,"1readint"
+                               :fS ["1printint" ,"1printchar","read"
                                        ,"isEmpty":map (\(FunDecl _ k _ _ _ _)->k) x] 0] ++ context
        where
 
@@ -76,7 +74,9 @@ programContext x = [Lab "1func"
                        ,Instr "ldr" [Raw "R5"] ""
                        ,Instr "ldc" [Lit n] $ "branch to: " +++ k
                        ,Instr "eq" [] ""
-                       ,Instr "brf" [L $ "1next" +++ (toString $ n + 1)] ""
+                       ,if (isEmpty xs)
+                               (Instr "nop" [] "")
+                               (Instr "brf" [L $ "1next" +++ (toString $ n + 1)] "")
                        ,Instr "bra" [L k] ""
                        :fS xs $ n+1]
                context :: SSMProgram
@@ -98,7 +98,7 @@ programContext x = [Lab "1func"
                     ,Instr "str" [Raw "RR"] ""
                     ,Instr "unlink" [] ""
                     ,Instr "ret" [] ""
-                    ,Lab "isempty"
+                    ,Lab "isEmpty"
                     ,Instr "link" [Lit 0] ""
                     ,Instr "ldl" [Lit -2] "load prt to list"
                     ,Instr "lda" [Lit 0] "derefrence ptr"
@@ -174,133 +174,141 @@ instance g Expr where
                Just (ADDR t arity) = tell [Instr "ldl" [Lit t] ""] >>| mapM_ g fs >>| pure ()
                Just (LAB l _ fn) = tell 
                        [Instr "ldc" [Lit fn] ""
+                       ,Instr "sth" [] ""
                        ,Instr "ldc" [Lit 0] ""
-                       ,Instr "stmh" [Lit 2] ""]
-    g (FunExpr _ k es fs) = getAdressbook >>= \ab->case 'Map'.get k ab of
-               //Identifier points to function
-               Just (LAB l arity fn) = if (arity <> (length es))
-                       //Function is not complete
-                       (       tell
-                                       [Instr "ldc" [Lit fn] "Store function number"
-                                       ,Instr "sth" [] ""
-                                       ,Instr "ldc" [Lit $ length es] "Store arity"
-                                       ,Instr "sth" [] ""
-                                       ,Instr "ajs" [Lit -1] ""]
-                               >>| mapM_ g es
-                               >>| if (isEmpty es) (pure ()) (tell 
-                                       [Instr "stmh" [Lit $ length es] "Store arguments"
-                                       ,Instr "ajs" [Lit -1] ""]))
-                       //Function is complete
-                       (       mapM_ g es
-                               >>| jump "bsr" k
-                       >>| tell 
-                                       [Instr "ajs" [Lit $ ~(length es)] "Clean arguments"
-                                       ,Instr "ldr" [Raw "RR"] ""])
-               //Identifier points to variable, thus higher order function
-               Just (ADDR t arity) = if (arity <> (length es))
-                       //Function is still  not complete
-                       ( fresh >>= \finish->fresh >>= \start->tell [
-                               //Store function number
-                                Instr "ldl" [Lit t] "STARTING HIGHER ORDER UPDATE"
-                               ,Instr "ldh" [Lit 0] "get function number"
-                               ,Instr "sth" [] "Store"
-                               //Store function arity
-                               ,Instr "ldl" [Lit t] "get pointer again"
-                               ,Instr "ldh" [Lit 1] "get function arity"
-                               ,Instr "ldc" [Lit $ length es] "add argument number"
-                               ,Instr "add" [] "add"
-                               ,Instr "sth" [] "Store"
-                               ,Instr "ajs" [Lit -1] "Adjust pointer"
-                               //load the arguments
-                               ,Instr "ldl" [Lit t] ""
-                               ,Instr "ldh" [Lit 1] "Load available arguments"
-                               ,Instr "str" [Raw "R5"] "Store available args in register"
-                               ,Instr "ldc" [Lit 0] "Store offset"
-                               ,Instr "str" [Raw "R6"] "Store offset in register"
-                               ,Lab start
-                               ,Instr "ldr" [Raw "R5"] ""
-                               ,Instr "ldc" [Lit 0] ""
-                               ,Instr "eq" [] ""
-                               ,Instr "brt" [L finish] "Done pushing arg, bye"
-                               //Load heapadress
-                               ,Instr "ldl" [Lit t] ""
-                               ,Instr "ldr" [Raw "R6"] ""
-                               ,Instr "add" [] "Corrected heapaddress"
-                               ,Instr "ldh" [Lit 2] "Load argument"
-                               ,Instr "sth" [] "And store it immediatly after"
-                               //Decrease available arguments
-                               ,Instr "ldr" [Raw "R5"] ""
-                               ,Instr "ldc" [Lit 1] ""
-                               ,Instr "sub" [] ""
-                               ,Instr "str" [Raw "R5"] ""
-                               //Increase available arguments
-                               ,Instr "ldr" [Raw "R6"] ""
-                               ,Instr "ldc" [Lit 1] ""
-                               ,Instr "add" [] ""
-                               ,Instr "str" [Raw "R6"] ""
-                               ,Instr "bra" [L start] ""
-                               ,Lab finish
-                               ]
-                         >>| mapM_ g es
-                         >>| tell 
-                                       [Instr "stmh" [Lit $ length es] "Store extra args"
-                                       ,Instr "ajs" [Lit -1] ""]
-                       )
-                       //Function is complete
-                       ( fresh >>= \finish->fresh >>= \start->tell [
-                                Instr "ldl" [Lit t] "STARTING HIGHER ORDER CALL"
-                               ,Instr "ldh" [Lit 1] "Load available arguments"
-                               ,Instr "str" [Raw "R5"] "Store available args in register"
-                               ,Instr "ldc" [Lit 0] "Store offset"
-                               ,Instr "str" [Raw "R6"] "Store offset in register"
-                       
-                               ,Lab start
-                               ,Instr "ldr" [Raw "R5"] ""
-                               ,Instr "ldc" [Lit 0] ""
-                               ,Instr "eq" [] ""
-                               ,Instr "brt" [L finish] "Done pushing arg, bye"
-                               //Load heapadress
-                               ,Instr "ldl" [Lit t] ""
-                               ,Instr "ldr" [Raw "R6"] ""
-                               ,Instr "add" [] "Corrected heapaddress"
-                               ,Instr "ldh" [Lit 2] "Load argument"
-                               //Decrease available arguments
-                               ,Instr "ldr" [Raw "R5"] ""
-                               ,Instr "ldc" [Lit 1] ""
-                               ,Instr "sub" [] ""
-                               ,Instr "str" [Raw "R5"] ""
-                               //Increase available arguments
-                               ,Instr "ldr" [Raw "R6"] ""
-                               ,Instr "ldc" [Lit 1] ""
-                               ,Instr "add" [] ""
-                               ,Instr "str" [Raw "R6"] ""
-                               ,Instr "bra" [L start] ""
-                               ,Lab finish
-                               ]
-                         >>| mapM_ g es
-                         >>| tell
-                               [Instr "ldl" [Lit t] ""
-                               ,Instr "ldh" [Lit 0] "Get function number"
-                               ,Instr "str" [Raw "R5"] ""
-                               ,Instr "bsr" [L "1func"] ""
-                               ,Instr "ldr" [Raw "RR"] ""
-       //                      ,Instr "ldl" [Lit t] ""
-       //                      ,Instr "ldh" [Lit $ 1] ""
-       //                      ,Instr "neg" [] ""
-       //                      ,Instr "ldr" [Raw "SP"] ""
-       //                      ,Instr "add" [] ""
-       //                      ,Instr "ldc" [Lit arity] ""
-       //                      ,Instr "sub" [] ""
-       //                      ,Instr "str" [Raw "SP"] ""
+                       ,Instr "sth" [] ""
+                       ,Instr "ajs" [Lit -1] ""]
+               Nothing = liftT $ Left $ Error "PANIC: unresolver variable expr"
+    g (LambdaExpr _ _ _) = liftT $ Left $ Error "PANIC: Lambdas should be unfolded"
+    g (FunExpr _ k es fs) = funnyStuff k es fs
+
+funnyStuff :: String [Expr] [FieldSelector] -> Gen ()
+funnyStuff k es fs = getAdressbook >>= \ab->case 'Map'.get k ab of
+       //Identifier points to function
+       Just (LAB l arity fn) = if (arity <> (length es))
+               //Function is not complete
+               (       mapM_ g es
+                       >>| tell
+                               [Instr "ldc" [Lit fn] "Store function number"
+                               ,Instr "sth" [] ""
+                               ,Instr "str" [Raw "R7"] ""
+                               ,Instr "ldc" [Lit $ length es] "Store arity"
+                               ,Instr "sth" [] ""
+                               ,Instr "ajs" [Lit -1] ""
                                ]
+                       >>| if (isEmpty es) (pure ()) (tell 
+                               [Instr "stmh" [Lit $ length es] "Store arguments"
+                               ,Instr "ajs" [Lit -1] ""
+                               ,Instr "ldr" [Raw "R7"] ""]))
+               //Function is complete
+               (       mapM_ g es
+                       >>| getAdressbook >>= \ab->(case 'Map'.get k ab of
+                               Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as function"])
+                               Just (LAB t _ _) = tell [Instr "bsr" [L t] (k +++"()")]
+                               Just (ADDR t arity) = liftT (Left $ Error "NO ADDRESS JUMPING FFS")
                        )
-               Nothing = liftT (Left $ Error "Undefined function!!!")
-
-jump :: String String -> Gen ()
-jump instr k = getAdressbook >>= \ab->case 'Map'.get k ab of
-       Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as function"])
-       Just (LAB t _ _) = tell [Instr instr [L t] (k +++"()")]
-       Just (ADDR t arity) = abort "NO ADDRESS JUMPING FFS"
+               >>| tell 
+                               [Instr "ajs" [Lit $ ~(length es)] "Clean arguments"
+                               ,Instr "ldr" [Raw "RR"] ""])
+       //Identifier points to variable, thus higher order function
+       Just (ADDR t arity) = if (arity <> (length es))
+               //Function is still  not complete
+               ( fresh >>= \finish->fresh >>= \start->tell [
+                       //Store function number
+                        Instr "ldl" [Lit t] "STARTING HIGHER ORDER UPDATE"
+                       ,Instr "ldh" [Lit 0] "get function number"
+                       ,Instr "sth" [] "Store"
+                       //Store function arity
+                       ,Instr "ldl" [Lit t] "get pointer again"
+                       ,Instr "ldh" [Lit 1] "get function arity"
+                       ,Instr "ldc" [Lit $ length es] "add argument number"
+                       ,Instr "add" [] "add"
+                       ,Instr "sth" [] "Store"
+                       ,Instr "ajs" [Lit -1] "Adjust pointer"
+                       //load the arguments
+                       ,Instr "ldl" [Lit t] ""
+                       ,Instr "ldh" [Lit 1] "Load available arguments"
+                       ,Instr "str" [Raw "R5"] "Store available args in register"
+                       ,Instr "ldc" [Lit 0] "Store offset"
+                       ,Instr "str" [Raw "R6"] "Store offset in register"
+                       ,Lab start
+                       ,Instr "ldr" [Raw "R5"] ""
+                       ,Instr "ldc" [Lit 0] ""
+                       ,Instr "eq" [] ""
+                       ,Instr "brt" [L finish] "Done pushing arg, bye"
+                       //Load heapadress
+                       ,Instr "ldl" [Lit t] ""
+                       ,Instr "ldr" [Raw "R6"] ""
+                       ,Instr "add" [] "Corrected heapaddress"
+                       ,Instr "ldh" [Lit 2] "Load argument"
+                       ,Instr "sth" [] "And store it immediatly after"
+                       //Decrease available arguments
+                       ,Instr "ldr" [Raw "R5"] ""
+                       ,Instr "ldc" [Lit 1] ""
+                       ,Instr "sub" [] ""
+                       ,Instr "str" [Raw "R5"] ""
+                       //Increase available arguments
+                       ,Instr "ldr" [Raw "R6"] ""
+                       ,Instr "ldc" [Lit 1] ""
+                       ,Instr "add" [] ""
+                       ,Instr "str" [Raw "R6"] ""
+                       ,Instr "bra" [L start] ""
+                       ,Lab finish
+                       ]
+                 >>| mapM_ g es
+                 >>| tell 
+                               [Instr "stmh" [Lit $ length es] "Store extra args"
+                               ,Instr "ajs" [Lit -1] ""]
+               )
+               //Function is complete
+               ( fresh >>= \finish->fresh >>= \start->tell [
+                        Instr "ldl" [Lit t] "STARTING HIGHER ORDER CALL"
+                       ,Instr "ldh" [Lit 1] "Load available arguments"
+                       ,Instr "str" [Raw "R5"] "Store available args in register"
+                       ,Instr "ldc" [Lit 0] "Store offset"
+                       ,Instr "str" [Raw "R6"] "Store offset in register"
+               
+                       ,Lab start
+                       ,Instr "ldr" [Raw "R5"] ""
+                       ,Instr "ldc" [Lit 0] ""
+                       ,Instr "eq" [] ""
+                       ,Instr "brt" [L finish] "Done pushing arg, bye"
+                       //Load heapadress
+                       ,Instr "ldl" [Lit t] ""
+                       ,Instr "ldr" [Raw "R6"] ""
+                       ,Instr "add" [] "Corrected heapaddress"
+                       ,Instr "ldh" [Lit 2] "Load argument"
+                       //Decrease available arguments
+                       ,Instr "ldr" [Raw "R5"] ""
+                       ,Instr "ldc" [Lit 1] ""
+                       ,Instr "sub" [] ""
+                       ,Instr "str" [Raw "R5"] ""
+                       //Increase available arguments
+                       ,Instr "ldr" [Raw "R6"] ""
+                       ,Instr "ldc" [Lit 1] ""
+                       ,Instr "add" [] ""
+                       ,Instr "str" [Raw "R6"] ""
+                       ,Instr "bra" [L start] ""
+                       ,Lab finish
+                       ]
+                 >>| mapM_ g es
+                 >>| tell
+                       [Instr "ldl" [Lit t] ""
+                       ,Instr "ldh" [Lit 0] "Get function number"
+                       ,Instr "str" [Raw "R5"] ""
+                       ,Instr "bsr" [L "1func"] "HIGHER ORDER END"
+                       ,Instr "ldl" [Lit t] ""
+                       ,Instr "ldh" [Lit 1] ""
+                       ,Instr "neg" [] ""
+                       ,Instr "ldr" [Raw "SP"] ""
+                       ,Instr "add" [] ""
+                       ,Instr "ldc" [Lit $ length es + 1] ""
+                       ,Instr "sub" [] ""
+                       ,Instr "str" [Raw "SP"] ""
+                       ,Instr "ldr" [Raw "RR"] ""
+                       ]
+               )
+       Nothing = liftT (Left $ Error $ "PANIC: Undefined function: " +++ k)
 
 instance g Stmt where
     g (IfStmt cond th el) = 
@@ -326,18 +334,35 @@ instance g Stmt where
         g e >>| getAdressbook >>= \ab->case 'Map'.get k ab of
             Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as var"])
             Just (LAB t _ _) = liftT (Left $ Error $ "PANIC: cannot assign to function")
-            Just (ADDR t ar) = tell [Instr "stl" [Lit t] ""]
-    g (FunStmt k es fs) = mapM_ g es 
-        >>| jump "bsr" k
-        >>| tell [Instr "ajs" [Lit (~(length es))] ""] //clean up args
-        >>| mapM_ g fs
-               >>| pure ()
+            Just (ADDR t ar) = case fs of 
+                []  = tell [Instr "stl" [Lit t] ""]
+                _   = tell [Instr "ldl" [Lit t] ""]
+                        >>| followFs fs
+                        >>| tell [Instr "sta" [Lit 0] ""]
+    g (FunStmt k es fs) = funnyStuff k es fs
     g (ReturnStmt Nothing) = tell [Instr "unlink" [] ""]
                >>| tell [Instr "ret" [] ""]
     g (ReturnStmt (Just e)) = g e
                >>| tell [Instr "str" [Raw "RR"] ""] 
                >>| g (ReturnStmt Nothing)
 
+//expects the heap address to the var to slect on to be on the stack
+//and leaves the heap adress to write to on the stack
+followFs :: [FieldSelector] -> Gen()
+followFs [] = tell []
+followFs [FieldHd:fs]   = tell [Instr "ldc" [Lit 1] "select hd"
+                             ,Instr "sub" [] "select hd"]
+                            >>| followFs fs
+followFs [FieldTl]      = tell []
+followFs [FieldTl:fs]   = tell [Instr "lda" [Lit 0] ""]
+                            >>| followFs fs
+followFs [FieldFst:fs]  = tell [Instr "ldc" [Lit 1] "select fst"
+                                ,Instr "sub" [] "select fst"]
+                            >>| followFs fs
+followFs [FieldSnd]     = tell []
+followFs [FieldSnd:fs]  = tell [Instr "lda" [Lit 0] ""]
+                            >>| followFs fs
+
 foldVarDecl :: Int VarDecl -> Gen Int
 foldVarDecl x (VarDecl _ mt k e) = g e 
     >>| annote x k