feat: fin de TP de métaprog xD

Co-authored-by: gdamms <gdamms@users.noreply.github.com>
This commit is contained in:
Laurent Fainsin 2021-12-10 10:17:56 +01:00
parent f635a09506
commit 0c5225da77
6 changed files with 223 additions and 16 deletions

BIN
src/itam.jar Normal file

Binary file not shown.

85
src/out.tam Normal file
View file

@ -0,0 +1,85 @@
; fichiersRat/src-rat-tam-test/testfun2.rat
JUMP main
pgcd
LOADL 0
LOAD (1) -2[LB]
LOAD (1) -1[LB]
boucle
LOAD (1) 5[LB]
JUMPIF (0) fin
LOAD (1) 4[LB]
LOAD (1) 5 [LB]
SUBR IMod
STORE (1) 3[LB]
LOAD (1) 5[LB]
STORE (1) 4[LB]
LOAD (1) 3[LB]
STORE(1) 5[LB]
JUMP boucle
fin
LOAD (1) 4[LB]
RETURN (1) 2
norm
LOAD (1) -2[LB]
LOAD (1) -1[LB]
CALL (LB) pgcd
LOAD (1) -2[LB]
LOAD (1) 3[LB]
SUBR IDiv
LOAD (1) -1[LB]
LOAD (1) 3[LB]
SUBR IDiv
RETURN (2) 2
ROut
LOADL '['
SUBR COut
LOAD (1) -2[LB]
SUBR IOut
LOADL '/'
SUBR COut
LOAD (1) -1[LB]
SUBR IOut
LOADL ']'
SUBR COut
RETURN (0) 2
RAdd
LOAD (1) -4[LB]
LOAD (1) -1[LB]
SUBR IMul
LOAD (1) -2[LB]
LOAD (1) -3[LB]
SUBR IMul
SUBR IAdd
LOAD (1) -3[LB]
LOAD (1) -1[LB]
SUBR IMul
CALL (ST) norm
RETURN (2) 4
RMul
LOAD (1) -4[LB]
LOAD (1) -2[LB]
SUBR IMul
LOAD (1) -3[LB]
LOAD (1) -1[LB]
SUBR IMul
CALL (ST) norm
RETURN (2) 4
f
LOAD (1) -1[LB]
POP (1) 1
RETURN (1) 1
main
LOADL 3
LOADL 4
SUBR IAdd
CALL (ST) f
SUBR IOut
HALT

113
src/out_test.tam Normal file
View file

@ -0,0 +1,113 @@
JUMP main
pgcd
LOADL 0
LOAD (1) -2[LB]
LOAD (1) -1[LB]
boucle
LOAD (1) 5[LB]
JUMPIF (0) fin
LOAD (1) 4[LB]
LOAD (1) 5 [LB]
SUBR IMod
STORE (1) 3[LB]
LOAD (1) 5[LB]
STORE (1) 4[LB]
LOAD (1) 3[LB]
STORE(1) 5[LB]
JUMP boucle
fin
LOAD (1) 4[LB]
RETURN (1) 2
norm
LOAD (1) -2[LB]
LOAD (1) -1[LB]
CALL (LB) pgcd
LOAD (1) -2[LB]
LOAD (1) 3[LB]
SUBR IDiv
LOAD (1) -1[LB]
LOAD (1) 3[LB]
SUBR IDiv
RETURN (2) 2
ROut
LOADL '['
SUBR COut
LOAD (1) -2[LB]
SUBR IOut
LOADL '/'
SUBR COut
LOAD (1) -1[LB]
SUBR IOut
LOADL ']'
SUBR COut
RETURN (0) 2
RAdd
LOAD (1) -4[LB]
LOAD (1) -1[LB]
SUBR IMul
LOAD (1) -2[LB]
LOAD (1) -3[LB]
SUBR IMul
SUBR IAdd
LOAD (1) -3[LB]
LOAD (1) -1[LB]
SUBR IMul
CALL (ST) norm
RETURN (2) 4
RMul
LOAD (1) -4[LB]
LOAD (1) -2[LB]
SUBR IMul
LOAD (1) -3[LB]
LOAD (1) -1[LB]
SUBR IMul
CALL (ST) norm
RETURN (2) 4
add
LOADL 0
LOADL 0
LOAD (1) -2[LB]
SUBR IAdd
STORE (1) 3[LB]
LOAD (1) -2[LB]
LOAD (1) -1[LB]
LOAD (1) 3[LB]
SUBR IAdd
SUBR IAdd
POP (1) 2
RETURN (1) 2
main
LOADL 3
LOADL 4
STORE (1) 0[SB]
LOAD (1) 0[SB]
LOADL 5
SUBR IAdd
LOAD (1) 0[SB]
LOADL 0
SUBR ILss
JUMPIF (0) label1
LOADL 1
LOADL 0
STORE (1) 2[SB]
JUMP label2
label1
LOADL 4
LOADL 3
CALL (ST) norm
LOADL 5
LOAD (2) 2[SB]
LOAD (1) -2[ST]
CALL (ST) add
LOAD (2) 2[SB]
CALL (ST) rout
label2
HALT

View file

@ -18,7 +18,7 @@ let rec analyse_code_expression e =
begin
let InfoFun(nom,_,_) = info_ast_to_info i in
(List.fold_right (fun e res -> res ^ analyse_code_expression e) le "") ^
"CALL (ST)" ^nom ^ "\n"
"CALL (ST) " ^nom ^ "\n"
end
| Ident(i) ->
@ -32,7 +32,7 @@ let rec analyse_code_expression e =
"LOADL 0\n"
| Entier(n) ->
"LOADL" ^ (string_of_int n) ^ "\n"
"LOADL " ^ (string_of_int n) ^ "\n"
| Unaire(u, e) ->
(analyse_code_expression e) ^
@ -66,7 +66,7 @@ and analyse_code_instruction i =
| Affectation(i, e) ->
let InfoVar(_,t,base,reg) = (info_ast_to_info i) in
analyse_code_expression e ^
"STORE (" ^ (string_of_int (getTaille t)) ^ ")" ^ (string_of_int base) ^ "[" ^ reg ^ "]\n"
"STORE (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n"
| AffichageInt(e) ->
(analyse_code_expression e) ^
@ -84,9 +84,9 @@ and analyse_code_instruction i =
let etiq1 = getEtiquette () in
let etiq2 = getEtiquette () in
(analyse_code_expression e) ^
"JUMPIF (0) " ^ etiq1 ^ "[ST]\n" ^
"JUMPIF (0) " ^ etiq1 ^ "\n" ^
(analyse_code_bloc b1) ^
"JUMP " ^ etiq2 ^ "[ST]\n" ^
"JUMP " ^ etiq2 ^ "\n" ^
etiq1 ^ "\n" ^
(analyse_code_bloc b2) ^
etiq2 ^ "\n"
@ -94,28 +94,37 @@ and analyse_code_instruction i =
| TantQue(e, b) ->
let etiq_while = getEtiquette () in
let etiq_fin = getEtiquette () in
etiq_while ^
etiq_while ^ "\n" ^
(analyse_code_expression e) ^
"JUMPIF (0) " ^ etiq_fin ^ "[ST]\n" ^
"JUMPIF (0) " ^ etiq_fin ^ "\n" ^
(analyse_code_bloc b) ^
"JUMP " ^ etiq_while ^ "[ST]\n" ^
"JUMP " ^ etiq_while ^ "\n" ^
etiq_fin ^ "\n"
(* | Retour(e) ->
(analyse_code_expression e) ^
"RETURN (" ^ r? ^ ") p\n" *)
| Retour(e) ->
(analyse_code_expression e)
| Empty -> ""
and analyse_code_bloc bloc =
List.fold_right (
fun i res -> (analyse_code_instruction i) ^ res
) bloc ""
and analyse_code_fonction (AstPlacement.Fonction(info, l_typinfo, bloc)) = failwith "TODO"
and analyse_code_fonction (AstPlacement.Fonction(info, l_typinfo, bloc)) =
match info_ast_to_info info with
| InfoFun(name, t, l_t) ->
let taille_return = string_of_int (getTaille t) in
let taille_args = string_of_int (List.fold_right (fun e res -> getTaille e + res) l_t 0) in
name ^ "\n" ^
(analyse_code_bloc bloc) ^
"POP (" ^ taille_return ^ ") " ^ taille_args ^ "\n" ^
"RETURN (" ^ taille_return ^ ") " ^ taille_args ^ "\n\n"
let analyser (AstPlacement.Programme(fonctions, prog)) =
let code = getEntete () in
(* let code = code ^ List.fold_right (fun e res_q -> (analyse_code_fonction e) ^ res_q) fonctions "" in *)
let code = code ^ List.fold_right (fun e res_q -> (analyse_code_fonction e) ^ res_q) fonctions "" in
let code = code ^ "main\n" in
let code = code ^ analyse_code_bloc prog in
code
code ^ "\nHALT"
end

BIN
src/runtam.jar Normal file

Binary file not shown.

View file

@ -24,7 +24,7 @@ let runtam ratfile =
let%expect_test "testprintint" =
runtam "../../fichiersRat/src-rat-tam-test/testprintint.rat";
[%expect{| 42 |}]
(*
let%expect_test "testprintbool" =
runtam "../../fichiersRat/src-rat-tam-test/testprintbool.rat";
[%expect{| true |}]
@ -84,7 +84,7 @@ let%expect_test "factfun1" =
let%expect_test "factfun2" =
runtam "../../fichiersRat/src-rat-tam-test/testfun2.rat";
[%expect{| 7 |}]
(*
let%expect_test "factfun3" =
runtam "../../fichiersRat/src-rat-tam-test/testfun3.rat";
[%expect{| 10 |}]