curry gotcha
[cc1516.git] / gen.icl
diff --git a/gen.icl b/gen.icl
index 9f515c9..87e4f8e 100644 (file)
--- a/gen.icl
+++ b/gen.icl
@@ -187,16 +187,19 @@ 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
-               (       tell
+               (       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] ""]
-                       >>| mapM_ g es
+                               ,Instr "ajs" [Lit -1] ""
+                               ]
                        >>| if (isEmpty es) (pure ()) (tell 
                                [Instr "stmh" [Lit $ length es] "Store arguments"
-                               ,Instr "ajs" [Lit -1] ""]))
+                               ,Instr "ajs" [Lit -1] ""
+                               ,Instr "ldr" [Raw "R7"] ""]))
                //Function is complete
                (       mapM_ g es
                        >>| getAdressbook >>= \ab->(case 'Map'.get k ab of
@@ -331,7 +334,11 @@ 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] ""]
+            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" [] ""]
@@ -339,6 +346,23 @@ instance g Stmt where
                >>| 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