added else and else if

This commit is contained in:
afonya2 2025-05-30 22:44:09 +02:00
parent a01b9876f1
commit aa57551763
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
2 changed files with 81 additions and 4 deletions

View file

@ -224,6 +224,83 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, 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);

View file

@ -1,5 +1,5 @@
gethelj a = 1
ha geny (1==1) {
gethelj b = 2
} ha nem geny akkor geny (2==2) {
}
gethelj c = 3