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
defaultGamma = extend "print" (Forall ["a"] ((IdType "a") ->> VoidType))
$ extend "isEmpty" (Forall ["a"] ((ListType (IdType "a")) ->> BoolType))
$ extend "read" (Forall [] (FuncType CharType))
+ $ extend "1printchar" (Forall [] (CharType ->> VoidType))
+ $ extend "1printint" (Forall [] (IntType ->> VoidType))
+ $ extend "1printbool" (Forall [] (BoolType ->> VoidType))
zero
sem :: AST -> Either [SemError] AST
preamble :: AST -> AST
preamble (AST fd) = AST (pre ++ fd)
where
- pre = [] //[
- //FunDecl zero "1printstr" ["x"] Nothing [] [
-// IfStmt (FunExpr zero "isEmpty" [VarExpr zero (VarDef "x" [])] [])
-// []
-// [FunStmt "1printchar" [VarExpr zero (VarDef "x" [FieldHd])] []
-// ,FunStmt "1printstr" [VarExpr zero (VarDef "x" [FieldTl])] []]]]
+ pre = [
+ FunDecl zero "1printstr" ["x"] Nothing [] [
+ IfStmt (FunExpr zero "isEmpty" [VarExpr zero (VarDef "x" [])] [])
+ []
+ [FunStmt "1printchar" [VarExpr zero (VarDef "x" [FieldHd])] []
+ ,FunStmt "1printstr" [VarExpr zero (VarDef "x" [FieldTl])] []]]
+ ,
+ FunDecl zero "1printbool" ["x"] Nothing [] [
+ IfStmt (VarExpr zero (VarDef "x" []))
+ [FunStmt "1printstr" [strOp2 $ fromString "True"] []]
+ [FunStmt "1printstr" [strOp2 $ fromString "False"] []]
+ ]]
+
+ strOp2 :: [Char] -> Expr
+ strOp2 [] = EmptyListExpr zero
+ strOp2 [x:xs] = Op2Expr zero (CharExpr zero x) BiCons (strOp2 xs)
Start :: *World -> *World
Start w