1. Corentin Derbois
  2. SmallCC

Commits

Corentin Derbois  committed 71bb5c0

Make class of visitor.

  • Participants
  • Parent commits 9ce187a
  • Branches develop

Comments (0)

Files changed (2)

File src/compiler.ml

View file
  • Ignore whitespace
 
   method compile (file:string) =
     let ast = this#makeAst file in
-      print_endline ("AST " ^ file)
+      let pp = new Visitor.prettyPrintVisitor in
+        pp#visitTranslationUnit ast;
+        print_endline ("AST " ^ file)
 
   method makeAst (file:string) =
     let file_in = open_in file in

File src/visitor.ml

View file
  • Ignore whitespace
-(*This class will be the mother of all other visitor *)
+(* This help to manage param *)
+
+type 'a value = None | Some of 'a
+
+exception NoValue
+exception NotImplemented of string
+
+let getValue v = match v with
+    Some a  -> a
+  | None    -> raise NoValue
+
+(* Begin of the visitor class*)
 
 class visitor =
-object(this)
+  object(this)
+
+    (* Complet this accept *)
+  method acceptFunctionDefinition decspe dec dec_list cs =
+    ();
+    if (decspe <> None) then ();
+    if (dec_list <> None) then ();
+    ()
+
+  method acceptTranslationUnit tu ed =
+    if (tu <> None) then this#visitTranslationUnit (getValue tu);
+    this#visitExternalDeclaration (getValue ed)
+
+  method visitFunctionDefinition ast = match ast with
+  	  Ast.FUN_spec_dec_list(decspe, dec, dec_list, cs)
+      -> this#acceptFunctionDefinition (Some decspe) (Some dec) (Some dec_list) (Some cs)
+	  | Ast.FUN_spec_dec(decspe, dec, cs)
+      -> this#acceptFunctionDefinition (Some decspe) (Some dec) None (Some cs)
+	  | Ast.FUN_dec_list(dec, dec_list, cs)
+      -> this#acceptFunctionDefinition None (Some dec) (Some dec_list) (Some cs)
+	  | Ast.FUN_dec(dec, cs)
+      -> this#acceptFunctionDefinition None (Some dec) None (Some cs)
+
+
+    (* Here i visite directly, but why not? *)
+  method visitExternalDeclaration ast = match ast with
+	    Ast.ED_fun_def(fd)  -> this#visitFunctionDefinition fd
+	  | Ast.ED_dec(dec)     -> raise (NotImplemented "ED_dec")
+
+
+  method visitTranslationUnit ast = match ast with
+	    Ast.TU_dec(ed)      -> this#acceptTranslationUnit None (Some ed) 
+	  | Ast.TU_tans(tu, ed) -> this#acceptTranslationUnit (Some tu) (Some ed)
+	  | Ast.None            -> ()
+	
+  end
+
+class prettyPrintVisitor =
+  object(this)
+    
+    inherit visitor as old
 
+    method acceptFunctionDefinition a b c d =
+      print_endline "Function"
 
-end
+  end