Skip to content

Commit

Permalink
Reduce use of Or_error in default scope
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbin committed Nov 10, 2024
1 parent 084b7c8 commit 4c12ffb
Show file tree
Hide file tree
Showing 15 changed files with 16 additions and 50 deletions.
2 changes: 0 additions & 2 deletions lib/bopkit_bdd_command/src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
-open
Stdio
-open
Or_error.Let_syntax
-open
Cmdlang)
(libraries
base
Expand Down
3 changes: 1 addition & 2 deletions lib/bopkit_block/src/bopkit_block.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
open! Base
open! Stdio
open! Or_error.Let_syntax

module Arity = struct
type ('kind, 'signal) t =
Expand Down Expand Up @@ -197,7 +196,7 @@ let run_line
~line
=
let output = create_output t.output_arity in
let%map () =
let%map.Or_error () =
try
let input = create_input t.input_arity ~line in
t.f ~arguments ~input ~output;
Expand Down
2 changes: 0 additions & 2 deletions lib/bopkit_command/src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
-open
Stdio
-open
Or_error.Let_syntax
-open
Cmdlang)
(libraries
base
Expand Down
2 changes: 0 additions & 2 deletions lib/bopkit_compiler/src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
-open
Fpath_base
-open
Or_error.Let_syntax
-open
Cmdlang)
(libraries
appendable-list
Expand Down
2 changes: 0 additions & 2 deletions lib/bopkit_compiler/src/pass_includes.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
open! Or_error.Let_syntax

let find_distribution_file ~path ~loc =
Bopkit_sites.Sites.stdlib
|> List.find_map ~f:(fun stdlib_directory ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
open! Or_error.Let_syntax

let execute_instruction
~(break : unit Or_error.t With_return.return)
~architecture
Expand All @@ -12,7 +10,7 @@ let execute_instruction
let st =
match In_channel.(input_line stdin) with
| Some line -> line
| None -> break.return (return ())
| None -> break.return (Or_error.return ())
in
let input_length = String.length st in
let expected_length = architecture * p in
Expand Down Expand Up @@ -52,7 +50,7 @@ let execute_code ~interpreted_code:{ Interpreted_code.architecture; memory; code
Array.iter code ~f:(fun instruction ->
execute_instruction ~break ~architecture ~memory ~instruction)
done;
return ())
Or_error.return ())
;;

let run_program ~architecture ~program =
Expand Down
2 changes: 0 additions & 2 deletions lib/bopkit_process_interpreter/src/interpreted_code.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
open! Or_error.Let_syntax

module Address = struct
type t = { memory_index : int } [@@deriving equal, sexp_of]
end
Expand Down
12 changes: 5 additions & 7 deletions lib/bopkit_process_interpreter/src/operator.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
open! Or_error.Let_syntax

type t =
{ operator_name : Bopkit_process.Operator_name.t
; arity : int
Expand All @@ -18,23 +16,23 @@ let sexp_of_t { operator_name; arity; compute = _ } =
let check_input_length ~input ~expected_length =
let length = Array.length input in
if expected_length = length
then return ()
then Or_error.return ()
else
Or_error.error_s
[%sexp "Unexpected input length", { expected_length : int; length : int }]
;;

let unary ~operator_name ~compute =
let compute ~operands:input =
let%bind () = check_input_length ~input ~expected_length:2 in
let%bind.Or_error () = check_input_length ~input ~expected_length:2 in
compute ~dst:input.(0) input.(1)
in
{ operator_name; arity = 1; compute }
;;

let binary ~operator_name ~compute =
let compute ~operands:input =
let%bind () = check_input_length ~input ~expected_length:3 in
let%bind.Or_error () = check_input_length ~input ~expected_length:3 in
compute ~dst:input.(0) input.(1) input.(2)
in
{ operator_name; arity = 2; compute }
Expand Down Expand Up @@ -70,13 +68,13 @@ let primitives : Env.t Lazy.t =
let operator_name = Bopkit_process.Operator_name.of_string operator_name in
unary ~operator_name ~compute:(fun ~dst a ->
f ~dst a;
return ())
Or_error.return ())
in
let b operator_name f : t =
let operator_name = Bopkit_process.Operator_name.of_string operator_name in
binary ~operator_name ~compute:(fun ~dst a b ->
f ~dst a b;
return ())
Or_error.return ())
in
lazy
([ u "not" p_not
Expand Down
2 changes: 0 additions & 2 deletions lib/bopkit_simulator/src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
-open
Stdio
-open
Or_error.Let_syntax
-open
Cmdlang)
(libraries
base
Expand Down
2 changes: 0 additions & 2 deletions project/visa/lib/visa_assembler/src/visa_assembler.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
open! Or_error.Let_syntax

module Assembly_construct = struct
type t =
| Label_introduction of { label : Visa.Label.t Loc.Txt.t }
Expand Down
2 changes: 0 additions & 2 deletions project/visa/lib/visa_dsl/test/test__visa_dsl.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
open! Or_error.Let_syntax

(* $MDX part-begin=program *)
let loop () : Visa.Program.t =
Visa_dsl.program (fun t ->
Expand Down
2 changes: 0 additions & 2 deletions project/visa/lib/visa_simulator/src/code.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
open! Or_error.Let_syntax

module Statement = struct
type t =
{ labels : Visa.Label.t Loc.Txt.t list
Expand Down
8 changes: 2 additions & 6 deletions project/visa/lib/visa_simulator/src/memory.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
open! Or_error.Let_syntax

module Ram = struct
type t = int Hashtbl.M(Int).t

Expand Down Expand Up @@ -79,15 +77,13 @@ let load t ~address ~register_name =
let store t ~register_name ~address =
let address = Visa.Address.to_int address in
let value = register_value t ~register_name in
Hashtbl.set t.memory ~key:address ~data:value;
return ()
Hashtbl.set t.memory ~key:address ~data:value
;;

let write t ~register_name ~address =
let address = Visa.Address.to_int address in
let value = register_value t ~register_name in
Output_device.set t.output_device ~address ~value;
return ()
Output_device.set t.output_device ~address ~value
;;

let add t =
Expand Down
12 changes: 2 additions & 10 deletions project/visa/lib/visa_simulator/src/memory.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,12 @@ val load_value : t -> value:int -> register_name:Visa.Register_name.t -> unit
(** [store t ~register_name ~address] is the opposite operation of [load] - it
will write to memory at the given address the value currently contained in
the register whose name is supplied. *)
val store
: t
-> register_name:Visa.Register_name.t
-> address:Visa.Address.t
-> unit Or_error.t
val store : t -> register_name:Visa.Register_name.t -> address:Visa.Address.t -> unit

(** [write t ~register_name ~address] is like [store] but writes to the output
device. It will write to the output device at the given address the value
currently contained in the register whose name is supplied. *)
val write
: t
-> register_name:Visa.Register_name.t
-> address:Visa.Address.t
-> unit Or_error.t
val write : t -> register_name:Visa.Register_name.t -> address:Visa.Address.t -> unit

(** {1 Operations on registers}
Expand Down
7 changes: 4 additions & 3 deletions project/visa/lib/visa_simulator/src/visa_simulator.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
open Or_error.Let_syntax
module Code = Code
module Execution_stack = Execution_stack
module Memory = Memory
Expand Down Expand Up @@ -91,6 +90,7 @@ let rec increment_code_pointer (t : t) =
;;

let execute_instruction t ~instruction =
let open Or_error.Let_syntax in
match (instruction : Visa.Label.t Visa.Instruction.t) with
| Nop -> return (increment_code_pointer t)
| Add ->
Expand Down Expand Up @@ -128,10 +128,10 @@ let execute_instruction t ~instruction =
return true)
else return (increment_code_pointer t)
| Store { register_name; address } ->
let%bind () = Memory.store t.memory ~register_name ~address in
Memory.store t.memory ~register_name ~address;
return (increment_code_pointer t)
| Write { register_name; address } ->
let%bind () = Memory.write t.memory ~register_name ~address in
Memory.write t.memory ~register_name ~address;
return (increment_code_pointer t)
| Load_address { address; register_name } ->
Memory.load t.memory ~address ~register_name;
Expand All @@ -153,6 +153,7 @@ module Step_result = struct
end

let step (t : t) =
let open Or_error.Let_syntax in
let environment = t.environment in
let assembly_instruction =
match Stack.top t.execution_stack.macro_frames with
Expand Down

0 comments on commit 4c12ffb

Please sign in to comment.