aboutsummaryrefslogtreecommitdiff
path: root/src/expression.ml
diff options
context:
space:
mode:
Diffstat (limited to 'src/expression.ml')
-rwxr-xr-xsrc/expression.ml86
1 files changed, 44 insertions, 42 deletions
diff --git a/src/expression.ml b/src/expression.ml
index 20227ad..ae6c85f 100755
--- a/src/expression.ml
+++ b/src/expression.ml
@@ -3,13 +3,13 @@ module Tuple2 = Tools.Tuple2
let u = UTF8.from_utf8string
type t =
- | Basic: 'a ScTypes.types -> t (** A direct type *)
- | Formula: formula -> t (** A formula *)
- | Undefined: t (** The content is not defined *)
+ | Basic: 'a ScTypes.Type.t -> t (** A direct type *)
+ | Formula: formula -> t (** A formula *)
+ | Undefined: t (** The content is not defined *)
and formula =
- | Expression of ScTypes.expression (** A valid expression *)
- | Error of int * UTF8.t (** When the expression cannot be parsed *)
+ | Expression of ScTypes.Expr.t (** A valid expression *)
+ | Error of int * UTF8.t (** When the expression cannot be parsed *)
let is_defined = function
@@ -34,11 +34,11 @@ let load content = begin
try String.sub content 0 (String.index content '\000')
with Not_found -> content in
try
- let ScTypes.Result r =
+ let ScTypes.Result.Ok r =
ExpressionParser.content ExpressionLexer.read
@@ Lexing.from_string content' in
Basic r
- with _ -> Basic (ScTypes.string (UTF8.from_utf8string content'))
+ with _ -> Basic (ScTypes.Type.string (UTF8.from_utf8string content'))
)
) else (
(* If the string in empty, build an undefined value *)
@@ -49,64 +49,66 @@ end
let load_expr expr = expr
+module EvalExpr = ScTypes.Expr.Eval(Evaluate)
(** Extract the parameters to give to a function.
return an Error if one of them is an error
*)
-let eval expr sources = begin
-
- let eval_exp f = Evaluator.eval sources f in
+let eval expr catalog mapper = begin
begin try match expr with
- | Basic value -> ScTypes.Result value
- | Formula (Expression f) -> eval_exp f
- | Formula (Error (i, s)) -> ScTypes.Error ScTypes.Error
- | Undefined -> ScTypes.Error Not_found
- with ex -> ScTypes.Error ex
+ | Basic value -> ScTypes.Result.Ok value
+ | Formula (Expression e) -> EvalExpr.eval e (catalog, mapper)
+ | Formula (Error (i, s)) -> ScTypes.Result.Error ScTypes.Error
+ | Undefined -> ScTypes.Result.Error Not_found
+ with ex -> ScTypes.Result.Error ex
end
end
-let collect_sources expr = begin
- let rec collect refs = function
- | ScTypes.Ref r ->
- begin match ScTypes.Refs.collect r with
- | ScTypes.Refs.Single r -> Cell.Set.add r refs
- | ScTypes.Refs.Array1 a1 ->
- List.fold_left (fun set elt -> Cell.Set.add elt set) refs a1
- | ScTypes.Refs.Array2 a2 ->
- List.fold_left (List.fold_left (fun set elt -> Cell.Set.add elt set)) refs a2
- end
- | ScTypes.Call (ident, params) -> List.fold_left collect refs params
- | ScTypes.Expression f -> collect refs f
- | _ -> refs
- in match expr with
- | Formula (Expression f) -> collect Cell.Set.empty f
+
+module EvalSources = ScTypes.Expr.Eval(Collect_sources)
+
+let collect_sources = begin function
+ | Formula (Expression f) -> EvalSources.eval f () Cell.Set.empty
| _ -> Cell.Set.empty
end
+module Printer = ScTypes.Expr.Eval(Show_expr.Show_Expr(Show_ref)(Show_type))
+
+(** Inherit the default representation, but print the float with all decimals *)
+module LongPrinter = ScTypes.Type.Eval(struct
+
+ include Show_type
+
+ let num n buffer =
+ if DataType.Num.is_integer n then
+ DataType.Num.to_int n
+ |> string_of_int
+ |> UTF8.from_utf8string
+ |> UTF8.Buffer.add_string buffer
+ else
+ let f = DataType.Num.to_float n
+ and to_b = UTF8.Format.formatter_of_buffer buffer in
+ ignore @@ UTF8.Format.fprintf to_b "%f" f;
+ Format.pp_print_flush to_b ()
+
+end)
+
let show e =
let buffer = UTF8.Buffer.create 16 in
begin match e with
| Formula (Expression f) ->
UTF8.Buffer.add_char buffer '=';
- ScTypes.show_expr buffer f
- | Basic b -> ScTypes.Type.show_full buffer b
+ Printer.eval f () buffer
+ | Basic b -> LongPrinter.eval b buffer
| Formula (Error (i,s)) -> UTF8.Buffer.add_string buffer s
| Undefined -> ()
end;
UTF8.Buffer.contents buffer
-let shift vector =
-
- let rec shift_exp: ScTypes.expression -> ScTypes.expression = function
- | ScTypes.Value v -> ScTypes.Value v
- | ScTypes.Call (ident, params) -> ScTypes.Call (ident, List.map shift_exp params)
- | ScTypes.Ref r -> ScTypes.Ref (ScTypes.Refs.shift vector r)
- | ScTypes.Expression expr -> ScTypes.Expression (shift_exp expr)
-
- in function
- | Formula (Expression f) -> Formula (Expression (shift_exp f))
+let shift vector = function
+ | Formula (Expression f) -> Formula (Expression (ScTypes.Expr.shift_exp vector f))
| other -> other
let (=) t1 t2 = match t1, t2 with