1%if false  
2  Copyright (c) 2009, ETH Zurich.
3  All rights reserved.
4  
5  This file is distributed under the terms in the attached LICENSE file.
6  If you do not find this file, copies can be found by writing to:
7  ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
8%endif
9
10%include polycode.fmt
11
12%if false
13
14> module Constructs.Strings where
15
16> import Data.Maybe
17
18> import Semantics
19> import Constructs
20> import PureExpressions
21> import {-# SOURCE #-} Expressions
22
23> import IL.FoF.FoF
24> import IL.FoF.Compile
25
26%endif
27
28\section{Strings}
29
30The |String| construct corresponds to static arrays of
31characters. However, they are implemented here as a special case as
32they are specially dealt with by the C compiler. 
33
34\subsection{Smart Constructors}
35
36We only provide string creation combinators: accessing a string can be
37achieved thanks to |Arrays| combinators. As usual, we provide two
38combinators: one to create an anonymous string, one to create a named
39string.
40
41> newString :: String -> FoFCode Loc
42> newString value = inject (NewString Nothing value return)
43>
44> newStringN :: String -> String -> FoFCode Loc
45> newStringN name value = inject (NewString (Just name) value return)
46
47\subsection{Compile Instantiation}
48
49The compilation is straightforward, on the model of static array
50declaration.
51
52> compileString (NewString name dat r) binding =
53>     let (publicName, binding1)
54>             = case name of
55>                 Just x -> (Provided x, binding)
56>                 Nothing -> 
57>                     let (loc, binding1) = getFreshVar binding in
58>                     (makeVarName Global loc,
59>                      binding1) in
60>     let ret = CLRef Global 
61>                     (TArray (StaticArray $ length dat) TChar) 
62>                     publicName in
63>     let (cont, binding2) = r ret binding1 in
64>     (FStatement (FNewString publicName dat) cont,
65>      binding2)
66
67
68\subsection{Run Instantiation}
69
70Similarly, the interpreter is simple.
71
72> runString (NewString a b r) heap = uncurry r $ runNewString b heap
73
74> runNewString :: String -> Heap -> (Loc, Heap)
75> runNewString string heap = 
76>     let loc = freshALoc heap in
77>     let size = length string in
78>     let name = makeVarName Dynamic loc in
79>     let ref = CLRef Dynamic (TArray (StaticArray size) TChar) name in
80>     let heap1 = heap { freshALoc = loc + 1,
81>                        arrayMap = (name, map cchar string) : (arrayMap heap) } in
82>     (ref, heap1)
83