implemented break

This commit is contained in:
Moritz Gmeiner 2024-08-27 02:46:17 +02:00
commit e288a626fd
6 changed files with 84 additions and 72 deletions

View file

@ -2,6 +2,7 @@ open Expr
type stmt =
| Expr of expr_node
| Break
| Print of expr_node
| VarDecl of { name : string; init : expr_node option }
| Block of stmt_node list
@ -16,6 +17,7 @@ let rec show_stmt ?(indent = 0) stmt =
let show_stmt_ind ?(add = 2) = show_stmt ~indent:(indent + add) in
match stmt with
| Expr expr -> indent_s ^ "Expr\n" ^ show_expr_ind expr.expr
| Break -> indent_s ^ "Break"
| Print expr -> indent_s ^ "Print\n" ^ show_expr_ind expr.expr
| VarDecl { name; init } -> (
indent_s ^ "Var " ^ name
@ -26,10 +28,10 @@ let rec show_stmt ?(indent = 0) stmt =
in
indent_s ^ "Block\n" ^ stmts_s ^ indent_s ^ "End"
| If { cond; then_; else_ } ->
let cond_s = show_expr_ind cond.expr in
let cond_s = show_expr_ind ~add:4 cond.expr in
let then_s = show_stmt_ind ~add:4 then_.stmt in
let else_s = Option.map (fun stmt -> show_stmt_ind ~add:4 stmt.stmt) else_ in
indent_s ^ "If\n" ^ indent_s ^ " Cond" ^ cond_s ^ "\n" ^ indent_s ^ " Then\n" ^ then_s
indent_s ^ "If\n" ^ indent_s ^ " Cond\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 ""
| While { cond; body } ->
let cond_s = show_expr_ind ~add:4 cond.expr in
@ -42,6 +44,8 @@ let make_stmt_node (pos : Error.code_pos) (stmt : stmt) : stmt_node = { stmt; po
let make_expr_stmt (pos : Error.code_pos) (expr : expr_node) : stmt_node =
Expr expr |> make_stmt_node pos
let make_break (pos : Error.code_pos) : stmt_node = Break |> make_stmt_node pos
let make_print (pos : Error.code_pos) (expr : expr_node) : stmt_node =
Print expr |> make_stmt_node pos