Skip to content

Commit 5e3251c

Browse files
committed
Debut de refactorisation de l'interpreteur
1 parent 9ffa7aa commit 5e3251c

11 files changed

Lines changed: 1468 additions & 29 deletions

File tree

src/mlang/backend_compilers/decoupledExpr.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,7 @@ let comp op (e1 : constr) (e2 : constr) (stacks : local_stacks)
313313
let comp (o : Com.comp_op) =
314314
match (e1, e2) with
315315
| Dlit f1, Dlit f2 ->
316-
if
317-
Mir_interpreter.FloatDefInterp.compare_numbers o
318-
(Mir_number.RegularFloatNumber.of_float f1)
319-
(Mir_number.RegularFloatNumber.of_float f2)
316+
if Mir_number.RegularFloatNumber.(compare o (of_float f1) (of_float f2))
320317
then Dtrue
321318
else Dfalse
322319
| Dvar v1, Dvar v2 ->

src/mlang/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
(flags
44
(:standard -open Utils))
55
(libraries re ANSITerminal parmap cmdliner threads dune-build-info num gmp
6-
menhirLib m_frontend m_ir irj_utils backend_compilers))
6+
menhirLib m_frontend m_ir irj_utils backend_compilers m_interpreter))
77

88
(documentation
99
(package mlang)

src/mlang/m_ir/mir_interpreter.ml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ module type S = sig
8787

8888
exception RuntimeError of run_error * ctx
8989

90-
val raise_runtime_as_structured : run_error -> 'a
91-
92-
val compare_numbers : Com.comp_op -> custom_float -> custom_float -> bool
93-
9490
val evaluate_expr : ctx -> Mir.expression Pos.marked -> value
9591

9692
val evaluate_program : ctx -> unit

src/mlang/m_ir/mir_interpreter.mli

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,6 @@ module type S = sig
108108

109109
exception RuntimeError of run_error * ctx
110110

111-
val raise_runtime_as_structured : run_error -> 'a
112-
(** Raises a runtime error with a formatted error message and context *)
113-
114-
val compare_numbers : Com.comp_op -> custom_float -> custom_float -> bool
115-
(** Returns the comparison between two numbers in the rounding and precision
116-
context of the interpreter. *)
117-
118111
val evaluate_expr : ctx -> Mir.expression Pos.marked -> value
119112

120113
val evaluate_program : ctx -> unit

src/mlang/m_ir/mir_number.ml

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
You should have received a copy of the GNU General Public License along with
1515
this program. If not, see <https://www.gnu.org/licenses/>. *)
1616

17-
module type NumberInterface = sig
17+
module type NumberInterfaceNoCompare = sig
1818
type t
1919

2020
val format_t : Format.formatter -> t -> unit
@@ -72,7 +72,28 @@ module type NumberInterface = sig
7272
val is_zero : t -> bool
7373
end
7474

75-
module RegularFloatNumber : NumberInterface = struct
75+
module type NumberInterface = sig
76+
include NumberInterfaceNoCompare
77+
78+
val compare : Com.comp_op -> t -> t -> bool
79+
end
80+
81+
module MakeComparable (N : NumberInterfaceNoCompare) : NumberInterface = struct
82+
include N
83+
84+
let compare op i1 i2 =
85+
let epsilon = of_float !Config.comparison_error_margin in
86+
let open Com in
87+
match op with
88+
| Gt -> i1 >. i2 +. epsilon
89+
| Gte -> i1 >. i2 -. epsilon
90+
| Lt -> i1 +. epsilon <. i2
91+
| Lte -> i1 -. epsilon <. i2
92+
| Eq -> abs (i1 -. i2) <. epsilon
93+
| Neq -> abs (i1 -. i2) >=. epsilon
94+
end
95+
96+
module RegularFloatNumber : NumberInterface = MakeComparable (struct
7697
type t = float
7798

7899
let format_t fmt f = Format.fprintf fmt "%f" f
@@ -138,7 +159,7 @@ module RegularFloatNumber : NumberInterface = struct
138159
let is_nan_or_inf x = not (Float.is_finite x)
139160

140161
let is_zero x = x = 0.
141-
end
162+
end)
142163

143164
let mpfr_abs (x : Mpfrf.t) : Mpfrf.t =
144165
let out = Mpfr.init2 (Mpfr.get_prec x) in
@@ -155,7 +176,7 @@ let mpfr_ceil (x : Mpfrf.t) : Mpfrf.t =
155176
ignore (Mpfr.ceil out x);
156177
Mpfrf.of_mpfr out
157178

158-
module MPFRNumber : NumberInterface = struct
179+
module MPFRNumber : NumberInterface = MakeComparable (struct
159180
type t = Mpfrf.t
160181

161182
let rounding : Mpfr.round = Near
@@ -214,9 +235,9 @@ module MPFRNumber : NumberInterface = struct
214235
let is_zero x = x =. zero ()
215236

216237
let is_nan_or_inf x = not (Mpfrf.number_p x)
217-
end
238+
end)
218239

219-
module IntervalNumber : NumberInterface = struct
240+
module IntervalNumber : NumberInterface = MakeComparable (struct
220241
type t = { down : Mpfrf.t; up : Mpfrf.t }
221242

222243
let v (x : Mpfrf.t) (y : Mpfrf.t) : t = { down = x; up = y }
@@ -335,9 +356,9 @@ module IntervalNumber : NumberInterface = struct
335356
let is_zero x = x =. zero ()
336357

337358
let is_nan_or_inf x = not (Mpfrf.number_p x.down && Mpfrf.number_p x.up)
338-
end
359+
end)
339360

340-
module RationalNumber : NumberInterface = struct
361+
module RationalNumber : NumberInterface = MakeComparable (struct
341362
type t = Mpqf.t
342363

343364
let format_t fmt f = Mpqf.print fmt f
@@ -407,11 +428,11 @@ module RationalNumber : NumberInterface = struct
407428
|| Mpzf.cmp (Mpqf.get_den x) max > 0
408429
|| Mpzf.cmp (Mpqf.get_num x) min < 0
409430
|| Mpzf.cmp (Mpqf.get_den x) min < 0
410-
end
431+
end)
411432

412433
module BigIntFixedPointNumber (P : sig
413434
val scaling_factor_bits : int ref
414-
end) : NumberInterface = struct
435+
end) : NumberInterface = MakeComparable (struct
415436
type t = Mpzf.t
416437

417438
let precision_modulo () =
@@ -498,4 +519,4 @@ end) : NumberInterface = struct
498519
let max x y = if x >. y then x else y
499520

500521
let is_nan_or_inf _ = false
501-
end
522+
end)

src/mlang/m_ir/mir_number.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ module type NumberInterface = sig
6868
val is_nan_or_inf : t -> bool
6969

7070
val is_zero : t -> bool
71+
72+
val compare : Com.comp_op -> t -> t -> bool
73+
(** Returns the comparison between two numbers in the precision context
74+
of the current configuration. *)
7175
end
7276

7377
module RegularFloatNumber : NumberInterface

src/mlang/mir_interpreter/dune

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(library
2+
(public_name mlang.interpreter)
3+
(name m_interpreter)
4+
(flags
5+
(:standard -open Utils))
6+
(libraries utils m_ir num gmp re))

0 commit comments

Comments
 (0)