Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/expr.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
type expr =
| IfElse of expr * statement * statement (* Add the IfElse type *)

type util =
| Int of int
| Double of float
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string> ident
Expand All @@ -17,6 +18,7 @@
%type <Expr.util list> call_args
%type <Expr.block> program block_items block
%type <Expr.statement> block_item var_decl func_decl
%type <Expr.statement> if_else_stmt
%type <string> comparison
%start program

Expand All @@ -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 }
Expand Down Expand Up @@ -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 *)
}
;


%%