Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
drewolson committed Nov 3, 2023
1 parent 97f8fa1 commit 3ec5685
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/util/parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ let integer =
Int.of_string tokens
;;

let signed_integer =
let open Syntax in
let%map sign = 1 <$ char '+' <|> (-1 <$ char '-') <|> return 1
and n = integer in
sign * n
;;

let parse_exn parser input =
input |> parse_string ~consume:Prefix parser |> Result.ok_or_failwith
;;
Expand Down
5 changes: 5 additions & 0 deletions lib/year2023/dune
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
(library
(name year2023)
(libraries
angstrom
core
ppx_jane
re
util)
(preprocess
(pps
ppx_jane))
(flags
-open Core))
9 changes: 9 additions & 0 deletions test/util/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(library
(name util_test)
(libraries
ppx_jane
year2022)
(inline_tests)
(preprocess
(pps
ppx_jane)))
19 changes: 19 additions & 0 deletions test/util/parser_test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module P = Util.Parser

let%expect_test "signed_integer works with positive signs" =
let result = P.parse_exn P.signed_integer "+10" in
Printf.printf "%i" result;
[%expect {| 10 |}]
;;

let%expect_test "signed_integer works with negative signs" =
let result = P.parse_exn P.signed_integer "-10" in
Printf.printf "%i" result;
[%expect {| -10 |}]
;;

let%expect_test "signed_integer works with no signs" =
let result = P.parse_exn P.signed_integer "10" in
Printf.printf "%i" result;
[%expect {| 10 |}]
;;

0 comments on commit 3ec5685

Please sign in to comment.