Commit 53ff196a authored by Armillon Damien's avatar Armillon Damien
Browse files

begin elang

parent e967b58a
......@@ -32,7 +32,7 @@ S -> FUNDEFS SYM_EOF { Node (Tlistglobdef, $1) }
BLOC -> SYM_LBRACE INSTRS SYM_RBRACE {Node(Tblock, $2)}
FUNDEFS -> {[]}
FUNDEFS -> FUNDEF FUNDEFS { (Node (Tfundef, $1)) :: $2 }
FUNDEF -> SYM_IDENTIFIER SYM_LPARENTHESIS LPARAMS SYM_RPARENTHESIS BLOC { [(Node (Tfunname, [StringLeaf($1)])); (Node(Tfunargs, $3)); $5] }
FUNDEF -> SYM_IDENTIFIER SYM_LPARENTHESIS LPARAMS SYM_RPARENTHESIS BLOC { [StringLeaf($1); (Node(Tfunargs, $3)); $5] }
LPARAMS -> {[]}
LPARAMS -> SYM_IDENTIFIER REST_PARAMS {(Node(Targ, [StringLeaf($1)])) :: $2}
REST_PARAMS -> SYM_COMMA SYM_IDENTIFIER REST_PARAMS {(Node(Targ, [StringLeaf($2)])):: $3}
......
......@@ -39,14 +39,33 @@ let binop_of_tag =
| Tne -> Ecne
| _ -> assert false
let tag_is_unary_inst =
function
Treturn -> true
| Tprint -> true
| _ -> false
let uniinst_of_tag e=
function
Treturn -> Ireturn e
| Tprint -> Iprint e
| _ -> assert false
(* [make_eexpr_of_ast a] builds an expression corresponding to a tree [a]. If
the tree is not well-formed, fails with an [Error] message. *)
(* let (>>=) = Result.bind *)
let rec make_eexpr_of_ast (a: tree) : expr res =
let res =
match a with
| Node(t, [e1; e2]) when tag_is_binop t ->
Error (Printf.sprintf "Unacceptable ast in make_eexpr_of_ast %s"
(string_of_ast a))
make_eexpr_of_ast e1 >>= fun expr1 ->
make_eexpr_of_ast e2 >>= fun expr2 ->
OK (Ebinop(binop_of_tag t, expr1 , expr2))
| Node(t, [e1]) when t = Tneg -> make_eexpr_of_ast e1 >>= fun expr1 ->
OK(Eunop(Eneg,expr1))
| Node(t,[e1]) when t = Tint -> make_eexpr_of_ast e1
| IntLeaf(num) -> OK(Eint(num))
| StringLeaf(str) -> OK(Evar(str))
| _ -> Error (Printf.sprintf "Unacceptable ast in make_eexpr_of_ast %s"
(string_of_ast a))
in
......@@ -58,6 +77,27 @@ let rec make_eexpr_of_ast (a: tree) : expr res =
let rec make_einstr_of_ast (a: tree) : instr res =
let res =
match a with
| Node(t, e1::i1::i2) when t = Tif ->
make_eexpr_of_ast e1 >>= fun expr1 ->
make_einstr_of_ast i1 >>= fun inst1 ->
(match i2 with
| [] -> OK (Iif(expr1,inst1,Iblock([])))
| [i2'] -> make_einstr_of_ast (i2') >>= fun inst2 -> OK (Iif(expr1, inst1 , inst2))
| _ -> Error (Printf.sprintf "Unacceptable ast in make_einstr_of_ast %s" (string_of_ast a)))
| Node(t,[i1]) when t = Tassign -> make_einstr_of_ast i1
| Node(t,[StringLeaf(str);e1]) when t = Tassignvar ->
make_eexpr_of_ast e1 >>= fun expr1 ->
OK(Iassign(str,expr1))
| Node(t,[e1;i1]) when t = Twhile ->
make_eexpr_of_ast e1 >>= fun expr1 ->
make_einstr_of_ast i1 >>= fun inst1 ->
OK(Iwhile(expr1, inst1))
| Node(t,li) when t = Tblock ->
list_map_res (fun i -> make_einstr_of_ast i) li >>= fun inst_res ->
OK(Iblock(inst_res))
| Node(t,[e1]) when tag_is_unary_inst t ->
make_eexpr_of_ast e1 >>= fun expr1 ->
OK(uniinst_of_tag expr1 t)
| _ -> Error (Printf.sprintf "Unacceptable ast in make_einstr_of_ast %s"
(string_of_ast a))
in
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment