From: Mart Lubbers Date: Fri, 20 May 2016 14:20:41 +0000 (+0200) Subject: add funtype X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=b86517f1b390cf8fd13611aef6e65b472ff7e813;p=cc1516.git add funtype --- diff --git a/AST.dcl b/AST.dcl index ad3196f..4a5a6cb 100644 --- a/AST.dcl +++ b/AST.dcl @@ -15,6 +15,7 @@ from StdOverloaded import class toString, class ==, class zero, class < | BoolType | CharType | VoidType + | FuncType Type | (->>) infixl 7 Type Type :: Expr = VarExpr Pos VarDef diff --git a/AST.icl b/AST.icl index 24d2af1..6436619 100644 --- a/AST.icl +++ b/AST.icl @@ -65,6 +65,7 @@ instance print Type where print BoolType = print "Bool" print CharType = print "Char" print VoidType = print "Void" + print (FuncType t) = ["(-> ":print t] ++ [")"] print (t1 ->> t2) = ["(":print t1 ++ [" -> ":print t2]] ++ [")"] instance print String where diff --git a/gen.icl b/gen.icl index bf3d7c4..b4b9992 100644 --- a/gen.icl +++ b/gen.icl @@ -33,18 +33,18 @@ 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 "1printint" (LAB "1printint") - $ extend "1printchar" (LAB "1printchar") - $ extend "1readchar" (LAB "1readchar") - $ extend "1readint" (LAB "1readint") - $ extend "isEmpty" (LAB "isempty") +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 @@ -169,7 +169,7 @@ instance g Expr where 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 @@ -195,7 +195,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 @@ -220,7 +220,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] >>|