diff options
author | Alex AUVOLAT <alex.auvolat@ens.fr> | 2013-11-16 17:05:12 +0100 |
---|---|---|
committer | Alex AUVOLAT <alex.auvolat@ens.fr> | 2013-11-16 17:05:12 +0100 |
commit | f20d043f9d9ace0ed6cd1359c8308c0eb39e0919 (patch) | |
tree | 633b0a91780415e2cf9898e58c9cdf44d2a62e40 /src/pretty.ml | |
parent | ec7d5bcc03e4bad22b6cc790aa69cb310ec452e4 (diff) | |
download | LPC-Projet-f20d043f9d9ace0ed6cd1359c8308c0eb39e0919.tar.gz LPC-Projet-f20d043f9d9ace0ed6cd1359c8308c0eb39e0919.zip |
Finished parser (all tests pass!)
Diffstat (limited to 'src/pretty.ml')
-rw-r--r-- | src/pretty.ml | 49 |
1 files changed, 41 insertions, 8 deletions
diff --git a/src/pretty.ml b/src/pretty.ml index 8553c81..c3d12f3 100644 --- a/src/pretty.ml +++ b/src/pretty.ml @@ -1,3 +1,10 @@ +(* + PRETTY PRINTER + These functions enable the dumping of an AST + Used for debugging the parser. +*) + + open Parser open Ast @@ -61,6 +68,9 @@ let token_str = function let print_tok t = print_string ((token_str t) ^ "\n") +let csl f l = + List.fold_left (fun x t -> (if x = "" then "" else x ^ ", ") ^ (f t)) "" l + (* printing AST's *) let binop_str = function @@ -78,11 +88,14 @@ let rec expr_string = function | EInt(i) -> string_of_int i | EBool(b) -> (if b then "true" else "false") | ENull -> "NULL" + | EThis -> "this" | EIdent(i) -> i | EAssign(k, p) -> "(" ^ (expr_string k) ^ " = " ^ (expr_string p) ^ ")" - | ECall(e, f) -> (expr_string e) ^ "(" ^ (List.fold_left (fun x k -> x ^ ", " ^ (expr_string k)) "" f) ^ ")" + | ECall(e, f) -> (expr_string e) ^ "(" ^ (csl expr_string f) ^ ")" | EUnary(e, f) -> (unop_str e) ^ (expr_string f) | EBinary(e1, o, e2) -> "(" ^ (expr_string e1) ^ " " ^ (binop_str o) ^ " " ^ (expr_string e2) ^ ")" + | EMember(e1, x) -> "(" ^ (expr_string e1) ^ ")." ^ x + | ENew(c, arg) -> "new " ^ c ^ " (" ^ (csl expr_string arg) ^ ")" let rec print_stmt l x = for i = 1 to l do print_string " " done; @@ -104,9 +117,15 @@ let rec print_stmt l x = print_stmt (l+1) s | SBlock(b) -> print_block l b | SReturn(None) -> print_string "return\n" - | SReturn(Some k) -> print_string ("return" ^ (expr_string k) ^ "\n") - | SDeclare(i, t, None) -> print_string (i ^ " : " ^ (var_type_str t) ^ "\n") - | SDeclare(i, t, Some e) -> print_string (i ^ " : " ^ (var_type_str t) ^ " = " ^ (expr_string e) ^ "\n") + | SReturn(Some k) -> print_string ("return " ^ (expr_string k) ^ "\n") + | SDeclare(t, i) -> print_string (i ^ " : " ^ (var_type_str t) ^ "\n") + | SDeclareAssignExpr(t, i, e) -> print_string (i ^ " : " ^ (var_type_str t) ^ " = " ^ (expr_string e) ^ "\n") + | SDeclareAssignConstructor(t, i, c, a) -> print_string + (i ^ " : " ^ (var_type_str t) ^ " = " ^ c ^ "(" ^ + (csl expr_string a) ^ ")\n") + | SWriteCout(k) -> print_string ("std::cout" ^ + (List.fold_left (fun x k -> x ^ " << " ^ (match k with + | SEExpr(k) -> expr_string k | SEStr("\n") -> "std::endl" | SEStr(s) -> "`" ^ s ^ "`")) "" k) ^ "\n") and print_block n b = let prefix = String.make n ' ' in print_string (prefix ^ "{\n"); @@ -116,12 +135,26 @@ and print_block n b = print_string (prefix ^ "}\n") let proto_str p = - p.p_name ^ " (" ^ (List.fold_left (fun x (i, t) -> x ^ ", " ^ i ^ " : " ^ (var_type_str t)) "" p.p_args) - ^ ") : " ^ (var_type_str p.p_ret_type) + (match p.p_class with | Some c -> c ^ "::" | None -> "") ^ p.p_name + ^ " (" ^ (csl (fun (t, i) -> i ^ " : " ^ (var_type_str t)) p.p_args) + ^ ") : " ^ (match p.p_ret_type with | Some k -> var_type_str k | None -> "constructor") + +let print_class_decl c = + print_string ("class " ^ c.c_name ^ + (match c.c_supers with | None -> "" | Some(s) -> " : " ^ + (List.fold_left (fun x t -> x ^ " public " ^ t) "" s)) ^ " {\n"); + List.iter (function + | CVar(t, i) -> print_string (" " ^ i ^ " : " ^ (var_type_str t) ^ "\n") + | CMethod(p) -> print_string (" " ^ (proto_str p) ^ "\n") + | CVirtualMethod(p) -> print_string (" virtual " ^ (proto_str p) ^ "\n") + ) c.c_members; + print_string "}\n" let print_prog p = List.iter (function - | DGlobal(i, t) -> print_string ("decl " ^ i ^ " : " ^ (var_type_str t) ^ "\n") + | DGlobal(t, i) -> print_string ("decl " ^ i ^ " : " ^ (var_type_str t) ^ "\n") | DFunction(p, b) -> print_string (proto_str p ^"\n"); - print_block 0 b) p + print_block 0 b + | DClass(c) -> print_class_decl c + ) p |