mirror of
https://github.com/MorizzG/MLox.git
synced 2025-12-06 04:22:41 +00:00
global and local variables with scope
also reworked arg parsing, now has --debug flag
This commit is contained in:
parent
222de81a19
commit
77e57cd8c2
8 changed files with 341 additions and 95 deletions
41
lib/stmt.ml
41
lib/stmt.ml
|
|
@ -1,13 +1,44 @@
|
|||
type stmt = Expr of Expr.expr_node | Print of Expr.expr_node
|
||||
open Expr
|
||||
|
||||
type stmt =
|
||||
| Expr of expr_node
|
||||
| Print of expr_node
|
||||
| VarDecl of { name : string; init : expr_node option }
|
||||
| Block of stmt_node list
|
||||
|
||||
and stmt_node = { stmt : stmt; pos : Error.code_pos }
|
||||
|
||||
let show_stmt stmt =
|
||||
match stmt with Expr expr -> Expr.show_expr expr.expr | Print expr -> Expr.show_expr expr.expr
|
||||
let rec show_stmt ?(indent = 0) stmt =
|
||||
let indent_s = String.make indent ' ' 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
|
||||
| VarDecl { name; init } -> (
|
||||
indent_s ^ "Var " ^ name
|
||||
^
|
||||
match init with Some init -> " = \n" ^ show_expr ~indent:(indent + 2) init.expr | None -> "")
|
||||
| Block stmts ->
|
||||
let stmts_s =
|
||||
List.fold_left
|
||||
(fun acc stmt -> acc ^ show_stmt ~indent:(indent + 2) stmt.stmt ^ "\n")
|
||||
"" stmts
|
||||
in
|
||||
"Block" ^ stmts_s ^ "End"
|
||||
|
||||
let make_expr_stmt (pos : Error.code_pos) (expr : Expr.expr_node) : stmt_node =
|
||||
let show_stmt_node stmt_node = show_stmt stmt_node.stmt
|
||||
|
||||
let make_expr_stmt (pos : Error.code_pos) (expr : expr_node) : stmt_node =
|
||||
let stmt = Expr expr in
|
||||
{ stmt; pos }
|
||||
|
||||
let make_print (pos : Error.code_pos) (expr : Expr.expr_node) : stmt_node =
|
||||
let make_print (pos : Error.code_pos) (expr : expr_node) : stmt_node =
|
||||
let stmt = Print expr in
|
||||
{ stmt; pos }
|
||||
|
||||
let make_var_decl (pos : Error.code_pos) (name : string) (init : expr_node option) =
|
||||
let stmt = VarDecl { name; init } in
|
||||
{ stmt; pos }
|
||||
|
||||
let make_block (pos : Error.code_pos) (stmts : stmt_node list) : stmt_node =
|
||||
let stmt = Block stmts in
|
||||
{ stmt; pos }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue