diff --git a/lib/back/cprint1.ml b/lib/back/cprint1.ml index 01b6c37..d87ee27 100644 --- a/lib/back/cprint1.ml +++ b/lib/back/cprint1.ml @@ -6,7 +6,7 @@ let version = "Cprint 4.0 Hugues Cassé et al." (* ** FrontC Pretty printer *) -let out = ref stdout +let out = ref (Buffer.create 50) let width = ref 80 let tab = ref 8 let max_indent = ref 60 @@ -19,6 +19,11 @@ let spaces = ref 0 let follow = ref 0 let roll = ref 0 +(* override output_string in stdlib *) +let output_string = Buffer.add_string +let output_char = Buffer.add_char + + let print_tab size = output_string !out (String.make (size / 8) '\t'); output_string !out (String.make (size mod 8) ' ') @@ -755,12 +760,6 @@ and print_def def = force_new_line () -(* print abstrac_syntax -> () - ** Pretty printing the given abstract syntax program. -*) -let print (result : out_channel) (defs : file) = - out := result; - print_defs defs let set_tab t = tab := t let set_width w = width := w diff --git a/lib/test/back_test.ml b/lib/test/back_test.ml new file mode 100644 index 0000000..0d18801 --- /dev/null +++ b/lib/test/back_test.ml @@ -0,0 +1,13 @@ +open Cabs +module P = Back.Cprint1 + +let%expect_test "Test: Backend" = + let bin = + BINARY (ADD, CONSTANT (CONST_INT "1"), CONSTANT (CONST_INT "2")) + in + P.out := Buffer.create 50; + P.print_expression bin 0; + P.commit (); + P.flush (); + !P.out |> Buffer.contents |> print_string; + [%expect {| 1 + 2 |}] diff --git a/lib/test/dune b/lib/test/dune index 8e3c9a9..0238c37 100644 --- a/lib/test/dune +++ b/lib/test/dune @@ -1,6 +1,6 @@ (library (name test) - (libraries sexplib syntax) + (libraries sexplib syntax back) (inline_tests) (wrapped false) (preprocess