Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/constants/const-expr.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/constants/const-expr.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3454 - (view) (download)

1 : jhr 3385 (* const-expr.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 : jhr 3407 *
8 :     * TODO: add support for evaluating constants from the command-line
9 : jhr 3385 *)
10 :    
11 :     structure ConstExpr : sig
12 :    
13 :     (* compile-time constant values *)
14 : jhr 3454 datatype t
15 : jhr 3385 = String of string
16 :     | Bool of bool
17 :     | Int of IntLit.t
18 :     | Real of RealLit.t
19 : jhr 3433 | Tensor of const_value list * Types.ty
20 :     | Seq of const_value list * Types.ty
21 : jhr 3385 | Expr of AST.expr (* for more complicated tensor-valued expressions *)
22 :    
23 : jhr 3407 (* a property to attach to 'const' variables to track their value *)
24 : jhr 3454 val define : Var.t * t -> unit
25 :     val valueOf : Var.t -> t option
26 : jhr 3385
27 : jhr 3407 (* convert a constant value to an AST expression *)
28 : jhr 3454 val valueToExpr : t -> AST.expr
29 : jhr 3385
30 : jhr 3433 (* return the type of a constant value *)
31 : jhr 3454 val typeOfConst : t -> Types.ty
32 : jhr 3433
33 : jhr 3407 (* evaluate a constant expression; this returns NONE if the expression is not a valid
34 :     * constant expression and will also emit an error message into the error stream.
35 : jhr 3433 * The bool should be true if the constant is the default value for an input variable,
36 :     * since we then allow "load" and "image".
37 : jhr 3407 *)
38 : jhr 3454 val eval : ((Error.err_stream * Error.span) * bool * AST.expr) -> t option
39 : jhr 3407
40 : jhr 3385 end = struct
41 :    
42 : jhr 3407 structure L = Literal
43 :     structure BV = BasisVars
44 :     structure Ty = Types
45 :    
46 :     datatype token = datatype TypeError.token
47 :    
48 : jhr 3385 (* compile-time constant values *)
49 : jhr 3454 datatype t
50 : jhr 3385 = String of string
51 :     | Bool of bool
52 :     | Int of IntLit.t
53 :     | Real of RealLit.t
54 : jhr 3433 | Tensor of const_value list * Ty.ty
55 :     | Seq of const_value list * Ty.ty
56 : jhr 3385 | Expr of AST.expr (* for more complicated tensor-valued expressions *)
57 :    
58 : jhr 3407 (* a property to attach to 'const' variables to track their value *)
59 :     local
60 :     val {peekFn : Var.t -> const_value option, setFn, ...} =
61 :     Var.newProp (fn x => raise Fail("undefined constant " ^ Var.uniqueNameOf x))
62 :     in
63 :     val define = setFn
64 :     val valueOf = peekFn
65 :     end (* local *)
66 : jhr 3385
67 : jhr 3433 fun typeOfConst (String _) = Ty.T_String
68 :     | typeOfConst (Bool _) = Ty.T_Bool
69 :     | typeOfConst (Int _) = Ty.T_Int
70 :     | typeOfConst (Real _) = Ty.realTy
71 :     | typeOfConst (Tensor(_, ty)) = ty
72 :     | typeOfConst (Seq(_, ty)) = ty
73 :     | typeOfConst (Expr e) = TypeOf.expr e
74 :    
75 : jhr 3407 fun valueToExpr (String s) = AST.E_Lit(L.String s)
76 :     | valueToExpr (Bool b) = AST.E_Lit(L.Bool b)
77 :     | valueToExpr (Int i) = AST.E_Lit(L.Int i)
78 :     | valueToExpr (Real r) = AST.E_Lit(L.Real r)
79 : jhr 3433 | valueToExpr (Tensor(vs, ty)) = AST.E_Tensor(List.map valueToExpr vs, ty)
80 :     | valueToExpr (Seq(vs, ty)) = AST.E_Seq(List.map valueToExpr vs, ty)
81 : jhr 3407 | valueToExpr (Expr e) = e
82 : jhr 3385
83 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0