Commits

Corentin Derbois  committed 4c5af4c

Compiler OK

  • Participants
  • Parent commits 3cd4baf

Comments (0)

Files changed (4)

File src/Makefile

 SRCSYACC		=	parser.mly
 
 SRCS				= $(SRCSLEX:.mll=.ml) $(SRCSYACC:.mly=.ml)
-SRCS				+= option.ml main.ml
+SRCS				+= wrapper.ml compiler.ml option.ml main.ml
 
 OBJS				= $(SRCS:.ml=.cmx)
 

File src/compiler.ml

-class compiler file = 
-object(this)
+class compiler = 
+object(this) 
 
-  
+  method compile (file:string) =
+    let ast = this#makeAst file in
+      print_endline ("AST " ^ file)
+
+  method makeAst (file:string) =
+    let file_in = open_in file in
+        let wrapper = new Wrapper.wrapper in
+            wrapper#from_channel file_in ()
+
+  method run (files:string list) =
+    List.iter this#compile files
 
 end
+
+let compiler =
+  let a = new compiler in
+    a
 let main () =
   print_endline "SmallCC, oh yeah";
   if (Array.length Sys.argv) == 1 then usage ();
-  Option.option#parse ()
+  Option.option#parse ();
+  Compiler.compiler# run Option.option#getFiles
   
 
 let _ = main ()

File src/wrapper.ml

+exception ParseError of exn * (int * int * string)
+
+class wrapper =
+object(this)
+
+  method from_lexbuf lexbuf () =
+    try
+      Parser.translation_unit Lexer.token lexbuf
+    with
+      exn ->
+        let lex = lexbuf.Lexing.lex_curr_p in
+          raise (ParseError (exn,
+                            (lex.Lexing.pos_lnum,
+                             lex.Lexing.pos_cnum - lex.Lexing.pos_bol,
+                             Lexing.lexeme lexbuf)))
+
+  method wrappe lexbuf () = 
+    try
+      this#from_lexbuf lexbuf ()
+    with
+      ParseError(exn, (line, col, tok)) -> 
+        Printf.printf "Error line:%d col:%d token:\"%s\"\n" line col tok;
+        raise exn
+
+  method from_channel fdin () = 
+    let lexbuf = Lexing.from_channel fdin in
+      this#wrappe lexbuf ()
+
+end