From: pimjager Date: Thu, 19 May 2016 14:51:38 +0000 (+0200) Subject: FunStmt and Stmts added X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=8472f6d9507fd0116ac85d02e8ed5566ef8e7980;p=cc1516.git FunStmt and Stmts added --- diff --git a/examples/codeGen.spl b/examples/codeGen.spl index 5b366ba..30fe668 100644 --- a/examples/codeGen.spl +++ b/examples/codeGen.spl @@ -3,14 +3,8 @@ f(x, y) { } main() { -// Int x1 = 3; -// Char x2 = '\n'; -// Bool x3 = True; -// (Int, Char) x4 = (4, '\n'); -// [Bool] x5 = True : False : True : True : []; -// [Int] x1 = 42 : []; Int x1 = 1; - Int x2 = f(x1, 2); + x1 = 5; f(x1, 2); return x1; } diff --git a/gen.icl b/gen.icl index 7ad9176..a00275d 100644 --- a/gen.icl +++ b/gen.icl @@ -121,7 +121,6 @@ instance g Expr where Just (LAB t) = liftT (Left $ Error "PANIC: variable and function name clash") //load k >>= \instr-> tell [instr] //note: pure is pure for list, i.e. [] g (FunExpr _ k es fs) = -// tell [Instr "ldr" [Raw "MP"] ("old frame pointer")] mapM g es >>| jump "bsr" k >>| tell [Instr "ldr" [Raw "RR"] ""] @@ -133,30 +132,31 @@ jump instr k = getAdressbook >>= \ab->case 'Map'.get k ab of Just (ADDR t) = liftT (Left $ Error $ "PANIC: jump should go to label") instance g Stmt where -// g (IfStmt cond th el) = -// fresh >>= \elseLabel-> -// fresh >>= \endLabel-> -// g cond >>| -// tell [Instr "brf" [L elseLabel] "branch else"] >>| -// mapM_ g th >>| -// tell [Instr "bra" [L endLabel] "branch end if"] >>| -// tell [Lab elseLabel] >>| -// mapM_ g el >>| -// tell [Lab endLabel] -// g (WhileStmt cond th) = -// fresh >>= \startLabel-> -// fresh >>= \endLabel -> -// tell [Lab startLabel] >>| -// g cond >>| -// tell [Instr "brf" [L endLabel] "branch end while"] >>| -// mapM_ g th >>| -// tell [Instr "bra" [L startLabel] "branch start while"] >>| -// tell [Lab endLabel] -// g (AssStmt (VarDef k fs) e) = -// g e >>| -// abort "Shit, an assignment, figure out something with storing vars or something" -// //vars will be on stack in locals (possible pointers to heap) -// g (FunStmt _ _) = abort "CodeGen, FunStmt unused" //not used + g (IfStmt cond th el) = + fresh >>= \elseLabel-> + fresh >>= \endLabel-> + g cond >>| + tell [Instr "brf" [L elseLabel] "branch else"] >>| + mapM_ g th >>| + tell [Instr "bra" [L endLabel] "branch end if"] >>| + tell [Lab elseLabel] >>| + mapM_ g el >>| + tell [Lab endLabel] + g (WhileStmt cond th) = + fresh >>= \startLabel-> + fresh >>= \endLabel -> + tell [Lab startLabel] >>| + g cond >>| + tell [Instr "brf" [L endLabel] "branch end while"] >>| + mapM_ g th >>| + tell [Instr "bra" [L startLabel] "branch start while"] >>| + tell [Lab endLabel] + 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 (ADDR t) = tell [Instr "stl" [Lit t] ""] + g (FunStmt k es) = mapM g es >>| jump "bsr" k g (ReturnStmt Nothing) = tell [Instr "unlink" [] ""] >>| tell [Instr "ret" [] ""] g (ReturnStmt (Just e)) = g e diff --git a/sem.icl b/sem.icl index 2341eef..af49fdf 100644 --- a/sem.icl +++ b/sem.icl @@ -278,7 +278,7 @@ instance infer Stmt where changeGamma (extend k (Forall [] given)) >>| //todo: fieldselectors pure (s, VoidType) - FunStmt f es = undef //what is this? + FunStmt f es = pure (zero, VoidType) ReturnStmt Nothing = pure (zero, VoidType) ReturnStmt (Just e) = infer e