feat: avancement TAM
Co-authored-by: gdamms <gdamms@users.noreply.github.com>
This commit is contained in:
parent
96955b19ba
commit
f635a09506
|
@ -4,43 +4,37 @@ struct
|
|||
|
||||
open Tds
|
||||
open Ast
|
||||
open AstType
|
||||
open AstPlacement
|
||||
open Type
|
||||
open Code
|
||||
|
||||
type t1 = Ast.AstPlacement.programme
|
||||
type t2 = string
|
||||
|
||||
let rec analyse_code_expression e =
|
||||
match e with
|
||||
| AstPlacement.AppelFonction(InfoFun(nom,_,_), le) ->
|
||||
begin
|
||||
(List.fold_right (fun e res_q -> res ^ analyse_code_expression e) le "") ^
|
||||
"CALL (ST)" ^
|
||||
nom ^
|
||||
"\n"
|
||||
end
|
||||
| AppelFonction(i, le) ->
|
||||
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"
|
||||
end
|
||||
|
||||
| AstPlacement.Ident(InfoIdent(_,t,base,reg)) ->
|
||||
"LOAD (" ^
|
||||
(getTaille t) ^
|
||||
") " ^
|
||||
(string_of_int base) ^
|
||||
"[" ^
|
||||
reg ^
|
||||
"]\n"
|
||||
| Ident(i) ->
|
||||
let InfoVar(_,t,base,reg) = info_ast_to_info i in
|
||||
"LOAD (" ^ (string_of_int (getTaille t)) ^ ") " ^ (string_of_int base) ^ "[" ^ reg ^ "]\n"
|
||||
|
||||
| AstPlacement.Booleen(b) ->
|
||||
| Booleen(b) ->
|
||||
if b then
|
||||
"LOADL 1\n"
|
||||
else
|
||||
"LOADL 0\n"
|
||||
|
||||
| AstPlacement.Entier(n) ->
|
||||
"LOADL" ^
|
||||
(string_of_int n) ^
|
||||
"\n"
|
||||
| Entier(n) ->
|
||||
"LOADL" ^ (string_of_int n) ^ "\n"
|
||||
|
||||
| AstPlacement.Unaire(u, e) ->
|
||||
| Unaire(u, e) ->
|
||||
(analyse_code_expression e) ^
|
||||
begin
|
||||
match u with
|
||||
|
@ -48,12 +42,12 @@ let rec analyse_code_expression e =
|
|||
| Denominateur -> "LOAD (1) -1[ST]\n"
|
||||
end
|
||||
|
||||
| AstPlacement.Binaire(b, e1, e2) ->
|
||||
| Binaire(b, e1, e2) ->
|
||||
(analyse_code_expression e1) ^
|
||||
(analyse_code_expression e2) ^
|
||||
begin
|
||||
match b with
|
||||
| Fraction -> ""
|
||||
| Fraction -> "CALL (ST) norm\n"
|
||||
| PlusInt -> "SUBR IAdd\n"
|
||||
| MultInt -> "SUBR IMul\n"
|
||||
| EquInt -> "SUBR IEq\n"
|
||||
|
@ -64,31 +58,63 @@ let rec analyse_code_expression e =
|
|||
end
|
||||
|
||||
|
||||
and analyse_code_instruction i typefun =
|
||||
and analyse_code_instruction i =
|
||||
match i with
|
||||
| AstPlacement.Declaration ->
|
||||
| Declaration(i, e) ->
|
||||
analyse_code_expression e
|
||||
|
||||
| AstPlacement.Affectation ->
|
||||
| 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"
|
||||
|
||||
| AstPlacement.AffichageInt ->
|
||||
| AffichageInt(e) ->
|
||||
(analyse_code_expression e) ^
|
||||
"SUBR IOut\n"
|
||||
|
||||
| AffichageRat(e) ->
|
||||
(analyse_code_expression e) ^
|
||||
"CALL (ST) rout\n"
|
||||
|
||||
| AstPlacement.AffichageRat ->
|
||||
| AffichageBool(e) ->
|
||||
(analyse_code_expression e) ^
|
||||
"SUBR BOut\n"
|
||||
|
||||
| AstPlacement.AffichageBool ->
|
||||
| Conditionnelle(e, b1, b2) ->
|
||||
let etiq1 = getEtiquette () in
|
||||
let etiq2 = getEtiquette () in
|
||||
(analyse_code_expression e) ^
|
||||
"JUMPIF (0) " ^ etiq1 ^ "[ST]\n" ^
|
||||
(analyse_code_bloc b1) ^
|
||||
"JUMP " ^ etiq2 ^ "[ST]\n" ^
|
||||
etiq1 ^ "\n" ^
|
||||
(analyse_code_bloc b2) ^
|
||||
etiq2 ^ "\n"
|
||||
|
||||
| AstPlacement.Conditionnelle ->
|
||||
| TantQue(e, b) ->
|
||||
let etiq_while = getEtiquette () in
|
||||
let etiq_fin = getEtiquette () in
|
||||
etiq_while ^
|
||||
(analyse_code_expression e) ^
|
||||
"JUMPIF (0) " ^ etiq_fin ^ "[ST]\n" ^
|
||||
(analyse_code_bloc b) ^
|
||||
"JUMP " ^ etiq_while ^ "[ST]\n" ^
|
||||
etiq_fin ^ "\n"
|
||||
|
||||
| AstPlacement.TantQue ->
|
||||
(* | Retour(e) ->
|
||||
(analyse_code_expression e) ^
|
||||
"RETURN (" ^ r? ^ ") p\n" *)
|
||||
|
||||
| AstPlacement.Retour ->
|
||||
|
||||
and analyse_code_bloc base reg bloc = failwith "TODO"
|
||||
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"
|
||||
|
||||
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 = getEntete () 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
|
||||
|
|
|
@ -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 |}]
|
||||
|
@ -108,3 +108,4 @@ let%expect_test "factfuns" =
|
|||
let%expect_test "factrec" =
|
||||
runtam "../../fichiersRat/src-rat-tam-test/factrec.rat";
|
||||
[%expect{| 120 |}]
|
||||
*)
|
Loading…
Reference in a new issue