diff --git a/src/compiler.rs b/src/compiler.rs index 944a04b..f9cb713 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -84,11 +84,23 @@ fn allocate_register(registers: &Vec) -> AllocateResult { //Find the one that wasn't used for the longest time let mut oldest: usize = 0; let mut oldest_register: u8 = 0; + let mut oldest_temp: usize = 0; + let mut oldest_temp_register: u8 = 0; for register in registers { if ((register.last_used < oldest) || (oldest == 0)) && register.id != 0 { oldest = register.last_used; oldest_register = register.id; } + if ((register.last_used < oldest_temp) || (oldest_temp == 0)) && register.id != 0 && register.variable == 0 { + oldest_temp = register.last_used; + oldest_temp_register = register.id; + } + } + if oldest_temp_register != 0 { + return AllocateResult { + register: oldest_temp_register, + unbind_before: false, + }; } return AllocateResult { register: oldest_register, @@ -186,7 +198,13 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va }, ASTPart::If(if_part) => { let condition_reg = do_ast_op(*if_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: 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 if_op in if_part.body { @@ -198,6 +216,12 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va 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) => { @@ -273,6 +297,9 @@ fn compile_function(ast: Vec, args: Option>, registers: &mu let mut functions: HashMap = HashMap::new(); let mut next_function_id: u32 = 1; + var_ids.insert(String::from("__LASTIF"), next_var_id); + next_var_id += 1; + match args { Some(arg_list) => { for arg in arg_list { diff --git a/test.as b/test.as index 51acd34..a9b60f8 100644 --- a/test.as +++ b/test.as @@ -1 +1,5 @@ -gethelj a = !piszv \ No newline at end of file +gethelj a = 1 +ha geny (1==1) { + gethelj b = 2 +} +gethelj c = 3 \ No newline at end of file