From 53f1053727e36cb97e9670d1392c6e26848707a5 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Fri, 20 May 2016 11:26:56 +0200 Subject: [PATCH] codeGen fieldselectors --- examples/codeGen.spl | 13 +++++++------ gen.icl | 12 ++++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/examples/codeGen.spl b/examples/codeGen.spl index ebede23..5008ef9 100644 --- a/examples/codeGen.spl +++ b/examples/codeGen.spl @@ -27,10 +27,11 @@ isE(x) :: [a] -> Bool { } main() { - [Int] x1 = 1 : 2 : []; - [Int] x2 = 0 : x1; - [Int] x3 = []; - //Bool y1 = isEmpty(x2); //gives weird type error, not sure why - isEmpty(x2); - return; + // [Int] x2 = 0 : x1; + // [Int] x3 = []; + // (Int, Bool) x4 = (4, True); + // //Bool y1 = isEmpty(x2); //gives weird type error, not sure why +// isEmpty(x2); + [Int] x1 = 8 : 2 : []; + return x1.hd; } diff --git a/gen.icl b/gen.icl index d139804..8981738 100644 --- a/gen.icl +++ b/gen.icl @@ -119,6 +119,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 +143,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 -- 2.20.1