Commits

Antoine Pietri committed 1b13fc0

retrieved last data

Comments (0)

Files changed (4)

-let main () =
+let get_ast file =
   begin
-    let cin = open_in "ex01.bl" in
-    let ast =
+    let cin = open_in file in
       Parser.prg Lexer.token (Lexing.from_channel cin)
-    in
-    assert false;
-    exit 0;
   end
- 
-let _ = main ()
+
+let pretty_print file =
+  let ast = get_ast file in
+  print_string Prettyprint.sprint ast
+
+let eval file =
+  let ast = get_ast file in
+  (* TODO *)
+  assert false
+
+let specs =
+  [
+    ("-eval", Arg.String(eval), "interprets the given script");
+  ]
+
+let usage =
+  Printf.sprintf "Usage: %s -help | -eval script | script" Sys.argv.(0)
+
+let _ = Arg.parse specs String(pretty_print) usage

lexer.mll

-(* File lexer.mll *)
-{
-  open Parser        (* The type token is defined in parser.mli *)
-  exception Eof
-
-  let h = Hashtbl.create 29
-  let _ =
-    List.iter (fun (k,t) -> Hashtbl.add h k t)
-      [
-        ("+",ADD); ("*",MUL);
-        ("-",MIN); ("/",DIV);
-        ("<",LT); (">",GT);
-        ("<=",LEQ); (">=",GEQ);
-        ("==",CMPEQ);(",",COM);
-        ("!",NOT);("&&",AND);("||",OR);
-        ("{",LB); ("}",RB);
-        ("=",EQ); (";",SC);
-        ("(",LPAREN); (")",RPAREN);
-      ]
-
-  let keys = Hashtbl.create 13
-  let _ =
-    List.iter (fun (k,t) -> Hashtbl.add keys k t)
-      [
-        ("main",MAIN);
-        ("if",IF);
-        ("while",WHILE);
-        ("else",ELSE);
-        ("return",RETURN);
-        ("vars",VARS);
-      ]
-
-}
-let ident = ['a'-'z']['a'-'z' '_' '0'-'9']*
-
-let white = [' ' '\t' '\n']
-
-let num = '-'?['0'-'9']['0'-'9']*
-
-let op = "+" | "*" | "-" | "/" | "<" | ">" | ">=" | "<=" | "=="
-  | "," | "{" | "}" | "=" | ";" | "(" | ")" | "!" | "||" | "&&"
-
-rule token = parse
-    white+         { token lexbuf }     (* skip blanks *)
-  | num  as n      { INT (int_of_string n) }
-  | ident as ato   {
-    try Hashtbl.find keys ato with
-      | Not_found -> VAR ato
-  }
-  | op             { Hashtbl.find h (Lexing.lexeme lexbuf)}
-  | eof            { EOF }

parser.mly

-%token <string> VAR
-%token <int> INT
-%token ADD MUL DIV MIN LT GT LEQ GEQ CMPEQ NOT AND OR
-%token LPAREN RPAREN RB LB EQ SC COM
-%token MAIN IF WHILE ELSE RETURN VARS
-%token EOF
-%left LT GT LEQ GEQ CMPEQ
-%left ADD MIN AND OR
-%left MUL DIV
-%nonassoc UMIN NOT
-%start prg
-%type <Ast.prg> prg
-%%
-prg:
-fdecls main EOF                { {Ast.func = $1; Ast.main = $2} };
-
-expr:
-  VAR                   { Ast.Var $1 }
-  | INT                 { Ast.Int $1 }
-  | expr ADD expr       { Ast.BinOp($1,"+",$3) }
-  | expr MUL expr       { Ast.BinOp($1,"*",$3) }
-  | expr MIN expr       { Ast.BinOp($1,"-",$3) }
-  | expr DIV expr       { Ast.BinOp($1,"/",$3) }
-  | expr LT expr        { Ast.BinOp($1,"<",$3) }
-  | expr GT expr        { Ast.BinOp($1,">",$3) }
-  | expr LEQ expr       { Ast.BinOp($1,"<=",$3) }
-  | expr GEQ expr       { Ast.BinOp($1,">=",$3) }
-  | expr CMPEQ expr     { Ast.BinOp($1,"==",$3) }
-  | expr AND expr       { Ast.BinOp($1,"&&",$3) }
-  | expr OR expr        { Ast.BinOp($1,"||",$3) }
-  | MIN expr %prec UMIN { Ast.UniOp("-",$2) }
-  | NOT expr            { Ast.UniOp("!",$2) }
-  | VAR params
-      { Ast.Call ($1,$2) }
-  | LPAREN expr RPAREN  { $2 }
-;
-
-params:
-  | LPAREN RPAREN       { [] }
-  | LPAREN elist RPAREN { $2 }
-;
-
-elist:
-  | expr                { $1::[] }
-  | expr COM elist      { $1::$3 }
-;
-
-stm:
-  | VAR EQ expr SC      { Ast.Assign ($1,$3) }
-  | expr SC             { Ast.Expr $1 }
-  | IF LPAREN expr RPAREN stmblock
-      { Ast.If ($3, $5, []) }
-  | IF LPAREN expr RPAREN stmblock ELSE stmblock
-      { Ast.If ($3, $5, $7) }
-  | WHILE LPAREN expr RPAREN stmblock
-      { Ast.While ($3, $5) }
-  | RETURN expr SC      { Ast.Return $2 }
-
-stmblock:
-  | LB RB               { [] }
-  | LB stmlist RB       { $2 }
-;
-
-stmlist:
-  | stm                 { $1::[] }
-  | stm stmlist         { $1::$2 }
-;
-
-vars:
-  | VARS vlist SC       { $2 }
-;
-
-vlist:
-  | VAR                 { $1::[] }
-  | VAR COM vlist       { $1::$3 }
-;
-
-fparam:
-  | LPAREN RPAREN       { [] }
-  | LPAREN vlist RPAREN { $2 }
-;
-
-body:
-  | LB RB               { ([],[]) }
-  | LB stmlist RB       { ([],$2) }
-  | LB vars stmlist RB  { ($2,$3) }
-
-fdecl:
-  | VAR fparam body
-      {
-        let (v,b) = $3 in
-        {
-          Ast.fname   = $1;
-          Ast.fparams = $2;
-          Ast.fvars   = v;
-          Ast.fbody   = b;
-        }
-      }
-;
-
-fdecls:
-  |                     { [] }
-  | fdecl fdecls        { $1::$2 }
-;
-
-main:
-  | MAIN LPAREN RPAREN body
-      {
-        let (v,b) = $4 in
-        {
-          Ast.mainvars = v;
-          Ast.mainbody = b;
-        }
-      }
-;
 let print_vars = 
   assert false
 
+let print_functions =
+  assert false
+
 let sprint ast =
   print_functions ast.func;
   print_main ast.main;