Skip to content
Snippets Groups Projects

Master

Merged Sellami Youssef requested to merge master into main
28 files
+ 543
299
Compare changes
  • Side-by-side
  • Inline
Files
28
+ 23
8
open Batteries
open Prog
(* Les AST sont des arbres, du type [tree], étiquetés par des [tag].
@@ -22,26 +23,34 @@ open Batteries
*)
type tag = Tassign | Tif | Twhile | Tblock | Treturn | Tprint
| Tint
type tag = Tassign | Tif | Twhile | Tblock | Treturn | Tdeclare
| Tadd | Tmul | Tdiv | Tmod | Txor | Tsub
| Tclt | Tcgt | Tcle | Tcge | Tceq | Tne
| Tneg
| Tlistglobdef
| Tfundef | Tfunname | Tfunargs | Tfunbody | Tcall
| Tassignvar
| Tfundef | Tfuntype | Tfunname | Tfunargs | Tfunbody | Tcall
| Tassignvar (*never used*)
| Targ | Targs
| Tmem
type mem_op = | Asterik
| Ampersand
type tree = | Node of tag * tree list
| StringLeaf of string
| IntLeaf of int
| NullLeaf
| CharLeaf of char
| TypeLeaf of typ
| MemopsLeaf of mem_op list
let string_of_stringleaf = function
| StringLeaf s -> s
| _ -> failwith "string_of_stringleaf called on non-stringleaf nodes."
let string_of_memop = function
| Asterik -> "*"
| Ampersand -> "&"
type astfun = (string list * tree)
type ast = (string * astfun) list
@@ -51,8 +60,6 @@ let string_of_tag = function
| Twhile -> "Twhile"
| Tblock -> "Tblock"
| Treturn -> "Treturn"
| Tprint -> "Tprint"
| Tint -> "Tint"
| Tadd -> "Tadd"
| Tmul -> "Tmul"
| Tdiv -> "Tdiv"
@@ -75,6 +82,9 @@ let string_of_tag = function
| Targ -> "Targ"
| Tcall -> "Tcall"
| Targs -> "Targs"
| Tdeclare -> "Tdeclare"
| Tfuntype -> "Tfuntype"
| Tmem -> "Tmem"
(* Écrit un fichier .dot qui correspond à un AST *)
let rec draw_ast a next =
@@ -92,7 +102,6 @@ let rec draw_ast a next =
] @ List.map (fun n ->
Format.sprintf "n%d -> n%d\n" next n
)nodes)
| StringLeaf s ->
(next, next+1, [ Format.sprintf "n%d [label=\"%s\"]\n" next s])
| IntLeaf i ->
@@ -101,6 +110,10 @@ let rec draw_ast a next =
(next, next+1, [ Format.sprintf "n%d [label=\"null\"]\n" next])
| CharLeaf i ->
(next, next+1, [ Format.sprintf "n%d [label=\"%c\"]\n" next i])
| TypeLeaf t ->
(next, next+1, [ Format.sprintf "n%d [label=\"%s\"]\n" next (string_of_typ t)])
| MemopsLeaf memops ->
(next, next+1, [ Format.sprintf "n%d [label=\"%s\"]\n" next (String.concat "" (List.map string_of_memop memops))])
let draw_ast_tree oc ast =
let (_, _, s) = draw_ast ast 1 in
@@ -116,3 +129,5 @@ let rec string_of_ast a =
| IntLeaf i -> Format.sprintf "%d" i
| CharLeaf i -> Format.sprintf "%c" i
| NullLeaf -> "null"
| TypeLeaf t -> string_of_typ t
| MemopsLeaf memops -> String.concat "" (List.map string_of_memop memops)
\ No newline at end of file
Loading