Skip to content

Commit

Permalink
a test example for backend
Browse files Browse the repository at this point in the history
  • Loading branch information
butterunderflow committed Feb 8, 2024
1 parent 5577859 commit 5e039d0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
13 changes: 6 additions & 7 deletions lib/back/cprint1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) ' ')
Expand Down Expand Up @@ -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
13 changes: 13 additions & 0 deletions lib/test/back_test.ml
Original file line number Diff line number Diff line change
@@ -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 |}]
2 changes: 1 addition & 1 deletion lib/test/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(library
(name test)
(libraries sexplib syntax)
(libraries sexplib syntax back)
(inline_tests)
(wrapped false)
(preprocess
Expand Down

0 comments on commit 5e039d0

Please sign in to comment.