fix escapes in literal strings
[cc1516.git] / gen.icl
diff --git a/gen.icl b/gen.icl
index 76085ab..53a1de7 100644 (file)
--- a/gen.icl
+++ b/gen.icl
@@ -33,17 +33,19 @@ FALSE :== 0
 :: SSMProgram :== [Instr]
 :: GenError = Error String
 :: Addressbook :== 'Map'.Map String Address
-:: Address = LAB String | ADDR Int
+:: Address = LAB String Int | ADDR Int
 :: Gen a :== RWST () SSMProgram (Addressbook, [Label]) (Either GenError) a
 
 labelStream :: [Label]
 labelStream = ["lbl_" +++ toString i\\i<-[1..]]
 
 defaultAddressBook :: Addressbook 
-defaultAddressBook = extend "print" (LAB "print")
-                        $ extend "read" (LAB "read")
-                        $ extend "isEmpty" (LAB "isEmpty")
-                        'Map'.newMap
+defaultAddressBook = extend "1printint" (LAB "1printint" 1)
+       $ extend "1printchar" (LAB "1printchar" 1)
+       $ extend "1readchar" (LAB "1readchar" 0)
+       $ extend "1readint" (LAB "1readint" 0)
+       $ extend "isEmpty" (LAB "isempty" 1)
+       'Map'.newMap
 
 gen :: AST -> Either String String
 gen (AST fds) = case evalRWST prog () (defaultAddressBook, labelStream) of
@@ -57,18 +59,31 @@ gen (AST fds) = case evalRWST prog () (defaultAddressBook, labelStream) of
             >>| mapM_ g fds
 
 programContext :: SSMProgram
-programContext =    [Lab "print" //there is no actual IO in SSM
+programContext =    [Lab "1printint"
                     ,Instr "link" [Lit 0] ""
+                    ,Instr "ldl" [Lit -2] "load first argument"
+                                       ,Instr "trap" [Lit 0] "print int"
                     ,Instr "unlink" [] ""
                     ,Instr "ret" [] ""
-                    ,Lab "read" //there is no actual IO in SSM
+                                       ,Lab "1printchar"
                     ,Instr "link" [Lit 0] ""
-                    ,Instr "ldc" [Lit 0] ""
-                    ,Instr "sth" [] ""
+                    ,Instr "ldl" [Lit -2] "load first argument"
+                                       ,Instr "trap" [Lit 1] "print char"
+                    ,Instr "unlink" [] ""
+                    ,Instr "ret" [] ""
+                    ,Lab "1readint"
+                    ,Instr "link" [Lit 0] ""
+                                       ,Instr "trap" [Lit 10] "read int"
                     ,Instr "str" [Raw "RR"] ""
                     ,Instr "unlink" [] ""
                     ,Instr "ret" [] ""
-                    ,Lab "isEmpty"
+                    ,Lab "1readchar"
+                    ,Instr "link" [Lit 0] ""
+                                       ,Instr "trap" [Lit 11] "read char"
+                    ,Instr "str" [Raw "RR"] ""
+                    ,Instr "unlink" [] ""
+                    ,Instr "ret" [] ""
+                    ,Lab "isempty"
                     ,Instr "link" [Lit 0] ""
                     ,Instr "ldl" [Lit -2] "load prt to list"
                     ,Instr "lda" [Lit 0] "derefrence ptr"
@@ -144,17 +159,20 @@ instance g Expr where
                >>| tell [Instr "ajs" [Lit -1] ""]
     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 "PANIC: variable and function name clash")
-    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"] ""]
+               _ = liftT (Left $ Error "Higher order functions not implemented")
+    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
        Nothing = liftT (Left $ Error $ concat ["PANIC: ", k, " not found as function"])
-       Just (LAB t) = tell [Instr instr [L t] (k +++"()")]
+       Just (LAB t _) = tell [Instr instr [L t] (k +++"()")]
        Just (ADDR t) = liftT (Left $ Error $ "PANIC: jump should go to label")
 
 instance g Stmt where
@@ -180,7 +198,7 @@ instance g Stmt where
     g (AssStmt (VarDef k fs) e) = 
         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 (LAB t _) = liftT (Left $ Error $ "PANIC: cannot assign to function")
             Just (ADDR t) = tell [Instr "stl" [Lit t] ""]
     g (FunStmt k es fs) = mapM_ g es 
         >>| jump "bsr" k
@@ -205,7 +223,7 @@ addVars [x:xs] = \ab->extend x (ADDR (-2 - (length xs))) (addVars xs ab)
 instance g FunDecl where
     g (FunDecl _ k args _ vds stms) = 
         //varDecls can call the enclosing function, so first reserve a label for it 
-        updateAdressbook (extend k (LAB k)) >>|
+        updateAdressbook (extend k (LAB k (length args))) >>|
         getAdressbook >>= \oldMap ->
                updateAdressbook (addVars args) >>|
         tell [Lab k] >>|