Fix main, added extra functionality, added extra tests in main. TODO: add real unit tests
This commit is contained in:
121
bin/main.ml
121
bin/main.ml
@ -15,16 +15,125 @@ then print_endline "true"
|
||||
else print_endline "false"
|
||||
|
||||
*)
|
||||
|
||||
open Lang.MiniImp
|
||||
|
||||
let program1 =
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Identity program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
"b",
|
||||
(Sequence
|
||||
((Assignment "x" (Integer 1)),
|
||||
(Assignment "b" (Plus (Plus (Variable "a") (Variable "x")) (Variable "y"))))
|
||||
(Assignment ("b", (Variable "a")))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce 100 program)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* y not defined program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
"b",
|
||||
(Sequence (
|
||||
(Assignment ("x", (Integer 1))),
|
||||
(Assignment ("b",
|
||||
(Plus ((Plus (Variable "a", Variable "x")), (Variable "y")))))
|
||||
)
|
||||
)
|
||||
)
|
||||
Printf.printf "%d\n" (result program1 1)
|
||||
;;
|
||||
|
||||
try
|
||||
Printf.printf "%d\n" (reduce 100 program)
|
||||
with AbsentAssignment s ->
|
||||
Printf.printf "%s\n" s
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* factorial program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
"b",
|
||||
(Sequence (
|
||||
(Assignment ("b", (Integer 1))),
|
||||
(For (
|
||||
(Assignment ("i", (Integer 1))),
|
||||
(BCmpLessEq (Variable "i", Variable "a")),
|
||||
(Assignment ("i", (Plus ((Variable "i"), (Integer 1))))),
|
||||
(Assignment ("b", (Times (Variable "b", Variable "i"))))
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce 10 program)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* hailstone sequence lenght program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
"b",
|
||||
(Sequence (
|
||||
(Assignment ("b", (Integer 1))),
|
||||
(While (
|
||||
(BNot (BCmp ((Variable "a"), (Integer 1)))),
|
||||
(Sequence (
|
||||
(Assignment ("b", (Plus (Variable "b", Integer 1)))),
|
||||
(If (
|
||||
(BCmp (Modulo (Variable "a", Integer 2), Integer 1)),
|
||||
(Assignment ("a", Plus (Times (Integer 3, Variable "a"), Integer 1))),
|
||||
(Assignment ("a", Division (Variable "a", Integer 2)))
|
||||
))
|
||||
))
|
||||
))
|
||||
))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce 77031 program)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* sum multiples of 3 and 5 program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
"b",
|
||||
(Sequence (
|
||||
(Assignment ("b", (Integer 0))),
|
||||
(For (
|
||||
(Assignment ("i", Integer 0)),
|
||||
(BCmpLess (Variable "i", Variable "a")),
|
||||
(Assignment ("i", Plus (Variable "i", Integer 1))),
|
||||
(If (
|
||||
(BOr ((BCmp (Modulo (Variable "i", Integer 3), Integer 0)),
|
||||
(BCmp (Modulo (Variable "i", Integer 5), Integer 0)))),
|
||||
(Assignment ("b", Plus (Variable "b", Variable "i"))),
|
||||
(Skip)
|
||||
))
|
||||
))
|
||||
))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce 12345 program)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* rand program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
"b",
|
||||
(Assignment ("b", Rand (Variable "a")))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce 10 program)
|
||||
;;
|
||||
|
||||
Reference in New Issue
Block a user