Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

[diderot] Diff of /branches/pure-cfg/src/compiler/simplify/eval.sml
 [diderot] / branches / pure-cfg / src / compiler / simplify / eval.sml

# Diff of /branches/pure-cfg/src/compiler/simplify/eval.sml

revision 785, Fri Apr 8 03:11:37 2011 UTC revision 786, Fri Apr 8 03:34:06 2011 UTC
# Line 12  Line 12
12        = BV of bool        = BV of bool
13        | SV of string        | SV of string
14        | IV of IntInf.int        | IV of IntInf.int
15        | TV of (int list * real list)    (* tensors *)        | RV of real
16          | TV of value list        (* tensors: values will either be RV or TV *)
17        | ImgV of ImageInfo.info * Var.var        | ImgV of ImageInfo.info * Var.var
18
19      val evalStatics : Var.Set.set * Simple.block -> value Var.Map.map      val evalStatics : Var.Set.set * Simple.block -> value Var.Map.map
# Line 31  Line 32
32        | SV of string        | SV of string
33        | IV of IntInf.int        | IV of IntInf.int
34  (* FIXME: we probably should use FloatLit.float values instead of reals! *)  (* FIXME: we probably should use FloatLit.float values instead of reals! *)
35        | TV of (int list * real list)    (* tensors *)        | RV of real
36          | TV of value list        (* tensors: values will either be RV or TV *)
37        | ImgV of ImageInfo.info * Var.var        | ImgV of ImageInfo.info * Var.var
38
fun RV r = TV([], [r])

39      fun toString (BV b) = Bool.toString b      fun toString (BV b) = Bool.toString b
40        | toString (IV i) = IntInf.toString i        | toString (IV i) = IntInf.toString i
41        | toString (SV s) = concat["\"", String.toString s, "\""]        | toString (SV s) = concat["\"", String.toString s, "\""]
42        | toString (TV(s, v)) = "tensor"        | toString (RV r) = Real.toString r
43          | toString (TV _) = "tensor"
44  (* FIXME: should include x in output *)  (* FIXME: should include x in output *)
45        | toString (ImgV(info, x)) = ImageInfo.toString info        | toString (ImgV(info, x)) = ImageInfo.toString info
46
47      val tbl : (value list -> value) VTbl.hash_table = let      val tbl : (value list -> value) VTbl.hash_table = let
48            val tbl = VTbl.mkTable (128, Fail "Eval table")            val tbl = VTbl.mkTable (128, Fail "Eval table")
49            fun intBinOp rator [IV a, IV b] = IV(rator(a, b))            fun intBinOp rator [IV a, IV b] = IV(rator(a, b))
50            fun tensorBinOp rator [TV(s1, v1), TV(s2, v2)] =            fun tensorBinOp rator [v1, v2] = let
51                  TV(s1, ListPair.mapEq rator (v1, v2))                  fun f (TV v1, TV v2) = TV(ListPair.mapEq f (v1, v2))
52            fun realBinOp rator [TV([], [a]), TV([], [b])] = RV(rator(a, b))                    | f (RV r1, RV r2) = RV(rator(r1, r2))
53            fun realUnOp rator [TV([], [a])] = RV(rator a)                  in
54                      f (v1, v2)
55                    end
56              fun realBinOp rator [RV a, RV b] = RV(rator(a, b))
57              fun realUnOp rator [RV a] = RV(rator a)
58            fun intCmp rator [IV a, IV b] = BV(rator(a, b))            fun intCmp rator [IV a, IV b] = BV(rator(a, b))
59            fun realCmp rator [TV([], [a]), TV([], [b])] = BV(rator(a, b))            fun realCmp rator [RV a, RV b] = BV(rator(a, b))
60            fun boolCmp rator [BV a, BV b] = BV(rator(a, b))            fun boolCmp rator [BV a, BV b] = BV(rator(a, b))
61            fun stringCmp rator [SV a, SV b] = BV(rator(a, b))            fun stringCmp rator [SV a, SV b] = BV(rator(a, b))
62            in            in
# Line 177  Line 182
182              | S.E_Apply(f, mvs, xs, _) => apply(env, f, mvs, xs)              | S.E_Apply(f, mvs, xs, _) => apply(env, f, mvs, xs)
183              | S.E_Cons xs => (case evalArgs(env, xs)              | S.E_Cons xs => (case evalArgs(env, xs)
184                   of NONE => NONE                   of NONE => NONE
185                    | SOME vs => SOME(TV([List.length vs], List.map (fn (TV([], [r])) => r) vs))                    | SOME vs => SOME(TV vs)
186                  (* end case *))                  (* end case *))
187              | S.E_Slice(x, indices, _) => (case VMap.find (env, x)              | S.E_Slice(x, indices, _) => (case VMap.find (env, x)
188                   of SOME(TV(dd, vs)) => raise Fail "E_Slice"                   of SOME v => let
189                          fun slice (TV vs, SOME ix :: ixs) = (case VMap.find (env, ix)
190                                 of SOME(IV i) => slice (List.nth(vs, IntInf.toInt i), ixs)
191                                  | NONE => raise Subscript
192                                (* end case *))
193                            | slice (TV vs, NONE :: ixs) =
194                                TV(List.map (fn v => slice(v, ixs)) vs)
195                            | slice (v, []) = v
196                          in
197                            SOME(slice(v, indices)) handle Subscript => NONE
198                          end
199                    | _ => NONE                    | _ => NONE
200                  (* end case *))                  (* end case *))
201              | S.E_Input(ty, name, optDefault) => raise Fail "impossible"              | S.E_Input(ty, name, optDefault) => raise Fail "impossible"
# Line 212  Line 227
227                      (* then tokenize by white space and flatten *)                      (* then tokenize by white space and flatten *)
228                        val toks = List.concat(List.map (String.tokens Char.isSpace) flds)                        val toks = List.concat(List.map (String.tokens Char.isSpace) flds)
229                      (* then convert to reals *)                      (* then convert to reals *)
230                        val vals = List.map (valOf o Real.fromString) toks                        val vals = List.map (RV o valOf o Real.fromString) toks
231                        in                        in
232                          if (List.length vals = d)                          if (List.length vals = d)
233                            then SOME(TV([d], vals))                            then SOME(TV(vals))
234                            else NONE                            else NONE
235                        end                        end
236                        handle _ => NONE                        handle _ => NONE

Legend:
 Removed from v.785 changed lines Added in v.786