Skip to content
Snippets Groups Projects

Master

Merged Sellami Youssef requested to merge master into main
10 files
+ 218
143
Compare changes
  • Side-by-side
  • Inline
Files
10
+ 13
6
open Batteries
open Prog
(* Les AST sont des arbres, du type [tree], étiquetés par des [tag].
@@ -22,14 +23,14 @@ open Batteries
*)
type tag = Tassign | Tif | Twhile | Tblock | Treturn
| Tint
type tag = Tassign | Tif | Twhile | Tblock | Treturn | Tdeclare
| Tint | Tchar | Tvoid
| 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
type tree = | Node of tag * tree list
@@ -37,7 +38,7 @@ type tree = | Node of tag * tree list
| IntLeaf of int
| NullLeaf
| CharLeaf of char
| TypeLeaf of typ
let string_of_stringleaf = function
| StringLeaf s -> s
| _ -> failwith "string_of_stringleaf called on non-stringleaf nodes."
@@ -74,6 +75,10 @@ let string_of_tag = function
| Targ -> "Targ"
| Tcall -> "Tcall"
| Targs -> "Targs"
| Tdeclare -> "Tdeclare"
| Tfuntype -> "Tfuntype"
| Tchar -> "Tchar"
| Tvoid -> "Tvoid"
(* Écrit un fichier .dot qui correspond à un AST *)
let rec draw_ast a next =
@@ -100,7 +105,8 @@ 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)])
let draw_ast_tree oc ast =
let (_, _, s) = draw_ast ast 1 in
let s = String.concat "" s in
@@ -115,3 +121,4 @@ 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
\ No newline at end of file
Loading