From 5f51b53ee01b91e1996ede1c515167af41a44571 Mon Sep 17 00:00:00 2001 From: pimjager Date: Tue, 21 Jun 2016 12:12:11 +0200 Subject: [PATCH] Fieldselectors when assigning --- examples/old/quickTest.spl | 12 +++++++----- gen.icl | 16 +++++++++++++++- sem.icl | 2 +- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/examples/old/quickTest.spl b/examples/old/quickTest.spl index 8a9cc26..da09eeb 100644 --- a/examples/old/quickTest.spl +++ b/examples/old/quickTest.spl @@ -1,9 +1,11 @@ -x() { - return [[1,2,3]]; +printInts(xs) :: [Int] -> Void { + if(isEmpty(xs)) { return; } + else { print(xs.hd); printInts(xs.tl); } } main() { - - var z = x().hd.hd; - + var xs = [1,2,3]; + xs.hd = 18; + xs.tl = []; + printInts(xs); } \ No newline at end of file diff --git a/gen.icl b/gen.icl index 0707984..3dd4399 100644 --- a/gen.icl +++ b/gen.icl @@ -334,7 +334,11 @@ instance g Stmt where 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 ar) = tell [Instr "stl" [Lit t] ""] + Just (ADDR t ar) = case fs of + [] = tell [Instr "stl" [Lit t] ""] + _ = tell [Instr "ldl" [Lit t] ""] + >>| mapM_ followFs fs + >>| tell [Instr "sta" [Lit 0] ""] g (FunStmt k es fs) = funnyStuff k es fs g (ReturnStmt Nothing) = tell [Instr "unlink" [] ""] >>| tell [Instr "ret" [] ""] @@ -342,6 +346,16 @@ instance g Stmt where >>| tell [Instr "str" [Raw "RR"] ""] >>| g (ReturnStmt Nothing) +//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 [] + foldVarDecl :: Int VarDecl -> Gen Int foldVarDecl x (VarDecl _ mt k e) = g e >>| annote x k diff --git a/sem.icl b/sem.icl index fd9eb5c..8ca24cf 100644 --- a/sem.icl +++ b/sem.icl @@ -405,7 +405,7 @@ instance infer Stmt where reverseFs :: Type FieldSelector -> Typing Type reverseFs t FieldHd = pure $ ListType t -reverseFs t FieldTl = pure $ ListType t +reverseFs t FieldTl = pure t reverseFs t FieldFst = fresh >>= \tv -> pure $ TupleType (t, tv) reverseFs t FieldSnd = fresh >>= \tv -> pure $ TupleType (tv, t) -- 2.20.1