From f76cc00c77865639773051205d912a851b302bdc Mon Sep 17 00:00:00 2001 From: pimjager Date: Tue, 21 Jun 2016 12:20:49 +0200 Subject: [PATCH] fixed chaining of fieldselectors --- examples/passBy.spl | 36 ++++++------------------------------ gen.icl | 23 +++++++++++++++-------- 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/examples/passBy.spl b/examples/passBy.spl index 552eabe..d463ed1 100644 --- a/examples/passBy.spl +++ b/examples/passBy.spl @@ -1,34 +1,10 @@ -//modify1(s) :: [Char] -> [Char] { -// //s.hd = 'a'; -// s.tl = []; -// return s; -//}// - -//modify2(c) :: Char -> Void { -// c = 'a'; -//}// - -//main() { -// var zs = "bar"; -// var zc = 'b'; -// var zss = modify1(zs); -// modify2(zc);// - -// print("zs was bar, is now: "); -// print(zs); -// print(", m1 returned: "); -// print(zss); -// print("\n"); -// print("zc was 'b', is now: ", zc, "\n"); -//} - -modi(t) :: (Int,Int) -> Void { - t.fst = 1; +modif(str) :: [Char] -> Void { + str.tl.tl.hd = 'z'; } main() { - var x = (2,3); - print(x.fst); - modi(x); - print(x.fst); + var s = "bar"; + print(s, "\n"); + modif(s); + print(s, "\n"); } \ No newline at end of file diff --git a/gen.icl b/gen.icl index 3dd4399..b6e22d6 100644 --- a/gen.icl +++ b/gen.icl @@ -337,7 +337,7 @@ instance g Stmt where Just (ADDR t ar) = case fs of [] = tell [Instr "stl" [Lit t] ""] _ = tell [Instr "ldl" [Lit t] ""] - >>| mapM_ followFs fs + >>| followFs fs >>| tell [Instr "sta" [Lit 0] ""] g (FunStmt k es fs) = funnyStuff k es fs g (ReturnStmt Nothing) = tell [Instr "unlink" [] ""] @@ -348,13 +348,20 @@ instance g Stmt where //expects the heap address to the var to slect on to be on the stack //and leaves the heap adress to write to on the stack -followFs :: FieldSelector -> Gen() -followFs FieldHd = tell [Instr "ldc" [Lit 1] "select hd" - ,Instr "sub" [] "select hd"] -followFs FieldTl = tell [] -followFs FieldFst = tell [Instr "ldc" [Lit 1] "select fst" - ,Instr "sub" [] "select fst"] -followFs FieldSnd = tell [] +followFs :: [FieldSelector] -> Gen() +followFs [] = tell [] +followFs [FieldHd:fs] = tell [Instr "ldc" [Lit 1] "select hd" + ,Instr "sub" [] "select hd"] + >>| followFs fs +followFs [FieldTl] = tell [] +followFs [FieldTl:fs] = tell [Instr "lda" [Lit 0] ""] + >>| followFs fs +followFs [FieldFst:fs] = tell [Instr "ldc" [Lit 1] "select fst" + ,Instr "sub" [] "select fst"] + >>| followFs fs +followFs [FieldSnd:fs] = tell [Instr "lda" [Lit 0] ""] + >>| followFs fs +followFs [FieldSnd] = tell [] foldVarDecl :: Int VarDecl -> Gen Int foldVarDecl x (VarDecl _ mt k e) = g e -- 2.20.1