From 6ae019698886c8191682475926ccbc669b01129c Mon Sep 17 00:00:00 2001 From: Web_dev_learner Date: Sat, 19 Oct 2024 06:20:16 +0530 Subject: [PATCH] implement if-else Conditional --- src/codegen.ml | 12 ++++++++++++ src/expr.ml | 12 ++++++++++++ src/parser.mly | 12 ++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/codegen.ml b/src/codegen.ml index 43dcb41..d60cfec 100755 --- a/src/codegen.ml +++ b/src/codegen.ml @@ -26,6 +26,12 @@ let rec eval_expr env e = val_to_print | Return expr -> eval_expr env expr + | IfElse (cond, then_stmt, else_stmt) -> (* Evaluate if-else *) + let cond_val = eval_expr env cond in + if cond_val <> 0.0 then (* Assuming non-zero values mean true *) + interpreter env then_stmt (* Execute then block if true *) + else + interpreter env else_stmt (* Execute else block if false *) | _ -> raise (Error "Unsupported expression") let rec interpreter env stmts = @@ -34,6 +40,12 @@ let rec interpreter env stmts = | stmt :: rest -> let _ = match stmt with | Expr e -> eval_expr env e + | IfElseStatement (cond, then_block, else_block) -> (* Handle IfElseStatement *) + let cond_val = eval_expr env cond in + if cond_val <> 0.0 then + interpreter env then_block + else + interpreter env else_block | _ -> 0.0 (* Return 0.0 for unsupported statement types *) in interpreter env rest diff --git a/src/expr.ml b/src/expr.ml index 60d3ebf..d5d1f0b 100755 --- a/src/expr.ml +++ b/src/expr.ml @@ -1,3 +1,6 @@ +type expr = + | IfElse of expr * statement * statement (* Add the IfElse type *) + type util = | Int of int | Double of float @@ -16,6 +19,7 @@ type statement = | VariableDeclarationExpr of string * string * util | FunctionDeclaration of string * (string * string) list * statement list | ReturnStatement of util + | IfElseStatement of expr * block * block (* Add IfElseStatement for handling blocks *) type block = statement list @@ -49,6 +53,14 @@ and print_block_r body; Printf.printf "}\n"; | ReturnStatement(e) -> print_expr (Return e) + | IfElseStatement(cond, then_block, else_block) -> (* Handle IfElseStatement *) + Printf.printf "if ("; + print_expr cond; (* Print the condition expression *) + Printf.printf ") {\n"; + print_block_r then_block; (* Print the 'then' block *) + Printf.printf "} else {\n"; + print_block_r else_block; (* Print the 'else' block *) + Printf.printf "}\n"; and print_block_r (b:block): unit = match b with diff --git a/src/parser.mly b/src/parser.mly index 7d3c692..49ad79b 100755 --- a/src/parser.mly +++ b/src/parser.mly @@ -9,6 +9,7 @@ %token LPAREN RPAREN LBRACE RBRACE %token EQUAL EQ NE LT LE GT GE AND OR CARET %token COMMA SEMICOLON RETURN FN WRITE +%token IF ELSE %token EOF %type ident @@ -17,6 +18,7 @@ %type call_args %type program block_items block %type block_item var_decl func_decl +%type if_else_stmt %type comparison %start program @@ -31,6 +33,7 @@ block_item: var_decl { $1 } | func_decl { $1 } | RETURN expr SEMICOLON { ReturnStatement($2) } | expr SEMICOLON { Expr($1) } + | if_else_stmt { $1 } (* Added if-else support *) ; block: LBRACE block_items RBRACE { $2 } @@ -89,5 +92,14 @@ comparison: | OR { "||" } ; +if_else_stmt: IF LPAREN expr RPAREN block ELSE block { + IfElse ($3, $5, $7) + } + | IF LPAREN expr RPAREN block { + IfElse ($3, $5, [] ) (* No else block *) + } + ; + + %%