Skip to content

Commit

Permalink
Fix binary list operators
Browse files Browse the repository at this point in the history
`List_append_last v lst` was changed into `List_cons v lst`
which conses `v` to the head of `lst`.

`List_append lst v` was changed into `List_append l0 l1`
which appends l1 to l0.
  • Loading branch information
filipeom committed Sep 17, 2024
1 parent 24435b5 commit 7700213
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
8 changes: 6 additions & 2 deletions src/ast/expr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,12 @@ let rec binop ty (op : binop) (hte1 : t) (hte2 : t) : t =
let v = value (Eval.binop ty Mul v1 v2) in
binop' ty Mul v x
| At, List es, Val (Int n) -> List.nth es n
| List_append_last, List es, _ -> make (List (es @ [ hte2 ]))
| List_append, List es, _ -> make (List (hte2 :: es))
| List_cons, _, List es -> make (List (hte1 :: es))
| List_append, List _, (List [] | Val (List [])) -> hte1
| List_append, (List [] | Val (List [])), List _ -> hte2
| List_append, List l0, Val (List l1) -> make (List (l0 @ List.map value l1))
| List_append, Val (List l0), List l1 -> make (List (List.map value l0 @ l1))
| List_append, List l0, List l1 -> make (List (l0 @ l1))
| _ -> binop' ty op hte1 hte2

let triop' (ty : Ty.t) (op : triop) (e1 : t) (e2 : t) (e3 : t) : t =
Expand Down
8 changes: 4 additions & 4 deletions src/ast/ty.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type binop =
| Rotl
| Rotr
| At
| List_append_last
| List_cons
| List_append
(* String *)
| String_prefix
Expand Down Expand Up @@ -126,15 +126,15 @@ let binop_equal o1 o2 =
| Rotl, Rotl
| Rotr, Rotr
| At, At
| List_append_last, List_append_last
| List_cons, List_cons
| List_append, List_append
| String_prefix, String_prefix
| String_suffix, String_suffix
| String_contains, String_contains
| String_last_index, String_last_index ->
true
| ( ( Add | Sub | Mul | Div | DivU | Rem | RemU | Shl | ShrA | ShrL | And | Or
| Xor | Pow | Min | Max | Rotl | Rotr | At | List_append_last
| Xor | Pow | Min | Max | Rotl | Rotr | At | List_cons
| List_append | String_prefix | String_suffix | String_contains
| String_last_index )
, _ ) ->
Expand Down Expand Up @@ -326,7 +326,7 @@ let pp_binop fmt (op : binop) =
| Rotl -> Fmt.string fmt "rotl"
| Rotr -> Fmt.string fmt "rotr"
| At -> Fmt.string fmt "at"
| List_append_last -> Fmt.string fmt "append_last"
| List_cons-> Fmt.string fmt "cons"
| List_append -> Fmt.string fmt "append"
| String_prefix -> Fmt.string fmt "prefixof"
| String_suffix -> Fmt.string fmt "suffixof"
Expand Down
2 changes: 1 addition & 1 deletion src/ast/ty.mli
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type binop =
| Rotl
| Rotr
| At
| List_append_last
| List_cons
| List_append
(* String *)
| String_prefix (* (str.prefixof String String Bool) *)
Expand Down
4 changes: 2 additions & 2 deletions src/interpret/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ module Lst = struct
let i = Int.of_value 2 op' v2 in
try List.nth lst i
with Failure _ | Invalid_argument _ -> raise IndexOutOfBounds )
| List_append_last -> List (of_value 1 op' v1 @ [ v2 ])
| List_append -> List (v2 :: of_value 2 op' v1)
| List_cons -> List (v1 :: of_value 1 op' v2)
| List_append -> List (of_value 1 op' v1 @ of_value 2 op' v2)
| _ -> Fmt.failwith {|binop: Unsupported list operator "%a"|} Ty.pp_binop op

let triop (op : triop) (v1 : Value.t) (v2 : Value.t) (v3 : Value.t) : Value.t
Expand Down
9 changes: 5 additions & 4 deletions test/unit/test_binop.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ let () =
let () =
let clist = list [ Int 0; Int 1; Int 2 ] in
assert (binop Ty_list At clist (int 0) = int 0);
assert (binop Ty_list List_append_last (list [ Int 0; Int 1 ]) (int 2) = clist);
assert (binop Ty_list List_append (list [ Int 1; Int 2 ]) (int 0) = clist);
assert (binop Ty_list List_cons (int 0) (list [ Int 1; Int 2 ]) = clist);
assert (
binop Ty_list List_append (list [ Int 0; Int 1 ]) (list [ Int 2 ]) = clist );
let slist2 = make (List [ int 0; int 1 ]) in
let slist3 = make (List [ int 0; int 1; int 2 ]) in
assert (binop Ty_list At slist3 (int 0) = int 0);
assert (binop Ty_list List_append_last slist2 (int 2) = slist3);
assert (binop Ty_list List_append slist2 (list [ Int 2 ]) = slist3);
assert (
binop Ty_list List_append (make (List [ int 1; int 2 ])) (int 0) = slist3 )
binop Ty_list List_cons (int 0) (make (List [ int 1; int 2 ])) = slist3 )

(* i32 *)
let () =
Expand Down

0 comments on commit 7700213

Please sign in to comment.