added If, And, Or

This commit is contained in:
Moritz Gmeiner 2024-08-26 17:26:59 +02:00
commit dbcdbc216f
5 changed files with 120 additions and 58 deletions

View file

@ -5,40 +5,47 @@ type stmt =
| Print of expr_node
| VarDecl of { name : string; init : expr_node option }
| Block of stmt_node list
| If of { cond : expr_node; then_ : stmt_node; else_ : stmt_node option }
and stmt_node = { stmt : stmt; pos : Error.code_pos }
let rec show_stmt ?(indent = 0) stmt =
let indent_s = String.make indent ' ' in
let show_expr_ind ?(depth = 2) = show_expr ~indent:(indent + depth) in
let show_stmt_ind ?(depth = 2) = show_stmt ~indent:(indent + depth) in
match stmt with
| Expr expr -> indent_s ^ "Expr\n" ^ show_expr ~indent:(indent + 2) expr.expr
| Print expr -> indent_s ^ "Print\n" ^ show_expr ~indent:(indent + 2) expr.expr
| Expr expr -> indent_s ^ "Expr\n" ^ show_expr_ind expr.expr
| Print expr -> indent_s ^ "Print\n" ^ show_expr_ind expr.expr
| VarDecl { name; init } -> (
indent_s ^ "Var " ^ name
^
match init with Some init -> " = \n" ^ show_expr ~indent:(indent + 2) init.expr | None -> "")
^ match init with Some init -> " = \n" ^ show_expr_ind init.expr | None -> "")
| Block stmts ->
let stmts_s =
List.fold_left
(fun acc stmt -> acc ^ show_stmt ~indent:(indent + 2) stmt.stmt ^ "\n")
"" stmts
List.fold_left (fun acc stmt -> acc ^ show_stmt_ind stmt.stmt ^ "\n") "" stmts
in
"Block" ^ stmts_s ^ "End"
| If { cond; then_; else_ } ->
let cond_s = show_expr_ind cond.expr in
let then_s = show_stmt_ind ~depth:4 then_.stmt in
let else_s = Option.map (fun stmt -> show_stmt_ind ~depth:4 stmt.stmt) else_ in
indent_s ^ "If\n" ^ cond_s ^ "\n" ^ indent_s ^ " Then\n" ^ then_s
^ if Option.is_some else_s then "\n" ^ indent_s ^ " Else\n" ^ Option.get else_s else ""
let show_stmt_node stmt_node = show_stmt stmt_node.stmt
let make_stmt_node (pos : Error.code_pos) (stmt : stmt) : stmt_node = { stmt; pos }
let make_expr_stmt (pos : Error.code_pos) (expr : expr_node) : stmt_node =
let stmt = Expr expr in
{ stmt; pos }
Expr expr |> make_stmt_node pos
let make_print (pos : Error.code_pos) (expr : expr_node) : stmt_node =
let stmt = Print expr in
{ stmt; pos }
Print expr |> make_stmt_node pos
let make_var_decl (pos : Error.code_pos) (name : string) (init : expr_node option) =
let stmt = VarDecl { name; init } in
{ stmt; pos }
VarDecl { name; init } |> make_stmt_node pos
let make_block (pos : Error.code_pos) (stmts : stmt_node list) : stmt_node =
let stmt = Block stmts in
{ stmt; pos }
Block stmts |> make_stmt_node pos
let make_if (pos : Error.code_pos) (cond : expr_node) (then_ : stmt_node) (else_ : stmt_node option)
=
If { cond; then_; else_ } |> make_stmt_node pos