Semantics for RISC code
This commit is contained in:
44
bin/main.ml
44
bin/main.ml
@ -1,35 +1,37 @@
|
||||
open MiniImp
|
||||
|
||||
let () =
|
||||
(* let program = "def main with input x output y as *)
|
||||
(* x := 2; *)
|
||||
(* if y < 0 then ( *)
|
||||
(* y := x + 3; *)
|
||||
(* x := y; *)
|
||||
(* ) else *)
|
||||
(* x := 1 - y;" in *)
|
||||
let colorred s =
|
||||
"\027[31m" ^ s ^ "\027[0m"
|
||||
|
||||
let program = "def main with input x output y as
|
||||
x := 2;
|
||||
if y < 0 then (
|
||||
y := x + 3;
|
||||
if x > 0 then
|
||||
x := y;
|
||||
else
|
||||
x := y + 1;
|
||||
) else
|
||||
x := 1 - y;" in
|
||||
let () =
|
||||
let program = "
|
||||
def main with input a output b as
|
||||
b := 1;
|
||||
for (i := 1, i <= a, i := i + 1) do
|
||||
b := b * i;
|
||||
"
|
||||
in
|
||||
|
||||
Printf.printf "%s\n%s\n" (colorred "Program is") program;
|
||||
|
||||
let get_result x = Lexing.from_string x |> Parser.prg Lexer.lex in
|
||||
|
||||
let p = get_result program in
|
||||
|
||||
let convertedcfg = CfgImp.convert_io p 3 in
|
||||
Format.printf "%s\n%a\n@?" (colorred "AST is") Types.pp_p_exp p;
|
||||
|
||||
Printf.printf "%a" CfgImp.SSCfg.pp convertedcfg;
|
||||
let convertedcfg = CfgImp.convert_io 10 p in
|
||||
|
||||
Printf.printf "%s\n%a" (colorred "Converted CFG is") CfgImp.SSCfg.pp convertedcfg;
|
||||
|
||||
let convertedrisccfg = CfgRISC.convert convertedcfg in
|
||||
|
||||
Printf.printf "%s\n%a" (colorred "Converted RISC CFG is") CfgRISC.RISCCfg.pp convertedrisccfg;
|
||||
|
||||
let risc = RISC.convert convertedrisccfg in
|
||||
|
||||
Printf.printf "%a" RISC.RISCAssembly.pp risc
|
||||
Printf.printf "%s\n%a" (colorred "RISC code is") RISC.RISCAssembly.pp risc;
|
||||
|
||||
let computerisc = RISCSemantics.reduce risc in
|
||||
|
||||
Printf.printf "%s\n%d\n" (colorred "Output of RISC code is") computerisc;
|
||||
|
||||
Reference in New Issue
Block a user