Skip to content
Snippets Groups Projects
Commit d9659a6a authored by Putegnat Theo's avatar Putegnat Theo
Browse files

TP3 premiere partie

parent b23ce229
No related branches found
No related tags found
No related merge requests found
...@@ -77,7 +77,7 @@ ADD_EXPRS -> SYM_MINUS ADD_EXPR ADD_EXPRS { (Tsub,$2) :: $3 } ...@@ -77,7 +77,7 @@ ADD_EXPRS -> SYM_MINUS ADD_EXPR ADD_EXPRS { (Tsub,$2) :: $3 }
ADD_EXPRS-> {[]} ADD_EXPRS-> {[]}
EQ_EXPRS -> SYM_EQUALITY MUL_EXPR MUL_EXPRS { (Tceq,$2) :: $3 } EQ_EXPRS -> SYM_EQUALITY MUL_EXPR MUL_EXPRS { (Tceq,$2) :: $3 }
EQ_EXPRS -> SYM_NOTEQ MUL_EXPR MUL_EXPRS { (Tneg,$2) :: $3 } EQ_EXPRS -> SYM_NOTEQ MUL_EXPR MUL_EXPRS { (Tne,$2) :: $3 }
EQ_EXPRS -> {[]} EQ_EXPRS -> {[]}
CMP_EXPRS -> SYM_GEQ MUL_EXPR MUL_EXPRS { (Tcge,$2) :: $3 } CMP_EXPRS -> SYM_GEQ MUL_EXPR MUL_EXPRS { (Tcge,$2) :: $3 }
...@@ -87,7 +87,7 @@ CMP_EXPRS -> SYM_LEQ MUL_EXPR MUL_EXPRS { (Tcle,$2) :: $3 } ...@@ -87,7 +87,7 @@ CMP_EXPRS -> SYM_LEQ MUL_EXPR MUL_EXPRS { (Tcle,$2) :: $3 }
CMP_EXPRS -> {[]} CMP_EXPRS -> {[]}
INTEGER -> SYM_INTEGER {IntLeaf ($1)} INTEGER -> SYM_INTEGER {Node (Tint, [IntLeaf($1)])}
IDENTIFIER -> SYM_IDENTIFIER {StringLeaf ($1)} IDENTIFIER -> SYM_IDENTIFIER {StringLeaf ($1)}
......
CONF_OPTS= CONF_OPTS=
# Use handwritten lexer # Use handwritten lexer
CONF_OPTS+=-l #CONF_OPTS+=-l
# Use ocamllex lexer # Use ocamllex lexer
#CONF_OPTS+=-L CONF_OPTS+=-L
# Use alpaga parser # Use alpaga parser
CONF_OPTS+=-a CONF_OPTS+=-a
......
...@@ -45,8 +45,13 @@ let rec make_eexpr_of_ast (a: tree) : expr res = ...@@ -45,8 +45,13 @@ let rec make_eexpr_of_ast (a: tree) : expr res =
let res = let res =
match a with match a with
| Node(t, [e1; e2]) when tag_is_binop t -> | Node(t, [e1; e2]) when tag_is_binop t ->
Error (Printf.sprintf "Unacceptable ast in make_eexpr_of_ast %s" make_eexpr_of_ast e1 >>= (fun expr1 ->
(string_of_ast a)) make_eexpr_of_ast e2 >>= (fun expr2 ->
OK (Ebinop (binop_of_tag t,expr1,expr2))))
| Node(t,[e]) -> make_eexpr_of_ast e >>= fun expr -> OK (Eunop (Eneg,expr ))
| IntLeaf (a) -> OK (Eint a)
| StringLeaf (s) -> OK(Evar s)
| _ -> Error (Printf.sprintf "Unacceptable ast in make_eexpr_of_ast %s" | _ -> Error (Printf.sprintf "Unacceptable ast in make_eexpr_of_ast %s"
(string_of_ast a)) (string_of_ast a))
in in
...@@ -58,6 +63,34 @@ let rec make_eexpr_of_ast (a: tree) : expr res = ...@@ -58,6 +63,34 @@ let rec make_eexpr_of_ast (a: tree) : expr res =
let rec make_einstr_of_ast (a: tree) : instr res = let rec make_einstr_of_ast (a: tree) : instr res =
let res = let res =
match a with match a with
|Node (Tassign,tass) ->
begin
match tass with
| [Node(Tassignvar,[StringLeaf (s);e])] ->
make_eexpr_of_ast e >>= (fun expr ->
OK (Iassign(s,expr)))
| _ -> Error (Printf.sprintf "Pas encore d'autres options") end
| Node(Tif,[e;i1;i2]) ->
make_eexpr_of_ast e >>= (fun expr ->
make_einstr_of_ast i1 >>= fun instr1 ->
make_einstr_of_ast i2 >>= fun instr2 ->
OK (Iif(expr, instr1, instr2 )))
| Node (Twhile,[e;i]) ->
make_eexpr_of_ast e >>= (fun expr ->
make_einstr_of_ast i >>= fun instr ->
OK (Iwhile(expr, instr)))
| Node (Tblock,list_of_instr) ->
OK (Iblock (List.map (fun x -> make_einstr_of_ast x >>! fun instr -> instr) list_of_instr))
| Node( Treturn,[e]) ->
make_eexpr_of_ast e >>= fun expr ->
OK (Ireturn (expr) )
| Node(Tprint,[e]) ->
make_eexpr_of_ast e >>= fun expr ->
OK (Iprint ( expr))
| _ -> Error (Printf.sprintf "Unacceptable ast in make_einstr_of_ast %s" | _ -> Error (Printf.sprintf "Unacceptable ast in make_einstr_of_ast %s"
(string_of_ast a)) (string_of_ast a))
in in
...@@ -76,9 +109,9 @@ let make_ident (a: tree) : string res = ...@@ -76,9 +109,9 @@ let make_ident (a: tree) : string res =
let make_fundef_of_ast (a: tree) : (string * efun) res = let make_fundef_of_ast (a: tree) : (string * efun) res =
match a with match a with
| Node (Tfundef, [StringLeaf fname; Node (Tfunargs, fargs); fbody]) -> | Node (Tfundef, [StringLeaf fname; Node (Tfunargs, fargs); fbody]) ->
list_map_res make_ident fargs >>= fun fargs -> list_map_res make_ident fargs >>= (fun newfargs ->
(* TODO *) (* TODO *)
Error "make_fundef_of_ast: Not implemented, yet." OK (fname , {funargs = newfargs ; funbody = (make_einstr_of_ast fbody >>! (fun fonction_body -> fonction_body))}))
| _ -> | _ ->
Error (Printf.sprintf "make_fundef_of_ast: Expected a Tfundef, got %s." Error (Printf.sprintf "make_fundef_of_ast: Expected a Tfundef, got %s."
(string_of_ast a)) (string_of_ast a))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment