X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=gen.icl;h=bf3d7c45d61d2030d982ab3b392bfae857bc5fab;hb=9dcc80e7a580836c10157cd742eb1c805ef7311b;hp=d139804acf681111aa45a4279e72d4fc7bc3de00;hpb=f081c2c5e248331eb6e2f090f4afe818fd8259eb;p=cc1516.git diff --git a/gen.icl b/gen.icl index d139804..bf3d7c4 100644 --- a/gen.icl +++ b/gen.icl @@ -40,10 +40,12 @@ 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") + $ extend "1printchar" (LAB "1printchar") + $ extend "1readchar" (LAB "1readchar") + $ extend "1readint" (LAB "1readint") + $ extend "isEmpty" (LAB "isempty") + '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" @@ -119,6 +134,12 @@ instance g Op2 where BiOr = "or" BiCons = abort "Shit, Cons, how to deal with this?" +instance g FieldSelector where + g FieldFst = tell [Instr "lda" [Lit 0] "fst"] + g FieldSnd = tell [Instr "lda" [Lit 1] "snd"] + g FieldHd = tell [Instr "lda" [Lit -1] "hd"] + g FieldTl = tell [Instr "lda" [Lit 0] "tl"] + instance g Expr where g (IntExpr _ i) = tell [Instr "ldc" [Lit i] ""] g (CharExpr _ c) = tell [Instr "ldc" [Lit (toInt c)] ""] @@ -137,10 +158,8 @@ instance g Expr where >>| tell [Instr "sth" [] ""] >>| tell [Instr "ajs" [Lit -1] ""] g (VarExpr _ (VarDef k fs)) = getAdressbook >>= \ab->case 'Map'.get k ab of - Nothing = liftT (Left $ Error "PANIC: undefined variable") - Just (ADDR t) = tell [Instr "ldl" [Lit t] ""] - 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. [] + 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 @@ -178,10 +197,11 @@ instance g Stmt where 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 + g (FunStmt k es fs) = mapM_ g es >>| jump "bsr" k >>| mapM_ (const $ tell [Instr "ajs" [Lit -1] ""]) es //clean up args - >>| pure () + >>| mapM_ g fs + >>| pure () g (ReturnStmt Nothing) = tell [Instr "unlink" [] ""] >>| tell [Instr "ret" [] ""] g (ReturnStmt (Just e)) = g e