Skip to content
This repository was archived by the owner on Jan 30, 2026. It is now read-only.

Commit fe0dca6

Browse files
committed
Rebase artifact
1 parent 9a3cee9 commit fe0dca6

1 file changed

Lines changed: 39 additions & 18 deletions

File tree

src/mlang/mir_interpreter/eval.ml

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ module Make (N : Mir_number.NumberInterface) (RF : Mir_roundops.RoundOpsFunctor)
205205
let rec get_access_value ctx access =
206206
match access with
207207
| Com.VarAccess (m_sp_opt, v) -> get_var_value ctx m_sp_opt v
208-
| Com.TabAccess (m_sp_opt, v, m_idx) -> (
208+
| Com.TabAccess ((m_sp_opt, v), m_idx) -> (
209209
match evaluate_expr ctx m_idx with
210210
| Number z ->
211211
let i = Int64.to_int @@ N.to_int z in
@@ -228,7 +228,7 @@ module Make (N : Mir_number.NumberInterface) (RF : Mir_roundops.RoundOpsFunctor)
228228
| Com.VarAccess (m_sp_opt, v) ->
229229
let vsd, v, vorg = get_var ctx m_sp_opt v in
230230
Some (vsd, v, vorg)
231-
| Com.TabAccess (m_sp_opt, m_v, m_i) -> (
231+
| Com.TabAccess ((m_sp_opt, m_v), m_i) -> (
232232
match evaluate_expr ctx m_i with
233233
| Number z ->
234234
let vsd, v, vorg = get_var ctx m_sp_opt m_v in
@@ -292,7 +292,7 @@ module Make (N : Mir_number.NumberInterface) (RF : Mir_roundops.RoundOpsFunctor)
292292
and set_access ctx access value =
293293
match access with
294294
| Com.VarAccess (m_sp_opt, v) -> set_var_value ctx m_sp_opt v value
295-
| Com.TabAccess (m_sp_opt, v, m_idx) -> (
295+
| Com.TabAccess ((m_sp_opt, v), m_idx) -> (
296296
match evaluate_expr ctx m_idx with
297297
| Number z ->
298298
let i = Int64.to_int @@ N.to_int z in
@@ -387,6 +387,22 @@ module Make (N : Mir_number.NumberInterface) (RF : Mir_roundops.RoundOpsFunctor)
387387

388388
(* interpret *)
389389

390+
and same_variable ctx m_acc m_acc' : bool =
391+
let v0_opt = get_access_var ctx (Pos.unmark m_acc) in
392+
let v1_opt = get_access_var ctx (Pos.unmark m_acc') in
393+
match (v0_opt, v1_opt) with
394+
| Some (_, v0, _), Some (_, v1, _) ->
395+
Com.Var.name_str v0 = Com.Var.name_str v1
396+
| _, _ -> false
397+
398+
and evaluate_switch_expr (ctx : ctx) s_e =
399+
match s_e with
400+
| Com.SEValue e -> (
401+
match evaluate_expr ctx e with
402+
| Undefined -> `Undefined
403+
| Number n -> `Value n)
404+
| SESameVariable v -> `Var v
405+
390406
and evaluate_expr (ctx : ctx) (e : Mir.expression Pos.marked) : value =
391407
let out =
392408
try
@@ -507,15 +523,9 @@ module Make (N : Mir_number.NumberInterface) (RF : Mir_roundops.RoundOpsFunctor)
507523
then Number (N.one ())
508524
else Number (N.zero ())
509525
| None -> Undefined)
510-
| SameVariable (m_acc0, m_acc1) -> (
511-
let v0_opt = get_access_var ctx (Pos.unmark m_acc0) in
512-
let v1_opt = get_access_var ctx (Pos.unmark m_acc1) in
513-
match (v0_opt, v1_opt) with
514-
| Some (_, v0, _), Some (_, v1, _) ->
515-
if Com.Var.name_str v0 = Com.Var.name_str v1 then
516-
Number (N.one ())
517-
else Number (N.zero ())
518-
| _, _ -> Number (N.zero ()))
526+
| SameVariable (m_acc0, m_acc1) ->
527+
if same_variable ctx m_acc0 m_acc1 then Number (N.one ())
528+
else Number (N.zero ())
519529
| InDomain (m_acc, cvm) -> (
520530
match get_access_var ctx (Pos.unmark m_acc) with
521531
| Some (_, v, _) ->
@@ -581,21 +591,32 @@ module Make (N : Mir_number.NumberInterface) (RF : Mir_roundops.RoundOpsFunctor)
581591
| Number _ -> evaluate_stmts canBlock ctx t
582592
| Undefined -> ())
583593
| Com.Switch (c, l) -> (
584-
let v = evaluate_expr ctx c in
585594
let exception INTERNAL_STOP_SWITCH in
586595
let then_ () = raise INTERNAL_STOP_SWITCH in
596+
let v = evaluate_switch_expr ctx c in
597+
let default = ref None in
587598
try
588599
List.iter
589600
(fun (cases, stmts) ->
590601
List.iter
591602
(fun case ->
592603
match (case, v) with
593-
| Com.Default, _ | Value Undefined, Undefined ->
594-
evaluate_stmts ~then_ canBlock ctx stmts
595-
| Value (Float f), Number n when N.compare Eq n (N.of_float f)
596-
->
604+
| Com.CDefault, _ ->
605+
(* Trigged only if all other cases fail *)
606+
default := Some stmts
607+
| CValue Undefined, `Undefined ->
597608
evaluate_stmts ~then_ canBlock ctx stmts
598-
| _ -> ())
609+
| CValue _, `Undefined | CValue Undefined, _ -> ()
610+
| CValue (Float f), `Value v ->
611+
if N.of_float f = v then
612+
evaluate_stmts ~then_ canBlock ctx stmts
613+
| CValue _, `Var _ ->
614+
failwith "Cannot match value with variable"
615+
| CVar m_acc, `Var v ->
616+
if same_variable ctx m_acc v then
617+
evaluate_stmts ~then_ canBlock ctx stmts
618+
| CVar _, (`Value _ | `Undefined) ->
619+
failwith "Cannot match variable with value")
599620
cases)
600621
l
601622
with INTERNAL_STOP_SWITCH -> ())

0 commit comments

Comments
 (0)