implemented function calls

This commit is contained in:
Moritz Gmeiner 2024-08-28 17:29:36 +02:00
commit b717b91ee5
3 changed files with 94 additions and 21 deletions

View file

@ -7,6 +7,7 @@ type stmt =
| Continue
| Print of expr_node
| VarDecl of { name : string; init : expr_node option }
| FunDecl of { name : string; arg_names : string list; body : stmt_node }
| Block of stmt_node list
| If of { cond : expr_node; then_ : stmt_node; else_ : stmt_node option }
| While of { cond : expr_node; body : stmt_node }
@ -31,6 +32,11 @@ let rec show_stmt ?(indent = 0) stmt =
| VarDecl { name; init } ->
let init_s = match init with Some init -> " = \n" ^ show_expr_ind init.expr | None -> "" in
indent_s ^ "Var " ^ name ^ init_s
| FunDecl { name; arg_names; body } ->
let args_s = List.fold_left (fun acc arg -> acc ^ "\n" ^ arg) "" arg_names in
let body_s = show_stmt_ind ~add:4 body.stmt in
indent_s ^ "Fun " ^ name ^ "\n" ^ indent_s ^ " Args" ^ args_s ^ indent_s ^ " Body\n"
^ body_s
| Block stmts ->
let stmts_s =
List.fold_left (fun acc stmt -> acc ^ show_stmt_ind stmt.stmt ^ "\n") "" stmts
@ -89,6 +95,9 @@ let make_print (pos : code_pos) (expr : expr_node) : stmt_node = Print expr |> m
let make_var_decl (pos : code_pos) (name : string) (init : expr_node option) =
VarDecl { name; init } |> make_stmt_node pos
let make_fun_decl (pos : code_pos) (name : string) (arg_names : string list) (body : stmt_node) =
FunDecl { name; arg_names; body } |> make_stmt_node pos
let make_block (pos : code_pos) (stmts : stmt_node list) : stmt_node =
Block stmts |> make_stmt_node pos