From aa57551763da4d78eb29ba40eb9f900ac5fe96cb Mon Sep 17 00:00:00 2001 From: afonya2 Date: Fri, 30 May 2025 22:44:09 +0200 Subject: [PATCH] added else and else if --- src/compiler.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ test.as | 8 ++--- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index f9cb713..cfdc986 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -224,6 +224,83 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va } garbage_collect_registers(registers); }, + ASTPart::Else(else_part) => { + let reg = get_register_by_variable(registers, var_ids["__LASTIF"]); + let else_condition_reg; + if reg.id == 0 { + let reg = allocate_register(registers); + if reg.unbind_before { + ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None }); + } + ops.push(Operation { opcode: 2, arg1: Some(reg.register), arg2: Some(var_ids["__LASTIF"] as i64), arg3: None }); + set_register(registers, RegisterState { id: reg.register, used: true, variable: var_ids["__LASTIF"], last_used: *op_count }); + else_condition_reg = reg.register; + } else { + else_condition_reg = reg.id; + } + ops.push(Operation { opcode: 24, arg1: Some(else_condition_reg), arg2: Some(else_condition_reg as i64), arg3: None }); + + let op_placeholder = ops.len(); + ops.push(Operation { opcode: 0, arg1: None, arg2: None, arg3: None }); + for else_op in else_part.body { + do_ast_op(else_op, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers); + } + ops[op_placeholder] = Operation { + opcode: 26, + arg1: Some(else_condition_reg), + arg2: Some(ops.len() as i64), + arg3: None, + }; + //Unbind the lastif variable + if get_register_by_id(registers, else_condition_reg).variable == var_ids["__LASTIF"] { + ops.push(Operation { opcode: 8, arg1: Some(else_condition_reg), arg2: None, arg3: None }); + set_register(registers, RegisterState { id: else_condition_reg, used: false, variable: 0, last_used: 0 }); + } + garbage_collect_registers(registers); + }, + ASTPart::ElseIf(elseif_part) => { + let reg = get_register_by_variable(registers, var_ids["__LASTIF"]); + let else_condition_reg; + if reg.id == 0 { + let reg = allocate_register(registers); + if reg.unbind_before { + ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None }); + } + ops.push(Operation { opcode: 2, arg1: Some(reg.register), arg2: Some(var_ids["__LASTIF"] as i64), arg3: None }); + set_register(registers, RegisterState { id: reg.register, used: true, variable: var_ids["__LASTIF"], last_used: *op_count }); + else_condition_reg = reg.register; + } else { + else_condition_reg = reg.id; + } + ops.push(Operation { opcode: 24, arg1: Some(else_condition_reg), arg2: Some(else_condition_reg as i64), arg3: None }); + + let condition_reg = do_ast_op(*elseif_part.condition, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers); + ops.push(Operation { opcode: 30, arg1: Some(condition_reg), arg2: Some(2), arg3: None }); + ops.push(Operation { opcode: 16, arg1: Some(else_condition_reg), arg2: Some(condition_reg as i64), arg3: Some(condition_reg) }); + ops.push(Operation { opcode: 24, arg1: Some(condition_reg), arg2: Some(condition_reg as i64), arg3: None }); + + //Update the lastif variable + ops.push(Operation { opcode: 7, arg1: Some(condition_reg), arg2: Some(var_ids["__LASTIF"] as i64), arg3: None }); + set_register(registers, RegisterState { id: condition_reg, used: true, variable: var_ids["__LASTIF"], last_used: *op_count }); + + let op_placeholder = ops.len(); + ops.push(Operation { opcode: 0, arg1: None, arg2: None, arg3: None }); + for elseif_op in elseif_part.body { + do_ast_op(elseif_op, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers); + } + ops[op_placeholder] = Operation { + opcode: 26, + arg1: Some(condition_reg), + arg2: Some(ops.len() as i64), + arg3: None, + }; + //Unbind the lastif variable + if get_register_by_id(registers, condition_reg).variable == var_ids["__LASTIF"] { + ops.push(Operation { opcode: 8, arg1: Some(condition_reg), arg2: None, arg3: None }); + set_register(registers, RegisterState { id: condition_reg, used: false, variable: 0, last_used: 0 }); + } + garbage_collect_registers(registers); + }, ASTPart::Operation(op) => { let left_reg = do_ast_op(*op.left, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers); let right_reg = do_ast_op(*op.right, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers); diff --git a/test.as b/test.as index a9b60f8..0e76d7e 100644 --- a/test.as +++ b/test.as @@ -1,5 +1,5 @@ -gethelj a = 1 ha geny (1==1) { - gethelj b = 2 -} -gethelj c = 3 \ No newline at end of file + +} ha nem geny akkor geny (2==2) { + +} \ No newline at end of file